charleslinquist
Posts: 107
Joined: Wed Sep 05, 2012 12:43 pm

USB <-> serial adapter

Sun Mar 12, 2017 2:20 am

I needed a second serial port on a Raspberry (sometimes a third), so I connected a FTDI USB - serial adapter to my Raspberry pi model 3, and added a 'loopback' so I could tell when it was working.

lsusb shows:

Bus 001 Device 004: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

I used Minicom and tried as the device:

/dev/ttyUSB0
/dev/ttyS0
/dev/serial0
/dev/serial1

But none of them work, they simply hang the Raspberry..
It should be noted that I'm already using ttyAMA0

How can I get access to the additional serial ports?

moojuiceuk
Posts: 6
Joined: Sat Feb 07, 2015 1:10 am
Location: Southampton UK

Re: USB <-> serial adapter

Sun Mar 12, 2017 2:20 pm

I've just done this yesterday (connecting a Psion 5 on RS232 to get shell access).

First, I assume you're wanting shell access on these USB-RS232 adaptors?
You used to be able to add a line to /etc/inittab to enable shell on an additional serial device but that has since been dropped in favour of systemd. I found a good solution here:
http://0pointer.de/blog/projects/serial-console.html

Following this advice, once I had copied a default config file fron /lib/systemd/system/[email protected] to a config file named here: /etc/systemd/system/[email protected], edited the baudrate, then run:

Code: Select all

systemctl daemon-reload
systemctl start [email protected]
All worked. I did remove my console from /boot/cmdline.txt however, as I only wanted to use the external USB-RS232 adaptor.

This may or may not be the "right" way to do it, but it seems to work here.

User avatar
scruss
Posts: 2272
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: USB <-> serial adapter

Sun Mar 12, 2017 2:55 pm

I don't think the OP's looking for another serial terminal, unless I misread.

Your dmesg output only shows one serial port, the
Bus 001 Device 004: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
Look in dmesg output for FTDI, and you'll find something like:

Code: Select all

… FTDI USB Serial Device converter now attached to ttyUSB0
So that means it would be on /dev/ttyUSB0. Adjust appropriately for ttyUSB1, ttyACMn, etc.

For better serial access, it's a good idea to do this, then log out and back in again:

Code: Select all

sudo usermod -a -G dialout pi
This will add you to the dialout group once and for all, so you can read and write to the serial port as the regular pi user.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

charleslinquist
Posts: 107
Joined: Wed Sep 05, 2012 12:43 pm

Re: USB <-> serial adapter

Sun Mar 12, 2017 3:11 pm

Thanks! Yes, shell access is the start, but eventually I want access with Python or C. My goal is to accept packetized serial data at 57.6Kbaud from two different sources and validate them via their CRCs, then pass only the known good packets as telnet out the Ethernet connection with ser2net, or optionally out a third serial port (using a USB adapter). Data going the other way would be "split" and be sent out both serial ports simultaneously. The goal is to add redundancy to a system that doesn't have any.

The "master" would always send data via two different means simultaneously, but it would receive data only from whatever source was "good".

I guess I should also look at some other boards that have two or three serial ports.

User avatar
scruss
Posts: 2272
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: USB <-> serial adapter

Sun Mar 12, 2017 4:08 pm

Sounds doable.

If you do go for multiple USB adaptors, either get one with multiple ports or get individual different kinds. The /dev/ttyUSBn numbers are allocated roughly in connection order, so there's a chance they'll move around when you reboot. The /dev/serial/by-id/ and /dev/serial/by-path/ devices help you tell devices apart, though it doesn't help that often too-cheap-to-be-true adapters appearing to be FTDI are counterfeit and all have the same serial number.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

charleslinquist
Posts: 107
Joined: Wed Sep 05, 2012 12:43 pm

Re: USB <-> serial adapter

Sun Mar 12, 2017 5:43 pm

When I issue dmesg, I get the message

[ 5.585803] ftdi_sio 1-1.4:1.0: FTDI USB Serial Device converter detected
[ 5.586009] usb 1-1.4: Detected FT232RL
[ 5.590171] usb 1-1.4: FTDI USB Serial Device converter now attached to ttyUSB0

But. When I issue the command

minicom -b 9600 -o -B /dev/ttyUSB0

Minicom opens a window, but nothing comes out the USB serial adapter. So it still isn't working.

charleslinquist
Posts: 107
Joined: Wed Sep 05, 2012 12:43 pm

Re: USB <-> serial adapter

Sun Mar 12, 2017 9:14 pm

More information:

I run the following command, but I don't understand how that would add ttyUSB0 to the dialout group.

sudo usermod -a -G dialout pi

At any rate, minicom reports that the port is "off line"

User avatar
scruss
Posts: 2272
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: USB <-> serial adapter

Mon Mar 13, 2017 3:11 am

It doesn't add the serial port to the dialout group, it adds you to the dialout group so you can access the serial port.

For minicom, you need to do

Code: Select all

minicom -b 9600 -D /dev/ttyUSB0
I'd skip the -o, as you likely want minicom to init the port, the first time at least.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

charleslinquist
Posts: 107
Joined: Wed Sep 05, 2012 12:43 pm

Re: USB <-> serial adapter

Mon Mar 13, 2017 4:13 am

Unfortunately, that doesn't work either.

User avatar
scruss
Posts: 2272
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: USB <-> serial adapter

Mon Mar 13, 2017 11:02 pm

you did log out and log back in again? The groups thing doesn't happen until you do.

How is your loopback configured? Is it just a simple RX<->TX crossover at the DE-9 end?

