简体   繁体   中英

Serial Port Identity in Python

I am using Python v2.7 on Win 7 PC. I have my robot connected to computer and COM 4 pops out in device manager. My plan is to send API to robot through COM 4.

Here is the question, how could python identify which serial port is for which device? So far, I can list all the available ports in python, but I need to specifically talk to COM 4 to communicate with robot.

As a newie, any help would be appreciated.

ser = serial.Serial(3) # open COM 4

print ser.name # check which sport was really used and 'ser' is the serial object

Here is the python code to open specific serial port.

This should work, at least on Windows.

ser = serial.Serial('COM4')

Under Linux, the device name can be used instead.

ser = serial.Serial('/dev/ttyS0')   # first physical serial port
ser = serial.Serial('/dev/ttyUSB0') # first usb serial port

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM