使用python从RS 232端口读取

Ama*_*ose 6 python serial-port

我想用 python 编写一个程序来从 RS 232 端口读取数据。我的笔记本电脑没有这个端口。任何人都可以建议任何好的模拟器以及用于从 RS 232 端口读取的示例 python 程序。

Ama*_*ose 8

我得到了解决方案。我使用虚拟串行端口模拟器创建虚拟 2 个端口,并将这些端口配对。然后我使用 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)
Run Code Online (Sandbox Code Playgroud)