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.







Thursday, July 15, 2021

Linux Sucks!

 So I tried to take a screenshot today, but I couldn't find it.  I had previously set a setting that saved my screenshots in a specific place.  It took about an hour for me to figure out where they were going.  

 (I was slightly thwarted because I was hitting Ctrl-PrintScreen - on my last laptop the print screen button required hitting Fn and I got confused.  Ctrl-PrintScreen puts the screenshot into a paste buffer.)

So after finding out where the screenshots were going I tried to set the setting again to get the screenshots to once again save in the correct place.

Things like this shouldn't happen in a modern polished Linux Distribution like Debian; but the popular belief is that there are a lot of volunteer programmers who work on Linux, so mistakes happen, because they aren't professionals.

That's not the case here, check out the webpages linked below. These are small people who have the power to help, but are too focused on maintaining their power over their little corner of the operating system.  They refuse to communicate effectively; or solve a problem that many users are having.

After doing a bit of research, there are a few ways to fix it and make it work again.  Users shouldn't be expected to go to great lengths to change a setting...  I may give this more consideration later and see if I can unravel this web of garbage; but I have to work in the morning, I need to sleep now.

Relevant pages:

https://askubuntu.com/questions/114429/how-can-i-specify-the-default-save-directory-for-gnome-screenshot
https://bugzilla.gnome.org/show_bug.cgi?id=699642
https://cialu.net/how-to-change-default-gnome-screenshot-savings-folder/
https://help.gnome.org/users/gnome-help/stable/screen-shot-record.html.en
https://unix.stackexchange.com/questions/279985/how-do-i-change-the-location-where-screenshots-are-saved-in-linux-mint-17-cinnam
https://unix.stackexchange.com/questions/279985/how-do-i-change-the-location-where-screenshots-are-saved-in-linux-mint-17-cinnam
https://wiki.debian.org/ScreenShots
https://forum.manjaro.org/t/gnome-change-default-screenshot-location/67834
https://forums.debian.net/viewforum.php?f=6&sid=96b34c5e3e27354cd0582ff268e79c68

So you can install custom scripts in your home directory.  You can disable the six included shortcuts and make new ones pointing to a different screenshot program, you can install a 3rd party "Gnome Shell' extension, or. apparently there is a way to do it with systemd user units???  But you get the idea...

Petty bickering has removed this feature for millions of people.
Linux Sucks!

But at least you can fix it, if Microsoft doesn't want you to take a screenshot you simply won't be able to.

Monday, July 12, 2021

Its Time: Linux is better than Windows.

 I'm struggling to set the time on windows 10.  I'm working on an industrial computer/touchscreen that is not connected to any network.  For security we never connect these machines to the internet.  I need to set the time to within a couple/three seconds.

It appears this can't be done from the GUI.  Is Microsoft using so much code from Linux that a terminal window is becomming required?

Windows is so awful, I just don't understand why anyone would want to use it.  -  It's maybe a bit easier, but it's getting harder to use, and now Microsoft spys on you, and manufacturers can lock the BIOS.....  It's NOT your computer if it has Windows on it.  Why buy a computer that's not really yours?

Sunday, August 16, 2020

Working with computers is getting harder, because of obfuscation.

TLDR:   Fuck you Dell!

I purchased an Inspiron 17 1700 and although I really like many features of it, it's obvious that all the effort went into making a computer that looks good from the outside, but is crap on the inside. 

Right now I'm struggling with WiFi, the M.2 WiFi/Bluetooth card that it comes with is an ok B/G/N card.  But the pace of technology is accelerating, and I need at least AC support if not the latest and greatest AX.  

So, I'm hunting for a replacement WiFi card, and I want to know what cards the M.2 slot supports. Their specification document says "One M.2 slot for WiFi/Bluetooth" . Not helpful.  

From their own documentation there are several different keyings and sizes of M.2 slots that can accept WiFi/Bluetooth cards.  I think even among the differently keyed slots there are likely to be other support issues.  The whole M.2 slot ecosystem is the opposite of straight forward.

 Icing on the cake, is that their feedback system is currently down, so the only way I can shame them is on this blog.   Which no one reads because mostly it's just me ranting about problems.  (Sorry!)

I really want this to be a good day.  I'm going to keep trying.

  :-)

Update: As far as I've figured out an A-keyed or an A+E-keyed M.2 slot will accept A-keyed or A+E-keyed WiFi cards.  E-keyed cards must go only into E-keyed slots.

Still haven't found any detailed technical documents on these slots; but I'm feeling better about this.  I bought a pair of Intel AX200NGW cards, so hopefully they are backward compatible with me A-keyed M.2 slot. 

When I removed the Intel 9260NGW from the motherboard the antenna studs broke off from the WiFi card and got lodged in the antenna wires.  I'm so proud of me I didn't even get upset.  I just carefully pried the debris out of the connectors and moved on.

