Formatting string from serial device

I know I asked a similar question before (Formatting a return value from a serial device), but this time around my question is different. I am reading a value from an Arduino, that looks something like this:

value = b'446.45 mV\r\n'

What I need from this, is simply 446.45. My code below works to extract this value, but every once and a while it will decide to not work. I am calling this value many times, up to 10,000 or more. It is frustrating when I get the error: "ValueError: could not convert string to float: '' " when I am on the 9000th iteration of my data collection. Does anyone know what my issue might be?

value = ser2.readline() # gives something like: b'446.45 mV\r\n'
val_str = str(value)
count = val_str.count('.') #count number of decimal points
if count != 1: #make sure there is only one decimal point
    val_str = val_str[:6] #keep two decimal points 
w = val_str.strip("b")
x = w.strip("mV\\r\\n")
y = float(x) # usually gives: 446.45

Thanks in Advance.olehana

Please post a complete sketch that illustrates the problem

Are Python programs/scripts called sketches too?

Is there any error checking on the Arduino side to verify that you are indeed sending a float number in the proper format?

If there is indeed an actual error in transmission, then perhaps you need to add check sum or crc to the transmission and the python program can call for a resend if the receive is not valid.

IIRC there will be no 'b' in the string, the b'xxxxx' format is Pythons way to express binary data.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.