简体   繁体   English

从Windows到Linux的PHP USB串口调用

[英]PHP USB serial port call from Windows to Linux

I have a piece of simple code that works on a Windows - WAMP environment, eg 我有一段简单的代码,适用于Windows - WAMP环境,例如

<?php

`mode com3: BAUD=38400 PARITY=N data=8 stop=1 xon=off`;

file_put_contents(com3, chr(1).chr(255).chr(1).chr(4).chr(64).chr(5));

?>

It connects via a USB cable (using USB-Serial drivers) to a circuit-board to light some LEDs, and it works fine - so similarly windows software like 232Analyser, can connect to COM3 and send code in DEC like 1,255,255,255,255,5, and light the LEDs. 它通过USB电缆(使用USB串行驱动程序)连接到电路板以点亮某些LED,并且工作正常 - 因此类似于232Analyser的Windows软件可以连接到COM3并在DEC中发送代码,如1,255,255,255,255,5,以及点亮LED。 The number 255, is a DEC number from: 1,2,4,8,16,32,64,128,255 which will light a certain LED depending on which number is called. 数字255是DEC编号:1,2,4,8,16,32,64,128,255,它将根据调用的数字点亮某个LED。

Anyway, the code above works fine on Windows, and lights LEDs by calling this PHP file. 无论如何,上面的代码在Windows上工作正常,并通过调用此PHP文件点亮LED。 So can call a URL like: http://localhost/lightled.php which works ok. 所以可以调用一个URL: http://localhost/lightled.php ,它可以正常工作。

Now I need it to work via Linux, on a Raspberry Pi, so I have just installed standard Raspberry Linux, and Apache with PHP. 现在我需要它通过Linux在Raspberry Pi上工作,所以我刚刚安装了标准的Raspberry Linux和Apache的PHP。

Then attached the USB cable, and it appears as /dev/ttyUSB0 I have then CHMOD 777 /dev/ttyUSB0 然后连接USB线,它显示为/ dev / ttyUSB0我有CHMOD 777 / dev / ttyUSB0

And changed the PHP code to: 并将PHP代码更改为:

<?php

`mode /dev/ttyUSB0: BAUD=38400 PARITY=N data=8 stop=1 xon=off`;

file_put_contents('/dev/ttyUSB0', chr(1).chr(255).chr(1).chr(4).chr(64).chr(5));

?>

However calling this file on Linux in a browser is not Lighting the LEDs, as it does on Windows. 但是,在浏览器中在Linux上调用此文件并不像在Windows上那样点亮LED。

Now when I call this file it goes through with no errors, without chmod 777, it gave a permission denied error. 现在,当我调用此文件时,它没有错误,没有chmod 777,它给出了一个权限被拒绝错误。 So it seems like it goes through ok, but something else is wrong. 所以看起来它可以通过,但其他错误。

So question is anyone know how to make it work on Linux, it might be I am calling the USB wrong, or Raspberry Linux needs some kind of drivers, or " `mode " needs to be defined differently ...or maybe the decimal/binary code sent is not right like "chr(2)" etc. needs to be different and wont be sent in same way on a LAMP setup. 所以问题是任何人都知道如何使它在Linux上工作,可能是我调用USB错误,或者Raspberry Linux需要某种驱动程序,或者“模式”需要以不同的方式定义...或者可能是小数/发送的二进制代码不正确,如“chr(2)”等需要不同,并且不会以相同的方式在LAMP设置上发送。

Any ideas on what I can try? 关于我可以尝试的任何想法?

Thanks. 谢谢。

`mode /dev/ttyUSB0: BAUD=38400 PARITY=N data=8 stop=1 xon=off`;

There is no mode command in Linux. Linux中没有mode命令。

The closest equivalent is stty , but it doesn't take identical command-line arguments. 最接近的等价物是stty ,但它不需要相同的命令行参数。 You will need to perform some translation; 你需要进行一些翻译; read the manual page for stty for details. 有关详细信息,请阅读stty的手册页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM