Mattb1969
Posts: 8
Joined: Sat Aug 27, 2016 9:29 am

Detecting UART port on different versions of Pi

Sat Aug 27, 2016 9:38 am

HI,

I am trying to programatically identify whether the UART on the Pi is to be addresses as /dev/ttyAMA0 or as /dev/serial0. I have seen number of posts about checking your version against cpuinfo, but everything seems to suggest that there isn’t a clear distinction that I can use.

I would like my program to have something like this

Code: Select all

if Pi3:
     sp = serial.Serial(‘/dev/serial0’)
else
     sp = serial.Serial(‘/dev/ttyAMA0’)
but I can’t seem figure it out??

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Detecting UART port on different versions of Pi

Sat Aug 27, 2016 11:48 pm

If you think its unlikely that both can exist at the same time why not just check if serial0 exists, and if it doesnt use ttyAMA0.

And its probably not a hardware thing. its either a kernel- or firmware-version thing. I'm guessing that the newest raspbian will create serial0 on all RPi versions. ( but I might be wrong )

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

Re: Detecting UART port on different versions of Pi

Sun Aug 28, 2016 6:33 am

serial0 is used on all versions since sometime after the Pi3 launch and automatically sets itself to the GPIO UART. That's what I was told.

Both will exist iirc except ttyAMA0 directs to the Bluetooth on the Pi3*

Assuming this needs to be backwards compatible in case others use it (you could just recommend a specific Raspbian revision), then just do a device check.



*But with the Pi3 there may be more to it, since you might need to set up the mini UART to actually give consistent results via the config.txt depending on what you are using it for.
Or the user has disabled Bluetooth and set the Pi3 to have the gpio uart use the full uart then it will behave like the other Pis or switched them around, perhaps muddling the waters a bit. [ dtoverlay=pi3-disable-bt or pi3-miniuart-bt ]
I believe serial0 should still point to whatever the GPIO UART no matter what though as long as udev rules are running ?

dgordon42
Posts: 788
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Detecting UART port on different versions of Pi

Wed Aug 31, 2016 5:36 pm

Mattb1969 wrote:I am trying to programatically identify whether the UART on the Pi is to be addresses as /dev/ttyAMA0 or as /dev/serial0.
The trick, if you want your software to run on all models of Pi, is to run them all on up to date Raspbian Jessie.
Then:
Enable the Serial Port by adding:

Code: Select all

enable_uart=1
to the end of '/boot/config.txt'
If you don't want boot messages, or a log-in on the Serial Port, remove the phrase:

Code: Select all

console=serial0,115200
from '/boot/cmdline.txt'
Reboot.
Refer to the Serial Port as "/dev/serial0" in code. (That's serialZero, not serialOh!)

This procedure should take care of the differences between the Pi 3B's Serial Port, and the Serial Port on previous models. It also allows the Pi 3B to use it's built in Bluetooth (and WiFi) normally, and fixes the baud rate fluctuations which plagued the Pi 3B at launch.

There's more on the Serial Port saga here.

Finally don't forget that on all models, the Serial Port GPIO pins work at 3.3V, they are not 5V tolerant.

Hope this helps,
Dave.

Mattb1969
Posts: 8
Joined: Sat Aug 27, 2016 9:29 am

Re: Detecting UART port on different versions of Pi

Tue Sep 20, 2016 6:29 pm

Thanks for the responses.

So I have looked at the advice and checked my 2B, the one that I use for most of my development and works fine on the UART port ttyAMA0. There is no /dev/serial0 listed, even if I add enable_uart=1 to the config file. I had also been told that it was in all distributions, but it is not in my Jessie distribution. From what I understand the advent of serial0 came about because the latest boards have Bluetooth which uses the default serial port.

So I think the only solution I have is to use a try / except loop to detect the presence of serial0 and use ttyAMA0 if not.

I am really surprised there is no way of detecting the hardware of the pi from within the software.

dgordon42
Posts: 788
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Detecting UART port on different versions of Pi

Wed Sep 21, 2016 12:21 pm

Mattb1969 wrote:I had also been told that it was in all distributions, but it is not in my Jessie distribution
It should be, if your Jessie distribution is up to date. We are currently on:

Code: Select all

Linux RaspberryPi 4.4.13-v7+ #894 SMP Mon Jun 13 13:13:27 BST 2016 armv7l GNU/Linux
You can update with the "apt-get" command set.

Dave.

Return to “Advanced users”