Linux openwrt上的Php Serial到Arduino

I'm trying to get a web page based serial communication working with an Arduino which is connected to a router running Openwrt, it does work but only when either screen is running or remotely connected via putty, for some reason the php to serial is not starting a session properly? I use ser2net to manage the serial with the following setting

1000:raw:0:/dev/ttyACM0:9600

I have also tried stty with various settings advised on this forum

Update 1

also tried 80:raw:0:/dev/ttyACM0:9600 as setting for ser2net assuming port 80 is web/http port

my php code is

 if (isset($_GET['action'])) 
 {
     $serial = new phpSerial();
     $serial->deviceSet('/dev/ttyACM0');
     $serial->confBaudRate(9600);
     $serial->confParity('none');
     $serial->confCharacterLength(8);
     $serial->confStopBits(1);
     $serial->confFlowControl('none');
     $serial->deviceOpen();

     if ($_GET['action'] == "PIN_12_HIGH") 
     {
         $serial->sendMessage("A");
     }
     if ($_GET['action'] == "PIN_12_LOW") 
     {
         $serial->sendMessage("B");
     }
     if ($_GET['action'] == "PIN_11_HIGH") 
     {
         $serial->sendMessage("C");
     }
     if ($_GET['action'] == "PIN_11_LOW") 
     {
         $serial->sendMessage("D");
     }

     $serial->deviceClose();
 }

update 2

if i add sleep(1); to my php code it seems to solve some of the problems

a few points

  1. adding sleep to php code allows the commands to reach the arduino after a reset caused by the serial connection, but if i turn on led 1 when i turn led 2 on led 1 gets reset to off. so this would mean a more complicated code in the form of logging an array of choices and sending the whole updated array to arduino, i would like to avoid this and just send 1 command at a time.
  2. again when screen or putty are active the arduino does not have this reset problem? so the question is what does screen and putty do to keep the connection alive that stty and php serial does'nt?

Update 3

After finding this helpful post i seem to have discovered a possible solution with no reset hacking needed to the Arduino. Adding cat /dev/ttyACM0 & to the start up config of the Openwrt router enables serial communication with the Arduino without reseting it on every transmission of data.

problems left:

  1. I think this code is binding arduino to a session with the router? But im unsure.
  2. It seems to be eating the return value from the Arduino stopping my php page getting the data return?
  3. My System Log file contains the missing returned data from Arduino?

Update 4

  1. As i needed to get this working, i used a capacitor to stop the auto reset from serial communication on the arduino.
  2. As stated screen and putty dont create this reset problem when communicating with arduino, i tried many different settings with STTY but couldnt find a solution , but i am still trying and keen to discover how screen/putty does this.

1个回答



看起来你的PHP试图直接连接到tty。 ser2net可能已经连接到tty的地方。 一次只能将一个应用程序连接到tty。 要么停止ser2net,要么你的PHP应该根据你的ser2net.conf连接到所需tty的tcp监听器</ p>
</ div>

展开原文

原文

It looks like your PHP is attempting to directly connect to the tty. Where the ser2net is likely already connected to the tty. Only one application at a time can be connected to the tty. Either stop the ser2net or your PHP should connect to the tcp listener of the desired tty as per your ser2net.conf

dongtuo3530
dongtuo3530 我已经开始假设,因为灯光闪烁时,使用php发送数据到arduino通过stty它的重置虽然只有当屏幕或腻子没有打开时才会发生?
接近 7 年之前 回复
donglu5047
donglu5047 谢谢我将调查该选项并在此报告,感谢您的帮助
接近 7 年之前 回复
dougai3418
dougai3418 phpSerial的问题。 对不起,不是专家。 您可能希望让phpSerial首先在完整的Linux上运行,或者在没有被调用的Web上独立运行php应用程序。 或者用一个TCP套接字替换phpSerial到ser2net,你已经证明它应该可行。
接近 7 年之前 回复
dqde43215
dqde43215 是的,只是卸载了ser2net和屏幕,仍然没有更好
接近 7 年之前 回复
dpt62283
dpt62283 当您尝试访问tty时,您确定ser2net已停止吗?
接近 7 年之前 回复
duanjiong5023
duanjiong5023 它看起来像putty和屏幕启动ttyacm0而php没有
接近 7 年之前 回复
doulian1852
doulian1852 是的从我的Windows PC使用putty我可以连接到端口1000并命令arduino和本地使用屏幕我也可以命令arduino一切正常。 奇怪的想法是,当我登录我的网站时,从远程网络/位置我只能控制arduino如果连接已经从屏幕或腻子已经有效希望有意义
接近 7 年之前 回复
douxian5076
douxian5076 不是PHP专家。 听起来像一个phpSerial问题。 使用ser2net,从openwrt框的bash可以telnet或netcat到localhost 1000或80,无论你的ser2net正在监听,验证tty是否正常工作。
接近 7 年之前 回复
douqing5981
douqing5981 感谢您输入mpflaga,我禁用了ser2net,但没有任何改进。 我只是无法理解什么屏幕或腻子发起连接,我没有用PHP到串口?
接近 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