python pyserial usb2.0,使用Python和pyserial访问USB串行端口

本文介绍如何使用Python的PySerial库访问Mac上的USB串行端口。主要讲解了如何找到正确的设备文件名,如以cu.usbserial-开头的文件,并区分了USB转RS-232转换器和纯USB端口的访问方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值