在 Linux 上使用 perl 或 PHP 通过 USB 读取和写入串行设备

2022-01-18 00:00:00 linux usb serial-port php

我在 Linux 上从串行设备读取时遇到问题.这个问题很奇怪,我无法确定原因.

I'm having a problem reading from a serial device on Linux. The problem is rather weird, and I wasn't able to nail down the causes.

我正在使用 PHP 打开 /dev/ttyUSB0 文件,并开始根据设备的协议与设备进行通信.很多时候遇到过PHP脚本等待设备响应的情况.当我并行运行一个应该执行相同操作的 Perl 脚本时,它会向同一设备发送一个请求,并且应该在没有得到响应的情况下退出.然后我看到PHP脚本得到了响应(只有在Perl脚本发送请求之后).

I'm opening the /dev/ttyUSB0 file with PHP and beginning to communicate with the device according to the device's protocol. Many times I encountered a situation where the PHP script waits for the device to respond. When I ran a Perl script in parallel which supposed to do the same it sent a request to the same device, and quit supposedly without getting a response. Then I saw that the PHP script got the response (only after the Perl script sent a request).

我在尝试使用 PHP 阅读 Arduino 时遇到了类似的问题.PHP 没有从端口得到响应,但 Arduino IDE 的串行监视器打印了它.

I encountered a similar matter when trying to read Arduino with PHP. The PHP got no response from the port, but Arduino IDE's Serial Monitor printed it.

我认为我在这里遗漏了有关 Linux 文件和 USB 端口的关键信息.可能是什么问题?如何判断哪些程序使用了端口/文件?

I think I'm missing a crucial thing about Linux files and USB ports here. What might be the problem? How can I tell which programs use the port/file?

    $usb = 'ttyUSB0';        
    `stty -F /dev/$usb 9600`;
    `stty -F /dev/$usb -parity`;
    `stty -F /dev/$usb cs8`;
    `stty -F /dev/$usb -cstopb`;
    $f = fopen("/dev/$usb", "r+");
    if(!$f) {
        echo "error opening file
";
        exit;
    }

    statusRequest($f);
    sleep(1);
    $c = readPort($f);
    echo "$c
";

function statusRequest($port) {
    $data = "request";
    fwrite($port, $data);
    fflush($port);
}

function readPort($port) {
    $read = 1;
    $c = '';
    while($read > 0) {
        $bytesr = unpack("h*", fread($port, 1));
        $c .= $bytesr[1];
        //echo $bytesr[1];
        if($bytesr[1] == 'ff') {
            $read = 0;
        }
    }    
    return $c;
}

推荐答案

在我的 wiki 上查看这两篇文章.第一篇文章介绍了如何在设备节点上设置有用的权限.第二篇文章是打印出遥控器发送到 PC 的所有数据的示例.虽然是为 Arduino 编写的,但它很容易移植到其他用途.

Check these two articles on my wiki. The first article describes how to set useful permissions on the device node. The second article is an example that prints out all data that the remote sends to the PC. Although written for Arduino it is easily ported for other uses.

  • http://wirespeed.xs4all.nl/mediawiki/index.php/Udev_rules_file_for_Arduino_boards
  • http://wirespeed.xs4all.nl/mediawiki/index.php/Cat_ttyUSB0

使用 lsof 可以找出当前正在使用该端口的程序:

Using lsof you can find out which program is currently using the port:

<代码>lsof |grep/dev/ttyUSB0cat_ttyUS 19182 jhendrix 3u CHR 188,0 0t0 14519955/dev/ttyUSB0

使用 stty 命令,您不会锁定端口以供独占使用.

With the stty commands you don't lock the port for exclusive use.

相关文章