如何使用asio库打开USB端口并接收它的数据?

发布于 2025-01-08 02:34:47 字数 1171 浏览 0 评论 0原文

我有一个与另一个应用程序(不是我编写的)通信的设备,我想使用我的应用程序来获取它的价值。谷歌搜索我找到了ansio库,我安装了它并找到了这个教程入门。

问题:如何获取要在端口名称中设置的设备名称? 使用lsusb,我得到:

Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 002: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 004: ID 0ac8:305b Z-Star Microelectronics Corp. ZC0305 Webcam
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

我想处理网络摄像头设备:

Bus 001 Device 004: ID 0ac8:305b Z-Star Microelectronics Corp. ZC0305 Webcam

然后我写道:

try {
    SimpleSerial serial("/dev/ttyUSB4",115200); // get SimpleSerial class on link above
    cout<<serial.readLine()<<endl;
    return 0;

} catch(boost::system::system_error& e)
{
    cout<<"Error: "<<e.what()<<endl;
    return 1;
}

但它给出了:错误:打开:没有这样的文件或目录 如何解决这个问题?提前致谢。

I have a device that communicates with another application(not written by me) that I want to get value of it using my application. googling I found the ansio library,I installed it and found this tutorial for getting started.

the question: how I to get the device name to set in port name?
using lsusb, I get:

Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 002: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 004: ID 0ac8:305b Z-Star Microelectronics Corp. ZC0305 Webcam
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

I want to handle the webcam device:

Bus 001 Device 004: ID 0ac8:305b Z-Star Microelectronics Corp. ZC0305 Webcam

then I wrote:

try {
    SimpleSerial serial("/dev/ttyUSB4",115200); // get SimpleSerial class on link above
    cout<<serial.readLine()<<endl;
    return 0;

} catch(boost::system::system_error& e)
{
    cout<<"Error: "<<e.what()<<endl;
    return 1;
}

but it given an: Error: open: No such file or directory
how to fix this? thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2025-01-15 02:34:47

在 shell 中输入 ls /dev/tty* 并选择生成的端口之一。不幸的是,不能确定哪一个是正确的。否则,您可以尝试通过处理 dmesg 的输出来提取正确的端口。

Type ls /dev/tty* in the shell and choose one of the resulting ports. Unfortunately it can't be said for sure which one will be the right one. Otherwise you can try extracting the correct port by processing the output of dmesg.

一个人的夜不怕黑 2025-01-15 02:34:47

通常,当设备连接到 USB 端口时,它会在内核环形缓冲区中记录一条消息。此日志通常会为您提供所需的信息。要找到它,

  1. 请连接您的设备
  2. 运行dmesgdmesg | tail -20 查看缓冲区的最后 20 行。

dmesg 命令的输出应类似于以下内容。

.
.
.
[ 1562.334350] usb 2-1: USB disconnect, device number 2
[ 1566.526225] usb 1-1: new full-speed USB device number 7 using xhci_hcd
[ 1566.652880] usb 1-1: New USB device found, idVendor=067b, idProduct=2303, bcdDevice= 4.00
[ 1566.652885] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 1566.652888] usb 1-1: Product: USB-Serial Controller D
[ 1566.652891] usb 1-1: Manufacturer: Prolific Technology Inc. 
[ 1566.655451] pl2303 1-1:1.0: pl2303 converter detected
[ 1566.656634] usb 1-1: pl2303 converter now attached to ttyUSB1

注意:

  • 最后一行告诉您适当的 ttyUSB 设备号。这是特定于驱动程序的。许多 USB 设备无法通过 /dev/ttyUSB# 访问。

Typically when a device is connected to a USB port, it is records a message in the kernel ring buffer. This log will often give you the information you are looking for. To find it,

  1. Connect your device
  2. run dmesg or dmesg | tail -20 to see the last 20 lines of the buffer.

The output of the dmesg command should be something similar to the following.

.
.
.
[ 1562.334350] usb 2-1: USB disconnect, device number 2
[ 1566.526225] usb 1-1: new full-speed USB device number 7 using xhci_hcd
[ 1566.652880] usb 1-1: New USB device found, idVendor=067b, idProduct=2303, bcdDevice= 4.00
[ 1566.652885] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 1566.652888] usb 1-1: Product: USB-Serial Controller D
[ 1566.652891] usb 1-1: Manufacturer: Prolific Technology Inc. 
[ 1566.655451] pl2303 1-1:1.0: pl2303 converter detected
[ 1566.656634] usb 1-1: pl2303 converter now attached to ttyUSB1

Notes:

  • The last line tells you the appropriate ttyUSB device number. This is driver specific. Many USB devices are not accessible via /dev/ttyUSB#.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文