Monday, 13 January 2020

rs485 - RS232 hotplugging


I've noticed that when I connect/disconnect the RS232 cable to my micro-controller, I receive a garbage character at the other end (my pc). Is there a way to prevent this?


I want to eventually move to using RS485 with a number of micro-controllers sharing the same bus, and this would prevent adding or removing nodes while the bus is in operation.



Answer



A couple of hints that come to mind to make the protocol between MCU and PC more robust:




  • Use a predefined frame, with given length and formatting that always fits your data. It can contain multiple pieces of data, but use a predefined format in such a way that you always know what data is contained at which position in the frame.

  • Use a checksum within the frame so you can reject the whole frame if the data is corrupt;

  • Use a header/preamble, a static known sequence of bytes to indicates the start of the frame. Any short sequence will do, I personally like to use 'AB' (0x41, 0x42) as it shows up nicely in a hexdump;

  • Use a minimum time gap between two frames that is at least longer than the frame duration itself. Some serial libraries allow to read a given number of bytes from the serial buffer for a given maximum number of microseconds. When the function call times out with the wrong number of bytes, you can simply reject the received data, retry and you will automatically fetch the next full frame from the start.

  • If you have duplex connectivity, you can use a similar frame to request retransmits or signal an OK message.


Take a look at the firmata protocol, pretty much Arduino based, but you can learn pretty much from the implementation.


No comments:

Post a Comment

arduino - Can I use TI's cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...