Just built two RS-485 adapters (version 2) and decided to write little note about RS-485 bus (and differential line) theory plus perform some testing of the devices. This material mostly interesting for newbies.
In the end of this article you can find a funny way to test this devices 🙂
Present adapters are based on the usb to serial converter which means that for computer’s OS this devices is just serial interface. Every byte written to the interface is transferred as is and should be received on the other end of this “pipe”.
Of course RS-485 is nothing to deal with transmission protocols.
This standard defines only electrical differential line with transmitter, receivers, line termination, etc. What is actually on the line is defined by the top level application.
Let’s do some experiments.
Connect adapter to the Linux PC and run ‘dmesg‘ command to find file name of the device. If everything is ok there must something like /dev/ttyUSBX, where X is number of the device.
Now writing and reading can be performed on the device.
Connect oscilloscope probe to the FT232 pin 1. This is TX. Now everything is ready to send some data and capture some bits. Of course at this point it’s not RS-485 but simple serial line.
Execute the following command:
echo -n "h" > /dev/ttyUSBX
Where X is your device number.
With this command we just sending to the device one symbol ‘h’ which is byte 0x68, according to ascii table.
Argument ‘-n’ prevents echo from using ‘\n’ (newline) symbol after ‘h’.
0x68 is 00010110 in binary so such pattern should captured.
Here can be seen data bits.. Also there is some additional bits in the beginning and in the end of the sequence.
This is start and stop bits which can helps to separate actual data frame. There can be additional bits called ‘parity bits‘ and can be used 2 stop bits. All this depends on the serial port configuration in the operating system.
You may mention that values are inverted, idle line is always represents ‘bit’ 1. This happens due to resistor between data line and power line inside the FT232. It’s default behavior for such data lines (but can be changed in FT232 EEPROM).
Also can be measured length of the one bit – 104 microseconds and measure total frequency which is 9.6 KHz (1/104 * 1000).
Hmm, 9.6 KHz sounds familiar, of course it’s speed of the line! Baud rate: 9600. This is the a way how unknown baud rate can be found .
Let’s check serial port settings to ensure that correct data was captured. Run the following command:
tty -F /dev/ttyUSBX -a
(don’t forget to replace ‘X’ with your device number)
In the output should be following values:
speed 9600 baud cs8 -cstopb -parenb
This means: speed 9600, 8 data bits, 1 stop bit, no parity.
Now we can do some measures on the RS-485 line. It’s a differential line which uses two wires. Both wires are transferring the same signal but one signal is positive and second is negative. Both signals is corresponding to each other, common-ground wire is not required (typically).
On the receiver side device measures voltage difference between two wires. High enough (±200mV) difference interpreted as single bit of the data. This allows to exclude interference on the line because both wires is affected by interference in the same way and this interference is not-differential signal so it just subtracting from itself, giving almost zero level.
Additional shielding allows to use very long and stable line (but low speed) up to 1.2 kilometers which is great for industrial applications.
Please note, that it’s highly recommended to use common-ground wire in case long line, especially between different buildings. This can help to equalize ground voltages between devices and avoid problems that can ruin your day.
End points of the RS-485 line should be terminated with resistor which is equal to the resistance of the cable. For typical CAT5 twisted pair this is 120 Ω.
Also you can see in my (and other) adapter additonal resistors that pulls up positive and pulls down negative wires of the RS-485 line.
This resistors forms voltage divider which can helps to set initial bias voltage on the receiver inputs. This can help to stabilize the line so receiver can easely detect voltage changes during data transmission.
Differential nature of the RS-485 means that we can’t directly connect our oscilloscope probes to the line. Actually we can, but all measures will be not precise and sometimes incorrect.
To capture differential line signals should be used differential probes, like this one:
As bonus differential probe can be used to perform measures on the circuits connected to the generic AC voltage (like switching power supply) without destroying the oscilloscope and probe wires.
I don’t have such probe so let’s do it in incorrect way 🙂
Connect first channel of the oscilloscope to the line A+ (pin next to the common wire) and second channel to the line B-.
In this situation ground terminals of the probes can be left unconnected but this increases noise level. To reduce noise level connect this clips to the common ground wire. Please note that this ground connection will not help to correctly measure actual signal because it’s differential line and it’s not referenced to the common wire.
Let’s repeat sending ‘h’ symbol using echo. We should capture something like this:
As expected there are two signals, one of the signals is inverted. But overall picture is looks familiar to the previous measures on the FT232 TX pin. Here even can be measured baud rate. Of course both signals is quite noisy.
Most of the oscilloscopes contains simple Math functions. Using this functionality second channel can be subtracted from the first channel.
Subtraction result must be inverted. After this result should looks more correct. Noise level was decreased.
Data can be easily decoded.
Actually I’m not sure about this ‘bit’ between idle state and transmission of the start bit. Looks like transmitter switching from the high-z state to the tx mode. Area was marked on the picture. If I’m wrong please write in the comments.
Now it’s time to have some fun. How about ‘movie’ through the RS-485 line?
Of course not real movie but very cool ASCII-Art version of the Star Wars.
Let’s test the ‘movie’. Connect to the special server using telnet:
If everything is correct you should see the ‘movie’ in ASCII. To exit type Ctrl-] and then Ctrl-d
Due to nature of this ‘movie’ we can transfer this stream using any kind of serial line, in our case it’s RS-485.
Connect two adapters to each other and connect USB to the same (or even different) computers. One terminal will be server and other is client.
For the client we need some kind of the terminal program which can work with a serial line. Most popular utility for the console mode is minicom. Install minicom if you don’t have it.
Now on the server side run telnet again but this time redirect all output to the connected adapter:
telnet towel.blinkenlights.nl > /dev/ttyUSBX
Replace X with your device number.
On the client side run minicom for the adapter device:
minicom -D /dev/ttyUSBX
Enjoy the ASCII movie on speed 9600 via RS-485 line 🙂
How it looks like on video:
Thanks for the reading!