0
\$\begingroup\$

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.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Why not design your protocol to reject garbage? Checksums, forward error correction, acknowledgements, resend requests... \$\endgroup\$
    – Phil Frost
    Aug 17, 2013 at 17:08

2 Answers 2

3
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Did you try a pullup resistor - about 10k - on the RX line?

When the PC is transmitting data at the same time you connect the cable, the µC will always receive at least one garbage character.

\$\endgroup\$
2
  • \$\begingroup\$ Actually, that depends on the serial driver and UART. If there are flags such as for framing errors, and the driver rejects any reads when these are present, then the possibility of passing bogus characters through to the application is greatly reduced. \$\endgroup\$ Aug 17, 2013 at 18:26
  • 1
    \$\begingroup\$ Framing error triggers only when the stop bit is not high level, which can happen for garbage bytes, too. YMMV. \$\endgroup\$
    – Turbo J
    Aug 17, 2013 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.