如何使用nodejs读取串口数据并使用rs232电缆将输出发送到PHP服务器?

我需要读取串口以使用RS232电缆获取数据,并希望将输出发送到PHP 服务器,但我找到了一种方法使用node.js.
我搜索了很多,也找到了 PHP解决方案但它对我不起作用。
如何做到这一点?</ p>
</ div>

展开原文

原文

I need to read a serial port to get data using a RS232 cable, and want to send the output to a PHP server, but i found a way using node.js. I searched a lot and also found a solution in PHP but it doesn't work for me. How to do this?

1个回答



如果你在Linux上,我会尝试打开文件描述符到/ dev / your_serial_port。 您的PHP脚本可能需要权限(root)才能从此端口读取。 您可能有一个tty组或类似的东西允许从此设备读取。 在这种情况下,我建议以root身份启动脚本,并使用 posix_setgid()</ code>和 posix_getgrnam()</ code>来更改进程组,而不是以root身份游行。< / p>

  $ groupInfo = posix_getgrnam(“tty”); 
if(!isset($ groupInfo [“gid”])){
//“无效的工作组。
} else if(!posix_setgid($ groupInfo [“gid”])){
//无法更改工作组。(需要特权)
}
</ code> </ pre>

我感觉不好你可能在Windows上,所以我挖了一下,我认为你可以在Windows上打开文件'com1:'并且可能读取串行数据。如果这不起作用,找一个可以转储的cli程序 串行数据到stdout并使用类似proc_open的东西从中读取数据。我没有Windows计算机可以尝试任何东西;(</ p>
</ div>

展开原文

原文

If you are on Linux, I would try opening a file descriptor to /dev/your_serial_port. Your php script will probably need permissions (root) to read from this port. You may have a tty group or something similar that allows read from this device. In that case, I would recommend starting your script as root, and using posix_setgid() possibly with posix_getgrnam() to change your process group and not parade around as root.

$groupInfo = posix_getgrnam("tty");
if (! isset($groupInfo["gid"])) {
    // "Invalid worker group.
} else if (! posix_setgid($groupInfo["gid"])) {
    // Failed to change worker group. (privilege required)
}

I feel bad that you are probably on windows, so I dug around and I think you can open the file 'com1:' on windows and probably read serial data. If that doesn't work, look for a cli program that can dump serial data to stdout and use something like proc_open to read data from it. I have no windows computer to try anything ;(

doutu2017
doutu2017 我试图在Windows中阅读,但它不起作用
接近 3 年之前 回复
dongsi7067
dongsi7067 谢谢你的宝贵回应..我有Rs232电缆,我如何使用上面的代码从设备(Accuref r-800)获取数据,也可能我知道它必须创建一个插座...
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问