如何使用win32 api从串口读取正确顺序的两个字节?

时间:2011-01-23 04:36:14

标签: winapi serial-port readfile


我正在使用win32 api,visual studio 2008.我能够从串口读取字节但不是我想要的方式,我从arduino以x,y格式传递数据。但我的下面的代码有时先读取y然后读取x。我如何以正确的顺序读取数据,我的意思是x,y顺序。如果有人感兴趣,坐标将传递给另一个函数,鼠标光标将相应移动。这是我的代码:

int _tmain( int argc, TCHAR *argv[] )

{   
    HANDLE hComm;
    DWORD bytesRead = 0;
    POINT mouseCoords;  /*structur to hold mouse coordinates*/
    DCB dcb = {0};      /*Device Control Block, used to configure serial port settings
                          here we initialize dcb structure to zero. Good practice!*/
    COMMTIMEOUTS ct;

    int loop = 1;
    int counter = 10;

    /*get handle to serial port*/
    hComm = CreateFile(g_pcCommPort, 
                       GENERIC_WRITE | GENERIC_READ,
                       0,                           /* must be opened with exclusive-access */
                       NULL,                        /* default security attributes*/
                       OPEN_EXISTING,               /*  must use OPEN_EXISTING for serial ports*/
                       0,                           /*non overlapped I/O, blocking*/
                       NULL );                      /*hTemplate must be NULL for comm devices*/

    /*Make sure that serial port is successfuly opened*/
    if ( hComm == INVALID_HANDLE_VALUE ) {
            errMsg(TEXT("Cannot access serial port!"));
        return 0;   
    }

    ASSERT("Serial port access successful!");

    /*get serial port status i.e default settings and make
      sure we can access them*/
    if ( !(GetCommState(hComm, &dcb))) {
        errMsg(TEXT("Cannot access current DCB settings!"));
        return 0;
    }

    ASSERT("DCB settings access successful!");

    /*print dcb settings so that we can get an idea of default settings*/
    printCommSettings(dcb);


    /*Since we were successful in accessing the com port
      we can go ahead and set it manually to our desired
      settings*/
    if ( !(setupSerialPort(&dcb))) {
        errMsg(TEXT("Cannot setup serial port!"));
        return 0;
    }

    ASSERT("DCB config successful!");


    /*lets print configuration after setting up the
      serial port just to make sure everything is ok*/
    printCommSettings(dcb);

    ct.ReadIntervalTimeout = 50;
    ct.ReadTotalTimeoutConstant = 50;
    ct.ReadTotalTimeoutMultiplier = 10;
    ct.WriteTotalTimeoutConstant = 50;
    ct.WriteTotalTimeoutMultiplier = 10;

    if(SetCommTimeouts(hComm, &ct) == 0) {
         errMsg(TEXT("Cannot setup comm timout!"));
    }

while (loop) 
{
  if ( !(ReadFile(hComm,g_buffer,5,&bytesRead,NULL)) ) {    loop = 0; }
    printf("Message Read: %s==%d\r", g_buffer, bytesRead);

} /*while loop*/


    if ( !(CloseHandle(hComm))) {
        errMsg(TEXT("Serial port handle error!"));
    }

    NEWLINE;

    return 0;
}

编辑: 这些是串口设置: “9600,N,8,1”

我在10,12过去了,这就是我得到的: OUTPUT:
留言阅读:,12
留言阅读:10,12
留言阅读:12
留言阅读:0,12
留言阅读:2
留言阅读:,12
留言阅读:10,12
留言阅读:12
留言阅读:0,12
留言阅读:2
留言阅读:,12
留言阅读:10,12

1 个答案:

答案 0 :(得分:2)

看起来你发现你没有在串口传递两个字节,但事实上你正在发送ASCII。根据您的示例输出,我没有看到Y在X之前进入(将显示为12,10而不是10,12

实际发生的事情是,当您期望它们时,读取并不总是完成。您将在一次阅读中获得部分信息,在下一次阅读中获得下一部分。

您需要做的是将传输与消息文本中找不到的某些字符同步。例如,如果您发送了(10,12),那么您会知道(是您的号码的开头,)就是结束。这样你就可以在缓冲区中读取足够多的字符,直到你有一个(后跟一个)然后解析它们之间的字符,然后丢弃以)字符结尾的缓冲区部分