我写的程序是从linux发数据到win!在win下写的串口程序好像要接受到两个字符才能返回!
我的代码如下: while((new_time - old) < delay_time)
{
read_port( buffer, 1, &res);
new_time = time((time_t *)NULL); if( res <= 0) continue;
if( buffer[0] == 0x15) break; }其中read_port的定义如下!
read_port(unsigned char *read_buffer, unsigned int read_buffer_length, unsigned long *length)
{
if( ReadFile(serial_handle, // handle of file to read
read_buffer, //
read_buffer_length, //size of buffer to read
length,
NULL) == 0)
{
AfxMessageBox(_T("wrong in reading of serial communication"));
return FALSE;
} return TRUE;
}我当单独在linux下发送0x15(这是1BYTE的数据),则无法接受到!随便加一个BYTE例如发两BYTE,第一个BYTE是0x15则可以接收到~到底是为什么呢?

解决方案 »

  1.   

    你把設置串口的部分寫出來 好像有個地方設置接收buffer的深度 你可能設為了2 
      

  2.   

    BOOL serial_ctrl::open_set_serial_port(LPCTSTR serial_port, DCB dcb)
    {
    serial_handle = CreateFile( serial_port,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    0,
    NULL); if( serial_handle == (HANDLE)0xFFFFFFFF)
    {
    AfxMessageBox("Open the serial port wrong");
    return FALSE;
    } if( GetCommState(serial_handle, &config) == 0)
    {
    AfxMessageBox(_T("Wrong in get configuration port."));
    return FALSE;
    } if( SetCommState(serial_handle, &config) == 0)
    {
    AfxMessageBox(_T("Wrong in set config of serial port"));
    return FALSE;
    } //set the buffer of sending and reciving
    SetupComm(serial_handle, 1024, 1024);
    PurgeComm(serial_handle, PURGE_TXCLEAR);

    //set the time out parity
    COMMTIMEOUTS com_time_out;
    memset(&com_time_out, 0, sizeof(com_time_out));
    GetCommTimeouts(serial_handle, &com_time_out);//get the time out set now //set the read time out
    //return at once while get one charater
    com_time_out.ReadIntervalTimeout = MAXDWORD;
    com_time_out.ReadTotalTimeoutConstant = 0;
    com_time_out.ReadTotalTimeoutMultiplier = 0; //set the write time out
    com_time_out.WriteTotalTimeoutConstant = 2;
    com_time_out.WriteTotalTimeoutMultiplier = 3; //set the time out parity SetCommTimeouts(serial_handle, &com_time_out); return TRUE;
    }我串口设置参数是按照以上设置的!麻烦您看看,不知道哪错了!我应该没有加握手吧~
      

  3.   

    WriteTotalTimeoutMultiplier:写入每字符间的超时。WriteTotalTimeoutConstant:一次写入串口数据的固定超时。
    可能WriteTotalTimeoutConstant设置的太小而导致写入超时.把它设置的大点.比如3000;