Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Problem using serial port /dev/ttyAMA0

Fri Oct 09, 2020 7:55 am

I have an application that uses serial communications and it can be configured to use 5 different ports.
4 of these are USB serial adapters connected to one of the 4 USB sockets on the RPi4.
I have added rules on my RPi4 to make the serial ports available with device names associated to the USB socket the USB2SERIAl adapter is plugged into:

Code: Select all

$ sudo nano /etc/udev/rules.d/99-usb-serial.rules
SUBSYSTEM=="tty",ENV{ID_PATH}=="platform-fd500000.pcie-pci-0000:01:00.0-usb-0:1.1:1.0",SYMLINK+="ttyUSB01"
SUBSYSTEM=="tty",ENV{ID_PATH}=="platform-fd500000.pcie-pci-0000:01:00.0-usb-0:1.2:1.0",SYMLINK+="ttyUSB02"
SUBSYSTEM=="tty",ENV{ID_PATH}=="platform-fd500000.pcie-pci-0000:01:00.0-usb-0:1.3:1.0",SYMLINK+="ttyUSB03"
SUBSYSTEM=="tty",ENV{ID_PATH}=="platform-fd500000.pcie-pci-0000:01:00.0-usb-0:1.4:1.0",SYMLINK+="ttyUSB04"
This works fine and I can use the device name so created for communication.
Here is a list of the devices as they appear when listing them on the command line (I have removed the non-interesting entries):

Code: Select all

$ ls -la /dev/tty*
crw-rw---- 1 root dialout 204, 64 Sep 23 17:57 /dev/ttyAMA0
crw-rw---- 1 root dialout 188,  0 Oct  9 09:32 /dev/ttyUSB0
lrwxrwxrwx 1 root root          7 Sep 24 10:09 /dev/ttyUSB01 -> ttyUSB0
lrwxrwxrwx 1 root root          7 Oct  6 12:29 /dev/ttyUSB03 -> ttyUSB1
crw-rw---- 1 root dialout 188,  1 Oct  8 23:59 /dev/ttyUSB1
As you can see the "real" device names ttyUSB0 and ttyUSB1 have been symlinked to names that are tied to the USB sockets.

PROBLEM
When I use any of the USB devices ttyUSB01..ttyUSB04 my code works just fine, but if I try to open the device /dev/ttyAMA0 it fails!
This device is the built-in serial device on the RPi4 with its connections to the GPIO pins #8 and #10, and here I have connected a level converter to get true RS232 levels. This converter is on an I/O board plugged into the 40-pin header and it also contains some relays I need in the application.
I want to remove one of the USB/RS232 converters and instead connect the external unit to the built-in serial port.
But the problem is that even opening this port fails!

Can someone advice what to do in order to get this built-in port to work?
Maybe the built-in serial port is not the /dev/ttyAMA0 I assumed? In such a case which device name should I use?
Bo Berglund
Sweden

cleverca22
Posts: 1816
Joined: Sat Aug 18, 2012 2:33 pm

Re: Problem using serial port /dev/ttyAMA0

Fri Oct 09, 2020 8:19 am

did you set enable_uart=1 in config.txt?
did you read http://raspberrypi.org/documentation/co ... on/uart.md ?
i think you want /dev/serial0 once its enabled

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Problem using serial port /dev/ttyAMA0

Fri Oct 09, 2020 8:41 am

cleverca22 wrote:
Fri Oct 09, 2020 8:19 am
did you set enable_uart=1 in config.txt?
No it was not set..
I was not aware of this document...
I have now looked at it and I must say I am a bit confused still.
Seems like bluetooth is involved too (I never use BT), could that be the cause of my problems?
i think you want /dev/serial0 once its enabled
Mistake in naming and configuration...

Now I used raspi-config to enable the serial port on this RPi4 (I thought that I had already done so, but apparently not).
And now a new ttyS0 device appeared:

Code: Select all

crw-rw---- 1 root dialout 204, 64 Oct  9 10:09 /dev/ttyAMA0
crw------- 1 root root      5,  3 Feb 14  2019 /dev/ttyprintk
crw-rw---- 1 root dialout   4, 64 Oct  9 10:17 /dev/ttyS0
crw-rw---- 1 root dialout 188,  0 Oct  9 10:17 /dev/ttyUSB0
lrwxrwxrwx 1 root root          7 Feb 14  2019 /dev/ttyUSB01 -> ttyUSB0
lrwxrwxrwx 1 root root          7 Feb 14  2019 /dev/ttyUSB03 -> ttyUSB1
crw-rw---- 1 root dialout 188,  1 Oct  9 10:19 /dev/ttyUSB1
So I tried using that instead, but no go...
Also:

Code: Select all

 $ ll /dev | grep ser
