根据发布的代码,您正试图执行某种函数,根据指定的文本打开串行端口。
try:
serial_port = '/dev/cu.usbserial-10'
#you're only assigning the value to a variable . you actually need to call the function that youre testing with that variable here
except:
try:
serial_port = '/dev/cu.usbserial-110'
except:
print("Unable to find sensor. Try again")
这是一个示例代码:
try:
serial_port = '/dev/cu.usbserial-10'
response=open_serial_port(serial_port) #assuming 'open_serial_port' is the function name.
except:
try:
serial_port = '/dev/cu.usbserial-110'
response=open_serial_port(serial_port) #assuming 'open_serial_port' is the function name.
except:
print("Unable to find sensor. Try again")
这是一个如何修复它的例子。
以下是您提供的更新代码:
import serial
serial_port = 0
baud_rate = 115200
try:
serial_port = '/dev/cu.usbserial-10'
ser = serial.Serial(serial_port, baud_rate) # serial data, non-usable
except:
try:
serial_port = '/dev/cu.usbserial-110'
ser = serial.Serial(serial_port, baud_rate) # serial data, non-usable
except:
print("no")
try:
while True:
rawdata = ser.readline().decode().strip() # raw string data from serial
print(rawdata)
except:
pass