【问题标题】:Reading a serial signal from a USB port with Python使用 Python 从 USB 端口读取串行信号
【发布时间】:2020-05-26 21:25:19
【问题描述】:

我是新手,所以如果我的问题没有受过教育,我深表歉意: 我有一个我知道 ProductID 和 VendorID 的 USB 设备。据我了解,它应该是一个向我的计算机发送串行流的 HID 设备。我打算用 python 编写一个脚本来读取那个串行流,但我一无所获。 Python 的串行库有没有办法从 PID 和 VID 中找到设备?

【问题讨论】:

  • 您好,欢迎初学者,新手一点都没有错。但首先,当你说,“我打算用 python 编写一个脚本来读取那个串行流,但我无处可去。”你都尝试了些什么?你怎么失败了?错误信息?stackoverflow.com/help/how-to-ask

标签: python pyserial libusb usbserial


【解决方案1】:

你可以在 OSX 上找到:

tty
/dev/ttys000

或者:

$ who
trane    console  Sep  1 05:18 
trane    ttys000  Sep  1 05:19 
trane    ttys001  Sep  1 05:19

$ w
13:04  up 1 day,  7:46, 3 users, load averages: 1.85 2.02 3.87
USER     TTY      FROM              LOGIN@  IDLE WHAT
trane    console  -                Sun05   31:45 -
trane    s000     -                Sun05       - w
trane    s001     -                Sun05       9 -bash

你可以试试这样的:

import serial;
import io;
import time;
import os;

if __name__ == '__main__' :
    try :
        # configure the serial connections (the parameters differs on the device you are connecting to)
        with serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=1,
                       xonxoff=False, rtscts=False, dsrdtr=True) as s:
            for line in s:
                print(line)

    except :
        print('Program exit !')
        pass
    finally :
        ser.close()
    pass

或者:

import serial, sys
port = your_port_name
baudrate = 9600
ser = serial.Serial(port,baudrate,timeout=0.001)
while True:
    data = ser.read(1)
    data+= ser.read(ser.inWaiting())
    sys.stdout.write(data)
    sys.stdout.flush()

根据您的设备,您可以调整一些参数为:

parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
相关资源
最近更新 更多