serial-port icon indicating copy to clipboard operation
serial-port copied to clipboard

Problems handling XMODEM protocol packets

Open SteMMo33 opened this issue 8 months ago • 0 comments

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:

![image](https://github.com/fedetft/serial-port/assets/8493374/a784bc25-1046-4ea7-b950-e57135bc3352)

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):

![image](https://github.com/fedetft/serial-port/assets/8493374/a641673a-3b5e-43cc-8f77-94d86fa7ca03)

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 !!

SteMMo33 avatar May 31 '24 14:05 SteMMo33