Problem with Serial Port Transmission
-
Hello, I am using the bit of code below in a function to send instructions thru the serial port to some equipment and also to read off some feedback from the equipment: WriteFile(hCOM1, txbuff, 8, &bytesRead, NULL); ReadFile(hCOM1, rxbuff, 8, &bytesRead, NULL); Power= (((int)rxbuff[6])%4)*256 + (int)rxbuff[5]; When i am testing this function alone, it seem to work perfectly and obtain nice readings. However, when i integrate it with the rest of the program i find that the function gives wierd readings.I later found that this was due to the ReadFile portion reading the recieved portion at an offset, meaning there was some meaningless data before my actual portion of the feedback. eg I expect 255 255 43 ..... instead i get 05 04 255 255 43... I managed to solve this problem by making use of the PURGE command. However, i would like to know, why does the offset of data occurr ? Is there any better way to solve this problem instead of PURGE ? Thanks
-
Hello, I am using the bit of code below in a function to send instructions thru the serial port to some equipment and also to read off some feedback from the equipment: WriteFile(hCOM1, txbuff, 8, &bytesRead, NULL); ReadFile(hCOM1, rxbuff, 8, &bytesRead, NULL); Power= (((int)rxbuff[6])%4)*256 + (int)rxbuff[5]; When i am testing this function alone, it seem to work perfectly and obtain nice readings. However, when i integrate it with the rest of the program i find that the function gives wierd readings.I later found that this was due to the ReadFile portion reading the recieved portion at an offset, meaning there was some meaningless data before my actual portion of the feedback. eg I expect 255 255 43 ..... instead i get 05 04 255 255 43... I managed to solve this problem by making use of the PURGE command. However, i would like to know, why does the offset of data occurr ? Is there any better way to solve this problem instead of PURGE ? Thanks
The serial port has an input buffer of usually around 16 characters. Anything that is received by the serial port but not read by an application will be just sitting there waiting to be read. A good practice is to clear the input buffer (just read until you can't read any more) when you open the serial port, and then make sure you keep up with all the reads after that.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"