在Linux上使用Perl或PHP通过USB读写串行设备 [英] Reading and writing to/from serial device via USB on Linux with perl or PHP

查看:136
本文介绍了在Linux上使用Perl或PHP通过USB读写串行设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从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 file,并开始根据设备的协议与设备进行通信.我很多次遇到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\n";
        exit;
    }

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

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/ttyUSB0 cat_ttyUS 19182 jhendrix 3u CHR 188,0 0t0 14519955 /dev/ttyUSB0

lsof | grep /dev/ttyUSB0 cat_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.

这篇关于在Linux上使用Perl或PHP通过USB读写串行设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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