In this article, Iam going to explain how you get device serial number in python with a program.
I would like to explain, on how to do this in python:
·
Identify the port named a specific name in the serial com
(\Device\VCP0 and \Device\VCP1 these are get by browsing in regedit window)
·
And get the id of the device that is pluged.
We have two answer :-
1) Because this relies on the hardware available, it is
perfectly possible that the test code worked in the environment it was written
on, but doesn't work in your environment - may be quite likely if you are on
Windows and this was written on Linux. The code uses port 0 - don't know how
that maps to COM1 etc.
2) On Windows, COM ports used to have DOS names like COM1, COM2
- i.e. A string, not an int (they aren't like TCP/IP port numbers).
More recently in Windows there is the \.\COMnotanumber format
which allows a more generic name, I've seen these used by a USB to serial
converter. Having had a quick look at the source code of pyserialSerialBase in
serialutil.py, it's a bit odd IMO, because AFAICT self.name only gets set when
you use an explicit port setting by calling self.port(portname).
You might want to try intializing the serial port instance with
serport = Serial(0) then explicitly calling serport.port('COM1') (or whatever
your port name is instead of COM1).
import serial
def scan():
available =[]
for i in range(256):
try:
s =serial.Serial('COM'+str(i))
available.append((s.portstr))
s.close()
except serial.SerialException:
pass
for s in available:
print"%s"%(s)
if __name__=='__main__':
print"Found ports:"
scan()
PySerial is very good
multi-platform Serial library but offers useless features for basic
communication between a Pi and electronic components like Arduino.
WebIOPi allows to use
Serial devices natively without PySerial. It uses a lightweight library
instead, dedicated to electronic interfacing, and shares a common abstraction
with I2C and SPI drivers. So you will found the same write & read functions
on all three interfaces.
Serial driver also allows
a RAW access directly with the REST API and so with the Javascript client
library. This is only intended to be used with full-text Serial protocols, not
for low-level and binary interfacing, for which macros still remains the best
option.
You can use the WebIOPi
Serial driver with the onboard UART as well as USB adapters. For exemple, you
can can connect an Arduino through the USB port or by wiring headers.
This tutorials will learn
you how to configure and use the embedded Serial driver from the REST API as
well as Python scripts.
If you prefer PySerial,
you can use it inside WebIOPi macros, but you will not benefit from the REST
binding and the Serial Monitor.
serial.tools.list_ports
This module can be executed to get a list of ports (python -m serial.tools.list_ports).
It also contains the
following functions :-
serial.tools.list_ports.comports
(include_links=False)
Under Linux, OSX and
Windows, extended information will be available for USB devices (e.g. theListPortInfo.hwid string containsVID:PID, SER (serial number), LOCATION(hierarchy),
which makes them searchable via grep().
The USB info is also available as attributes ofListPortInfo.
If include_links is true, all devices under/dev are inspected and tested if they are a link to a known serial port device. These entries will include LINK in theirhwid string.
This implies
that the same device listed twice, once under its original name and once under
linked name.
0 Comments