I'm writing some code to read from a serial interface and return the integer value of the data received.
It seems that i need to remove the "\r\n" from it. I tried splitting, but it doesn't work.
Here's my code:
import time
import serial
import string
ser = serial.Serial(
port='/dev/ttyACM1',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
counter = 0
while 1:
x = ser.readline()
if "t" in x:
print x
x = int(x)
print x
print "temp"
elif "h" in x:
print "hum "
elif "g" in x:
print "gas "
else:
pass
time.sleep(1)
Then I have this error:
Traceback (most recent call last):
File "/home/pi/read.py", line 26, in <module>
x=int(x)
ValueError: invalid literal for int() with base 10: 't0\r\n'
Could anyone help?
tis what cause your problemtbefore attempting to convert it. What kind of processing? I'll let you figure it out yourself.