serial-port
serial-port copied to clipboard
Problems handling XMODEM protocol packets
hello, thanks a lot for your serial-port examples: I'm interested in particular into 2_with_timeout sample.
Implementing XMODEM protocol I'm able to read the 'standard' 133-bytes packet, but the last packet has only 1 byte (EOT = 0x04). To avoid the timeout exception on the last packet I initially read only 1 byte and, if different from EOT, I read the remaining 133-1 bytes.
_serialPort.setTimeout( boost::posix_time::millisec(timeout));
char data[XMODEM_PACKET_LENGTH];
try {
// Per non far andare in timeout ..
_serialPort.read( data, 1);
// .. eventualmente il resto del pacchetto (-1)
if (data[0] != XMODEM_CHARS_EOT)
{
// Resto del pacchetto
_serialPort.read( (char*)(data+1), XMODEM_PACKET_LENGTH-1);
std::cout << ">> '";
for(auto i:data)
std::cout << i;
std::cout << "'" << std::endl;
_XModemReceiveResponse = std::vector<byte>( data, data + XMODEM_PACKET_LENGTH);
}
else {
std::cout << "[WaitForXModemResponse] EOT !!" << std::endl;
_XModemReceiveResponse = std::vector<byte>( data, data + 1);
}
}
catch (timeout_exception){
...
```
In this way I have randomly packet error caused by a data misalignment in the data buffer.
This is my log:

Packet 22 (and previous ones) is ok, for packet 23 the data buffer is filled 1 position ahead (?) ...
I checked the USB packet and it doesn't contain the char in first position ('M' in this case):

If I run the program without this hack, but only with:
` _serialPort.read( data, XMODEM_PACKET_LENGTH);
`
I have not problem at all, only the last 1-bytes packet causes the timeout_exception (waiting 133 bytes ..).
Any idea?
thanks a lot !!