多产的USB到串行Com端口

时间:2013-06-17 22:30:16

标签: c++ serial-port usb

我的项目使用PICDEM 2 PLUS演示板,Windows 7中的VC ++。为了与代码和PIC板通信,我使用了UART / USB电缆(USB串口8)。到目前为止,我能够连接PIC板并能够接收/传输数据。但我想使用“Prolific USB to Serial Com Port”。我尝试了所有可能的方式(在互联网上找到),比如更新驱动程序,更改端口号(最初当我使用Prolific电缆连接时,它会自动获取port4)。但没有什么可以帮助我摆脱这个错误。我认为我的PC与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

如果有人帮我解决这个问题,我将非常感激。真的是大问题

谢谢:-)

0 个答案:

没有答案