使用 Python 和 pyserial 访问 USB 串口

2022-01-18 00:00:00 python macos pyserial usb serial-port

问题描述

如何使用 pyserial 访问 USB 端口?我看过 示例与:

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

import serial

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

我曾经在 Windows 上从 MATLAB 访问串行端口,并使用适当的语法,/dev/ttyUSB0 将替换为 COM1 或任何其他 COM 端口.

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.

我在 Mac 上,我尝试使用 串行端口扫描器 在 pyserial 文档上无济于事.我想我应该这样写:

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)

我如何找出 Mac 上的 name 应该是什么?

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

针对下面的回答,我想了解如何访问 USB 到 RS232 转换器以及纯 USB 端口.

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.


解决方案

您只能使用 pyserial(即 USB RS-232 加密狗)访问 USB 串行适配器.如果您想要通用 USB 访问,您应该查看libusb".如果您尝试通过 USB 访问的是 RS-232,那么您应该在/dev 中查找以 cu.usb* 开头的文件(例如/dev/cu.usbserial-181).

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

相关文章