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