gpt4 book ai didi

python - Python 中的 read() 返回类型

转载 作者:行者123 更新时间:2023-11-30 23:18:33 32 4
gpt4 key购买 nike

我正在使用 Arduino 从引脚读取数据并通过串行端口发送值。根据返回的值,我想让 python 做一些事情,但我的 if 语句永远不会返回 true。

import serial
ser = serial.Serial('COM7',9600)

while True:
data = ser.read()
print data
if data == 'a':
print 'data received'

我能够更改引脚状态并打印出数据值,它确实返回“a”,但永远不会打印“收到的数据”。我做错了什么?

最佳答案

这是因为隐式换行符。将您的 if 语句更改为:

if data == 'a\n':

关于python - Python 中的 read() 返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26636786/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com