【发布时间】:2021-05-04 16:16:09
【问题描述】:
我将光探测器传感器连接到数据采集盒,它通过 RS232 USB 电缆连接到我的笔记本电脑。我已经在 python 中稳定了与该端口的串行通信。但是当我尝试读取数据时,它只是继续运行并且不显示任何值。我在 MATALB 中尝试过同样的想法,它工作正常,所以我知道端口和传感器工作正常。我只是无法读取 python 中的数据。我有三种方法(如下python代码所示)但没有任何效果。请帮帮我。
这是我的python代码:
import serial
from serial import Serial
s = serial.Serial('COM3') # open serial port
print(ser.name)
# Set the serial port to desired COM
s = serial.Serial(port = 'COM3', bytesize = serial.EIGHTBITS, parity = serial.PARITY_NONE, baudrate = 9600, stopbits = serial.STOPBITS_ONE)
# Read the data(method 1)
while 1:
while (s.inWaiting() > 0):
try:
inc = s.readline().strip()
print(inc)
# Read the data(method 2)
data = str(s.read(size = 1))
print(data)
# Read all the data(method 3)
while i <10:
b = s.readlines(100) # reading all the lines
【问题讨论】:
-
设备是否输出换行符?快速查看pyserial.readthedocs.io/en/latest/shortintro.html 表明您想要
s.read。 -
我不确定。我什至如何检查。当我在 matlab 中执行此操作时,它给出了带有空格但在同一行中的值。我也尝试过 s.read ,但它没有返回任何值。它只是继续运行(执行)代码,但不显示任何值。
-
我运行了代码,它在 print(inc) 行暂停。它不会移动到其他行,甚至不会移动到超时异常。我认为到目前为止的代码读取了端口,但我缺少从 DAQ 的不同通道中提取数据的代码行。在 matlab 中,我使用以下代码读取数据: MATLAB Code: fwrite(s,strcat('#010',hex2dec('0D'))); out1 = fgetl(s) out1 = out1(2:end); out1 = str2num(out1);如果(out1
标签: python-3.x readline pyserial