So i set up my rpi zero w to comunicate at 11500 serial with an arduino... the arduino sends over serial lines such as "~1 11443". My idea is that the ~ character tells the pi when to look for data, and the following number will tell the pi what kindof data is after the space. I wrote some code but i keep getting the error:
Traceback (most recent call last):
File "serial2rpi.py", line 11, in <module>
key = int(line[0])
IndexError: list index out of range
My code is this:
import serial
# check this below
serialport = serial.Serial("/dev/ttyAMA0", 9600, timeout=0.5)
while True:
line = serialport.readlines(None)
# good data
if '1' or '2' or '3' or '4' or '5' in line:
key = int(line[0])
else:
print("nope")
if line[0] == '~':
if key == 1:
lat = int(line.replace('~1 ', ''))
print(lat)
if key == 2:
lng = int(line.replace('~2 ', ''))
print(lng)
if key == 3:
alt = int(line.replace('~3 ', ''))
print(alt)
if key == 4:
sat = int(line.replace('~4 ', ''))
print(sat)
if key == 5:
crs = int(line.replace('~5 ', ''))
print(crs)
else:
print("oops:")
If anyone knows what error(s) i have made it would be greatly appreciated.
line[0]
is a character, not a nubmer. The character code for1
is 49. – Dmitry Grigoryev Nov 28 '19 at 08:39/dev/ttyAMA0
connects to Bluetooth - nothing to do with Arduino. See How do I make serial work on the Raspberry Pi3 or later You probaly have coding errors as well but we have no idea what you are doing. – Milliways Nov 28 '19 at 08:46