使用 Python 和 pyserial 访问 USB 串口 [英] Access USB serial ports using Python and pyserial

查看:42
本文介绍了使用 Python 和 pyserial 访问 USB 串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 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).

这篇关于使用 Python 和 pyserial 访问 USB 串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