Serial Communication

Its old and of no use. No body wants it around anymore. No, that’s not what they are saying about me around the office (at least I hope not). I am talking about serial communication.

Few days ago I saw a video on YouTube showing some kids reactions of using windows 95 and couple of them were scared when they saw the message “Its safe to turn off your computer now” (one was like, “was that not safe before?”) , and I was thinking, really? was that so long ago? (Those days, we were worried when that message did not show up 🙂 BTW you can search the video on YouTube.)

I am positive that if you ask someone today, about serial, many will be stunned to know that there was life before Ethernet.

I wrote couple of posts about serial communication already (you can find the links in reference section) and today I will continue that journey forward. The “Serial Communication” refers to the fact that bits are put on a low speed wire one after another where groups of bits are framed into bytes and specific bit patterns are used to identify the beginning and end of those bytes. The most common ways to do that are RS232, RS422 or RS485.

RS232 is commonly used for point-to-point wiring, one node directly connected to another node. It is ideal for small packets of short distance communication between two nodes where speed of transmission isn’t of great importance. The disadvantage are lack of noise immunity, ground loops issues and short distance.

RS485 and RS422 are used to avoid some of the problems of RS232. Both use differential signaling. They have greater noise immunity and support for multiple nodes. Due to multiple nodes ability, they are used in serial communication networks like Modbus RTU.

Data formats (Binary, Hex, and ASCII)

It is also important to understand data formats if you really want to advance in serial communication. However, I will try to keep it simple because you will definitely encounter these if you try to peek behind the curtins.

  • Serial devices use Binary for communication, which consists of just two unique numbers: 0 and 1.
  • Binary is the Base-2 numbering system. One byte of data consists of 8 binary digits, from 0000 0000 to 1111 1111.
  • Hexadecimal is the base-16 system, which consists of 16 numbers: 0 to 9 and the letters A to F (decimal number 15).
    • The Hexadecimal numbering system is useful because it can represent every byte as two consecutive hexadecimal digits, and it is easier for humans to read Hexadecimal numbers than Binary numbers.
    • Most of the manufacturers use Hexadecimal in their protocol documentation.
    • It is simple to convert a value from Hexadecimal to Binary.
    • Just translate each Hexadecimal digit into its 4-bit binary equivalent. E.g. Hexadecimal number F3 equals Binary number 1111 0011.
  • ASCII (American Standard Code for Information Interchange) is a character encoding based on the English alphabet.
    • ASCII codes (both readable and unreadable) are widely used in communications, such as Modem communications.
    • Letters A to Z and numbers 0 to 9 are readable ASCII codes.
    • Some ASCII codes are unreadable, such as the control codes: XON and XOFF, which are used in Software flow control.

Demo : Command Parser

In this demo I used Arduino to build a command parsing system of “two-parts command” to Turn Pins ON or OFF. This is a very simple example but you can extend it for more complex command structure to a full fledged useful protocol.

Arduino have built-in serial terminal and for simplicity, I used it to send the commands to the board. Command structure is as follows:

  • Command PinNumber (commands are pinON and pinOFF only, any-other will not be recognized)
    • pinON 3 (Turn pin-3 ON)
    • pinOFF 3 (Turn pin-3 OFF)

Here is the corresponding arduino sketch and commands results:

Commands Output

Arduino Sketch

ParseCommand is the function where the commands are being processed. You can put your own logic to process the commands in the way you like. The code is very simple, however, if something is not clear, feel free to ask in comments.

Summary

Serial communication is an interesting topic and there is lot we haven’t talked about. In upcoming posts, we will continue talking about it in more details. So, even if you are thinking, breathing and talking Ethernet all day long, remember two things. One, serial communication is not going away soon and two; all communications including Ethernet is at its heart Serial. Till next time. Happy Coding.

References