识别串口/USB设备python [英] Identifying serial/usb device python

查看:81
本文介绍了识别串口/USB设备python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的解决方案可能非常简单,但我是动态连接设备的新手.我正在做的是我正在制作一个 python 可执行代码,所以用户不必在他们的计算机或任何类型的 python 解释器上有空闲,这意味着我不知道设备将插入哪个 USB 端口至.该程序需要能够打开与通过串行转 USB 转换器连接的设备的连接.如何确定哪个连接的设备是打开端口的正确设备?我正在使用 pySerial 与设备进行交互.任何帮助将不胜感激.

The solution to this problem is probably pretty simple, but I am new to interfacing with a device dynamically. What I'm doing is I am making a python executable code, so the user doesn't have to have Idle on their computer or any kind of python interpreter, which means I don't know which USB port the device will be plugged in to. The program needs to be able to open a connection to a device that is connected through a serial to usb converter. How can I determine which connected device is the correct device to open a port to? I am using pySerial to interact with the device. Any help would be greatly appreciated.

推荐答案

无论你如何配置你的设备,在某些时候你可能不得不询问用户端口在哪里,或者轮询所有串行设备一个已知的响应.(虽然投票有陷阱,所以请继续阅读!).与 USB 设备不同,当您连接一个普通的串行设备时,操作系统不会知道供应商/设备 ID.

No matter how you configure your device, at some point you're probably going to have to ask the user where the port is, or poll all serial devices for a known response. (Polling has it's pitfalls though, so read on!). Unlike USB devices, there is no vendor/device ID that is made known to the OS when you attach a plain-old serial device.

首先你需要找到串口.这是一个可能有帮助的问题:Python中枚举串口(包括虚拟端口)的跨平台方法是什么?.

First you need to find the serial ports. Here's a question that might help: What is the cross-platform method of enumerating serial ports in Python (including virtual ports)?.

一旦您有了串行端口列表,您就可以询问用户是否知道要使用哪个.如果他们这样做,问题就解决了!

Once you have a list of serial ports, you could ask the user whether they know which one to use. If they do, problem solved!

如果他们不这样做,您可以提供一些数据来轮询所有串行设备,知道会从您的设备产生特定响应.请记住,如果用户连接了其他串行设备,则您的hello"字节字符串实际上可能是其他设备的自毁序列!因此,您应该警告用户轮询可能会干扰其他设备,并在您这样做之前始终提示他们.

If they don't, you could offer to poll ALL serial devices with some data that you know will yield a certain response from your device. Keep in mind though that if the user has other serial devices attached, your string of "hello" bytes might actually be the self-destruct sequence for some other device! Hence, you should warn the user that polling may interfere with other devices, and always prompt them before you do so.

如果不了解更多关于您的代码的信息(例如,您正在使用什么通信框架,如果有的话;您是在控制台中执行此操作还是您使用的是 GUI 工具包等),就不可能说出最好的方法是什么编码这可能是.在最简单的情况下,您可以循环遍历所有串行设备,发送问候语并检查响应.(您也可以并行执行此操作:为问候语循环一次,然后再次循环以检查缓冲区中的内容.如果您想获得更多幻想,请使用适当的库.)

Without knowing more about your code (eg. what comms framework, if any, you're using; are you doing this in the console or are you using a GUI toolkit, etc), it's impossible to say what the best way to code this might be. In the simplest case, you could just loop over all of your serial devices, send the greeting and check for a response. (You could also do this in parallel: loop once for the greeting, and loop again to check what's in the buffer. If you're going to get more fancy than that, use a proper library.)

旁注:如果您有一个可以为其设置供应商/设备 ID 的内置转换器,您可能能够解决这个问题,但是转换器任何现代操作系统仍会自动检测到并枚举为串行端口;您将无法作为 USB 设备直接与它通话.可以确定哪个端口与哪个 ID 对应,但我从未尝试过这样做.但是,如果您不是选择转换器的人(例如,如果它是用户提供的电缆),则这种方法是无用的.

Side note: You might be able to get around this if you have a built-in converter that you can set the vendor/device ID for, but the converter will still be automatically detected by any modern OS and enumerated as a serial port; you won't get to talk to it directly as a USB device. It could be possible to figure out which port goes with which ID, but I've never tried to do that. But this approach is useless if you're not the one who gets to pick the converter (eg. if it's a user-supplied cable).

这篇关于识别串口/USB设备python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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