data coming from arduino to python (raspberryPi) invalid literal error -


i have seen invalid literal error meaning data in int() on python side isn't base 10 digit expecting. that, on arduino side send across serial with:

void loop() {   temperature = bmp085gettemperature(bmp085readut());   pressure = bmp085getpressure(bmp085readup());   serial.println(temperature, dec);  //fails on python side, outputs 240   delay(50);   serial.println(pressure, dec); //pa   delay(1000); } 

and on python side pick this:

while true:      if(serialfromarduino.inwaiting() > 0):           input = serialfromarduino.readline().rstrip()           print(input)           inputasinteger =int(input)   #fails           print("done") 

even did rstrip make sure rid of \t\r\n etc.

so puzzled why constant

pi@raspberrypi ~/pythoncode $ python serialtest.py running 240 traceback (most recent call last):  file "serialtest.py", line 18, in <module>    inputasinteger =int(input)  valueerror: invalid literal int() base 10: '' 

just not sure missing? new python though feasible incredibly stupid doing.

sounds data has bunch of null bytes (ascii code 0x00). you'll want remove them strip('\x00'). default strip() removes whitspace, doesn't include null bytes. specify bytes remove manually.

 if(serialfromarduino.inwaiting() > 0):       input = serialfromarduino.readline().strip().strip('\x00')      # <--       print(input)       inputasinteger = int(input)       print("done") 

perhaps better solution identify where null bytes coming , prevent huge waste of serial bandwidth happening.

ps. reason error message unhelpful here:

valueerror: invalid literal int() base 10: '' 

is because string formatting invalid literal error message. however, library call along way interpreting null byte null-terminator , not printing rest of string.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -