-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
  • 2
  • I followed https://di-marco.net/blog/it/2020-06-06-raspberry_pi_3_4_and_0_w_serial_port_usage/#option-1--using-the-real-pl011-uart-port to enable uart and disable bluetooth. – Karthik Aug 15 '21 at 00:27

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
  • 2
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
  • 59,890
  • 31
  • 101
  • 209
  • thanks for the reply! Made the changes u recommended. i replaced /dev/ttyAMA0 with /dev/serial0 in my code and now read 4 bytes. But still no difference. Also, I disabled the bluetooth module so that i can use /dev/ttyAMA0. And i did follow the instructions to enable uart. I was able to do a local loopback test, where i write and read the data. This test works fine. But when i connect it to the external source, i dont read anything. – Karthik Aug 15 '21 at 00:15
  • I followed https://di-marco.net/blog/it/2020-06-06-raspberry_pi_3_4_and_0_w_serial_port_usage/#option-1--using-the-real-pl011-uart-port to enable uart and disable bluetooth. – Karthik Aug 15 '21 at 00:26
  • @Karthik I didn't read all the tutorial you followed, but it is outdated, not best practice and contains some erroneous statements. Use raspi-config rather than fiddling with files. Frankly I don't understand the fixation so many have with disabling Bluetooth - certainly at 9600 baud you will see absolutely no problems with /dev/ttyS0. – Milliways Aug 15 '21 at 00:50
  • Without details of what is connected and diagnostics it is impossible to say what your problem is. NOTE additional detail belongs in your Question, not comments. – Milliways Aug 15 '21 at 00:52