Python decoding data from serial port and storing into a list

43 views Asked by At

I'm using python and pyserial to communicate with an external hardware. Right now, I can send data and receive data from the hardware.

My problem is that when I read data as below , I can already read and split the channel from value. Everything is okay, until I try to add the channels into a list.

read_data:

response_line = self.main.serial_port.readline().decode('utf-8')
                if response_line and "calling" not in response_line :  
                    print(f"\t\t{response_line}")
                    self.add_channel(response_line)
                    if time.time() - start_time > 0.01 and not self.channel_detected:
                        self.channel_detected_number = len(self.channels_detected_list)
                        print(f"{self.channel_detected_number} channels detected")
                        break

add_channel:

data_parts = response.split('=')
 
    if self.channels_detected_list.count(data_parts[0]) > 0:
        print("Channel already present.")
    else: 
        #add channels 
        self.channels_detected_list.append(data_parts[0])
        print(self.channels_detected_list)

When I print the list -> These strange values will output

But when I access the element's like the second element of the list or whatever, the values /x00 will not print and only the real value of the channel will print.

1

There are 1 answers

0
toppk On

Nulls (\x00) don't print. If you want to see the raw hex value, then use a print formatter, for example:

>>> a = '\x00'
>>> print(f'{a!r}')
'\x00'