altanhaligur
Posts: 31
Joined: Mon Jul 08, 2019 6:54 am

UART communication between Pis

Tue Jul 16, 2019 12:52 pm

I want to communicate two RPis with UART. I connect Rx-tx and tx-rx and grounds of two RPis. when im starting communication with ser = serial.Serial("........") which port should i use?

hippy
Posts: 5568
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: UART communication between Pis

Tue Jul 16, 2019 1:32 pm

I believe it should be "/dev/serial0" for both sides. That will select the UART which is routed to GPIO pins 14 and 15 regardless of PI model and which UART is actually routed to those - /dev/AMA0 or /dev/ttyS0 otherwise.

You can use "ls -l /dev/serial*" to determine how things are routed on a particular Pi -

Code: Select all

pi@Pi3B:~ $ ls -l /dev/ser*
lrwxrwxrwx 1 root root 5 Jul 12 10:54 /dev/serial0 -> ttyS0
lrwxrwxrwx 1 root root 7 Jul 12 10:54 /dev/serial1 -> ttyAMA0
pi@Pi3B:~ $ 
There was I recall a bug with Buster that /dev/serial* were not being created but, from the above, that seems to have been resolved. Run "sudo apt-get update", "sudo apt-get upgrade" to ensure you have the latest software installed.
Last edited by hippy on Tue Jul 16, 2019 1:39 pm, edited 1 time in total.

altanhaligur
Posts: 31
Joined: Mon Jul 08, 2019 6:54 am

Re: UART communication between Pis

Tue Jul 16, 2019 1:38 pm

Oh okay i will try this, thanks

altanhaligur
Posts: 31
Joined: Mon Jul 08, 2019 6:54 am

Re: UART communication between Pis

Tue Jul 16, 2019 1:46 pm

GND pins RX and TX pins wired correctly.
I use this code for one RPi:

Code: Select all

import serial
from time import sleep
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=None)

while 1:
    pin = raw_input("Activate pin: ").strip("\r\n")
    port.write(pin)
    sleep(0.1)
And this code for another RPi:

Code: Select all

import serial
from time import sleep
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=None)
while 1:
  try:
    data_chunk = port.read()         
    time.sleep(0.1)         
    remaining_bytes = port.inWaiting() 
    data_chunk += port.read(remaining_bytes)
    print data_chunk
  except Exception, e:
    print str(e)
    pass
This codes doing nothing.

hippy
Posts: 5568
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: UART communication between Pis

Tue Jul 16, 2019 2:08 pm

altanhaligur wrote:
Tue Jul 16, 2019 1:46 pm
This codes doing nothing.
Maybe try "/dev/serial0" as suggested, or tell us which Pi model you are using so what else it should be can be determined.

I would also start with simpler code and slower baud rates, something like, untested ..

Code: Select all

import serial
import time
transmitter = serial.Serial("/dev/serial0", baudrate=4800)
while True:
  for c in "ABCDEF":
    transmitter.write(c+"\n")
    time.sleep(1)

Code: Select all

import serial
receiver = serial.Serial("/dev/serial0", baudrate=4800)
while True:
    c = receiver.read()
    if c:
      print("Got "+c)

altanhaligur
Posts: 31
Joined: Mon Jul 08, 2019 6:54 am

Re: UART communication between Pis

Tue Jul 16, 2019 2:27 pm

One is Pi4 one is Pi3B+, im using TXD0 RXD0 TXD0 RXD0

hippy
Posts: 5568
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: UART communication between Pis

Tue Jul 16, 2019 2:38 pm

altanhaligur wrote:
Tue Jul 16, 2019 2:27 pm
One is Pi4 one is Pi3B+, im using TXD0 RXD0 TXD0 RXD0
I am not familiar with the Pi 4B and its UART configurations. There may be additional things you need to do to get those working or changes which I am unaware of.

I would suggest using two pre-4B's to get things working with "/dev/serial0" so you know your software and wiring does work, then see what happens when you replace one with a 4B.

It will be far easier to resolve with a "this works but this doesn't" situation than trying to resolve things where one does not know what does and doesn't work.

User avatar
RaTTuS
Posts: 10365
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: UART communication between Pis

Tue Jul 16, 2019 2:43 pm

not tested on Pi4
but this is what I used previously
https://www.raspberrypi.org/forums/view ... 25#p828064
it may or may not help but YMMV
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

altanhaligur
Posts: 31
Joined: Mon Jul 08, 2019 6:54 am

Re: UART communication between Pis

Tue Jul 16, 2019 4:37 pm

hippy wrote:
Tue Jul 16, 2019 2:08 pm
altanhaligur wrote:
Tue Jul 16, 2019 1:46 pm
This codes doing nothing.
Maybe try "/dev/serial0" as suggested, or tell us which Pi model you are using so what else it should be can be determined.