drwxr-xr-x   4 root root          80 Feb 14  2019 serial
lrwxrwxrwx   1 root root           5 Feb 14  2019 serial0 -> ttyS0
lrwxrwxrwx   1 root root           7 Feb 14  2019 serial1 -> ttyAMA0
I am now using /dev/ttyS0 in my application but I get no data out to the attached device, which works well if hooked to the USB adapter and using /dev/ttyUSB03.
Should I try /dev/serial0 instead?
Or is it a permission problem?
Bo Berglund
Sweden

User avatar
bensimmo
Posts: 4988
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Problem using serial port /dev/ttyAMA0

Fri Oct 09, 2020 9:35 am

The Pi4 has a few more UART ports on the GPIO pins as well.

When you enabled the GPIO Serial port, you should also have disabled the console when it asked if you did it through raspi-config or the desktop configuration.
(just checking you did), if you manually added it then you've missed a step, read Disable Linux serial console from the link already provided.

Since you do not intend to use bluetooth then add
dtoverlay=disable-bt,
to config.sys before all the serial stuff* and run sudo systemctl disable hciuart to stop the bluetooth service.
This means your Serial Port is using a full UART, it will be available at the symbolic link /dev/serial0 and it now links to /dev/ttyAMA0 (normally it would link to /dev/ttyS0 the miniUART on a Pi4, read the bottom of that linked page).





*unfortunately this is still not an option in the serial setup in raspi-config so has to be done manually.

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Problem using serial port /dev/ttyAMA0

Fri Oct 09, 2020 12:05 pm

Now I have done all of this and used /dev/serial0 as the port and yet it does not work...
Must get down to low-level debugging with an oscilloscope next...
Is there some simple way to circumvent software and just from the command prompt make the serial port send out some text?
I have never done that ever, but I have a feeling that it might be possible.
maybe just simply:

Code: Select all

echo "hello" > /dev/ttyAMA0
Nothing happens...
I tested also as follows (after chaning connectors):

Code: Select all

echo "hello" > /dev/ttyUSB03
This gave me output...

EDIT:
I hooked up an oscilloscope to the TxD0 pin of the GPIO header (pin 08) and exercised the command towards ttyAMA0 above.
No dice! there was no indication of any signal at all.
The line is at a constant +3.3V.
So somehow one needs to do something else to enable this serial port on the command line (or at all).
Bo Berglund
Sweden

User avatar
bensimmo
Posts: 4988
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Problem using serial port /dev/ttyAMA0

Fri Oct 09, 2020 1:15 pm

you can loop TX to RX, a loopback

quick search and this one seems appropriate
https://medium.com/@amitasinghchauhan/s ... 7e40da9055

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Problem using serial port /dev/ttyAMA0

Fri Oct 09, 2020 2:29 pm

I can't do the pin shorting because there is a board plugged into the header already. It carries the digital to RS232 converter plus all the relays and stuff.

What happens when I try minicom:

Code: Select all

$ minicom -D /dev/ttyS0
-bash: minicom: command not found
Seems like this is (no longer) part of PiOS...

EDIT:

Code: Select all

sudo apt install minicom
However when doing this and shorting the Tx and Rx pins there is nothing still happening when I type anything into minicom...
It is like it is not outputting to the pin.
Bo Berglund
Sweden

cleverca22
Posts: 1816
Joined: Sat Aug 18, 2012 2:33 pm

Re: Problem using serial port /dev/ttyAMA0

Fri Oct 09, 2020 6:33 pm

if you unplug the adapter on the gpio header, and scope the tx pin again, does it give different results?
maybe the rx and tx are swapped, and you have 2 tx's fighting over the line?

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Problem using serial port /dev/ttyAMA0

Fri Oct 09, 2020 8:37 pm

cleverca22 wrote:
Fri Oct 09, 2020 6:33 pm
if you unplug the adapter on the gpio header, and scope the tx pin again, does it give different results?
maybe the rx and tx are swapped, and you have 2 tx's fighting over the line?
:shock: :oops: :oops: :oops:
After some work on re-configuring the RPi4 by editing the config.txt file and shutting down a service and what have you I could get the minicom to echo back what I typed into it after shorting pins 8 and 10 together. So I thought that all was well now.
But not...
Turns out after tracing a bit that I had connected the Tx/Rx pins to pin numbers on the 9-pin D-sub but using the 25-pin D-sub connection standard for the Tx and Rx lines. So it did work fine in minicom when I shorted pins 2 and 3 together (typed keys echoed back fine) but when I connected to the external device nothing.
So I traced the wires and found that Rx and Tx were switched in the cable....

Which means that initially serial was not even enabled (raspi-config), but additional config was then needed to actually get data out the door from ttyAMA0 (difference between RPi3 and RPi4). And so my mis-wiring on top of that...

Anyway, it now works and I have learned a lot, which is now written into my growing documentation file for RPi handling and configuration.
Thanks for your patience!! :D :lol:
Bo Berglund
Sweden

User avatar
bensimmo
Posts: 4988
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Problem using serial port /dev/ttyAMA0

Sat Oct 10, 2020 7:33 am

It's all a learning journey :-)

