[英]pySerial wait for “#” character to print output from USB serial device
I'm writing a python script that connects to a USB serial device. 我正在编写一个连接USB串行设备的python脚本。 Whenever a command is sent and executed, the PIC returns with a hashtag.
无论何时发送和执行命令,PIC都会返回一个#标签。 Ie.
IE浏览器。 "Command executed successfully. \\n# "
“命令执行成功。\\ n#”
I'd like my python script to wait for the hashtag before outputting the data. 我希望我的python脚本在输出数据之前等待hashtag。 How can I do this?
我怎样才能做到这一点? Here's what I have.
这就是我所拥有的。 It doesn't seem to actually print the text received from the PIC.
它似乎没有实际打印从PIC收到的文本。 Any help is appreciated
任何帮助表示赞赏
if port.isOpen():
try:
for x in range(0,100):
time.sleep(0.05)
port.write("command 1" + "\r\n")
numLines = 0
// wait for "#" to print output
while True:
response = port.readline()
if "#" in response:
print(response)
numLines = numLines + 1
if(numLines >= 1):
break
time.sleep(0.05)
port.write("command 2" + "\r\n")
numLines = 0
// wait for "#" to print output
while True:
response = port.readline()
if "#" in response:
print(response)
numLines = numLines + 1
if(numLines >= 1):
break
time.sleep(0.05)
port.write("command 3" + "\r\n")
numLines = 0
// wait for "#" to print output
while True:
response = port.readline()
if "#" in response:
print(response)
numLines = numLines + 1
if(numLines >= 1):
break
except Exception, e1:
print("An error occured: " + str(e1))
port.close()
port.readline()
will read the serial port till it receives a \\n
. port.readline()
将读取串口,直到它收到\\n
。 So, the response will contain the string "Command executed successfully. \\n". 因此,响应将包含字符串“Command successfully successfully。\\ n”。 Since there is no "#" in this string, again the code will encounter the
port.readline()
statement. 由于此字符串中没有“#”,因此代码将再次遇到
port.readline()
语句。 This time it will read "#" but since there is no "\\n", the code will be stuck there resulting in an infinite loop. 这次它将显示“#”但由于没有“\\ n”,代码将被卡在那里导致无限循环。
Pyserial provides a method called read():
Pyserial提供了一个名为
read():
的方法read():
read(size=1)
Parameters: size – Number of bytes to read.
参数:size - 要读取的字节数。 Returns: Bytes read from the port.
返回:从端口读取的字节数。 Return type: bytes Read size bytes from the serial port.
返回类型:bytes从串行端口读取大小字节。 If a timeout is set it may return less characters as requested.
如果设置了超时,则可能会根据请求返回较少的字符。 With no timeout it will block until the requested number of bytes is read.
没有超时,它将阻塞,直到读取所请求的字节数。
read() provides a parameter size (with default =1) which specifies the number of bytes to be read. read()提供参数大小(默认值= 1),指定要读取的字节数。 So, you can specify the number of bytes in the string sent by the PIC as a parameter.
因此,您可以将PIC发送的字符串中的字节数指定为参数。 You can also use the following alternative:
您还可以使用以下替代方法:
// wait for "#" to print output
while True:
response += port.read()
if "#" in response:
print(response)
numLines = numLines + 1
if(numLines >= 1):
break
If you send some white space to the device, as if it were a terminal command, it will prod it into a response with your "#" in it. 如果您向设备发送一些空白区域,就好像它是一个终端命令一样,它会将其转换为带有“#”的响应。 I have been successfully using that method.
我已成功使用该方法。 Specifically I send a single space " " plus the terminal line ending (ie "\\n" or "\\r\\n" depending on the device).
具体来说,我发送单个空格“”加上终端行结束(即“\\ n”或“\\ r \\ n”取决于设备)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.