使用python从RS 232端口读取

时间:2014-05-08 06:39:14

标签: python serial-port

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

1 个答案:

答案 0 :(得分:1)

我得到了解决方案。我使用虚拟串口模拟器来创建虚拟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)
相关问题