It's working and you know everything in more detail than you probably needed, that may come in useful.

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Solved: Problem using serial port /dev/ttyAMA0

Sat Oct 10, 2020 8:40 am

Yes, I definitely did not know all the things you need to do in order to get the serial port working!
Needs to go into a setup document for the project.
:D
Bo Berglund
Sweden

User avatar
bensimmo
Posts: 4988
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Solved: Problem using serial port /dev/ttyAMA0

Sat Oct 10, 2020 9:26 am

Bosse_B wrote:
Sat Oct 10, 2020 8:40 am
Yes, I definitely did not know all the things you need to do in order to get the serial port working!
Needs to go into a setup document for the project.
:D
It's really just use raspi-config to set it up.

The Bluetooth disable part is just an extra if you are not using it or have trouble with the mini-UART, which is not normally a problem for GNSS/GPS style, dust sensors etc. For what you are doing, I have no idea.

It's also why they setup the symbolic link to /dev/serial0 so you program it to use that and then it doesn't matter which way it's setup up or if it's on a Pi without BT (Pi2 or before and Zero 'none W').

Now the Pi4 has 4 other UART and pin sets it can use, but with a HAT you don't have the flexibility. Unless you made it yourself.

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Problem using serial port /dev/ttyAMA0

Sat Oct 10, 2020 12:43 pm

It's really just use raspi-config to set it up.
I have 2 RPi units for this project:
- One is my development device, an RPi4B in my office
- The other is an RPi3B hooked up to the prototype test equipment at a remote location

Now after the exercise above I have checked the state of the remote device (via VPN):

Code: Select all

$ ll /dev/se*
lrwxrwxrwx 1 root root  5 Oct  2 12:17 /dev/serial0 -> ttyS0
lrwxrwxrwx 1 root root  7 Oct  2 12:17 /dev/serial1 -> ttyAMA0
$ ll /dev/tty*
crw-rw---- 1 root dialout 204, 64 Oct  2 12:52 /dev/ttyAMA0
crw-rw---- 1 root dialout   4, 64 Oct  2 12:17 /dev/ttyS0
crw-rw---- 1 root dialout 188,  0 Oct  2 12:17 /dev/ttyUSB0
lrwxrwxrwx 1 root root          7 Oct  2 12:17 /dev/ttyUSB01 -> ttyUSB0
lrwxrwxrwx 1 root root          7 Oct  2 12:17 /dev/ttyUSB04 -> ttyUSB1
crw-rw---- 1 root dialout 188,  1 Oct  2 12:17 /dev/ttyUSB1
On this I am communicating to the test equipment via two USB-to-Serial adapters, which show up as ttyUSB0 and ttyUSB1 but for which I have made symlinks using the USB socket number so they are ttyUSB01 and ttyUSB04 respectively.
Since I am not using the built-in RPi serial port it does not matter where ttyAMA0 shows up, but when we install the proprietary RPi I/O board, which holds both the serial comm and the digital outputs it will (digital outputs on the prototype are the second USB adapter because it has a serial interface to the digital I/O).

As you see the situation here is opposite the one I have in the case of my dev RPi4B where it looks like this:

Code: Select all

$ ll /dev/se*
lrwxrwxrwx 1 root root  7 Feb 14  2019 /dev/serial0 -> ttyAMA0
lrwxrwxrwx 1 root root  5 Feb 14  2019 /dev/serial1 -> ttyS0
So if I were to use the RPi board instead of the two USB adapters on the prototype I will have to perform the extra steps too in order to get ttyAMA0 onto serial0.
So only using raspi-config as I did on the RPi3 is not sufficient.
Bo Berglund
Sweden

User avatar
bensimmo
Posts: 4988
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Problem using serial port /dev/ttyAMA0

Sat Oct 10, 2020 1:09 pm

Your Pi4 is switched around because you have disabled bluetooth. As mentioned before that still cannot be done in raspi-config.
I don't know if you need to disable it, I only mentioned it because you were not using it and can take advantage of not using it.

If your code points to /dev/serial0 it will point to the two GPIO pins, irrelevant of BT being enable or disabled (or switch around)
Talking to ttyS0 or ttyAMA0 causes the problem.

(USB is irrelevant here, that not controlled by 'serial setting' in any way)

If it's for production, then I would look at making a script to edit /boot/config.txt (to add the needed serial on and BT off) and /boot/cmdline.txt (to remove the console line) then disable the hciuart system service and anything else you need and reboot.
Or just have it all done in the initial image at the start, you don't really need to set each one up if you have a base image.

Steve1428
Posts: 1
Joined: Thu Nov 05, 2020 12:47 pm

Re: Problem using serial port /dev/ttyAMA0

Thu Nov 05, 2020 1:06 pm

Hi, I've got an Xbee that uses serial com's.
How do I scan for what is connected by serial and find out the exact name of the serial port.

I have a python script that queries for the number of a serial port, everything I try just says failure to communicate

Return to “General discussion”