使用Python从RS 232端口读取数据

6

我想用Python编写一个读取RS 232端口的程序,但我的笔记本电脑没有这个接口。

请问有没有好的仿真器可以使用,并提供一个从RS 232端口读取数据的Python示例程序。


1
你可以花几美元买一个便宜的USB-RS-232转接器,然后使用它。 - Paul R
1个回答

8
我有解决方案。我使用了一个虚拟串口模拟器来创建两个虚拟端口,并将它们配对。然后,我使用了pyserial python库来读取和写入这些端口。 从端口读取的示例代码
import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port='COM2',
    baudrate=9600,
    timeout=1,
    parity=serial.PARITY_ODD,
    stopbits=serial.STOPBITS_TWO,
    bytesize=serial.SEVENBITS
)
ser.isOpen()
# Reading the data from the serial port. This will be running in an infinite loop.

while 1 :
        # get keyboard input
        bytesToRead = ser.inWaiting()
        data = ser.read(bytesToRead)
        time.sleep(1)
        print(data)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接