Sunday, November 28, 2021

Serial RS232 Lessons and Musings

Yesterday and today I made all the mistakes I could possibly make connecting to a serial device, EXCEPT for letting the smoke out.   Win? ¯\_(ツ)_/¯  For posterity, and for future Josiah, here are the two big mistakes and I made and lessons learned.

First mistake: I used an Isolated 5v RS485 adapter to attempt to connect to an RS232 device. - It didn't work; but I do wonder if it would have worked, if I had used a level shifter.  I might try that later.

The differences between RS232, RS485:
(Table copied from here.)


RS232RS485
Voltage SystemVoltage level-basedDifferential
Total Drivers and Receivers on One Line1 Driver, 1 Receiver32 Drivers, 32 Receivers(One Driver active at a time)
Line ConfigurationPoint-to-pointMultidrop
Maximum Operational Distance15M / 50FT1,200M / 3000FT
Maximum Data Transmission Rate1MBit/s10MBit/s
Duplex ModeFull DuplexHalf Duplex or Full Duplex
Maximum Driver Output Voltage+/-25V-7V to +12V
Receiver Input Resistance3 to 7 kΩ12 kΩ
Receiver Input Voltage Range+/-15V-7V to +12V
Receiver Sensitivity+/-3V±200mV
 

  • The RS485 standard does not  define a communications protocol, these are the ones I'v been able to find so far.:
    • Modbus - Open Protocol, common in factory automation.
    • Profibus - IEC 61158 standard, common in factory automation.
    • (?) - Commercial aircraft cabins' vehicle bus
    • DMX512 - Stage lighting and effects.
    • AES3 - Digital audio transmission between some 'pro' audio gear.
    • (?) -Building automation, surveillance and door control devices.
    • DCC - Digital Command Control for Model Railways
  • Regarding distance for RS485: A quick approximation is that the length of the line in meters and the data rate in bits per second should not exceed 108. For example, a 20-meter cable would probably allow a data rate of 5 Mbits/s.
  • RS-422 is a variation of RS-485 with similar specifications but is designed only for one driver and up to 10 receivers. 
  • RS-423 is a single-ended rather than differential variation of RS-422.

Both RS485 and RS232 send data serially, but that's about where the similarities end.  RS485 is great for long distance multi device connections. RS232 is great for cheap device to host connections.

 Why use RS232 at all if RS485 is better?  Many reasons. First like the width of railroad tracks, RS232 has been around forever.  It has historical inertia.

(Historical information from LinusAkesson, and Wikipedia.)
    RS232 became a standard in May of 1960; but it's roots go MUCH farther back than that.  The stock ticker, invented in 1869, is really the start of serial data transmission standards.
    But it gets so much more wild than that!!  The stock ticker evolved into the ASCII teletype.  Teletypes were once connected across the world in a network called Telex; used to send telegrams.  Global text messaging(!); but you know, on paper at both ends.
    As computers moved from batch processing static data, like punch cards, to becoming fast enough to interact with humans in real time; some sort of user interface was needed, and Telex machines were readily available.  Underneath the hood the Unix/Linux Kernel still today relies heavily on manipulating streams of data; exactly like those from a serial teletype, or to a cast iron stock ticker from the mid 1800's.  If you want to understand Linux, I strongly recommend digging into this.  Amazingly technology from 1869 influenced how the terminal on modern computers today operate.  Seriously, check out the Linus Akesson site, link above; it's good!

Second reason why you pretty much never see RS485:  I feel like RS485 never really caught on.  Today on Digi-Key a 500kbps RS485 chip costs $3.37, a 10mbps Ethernet chip is $3.06, and if you want them in bulk you can get 100mbps Ethernet chips for less than a dollar.  I suspect that RS485 didn't catch on because for single devices RS232 was easier and cheaper, and for multiple devices networks like Ethernet were in most cases a better option.  -  Clocks in buildings are an interesting exception.  I've seen some strange systems for synchronizing the clocks throughout a building, including RS485.

 Second mistake: For my second attempt I used an isolated 5v RS232 to USB adapter to connect to a 3.3v serial device.  

I was able to get some garbled data from the device using this.  At first I thought it was a problem with baud rate, or something more esoteric like a bit-wise inversion.  However it was just the wrong voltage.

 A few notes on real world use of RS232:
    The early RS232 official standard "zero" was represented by a voltage of between +3 and +15 volts, and "one" was a voltage between -3 to -15 volts.  In the EIA-232 standard it was increased to +/-25 volts!  Today, "nobody" does this.  RS232 voltage is either 0v/5v - or - 0v/3.3v.  If you connect your expensive computer to a lot of serial devices it's a good idea to get an isolated USB to serial adapter.  Then if you do happen to plug into a real/old RS232 port you don't fry your computer with 50v AC into your USB port.
    RS232 officially has 11 different wires (DCD, DSR, RI, RTS, RTR, CTS, TxD, RxD, GND, PG) - Unless your using a modem over a POTS phone line you will only use three wires, TxD, RxD, and GND (maybe also a shield for PG).  I remember connecting to BBS's as a kid and having a vague knowledge of RI and CTS and such.  I'll be surprised if I ever have to do that again. Three wires and a shield are all you need.
    I plan on making a level shifter with DB-9 plugs on either side that I can add to my little collection of gender adapters and null-modem adapters.

Third lesson: Not all inexpensive USB to serial adapters are the same or even sane.
    Some inexpensive USB to serial adapters that have a 3.3v Vout pin: still output 5v on the Tx pin!  -  Also strange, but more useful are adapters that are the reverse:  3.3v Rx and Tx, with a 5v Vout to maybe power a device with.

    Be careful when buying a USB to serial adapter, know what your getting, and know that eventually you might need more than one type!

If you use USB serial adapters professionally: don't use a cheap adapter.  It's not worth your job.  When I'm connecting to an expensive PLC processor, or the console port of a fancy network switch, I don't use a serial adapter I got from a stranger in another country for 5$.  For professional work I really like the ones made by Advantech, I have an older version of BB-USO9ML2-A, 2,000+ volt isolation, quality design, and no corners cut provide a lot of piece of mind.

    But for hobby work, if you want to spend much, much, less you can get isolated or non-isolated ones from Aliexpress.  If you want to buy American, Adafruit sells a nice selection.

*** NOTE!!  If you don't want headaches, consider paying a little bit more and get a USB to serial adapter with a real FTDI chip.  The cheaper chips like the PL2303 and CP2102 usually work fine after you find, download, and install drivers for them.  But there are some devices that you just can't connect to if you don't have a real FTDI chip;  and yes there are fake FTDI chips out there.  I've held one in my hand, it was labled FTDI, but it was not.  Even with the correct drivers it could not connect to many Allen Bradley PLC's.  ***

(These are not affiliate links, I don't care what you buy.  :-) )

Now that you know about the variety of USB to serial adapters, you are going to have much fewer problems transferring data over serial ports!  Have fun!

PS: I've already expanded and updated this post, several times.  This may end up less of a blog post and more of a 'living' document.