I have a Raspberry PI 3 and a GSM/GPRS/GNSS HAT.
I want to read the GPS data from the device with Python. I have used the example code from the documentation, and rewrote it a bit. It was working for a couple of hours perfectly, but one time when I rebooted the Raspberry(I have rebooted it before and it was working fine) it started throwing this after a couple of successful reads:
Traceback (most recent call last):
File "/home/ubuntu/gps.py", line 90, in listenForGpsInfo
while ser.inWaiting() > 0:
File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 594, in inWaiting
return self.in_waiting
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 531, in in_waiting
s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str)
OSError: [Errno 5] Input/output error
here is my code:
def listenForGpsInfo(callback):
ser = serial.Serial("/dev/ttyS0",baudrate=115200)
W_buff = [b"AT+CGNSPWR=1\r\n", b"AT+CGNSSEQ=\"RMC\"\r\n", b"AT+CGNSINF\r\n", b"AT+CGNSURC=2\r\n", b"AT+CGNSTST=1\r\n"]
ser.write(W_buff[0])
ser.flushInput()
data = ""
num = 0
while True:
time.sleep(utils.GPS_INTERVAL_IN_SECONDS)
try:
while ser.inWaiting() > 0:
data += ser.read(ser.inWaiting()).decode()
print(data)
if data != "":
if num < len(W_buff)-1:
print(num)
ser.write(W_buff[num+1])
num =num +1
else:
ser.write(W_buff[2])
if "+CGNSINF" in data:
data = str(data)
gpsInfo = parseGpsData(findInfoLine(data))
if(gpsInfo is not None):
callback(gpsInfo)
data = ""
except Exception as e:
if ser != None:
ser.close()
traceback.print_exc()
listenForGpsInfo(callback)
return
Here is the documentation of the serial commands: https://www.waveshare.com/w/upload/3/3d/SIM868_GNSS_Application_Note_V1.00.pdf
I tried a lot of things, but I couldn't solve it. Couple of things I have tried:
AT+CGNSPWR=1\r\n
and the AT+CGNSINF\r\n
commandschmod 666 /dev/ttyS0
I had ubuntu on the raspberry and when I installed raspberry os the error went away.