Python Forum
Writing to two different serial ports independent of each other
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing to two different serial ports independent of each other
#1
I have a Python program in which I am trying to write to two different serial displays
I define port1 and port2 as shown below:
port1=serial.Serial("/dev/serial0",baudrate=9600,timeout=3.0)
port2=serial.Serial("/dev/devicename",baudrate=9600,timeout=3.0)
The output for port1 is on GPIO14 (pin 8)
I cannot find a devicename to use for port2 that does not give an error.

The Raspberry Pi file config.txt has an entry as follows:
[all]
enable_uart=1

I believe that I have to enable a uart for my second display.
I added the following under the [all] section:
dtoverlay=uart2
enable_uart=2
the Raspberry Pi documentation states uart2 TX is GPIO0 (pin27) and RX is GPIO1 (pin 28)

I can write to the display on GPIO14 using the port1 definition with no issue.

My issue is what do I use for the devicename in the port2 statement and is what I did to enable uart2 correct? I am using a Raspberry Pi 4B and Raspberry Pi OS version Bookworm.
Reply
#2
Read this https://www.raspberrypi.com/documentatio...gure-uarts

I guess you're missing following
dtoverlay=disable-bt
It disables the bluetooth device, which uses the faster secondary UART.
Disabling bluetooth, frees the secondary UART.

You should also disable hciurart service:
sudo systemctl disable hciuart
Then after a restart, a second UART should appear. Maybe you have to switch off the Linux console on the first UART, but I guess you did it already.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
I added the dtoverlay=disable-bt to the [all] section of config.txt and executed
sudo systemctl disable hciuart as you stated.

I then found device ttyAMA2 and added it as the device name in my port2 statement and now I can write independently to both LCD displays on GPIO14 and GPIO0 respectively.

Thank you so much for your response.

My Linux monitor is connected to the micro HDMI port on my RPI 4.

Is there any way to have two uarts to write to two different serial devices without having to disable but?

Thanks again.
Reply
#4
(Jul-17-2024, 12:12 AM)Aggie64 Wrote: Is there any way to have two uarts to write to two different serial devices without having to disable but?

Yes
  • using an additional UART-TTL USB Adapter 3.3V and 5V which is very cheap below 10$
  • deactivating the internal Bluetooth and using an external Bluetooth USB dongle (is expensiver)

With the UART-TTL USB Adapter you can even add more UARTs if you want.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(Jul-17-2024, 12:12 AM)Aggie64 Wrote: I added the dtoverlay=disable-bt to the [all] section of config.txt and executed
sudo systemctl disable hciuart as you stated.

I then found device ttyAMA2 and added it as the device name in my port2 statement and now I can write independently to both LCD displays on GPIO14 and GPIO0 respectively.

Thank you so much for your response.

My Linux monitor is connected to the micro HDMI port on my RPI 4.

Is there any way to have two uarts to write to two different serial devices without having to disable but?

Thanks again.
Reply
#6
With the help I received, I was finally able to get two serial ports working on my Raspberry Pi 4.
Below are the changes I made to enable two serial ports.
Edit config.txt which is located at /boot/firmware on Bookworm
scroll to the [all] section of config.txt
Add the following commands below enable_uart=1 which should already be there.
dtoverlay=disable-bt #disables bluetooth
dtoverlay=uart2
enable_uart=2

Save the above three changes to config.txt

at a terminal prompt, enter the following: sudo systemctl disable hciuart

enter ls/dev at a terminal prompt and you should see devices serial0 and ttyAMA2

In Python, I define my two serial ports with the three following statements:
import serial
port1=serial.Serial=("/dev/serial0",baudrate=9600,timeout=3.0)
port2=serial.Serial=("/dev/serial0",baudrate=9600,timeout=3.0)

write to serial port1 as port1.write(serialstring.encode("ascii")) where serialstring is a string of ascii characters to be serially transmitted.
Reply
#7
I would expect:
port1=serial.Serial=("/dev/serial0",baudrate=9600,timeout=3.0)
port2=serial.Serial=("/dev/ttyAMA2",baudrate=9600,timeout=3.0)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 11 22,840 Sep-27-2024, 12:18 PM
Last Post: NigelHalse
  Uninstall unused COM ports windows 10 adbrooker 1 2,879 Sep-22-2021, 03:16 AM
Last Post: Larz60+
  Mido and virtual midi ports dore_m 2 6,461 Dec-27-2020, 06:02 AM
Last Post: dore_m
  Handling Multiple USB ports in Python samalpramod 0 6,255 Aug-01-2020, 07:40 PM
Last Post: samalpramod
  Writing commands to serial python_beginner 8 70,551 Jun-23-2020, 08:59 PM
Last Post: cvh
  Loop independent of excecution time of a script Forelli 8 5,317 Feb-02-2020, 10:49 PM
Last Post: snippsat
  Trouble writing over serial EngineerNeil 1 3,336 Apr-07-2019, 08:17 PM
Last Post: j.crater
  Independent Secondary index for dictionary Larz60+ 10 6,798 Dec-20-2018, 07:59 PM
Last Post: buran
  Get Serial ports via Function shift838 1 5,029 Dec-10-2018, 03:52 AM
Last Post: Alfalfa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020