-1

I have enabled uart serial port /dev/ttyAMA0 on my raspberry pi zero W. I was able to successfully connect the rpi uart rx and tx pins and check if i'm able send and receive data between these rpi port. But when i connect to an external source of serial data, my program does not receive anything.

My goal is to receive and process async serial data which looks like this -logic analyser output.

Here's my code

import serial
test_string = "Test serial port ...".encode('utf-8')
try:
    port="/dev/serial0" # maps to "/dev/ttyAMA0"
    serialPort = serial.Serial(port, 9600, timeout=None)
    print ("Serial port", port, " ready for test :")
    while True:
        loopback = serialPort.read(4)
        print ("Received ",len(loopback), "bytes. Port", port,"is OK ! \n")
    serialPort.close()
except Exception as e:
    print ("Error on", port, e, "\n")

Setup - Connected the TX of the external source to the RX of RPI (physical PIN 10/GPIO 15).

What am i missing?

Karthik
  • 9
  • 3

2 Answers2

1

Oops. I forgot to connect the rpi and external TX device to common ground. Once i did that, my code now reads all the messages.

Karthik
  • 9
  • 3
0

"What am i missing?"

/dev/ttyAMA0 is connected to Bluetooth and is in use by the driver.

It is unclear what you have done or what you are trying to do but How do I make serial work on the Raspberry Pi3 or later may help.

On Raspberry Pi OS you should use dev/serial0 for the default serial port (after enabling it).

NOTE serialPort.read(1) is incorrect syntax (I am not a python expert) but reading a single byte is unlikely to be useful, as the buffer probably contains rubbish until it is read.

Milliways
  • 62,573
  • 32
  • 113
  • 225