【问题标题】:Serial connection with Arduino, PHP & OpenWrt. Bug?与 Arduino、PHP 和 OpenWrt 的串行连接。漏洞?
【发布时间】:2012-07-28 19:06:11
【问题描述】:

我正在尝试使用 OpenWrt 固件通过 TP-Link MR3420 路由器上的 USB 接口创建一个 PHP Web 界面来控制 Arduino Uno

奇怪的是,我的 PHP 脚本只有在运行与 arduino 通信的 python 脚本后才能得到 arduino 的回复

我确定我的 PHP 脚本正在运行,因为它能够关闭 arduino 上的 LED,但没有得到 arduino 的回复

这是我的 PHP 代码:

require("lib/php_serial.class.php");

$serial = new phpSerial;

$serial->deviceSet("/dev/ttyACM0");
$serial->confBaudRate(9600);

$serial->deviceOpen();
$serial->sendMessage($cmd);
sleep(1);
$read = $serial->readPort();
$serial->deviceClose();

这是我的 arduino 草图:

int numSerial;
if (Serial.available() > 0)
{
char inByte = Serial.read();
switch (inByte)
{
case 'l':
  numSerial = numberFromSerial();
  if (numSerial >= 0)
  {
    Serial.print("LED Mode: ");
    switch (numSerial)
    {
    case 0:
      ledMode = 0;
      Serial.print("OFF");
      break;
    case 1:
      ledMode = 1;
      Serial.print("TEMP");
      break;
    case 2:
      ledMode = 2;
      Serial.print("KR");
      break;
    }
  }
  else
    Serial.print(ledMode);
  break;
case 't': //Data request
  Serial.print(getTemp());
  break;
}
}

这是我的python代码:

    import serial
ser = serial.Serial("/dev/ttyACM0", 9600, timeout=3)
ser.open()
ser.write("t")
print ser.readline()
ser.close()

请帮忙。 谢谢。

找到问题的真正根源!

在 phpSerial 类中:

$ret = $this->_exec("stty -F " . $this->_device . " " . (int) $rate, $out);

替换为:

$ret = $this->_exec("stty -F " . $this->_device . " raw speed " . (int) $rate, $out);

【问题讨论】:

  • 已解决!在每次路由器启动时运行“stty -F /dev/ttyACM0 raw speed 115200”解决了我的问题
  • 如果您解决了自己的问题,您应该发布您的解决方案作为答案并接受它。

标签: php serial-port arduino openwrt


【解决方案1】:

找到问题的真正根源!

在 phpSerial 类中:

$ret = $this->_exec("stty -F " . $this->_device . " " . (int) $rate, $out);

替换为:

$ret = $this->_exec("stty -F " . $this->_device . " raw speed " . (int) $rate, $out);

【讨论】:

    【解决方案2】:

    Arduino 有一个记录在案的Auto Reset on Serial Connection 问题。我的第一个猜测是您正在使用 PHP 代码触发此操作(这就是灯关闭但您没有收到任何响应的原因)。

    尝试使用上述链接中的一种解决方法。

    这是一个 perl 代码 sn-p,您可以使用它来查看您是否真的触发了这个问题。

    #!/usr/bin/perl
    use strict;
    use Device::SerialPort;
    my $port = Device::SerialPort->new("/dev/ttyUSB0");
    $port->databits(8);
    $port->baudrate(9600); # <-- match to arduino settings
    $port->parity("none");
    $port->stopbits(1);
    $port->dtr_active(0);  //toggle this to one to trigger reset
    

    【讨论】:

    【解决方案3】:

    我在我的 rc.local 文件中使用这一行来在运行任何东西之前设置端口。

    stty -F /dev/ttyUSB0 raw speed 38400 -echo -hupcl
    

    另外两个标志很有用: -echo 关闭 OpenWRT/USB 将每个输出回显到输入的习惯 -hupcl 通过在每次传输后不发送 hup 来关闭自动重置问题

    又是 YMMV - 我正在发送和接收单个字节,而不是字符串或复杂数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多