I would also start with simpler code and slower baud rates, something like, untested ..

Code: Select all

import serial
import time
transmitter = serial.Serial("/dev/serial0", baudrate=4800)
while True:
  for c in "ABCDEF":
    transmitter.write(c+"\n")
    time.sleep(1)

Code: Select all

import serial
receiver = serial.Serial("/dev/serial0", baudrate=4800)
while True:
    c = receiver.read()
    if c:
      print("Got "+c)
When im using serial0, it gives me could not open serial0.
Then i run this code with sudo, and again code doing nothing.
Then i tried with ttyAMA0 and again does nothinh

altanhaligur
Posts: 31
Joined: Mon Jul 08, 2019 6:54 am

Re: UART communication between Pis

Tue Jul 16, 2019 4:55 pm

RaTTuS wrote:
Tue Jul 16, 2019 2:43 pm
not tested on Pi4
but this is what I used previously
https://www.raspberrypi.org/forums/view ... 25#p828064
it may or may not help but YMMV
Im not trying to do this, my goal is data transfer

Andyroo
Posts: 3756
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: UART communication between Pis

Tue Jul 16, 2019 5:22 pm

I would start by checking:

1) Console is disabled using sudo raspi-config
2) UART is enabled using raps-config
3) Checking cabling - TX -> RX and RX -> TX
4) running Minicom on both machines and see if the basic serial link is working

Only then look to the program aspect of the problem.
Need Pi spray - these things are breeding in my house...

hippy
Posts: 5568
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: UART communication between Pis

Tue Jul 16, 2019 5:46 pm

altanhaligur wrote:
Tue Jul 16, 2019 4:37 pm
When im using serial0, it gives me could not open serial0.
What does "ls /dev/serial*" report ? If /dev/serial0 is not there then it won't be able to open it.
altanhaligur wrote:
Tue Jul 16, 2019 4:37 pm
Then i run this code with sudo, and again code doing nothing.
Then i tried with ttyAMA0 and again does nothinh
Does nothing or gives an error message ? And is that on the transmitting or receiving side ?

You should not need to use sudo and that will not make it magically work.

On a Pi 3B, and I believe on the 3B+, that /ttyAMA0 goes to the Bluetooth chip. Probably the same for the Pi 4B but you would have to check.

You may have to run raspi-config or manually edit configuration files to enable the UART and disable the UART command console.

Take your Pi 3B+, connect its TXD to its own RXD, and run this -

Code: Select all

#!/bin/usr/python2

import serial
import threading
import time

comPort = None

def TransmitThread():
  while comPort:
    for n in range(ord("A"),ord("Z")+1):
      if comPort:
        comPort.write(chr(n))
        time.sleep(1)

def ReceiveThread():
  while comPort:
    if comPort.inWaiting() > 0:
      n = comPort.read(1)
      print( n )
    else:
      time.sleep(0.1)

def LoopbackTest(comPortName):
  global comPort

  comPort = serial.Serial \
            (
              port=comPortName,
              baudrate=4800,
              parity=serial.PARITY_NONE,
              stopbits=serial.STOPBITS_ONE,
              bytesize=serial.EIGHTBITS
            )

  threading.Thread(target=TransmitThread).start()
  threading.Thread(target=ReceiveThread).start()

  try:
    while True:
      time.sleep(1)
  except KeyboardInterrupt:
    print("")
  finally:
    comPort = None

if __name__ == "__main__":
  LoopbackTest("/dev/serial0")
That works on my 3B and should work on anything else. Once it's working on your 3B+ try the same on your 4B -

Code: Select all

pi@Pi3B:~/tmp $ python loopback.py
A
B
C
D
etc

altanhaligur
Posts: 31
Joined: Mon Jul 08, 2019 6:54 am

Re: UART communication between Pis

Tue Jul 16, 2019 7:04 pm

hippy wrote:
Tue Jul 16, 2019 5:46 pm
altanhaligur wrote:
Tue Jul 16, 2019 4:37 pm
When im using serial0, it gives me could not open serial0.
What does "ls /dev/serial*" report ? If /dev/serial0 is not there then it won't be able to open it.
altanhaligur wrote:
Tue Jul 16, 2019 4:37 pm
Then i run this code with sudo, and again code doing nothing.
Then i tried with ttyAMA0 and again does nothinh
Does nothing or gives an error message ? And is that on the transmitting or receiving side ?

You should not need to use sudo and that will not make it magically work.

On a Pi 3B, and I believe on the 3B+, that /ttyAMA0 goes to the Bluetooth chip. Probably the same for the Pi 4B but you would have to check.

You may have to run raspi-config or manually edit configuration files to enable the UART and disable the UART command console.

