Access USB serial ports using Python and pyserial

浪子不回头ぞ 提交于 2019-12-20 12:16:51

问题


How do I access the USB port using pyserial? I have seen an example with:

import serial

ser = serial.Serial('/dev/ttyUSB0')

I used to access the serial port from MATLAB on Windows and using the appropriate syntax, /dev/ttyUSB0 would be replaced by COM1 or any other COM port.

I'm on a Mac and I tried using the serial port scanners on the pyserial documentation to no avail. I think I should write it like this:

import serial

name = ? # Names of serial ports on Mac OS X
ser = serial.Serial(name)

How do I find out what name should be on a Mac?

EDIT: In response to an answer below, I'd like to find out how to access both USB to RS232 converters as well as pure USB ports.


回答1:


You can only access USB Serial Adapters using pyserial (i.e., USB RS-232 dongles). If you want generic USB access you should be looking into "libusb". If it is RS-232 you are trying to access through USB then you should look for a file in /dev starting with cu.usb* (/dev/cu.usbserial-181 for example).



来源:https://stackoverflow.com/questions/6316584/access-usb-serial-ports-using-python-and-pyserial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!