1

I'm new to this, so I apologize if my question is uneducated: I have a USB device for which I know the ProductID and VendorID. From what I understand, it should be a HID device which sends a serial stream to my computer. I was to write a script in python in order to read that serial stream, but I am getting nowhere. Is there a way for Python's serial library to find a device from a PID and VID?

Zachary E.
  • 11
  • 1
  • 1
  • 2
  • 1
    Hello and welcome begeiner, nothing at all wrong with being new. But first, when you said, "I was to write a script in python in order to read that serial stream, but I am getting nowhere." What have you tried so far? How have you failed? Error messages?https://stackoverflow.com/help/how-to-ask – Akib Rhast May 26 '20 at 21:36

2 Answers2

1

You can find on OSX:

tty
/dev/ttys000

Or:

$ 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

You can try something like this:

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

Or:

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()

According to your device, you can adjust some parameters as:

parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS
Mahsa Hassankashi
  • 2,086
  • 1
  • 15
  • 25
  • Problem I have is my mac does not seem to recognize it under the ttyUSB0 (or any other) port as far as I can tell I see it under the USB Device Tree in the system settings, but when is earch for used tty addresses, I don't get anything. – Zachary E. May 29 '20 at 16:21
  • Please write on terminal: ioreg -r -c IOUSBHostDevice -l , look at https://apple.stackexchange.com/questions/242104/is-there-a-way-to-access-a-usb-serial-port-by-the-device-id-not-by-the-tty-po and https://stackoverflow.com/questions/6316584/access-usb-serial-ports-using-python-and-pyserial I also changed my answer again. – Mahsa Hassankashi May 29 '20 at 16:31
0

You can also just do an 'ls /dev/tty*.*' and find the USB device rather than go through to voluminous amount of garbage dumped by 'ioreg'.

Steve
  • 1