简体   繁体   English

串行端口在Windows上读取不正确的数据

[英]serial port reads incorrect data on windows

this program is to receive and then send data through serial port. 这个程序是通过串口接收然后发送数据的。

After setting up serial, read a byte from serial(i'm using a tool called putty to send chars), if it is a 'a', read 16byte from a file and then send to serial, and then reads a 'b', and goes on to lettle 'p'. 设置好串行后,从串行中读取一个字节(我正在使用一个名为putty的工具发送字符),如果它是'a',则从文件中读取16byte,然后发送至串行,然后读取'b' ,然后继续“ p”。 total 16 loops. 总共16个循环。

but now, i can have the first two loops work, when it comes to the third loop, after readfile(), the readBuff is 50h, while it is what it reads from serial for the first 2 loops, like 'a', 'b'. 但是现在,我可以让前两个循环起作用,当涉及到第三个循环时,在readfile()之后, readBuff为50h,而前两个循环是从串行读取的,例如'a',' b'。 it always fails at the 3rd loop. 它总是在第三循环失败。

it is weird, does someone have a clue? 很奇怪,有人知道吗?

hSerial = CreateFile("COM1",                            
        GENERIC_READ | GENERIC_WRITE,       
        0,
        0,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        0
        );

if(hSerial==INVALID_HANDLE_VALUE){
    if(GetLastError()==ERROR_FILE_NOT_FOUND){
        printf("create serial handle file failed!-1");
        return 0;
    }
    printf("create serial handle file failed!-2");
    return 0;
}
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
dcbSerialParams.fBinary = TRUE;
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize   = 8;
dcbSerialParams.StopBits   = ONESTOPBIT;
dcbSerialParams.Parity       = NOPARITY;
if(!SetCommState(hSerial, &dcbSerialParams)){
    printf("set serial state failed");
    return 0;
}
    COMMTIMEOUTS cmt;

cmt.ReadIntervalTimeout = 1;
cmt.ReadTotalTimeoutMultiplier = 100000000;
cmt.ReadTotalTimeoutConstant = 100000000;
cmt.WriteTotalTimeoutConstant = 10;
cmt.WriteTotalTimeoutConstant = 10;

if(!SetCommTimeouts(hSerial, &cmt)){
    printf("set timeout failed");
    return 0;
}
char readBuff; //read 1 byte from serial which indicate the send loop
DWORD dwBytesRead; //number of bytes read from binary
char writeBuff[16]; /*read 16bytes from spd binary to write to serial */
DWORD dwBytesWrite; /*stores actual number of bytes writen*/
for(int i = 0; i<=15; i++){
    //initial the variables to 0 incase some weird behavior
    readBuff = 0; 
    dwBytesRead = 0;
    dwBytesWrite = 0;
    for(int j=0; j<=15; j++)
        writeBuff[j] = 0;

    if(!ReadFile(hSerial, &readBuff, 1 , &dwBytesRead, NULL)){
        if(dwBytesRead != 1){
            printf("read failed, please check!\n");
            return 0;
        }
    }
//when it comes to the 3rd loop, readfile() reads a 50h, even if i didn't send anything to serial line.
    if(readBuff != ('a' + i)){
        printf("failed the %dth loop\n", i);
        return 0;
    }
    else{
        //read spd binary then puts to serial

        if(fread(writeBuff, 1, 16, file) != 16){
            printf("read binary failed, please check!\n");
            return 0;
        }
        //send to serial
        if(!WriteFile(hSerial, writeBuff, 16, &dwBytesWrite, NULL)){
            if(dwBytesWrite != 16){
                printf("write to serial failed, please check!\n");
                return 0;
            }
        }
        printf("%c", readBuff);

    }
}//end of for loop

If ReadFile fails you need to read again until you have found some valid data. 如果ReadFile失败,则需要重新读取,直到找到一些有效数据为止。 But instead, you check dwBytesRead and makes the decision to continue based on what's in that variable. 但是,您可以检查dwBytesRead并根据该变量中的内容来决定继续。 When ReadFile fails, I wouldn't trust dwBytesRead to contain a valid value. 当ReadFile失败时,我将不信任dwBytesRead包含有效值。 Suppose the UART read 1 byte but got an overrun/framing error? 假设UART读取了1个字节,但出现溢出/成帧错误?

Change the program to 将程序更改为

   while(!ReadFile(...))
     ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM