Pyserial测试

2024-10-01 11:41:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Pyserial和硬件领域的新手。我试图运行http://pyserial.sourceforge.net/shortintro.html#opening-serial-ports中给出的示例应用程序

import serial

ser = serial.Serial(0)  # open first serial port
print ser.portstr       # check which port was really used
ser.write("hello")      # write a string
ser.close()

我用python文件编写了这个程序并运行它。现在,如果我想测试这个应用程序来检查我是否发送了正确的字符串(例如:超级终端或其他什么),我该怎么做呢。有人能引导我吗?在


Tags: 应用程序httpnet硬件porthtmlserial领域
2条回答

另一种测试物理串行端口的快速方法是用电线/螺丝刀、鳄鱼钳或任何你手头上的东西,把RX和TX(接收和发送)桥接在一起。在这一点上,你发出的一切都会被循环到你身上。您可以在下面使用以下代码片段接收它:

import serial

ser = serial.Serial(0, timeout = 1)  # open first serial port
print ser.portstr       # check which port was really used
ser.write("hello")      # write a string
msg = ser.read("100") #read the content of the input buffer until you get 100 byte or a timeout event
print(msg) #print the content you might need to decode it print(decode(msg))
ser.close()

这段代码正常工作的关键是将RX和TX桥接在一起。很多教程都会告诉你如何做到这一点。在

virtual serial port

使用虚拟串行端口进行测试。在

对于Windows,我在Linux上使用com0comsocat。 然后,使用Putty可视化发送。在

相关问题 更多 >