UART Serial Read Error (Device reports readiness but returned no data)

I’m trying to hook up an RFID reader to pin 15 on my raspberry pi. When I test this on stock Raspberry OS, it works, but when trying it on balena, I get errors on scanning an RFID. I have enabled UART in the fleet variables, and I’m running FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run as my image. The code is pretty basic (and as I mentioned works fine on stock) but here it is:

import serial, time

# Setup Serial Port
ser = serial.Serial(
    port='/dev/ttyS0',
    baudrate = 9600
)

while 1:
    rfid = ser.readline()
    data_left = ser.inWaiting()
    rfid += ser.read(data_left)
    print (rfid)

When I scan an RFID I get the message
Traceback (most recent call last):
File “serialtest.py”, line 12, in
rfid = ser.read()
File “/usr/local/lib/python3.6/site-packages/serial/serialposix.py”, line 596, in read
'device reports readiness to read but returned no data ’
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

Anyone have any ideas on this? Previously I used USB readers and they worked great, but this year I’m working with serial over the GPIO and having no end of trouble.

If you’re using a balenaOS dev image, the serial port is already used by balenaOS for debug infos.
So this could be a problem. Only the “production” image has a clean serial port :slight_smile:

login to your device, balena ssh <device ip> and type the following commands,

mount -o remount,rw /
systemctl mask serial-getty@ttyAMA0.service
systemctl mask serial-getty@serial0.service
systemctl mask serial-getty@serial1.service
reboot

These commands will prevent from being occupied by balenaOS.

1 Like

Ok, so this worked. My question now is, how do I do this programmatically instead of having to manually ssh in to change each device in the fleet? or do I just do like @nmaas87 mentions above and use a production image?

Maybe you should use production image or build your own image with yocoto.

1 Like

I really would go with a production image. It is also more secure (you do not have the root, empty password ssh login but need to integrate an ssh key into the config.txt to access it) - and I don’t know what happens if you update that modified dev version down the road. There is always the chance your changes will be lost and your services will then fail again.

Hello,

Did you have any luck using the production image? Please let us know if you’re still having trouble.