Reputation: 3
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'. total 16 loops.
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'. 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
Upvotes: 0
Views: 639
Reputation: 214920
If ReadFile fails you need to read again until you have found some valid data. But instead, you check dwBytesRead and makes the decision to continue based on what's in that variable. When ReadFile fails, I wouldn't trust dwBytesRead to contain a valid value. Suppose the UART read 1 byte but got an overrun/framing error?
Change the program to
while(!ReadFile(...))
;
Upvotes: 2