You might have local echo turned on (or off): if you're seeing nothing, local echo is off and you're not transmitting. If you see everything ttwwiiccee you have local echo on and you're receiving fine.

Can you talk to a real serial device? Could it be your cable?
Do you know that the baud rate is correct?
Can you connect the output to a scope to see if any signals are happening?
What does

Code: Select all

stty -F /dev/ttyUSB0
return?
Does running

Code: Select all

stty -F /dev/ttyUSB0 cs8 -parenb -cstopb 9600
make any difference? (the above is basically setting 9600 8N1)
Are there any other messages stored in the dmesg log that might indicate that your serial port is still happy?

The hardcore serial port reset is this:

Code: Select all

sudo stty -F /dev/ttyUSB0 0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0
It clears every setting on the port.

Serial is hard. Sometimes it takes a while.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

User avatar
scruss
Posts: 2272
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: USB <-> serial adapter

Mon Mar 13, 2017 11:06 pm

some other thoughts:

are you connecting through a USB hub, or directly? Some USB adapters don't like hubs.
does the USB port you're using work for other USB devices?
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

charleslinquist
Posts: 107
Joined: Wed Sep 05, 2012 12:43 pm

Re: USB <-> serial adapter

Wed Mar 15, 2017 1:02 am

Thanks for taking the time to respond.

Answers to your questions:

I'm not using any hubs.

The adapter I'm using is a simple FTDI USB <-> RS-232(TTL) adapter that works for everything else. Windows 10 has no problem with it.
My 'loopback' is just a wire between TxD and RxD. The device has LEDs on both lines, and they flash nicely when using Bray's TERMINALin Windows - regardless of what baud rate I choose. I get chars in the receive window when I press a key. Those chars stop when I remove the loopback wire.
There are no cable issues.

dmesg reports the following:

[ 2632.764056] usb 1-1.3: Product: FT232R USB UART
[ 2632.764068] usb 1-1.3: Manufacturer: FTDI
[ 2632.764081] usb 1-1.3: SerialNumber: A50285BI
[ 2632.772529] ftdi_sio 1-1.3:1.0: FTDI USB Serial Device converter detected
[ 2632.772759] usb 1-1.3: Detected FT232RL
[ 2632.773634] usb 1-1.3: FTDI USB Serial Device converter now attached to ttyUSB0


stty reports the following:

[email protected]:~ $ stty -F /dev/ttyUSB0
speed 0 baud; line = 0;
intr = <undef>; quit = <undef>; erase = <undef>; kill = <undef>; eof = <undef>; start = <undef>;
stop = <undef>; susp = <undef>; rprnt = <undef>; werase = <undef>; lnext = <undef>; flush = <undef>;
min = 0; time = 0;
-cread
-brkint -icrnl -imaxbel
-opost -onlcr
-isig -icanon -iexten -echo -echoe -echok -echoctl -echoke



I have tried every conceivable port. I'm already using ttyAMA0. I tried serial0, serial1,tty0,ttyS0. minicom hangs on all of them.

I logged out and logged in again. No difference.
Could it be that I need a driver? One is not supposed to be needed, but ....

I think I would be better off moving to an Orange pi zero. It has 3 actual serial ports.

User avatar
scruss
Posts: 2272
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: USB <-> serial adapter

Wed Mar 15, 2017 3:09 am

The adapter's working, and is likely to be a real FTDI, as it has a believable serial number. This is good. You don't need an additional driver, as the kernel has identified it correctly.

With the loopback installed, on the Raspberry Pi if you try this:

Code: Select all

stty -F /dev/ttyUSB0 cs8 -parenb -cstopb 9600
echo hello > /dev/ttyUSB0
do the lights flash at all?
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

charleslinquist
Posts: 107
Joined: Wed Sep 05, 2012 12:43 pm

Re: USB <-> serial adapter

Wed Mar 15, 2017 3:31 am

BINGO!

The echo works, but Minicom (apparently) doesn't.


Is there some .conf file that I need to fix in order to get minicom to work?

User avatar
scruss
Posts: 2272
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: USB <-> serial adapter

Wed Mar 15, 2017 4:41 pm

Yes, just start it without command line args, then Ctrl-A Z to go into menu, then O to edit config. Make sure you “Save setup as dfl”, or it will remember something old and weird.

You might find grabserial useful for serial capture.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

charleslinquist
Posts: 107
Joined: Wed Sep 05, 2012 12:43 pm

Re: USB <-> serial adapter

Wed Mar 15, 2017 11:39 pm

The problem is that it defaulted to hardware flow control. That is a bad choice from the writers.

User avatar
scruss
Posts: 2272
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: USB <-> serial adapter

Thu Mar 16, 2017 12:33 am

from your stty results, looks like you'd reset everything. Hardware flow control was standard for modems: XON/XOFF was a royal pain with data compression and serial speeds faster than the modem speed.

Pretty much everything about serial on Linux (and Unix) seems pretty terrible compared to PCs. PCs had it really easy with their simple hardware. Multi-user makes it a bit harder.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

charleslinquist
Posts: 107
Joined: Wed Sep 05, 2012 12:43 pm

Re: USB <-> serial adapter

Thu Mar 16, 2017 12:51 am

But Windows really screwed up USB serial ports. Why, oh why couldn't they read the serial number of the USB device, and if the serial number didn't already exist in a list, bring up a menu to give the user an easy choice for the port number - which would remain constant for that device?
I suppose that someone could have already written a utility that does just that - but I don't know of any.

I guess not many people care about the needs of embedded engineers anymore.

Return to “Advanced users”