I'll have to install the original card when I get home.

Monday, June 24, 2019

Bridge to the Shop

It works!

It has taken weeks and many many hours.  I have (almost) completely ignored everything else in my life like dishes, pets, family and friends.  The sacrifice has finally yielded success. - To my wife, I'm sorry, and thank you! - To the world I say:   Pbbbtth!    I did it, and I don't care if you think it's stupid or awesome.

I have two Raspberry Pi Model 3 B+'s on my desk, each with an EDUP 600mbps WiFi adapter, power adapter, and sdcard plugged into them.  They are a WiFi network cord.

If I use an ethernet cable to connect one RPi to my internet router, and another ethernet cable to connect the other RPi to a network switch in the garage...

Well it does exactly what you would expect of a long ethernet cable.  The two switches are connected together.  If multiple device are plugged into the switch in the garage, they get ip adresses from the internet router in the house, and connect to the internet. - Simple, functional, adds a few milliseconds to a ping, and a slight bandwidth hit.  ;-)

 Victory!

Ok, so I can ping.  Actual throughput is not great.

Normal 'N' WiFi ~19Mbps bandwidth
My WiFi Wire     ~ 65Kbps bandwidth

That's so bad it makes me laugh....

My WiFi adapters are currently only using one 20Mhz channel, and the Raspberry Pi's don't have a hardware network switch in them.  So bandwidth is, for now, terrible.

More testing is necessary.  Now that I know how to set this up maybe I need to move it to EspressoBin's, or an RPi4 (Just released today!)  :-)

I'll see if I can increase the speed and post a Edu-torial.

UPDATE:
Ha! I found the problem!  MTU on the clients MUST be set to 1446 or less.  -  I didn't realize that fragmenting packets would cause a 3000x slow down.  Once Packet size is enforced to be small enough to prevent fragmentation I get very good speeds of 30Mpbs
Next up increasing radio bandwith from 20mhz to 80mhz.  Dare I go to 160mhz?!?!?  - Only time will tell.  
(There are legal issues here in the US with using 160mhz outside, it interferes with like weather prediction stuff...(?) - Apparently it's possible with some WiFi equipment to use two different 80mhz bands, getting effectively 160mhz; not sure if there are two full 80mhz bands that I can use outdoors legally. - I think if you enable special power regulation and stuff that you can do it.)

Wednesday, May 22, 2019

xz man page

I either nominate xz for the worst man page ever or I gladly make a fool of my self for not understanding man pages.

There is no help on the man page for how to specify an input file with a separate output file name.

The man page goes on and on and on about different compression ratios and how to specify a custom compression profile.

But no mention of "-k" or "-c" or how to use them.

This is the sort of failure that our community needs to acknowledge.  This sort of error creates huge frustration for millions of people who mostly know what they are doing, and can completly stop new people from entering the community.

How do I fix this?  Can I submit a patch somehow to update the man page to make it more usefull?  -  {sigh}

EDIT: Ok, I was wrong.  "-k" and "-c" are mentioned.  I still find that reading this man page provides no understanding of how to use these options.  

This is how you compress a file and keep the origional:
 
xz -c original.file > compressed.file.xz
Hindsight is great eh?  Makes sense now.  -  But the point of a manual is to help the reader gain understanding.  man pages almost always fail at this.  They are good references to things which are already known, but terrible tools to learn from.

Monday, May 20, 2019

Bridge to the Shop

I tried out a 64bit Gentoo image for Raspberry Pi...    This is very Gentoo.  The Rapberry Pi 3B+ has a 64bit processor, but 32 interface to, like, everything.  Basicially no one runs a 64bit OS on the Raspberry Pi, but you could, and if you use Gentoo and you can do something, well why not?!

It ran very fast for a couple of minutes, then it crashed.

I went back to Raspbian, and ran into a problem.  One of my Raspbian images was running an updated kernel, that did not have anything in /lib/modules that matched.  So I was unable to update the WiFi drivers.  I found an article with the correct command to reinstall the Raspbian Kernel:

sudo apt-get install --reinstall raspberrypi-bootloader raspberrypi-kernel

So play time over and annoying problem accomplished I moved on to thinking about how to actually implement the bridge.  I think I'm going to setup two RaspberryPi's to make this bridge.  That will allow it to be more portable, and might make creating a educational guide for others to learn from easier.  (The alternative being to use the RPi access point that I already have as an end point.)

L2TPv3 (Layer 2 Tunneling Protocol Version 3) seems to be the key piece of technology that I need to make this work. :-)  I found NetworkLesons.com that has a very succieient explination of L2TPv3.  While Wikipedia's L2TPv3 entry has some interesting history.