Take your Pi 3B+, connect its TXD to its own RXD, and run this -

Code: Select all

#!/bin/usr/python2

import serial
import threading
import time

comPort = None

def TransmitThread():
  while comPort:
    for n in range(ord("A"),ord("Z")+1):
      if comPort:
        comPort.write(chr(n))
        time.sleep(1)

def ReceiveThread():
  while comPort:
    if comPort.inWaiting() > 0:
      n = comPort.read(1)
      print( n )
    else:
      time.sleep(0.1)

def LoopbackTest(comPortName):
  global comPort

  comPort = serial.Serial \
            (
              port=comPortName,
              baudrate=4800,
              parity=serial.PARITY_NONE,
              stopbits=serial.STOPBITS_ONE,
              bytesize=serial.EIGHTBITS
            )

  threading.Thread(target=TransmitThread).start()
  threading.Thread(target=ReceiveThread).start()

  try:
    while True:
      time.sleep(1)
  except KeyboardInterrupt:
    print("")
  finally:
    comPort = None

if __name__ == "__main__":
  LoopbackTest("/dev/serial0")
That works on my 3B and should work on anything else. Once it's working on your 3B+ try the same on your 4B -

Code: Select all

pi@Pi3B:~/tmp $ python loopback.py
A
B
C
D
etc
When i run your code i get this error:
https://hizliresim.com/Xb4b9D
Attachments
sdsgsdsgfsf.png
sdsgsdsgfsf.png (20.49 KiB) Viewed 451 times
sdsgsdsgfsf.png
sdsgsdsgfsf.png (20.49 KiB) Viewed 451 times

Andyroo
Posts: 3756
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: UART communication between Pis

Tue Jul 16, 2019 7:08 pm

Andyroo wrote:
Tue Jul 16, 2019 5:22 pm
I would start by checking:

1) Console is disabled using sudo raspi-config
2) UART is enabled using raps-config
3) Checking cabling - TX -> RX and RX -> TX
4) running Minicom on both machines and see if the basic serial link is working

Only then look to the program aspect of the problem.
Have you done steps one and two?
Can you post the output of

Code: Select all

ls /dev
Need Pi spray - these things are breeding in my house...

altanhaligur
Posts: 31
Joined: Mon Jul 08, 2019 6:54 am

Re: UART communication between Pis

Thu Jul 18, 2019 9:15 am

Andyroo wrote:
Tue Jul 16, 2019 7:08 pm
Andyroo wrote:
Tue Jul 16, 2019 5:22 pm
I would start by checking:

1) Console is disabled using sudo raspi-config
2) UART is enabled using raps-config
3) Checking cabling - TX -> RX and RX -> TX
4) running Minicom on both machines and see if the basic serial link is working

Only then look to the program aspect of the problem.
Have you done steps one and two?
Can you post the output of

Code: Select all

ls /dev
Attachments
WhatsApp Image 2019-07-18 at 12.13.31.jpeg
WhatsApp Image 2019-07-18 at 12.13.31.jpeg (202.29 KiB) Viewed 348 times

hippy
Posts: 5568
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: UART communication between Pis

Thu Jul 18, 2019 11:22 am

/dev/serial0, /dev/serial1, /dev/ttyS0 and /dev/ttyAMA0 all appear to be there. You appear to be logged in as the default "pi" user. So it's not clear to me why you are getting "permission denied" errors. You could run the following commands and report back what you get -

uname -a
groups
ls -l /dev/serial*
ls -l /dev/ttyS0
ls -l /dev/ttyAMA0

For me, Pi 3B running Buster, I see -

Code: Select all

pi@Pi3B:~ $ uname -a
Linux Pi3B 4.19.57-v7+ #1244 SMP Thu Jul 4 18:45:25 BST 2019 armv7l GNU/Linux
pi@Pi3B:~ $ groups
pi adm dialout cdrom sudo audio video plugdev games users input netdev gpio i2c spi
pi@Pi3B:~ $ ls -l /dev/serial*
lrwxrwxrwx 1 root root 5 Jul 16 18:39 /dev/serial0 -> ttyS0
lrwxrwxrwx 1 root root 7 Jul 16 18:39 /dev/serial1 -> ttyAMA0
pi@Pi3B:~ $ ls -l /dev/ttyS0
crw-rw---- 1 root dialout 4, 64 Jul 16 18:52 /dev/ttyS0
pi@Pi3B:~ $ ls -l /dev/ttyAMA0
crw-rw---- 1 root dialout 204, 64 Jul 16 18:39 /dev/ttyAMA0
pi@Pi3B:~ $
If you have done something we are not aware of, tried to set something up which uses the UART in some way, it may be worth starting from a fresh Buster install to see if that works any better.

Return to “Advanced users”