我在 Windows 7 中使用 PICDEM 2 PLUS DEMO BOARD、VC++ 的项目。为了与代码和 PIC 板通信,我使用了 UART/USB 电缆(USB 串行端口 8)。到目前为止,我可以连接 PIC 板,也可以接收/发送数据。但我想使用“Prolific USB to Serial Com Port”。我尝试了所有可能的方法(在互联网上找到),比如更新驱动程序、更改端口号(最初当我使用 Prolific 电缆连接时,它会自动使用端口 4)。但是没有什么能帮助我摆脱这个错误。我认为我的电脑与 USB 端口正确连接,但无法发送数据或接收数据。【不知道为什么!!!:-(] 可能需要编写一些特定的代码来建立连接。
下面是我得到错误的代码 -
bool SerialCommunicator::SerialRx(void) {
//the function to be called by the receiver thread
BYTE buf[1024*8];
unsigned int read;
static UINT16 crc;
static UINT8 ptr_rxd_buffer = 0;
UINT8 crc1, crc2;
UINT8 c;
do
{
read = port->ReadAvailable(buf,1000); // catch the received bytes, read= nb of received bytes in th buffer buf
for( unsigned int k =0; k<read; k++)
{
c= buf[k];
if(receiverLocked) return 0; // if rxd buffer is locked immediately return
// the rxd buffer is unlocked
if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
{
receivedPacket.data[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
crc = c; // init crc
}else if (ptr_rxd_buffer < serialComms::RXD_BUFFER_LEN) // collect incomming bytes
{
if(c != '\r') // no termination character
{
receivedPacket.data[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
crc += c; // update crc
}else // termination character was received
{
// the last 2 bytes are not subject for checksum calculation
// they are the checksum itself
crc -= receivedPacket.data[ptr_rxd_buffer-2];
crc -= receivedPacket.data[ptr_rxd_buffer-1];
// calculate checksum from transmitted data
crc %= 4096;
crc1 = (UINT8) ('=' + crc / 64);
crc2 = (UINT8) '=' + crc % 64;
// compare checksum to transmitted checksum bytes
if((crc1 == receivedPacket.data[ptr_rxd_buffer-2]) && (crc2 == receivedPacket.data[ptr_rxd_buffer-1]))
{ // checksum valid
receivedPacket.data[ptr_rxd_buffer] = '\r'; // set termination character
receivedPacket.size = ptr_rxd_buffer + 1;// store number of received bytes
receiverLocked = true; // lock the rxd buffer
USART_ProcessRxData();
}else
{ // checksum invalid
receiverLocked = false; // unlock rxd buffer
}
ptr_rxd_buffer = 0; // reset rxd buffer pointer
}
}else // rxd buffer overrun
{
ptr_rxd_buffer = 0; // reset rxd buffer
receiverLocked = false; // unlock rxd buffer
}
} // for( unsigned int k =0; k<read; k++)
}while(read>0); //end of do/while
return 0;
}
Project.exe 中 0x00316329 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000000。
错误在行中给出
read = port->ReadAvailable(buf,1000); // catch the received bytes, read= nb of received bytes in th buffer buf
如果有人帮助我解决这个问题,将不胜感激。真的是大问题
谢谢 :-)