Using a "walrus operator" for serial.read() error #15468
-
On an existing serial read of data from a battery management while True:
data = ser.read()
print (f'{data}')
if (data != None): # need to do this test first
var_sbms = data.decode('utf-8')
if len(var_sbms) == 61:
break # good chance you got valid data I get the following:
I was told that the serial data comes from this system once a second. Recently I was introduced to the "walrus operator", changing while (data := ser.read()):
print (f'{data}')
if (data != None): # need to do this test first
var_sbms = data.decode('utf-8')
if len(var_sbms) == 61:
break # good chance you got valid data Now gives me:
I observe that the print statement never executes BUT the processing It appears that the walrus operator "spits-the-dummy" and I can not What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Presuming the error is because you fall through the loop and # read serial data
# if data present, convert and check length
# if length is expected, stop
# keep trying
while True:
if (data:=ser.read()):
var_sbms = data.decode("utf-8")
if len(var_sbms) == 61:
break In your original walrus form, the loop will break on the first read where ser.read() returns None/empty string- returning an empty |
Beta Was this translation helpful? Give feedback.
Presuming the error is because you fall through the loop and
data
has no data, maybe what you want is:In your original walrus form, the loop will break on the first read where ser.read() returns None/empty string- returning an empty
data
string. Check the file and line-numbers to see when the string reference is failing.