0

I have seen this invalid literal error meaning the data in the int() on the python side isn't a base 10 digit like its expecting. I get that, but on the arduino side I send it across serial with:

void loop()
{
  temperature = bmp085GetTemperature(bmp085ReadUT());
  pressure = bmp085GetPressure(bmp085ReadUP());
  Serial.println(temperature, DEC);  //fails on python side, outputs a 240 usually
  delay(50);
  Serial.println(pressure, DEC); //pa
  delay(1000);
}

and on the python side I pick it up with this:

while True:
     if(serialFromArduino.inWaiting() > 0):
          input = serialFromArduino.readline().rstrip()
          print(input)
          inputAsInteger =int(input)   #FAILS
          print("done")

Even did an rstrip to make sure i get rid of any \t\r\n etc.

So I am puzzled as to why the 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 for int() with base 10: ''

Just not sure what I am missing? New to python though so its feasible its something incredibly stupid I am doing.

3
  • Just so you know exactly what you're dealing with, on the debug print, try this: print(input.encode('hex')) Commented May 20, 2013 at 5:31
  • awesome! okay didnt know I had that option, ran again got a bazillion 0's and it ends in: 0323430 is this an ascii value? before the previous number is a count of 2951 0s Commented May 20, 2013 at 5:35
  • Yes, 00 32 34 30 is (null-byte) '2' '4' '0'. So your data is being prepended with a bunch of null characters. See my answer for how to remove them. I'm not sure why it's happening though. What is serialFromArduino? Commented May 20, 2013 at 5:41

2 Answers 2

1

Sounds like your data has a bunch of null bytes (ASCII code 0x00). You'll want to remove them with strip('\x00'). By default strip() only removes whitspace, which doesn't include null bytes. So we specify what bytes to remove manually.

 if(serialFromArduino.inWaiting() > 0):
      input = serialFromArduino.readline().strip().strip('\x00')      # <--
      print(input)
      inputAsInteger = int(input)
      print("done")

Perhaps the better solution is to identify where the null bytes are coming from and prevent this huge waste of serial bandwidth from happening.

PS. The reason the error message was so unhelpful here:

ValueError: invalid literal for int() with base 10: ''

Is probably because they are string formatting the invalid literal into their error message. However, some library call along the way is interpreting the null byte as a null-terminator and not printing the rest of the string.

Sign up to request clarification or add additional context in comments.

4 Comments

If i had to guess its in the calls to the library im using i didn't write: ` temperature = bmp085GetTemperature(bmp085ReadUT()); pressure = bmp085GetPressure(bmp085ReadUP());`
p.s. stripped out the bytes using that strip \x00 and it printed same thing in the hex call
I think you realize it, but strip() returns the modified string; it doesn't modify it in-place.
ha so didn't realize, its working now :) woo hoo thanks! now my pachube library is woefully old it seems, thats for tomorrow. awesome guys thanks!
1

The error means that your input received an empty line '' and you try to convert it to an integer.

You should cautiously process the external input at input = serialFromArduino.readline(). If it is not a proper integer (like, empty line or some noise) you program will crash. You should, for example:

try:
    input = serialFromArduino.readline().rstrip()
except ValueError:
    raise InputError('got bad value {}'.format(input))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.