我正在使用 python 和 pyserial 与外部硬件进行通信。现在,我可以从硬件发送数据和接收数据。
我的问题是,当我读取如下数据时,我已经可以从值中读取并拆分通道。 一切都很好,直到我尝试将频道添加到列表中。
读取数据:
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
添加频道:
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)
当我打印列表时 ->
但是当我访问该元素(例如列表的第二个元素或其他元素)时,值 /x00 将不会打印,而只会打印通道的实际值。
不打印空值 (\x00)。如果您想查看原始十六进制值,请使用打印格式化程序,例如:
>>> a = '\x00'
>>> print(f'{a!r}')
'\x00'