dongnaoxia0927 2013-10-21 16:40
浏览 80

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条回答

  • dtlab08822 2013-10-21 18:34
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败