OPENWRT串口通信的实现

串口设置

串口名称(不同机型不同)

开机启动信息可以看到串口的名称,例如以下串口启动信息:

Serial: 8250/16550 driver, 1 ports, IRQ sharing disabled

ar933x-uart: ttyATH0 at MMIO 0x18020000 (irq = 11) is a AR933X UART

console [ttyATH0] enabled, bootconsole disabled

可以看到串口的名称为ttyATH0,此时可以ls /dev/查看确认。

stty 串口波特率设置

opkg update  //更新软件列表

opkg install coreutils-stty //安装stty

stty -F /dev/ttyATH0 raw speed 9600  //设置ttyATH0串口的波特率为9600

echo "hello" > /dev/ttyACM0  //向串口输出字符"hello"

cat /dev/ttyATH0  //读取串口

屏蔽串口的系统显示(消除乱码和串口BusyBox 影响

编辑文件:vi /etc/inittab

::sysinit:/etc/init.d/rcS S boot

::shutdown:/etc/init.d/rcS K shutdown

ttyS0::askfirst:/bin/ash --login

#ttyATH0::askfirst:/bin/ash --login  #注释此行

将串口波特率设置添加到开机启动

编辑文件:vi /etc/init.d/usart

文件属性:chmod 777 /etc/init.d/usart

#!/bin/sh /etc/rc.common

#/etc/init.d/usart

START=80

start() {

/usr/bin/stty -F /dev/ttyS0 raw speed 9600

}

stop(){

killall usart

}

启用开机启动:/etc/init.d/usart enable

UHTTP配置修改

配置lua有效路径、默认访问端口、页面位置
编辑文件:vi /etc/config/uhttpd

list 'listen_http' '0.0.0.0:80'      // 默认端口设置

list 'listen_https' '0.0.0.0:443'  // 通过https访问,端口设定

option 'home' '/www'               // 页面所在的位置

option 'cert' '/etc/uhttpd.crt'    // https访问对应的证书

option 'key' '/etc/uhttpd.key'   // 和上面的cert有关系

option 'cgi_prefix' '/cgi-bin'      //cgi有效路径,相对于home
/etc/init.d/uhttpd restart //重启uhttpd加载新的配置文件

LUA操作系统

函数os.exit终止一个程序的执行。函数os.getenv得到“环境变量”的值。以“变量名”作为参数,返回该变量值的字符串:

print(os.getenv("HOME"))    --> /home/rming
如果没有该环境变量则返回nil。函数os.execute执行一个 系统命令(和C中的system函数等价)。该函数获取一个命令字符串,返回一个错误 代码。例如在Unix系统里都可以执行如下 代码创建一个新目录:
function createDir (dirname)

    os.execute("mkdir " .. dirname)

end

os.execute函数较为强大,同时也更加倚赖于计算机系统。

典型代码

从web调用GET方式输出到到串口

路由器开机输出过字符串,需要在路由器和单片机中添加命令标志位。

可以web调用,url/lua/output?1
eg:php调用file_get_content('http://url/output?1')
#!/usr/bin/lua

ser_out=io.output("/dev/ttyATH0")

io.write("codecommond")

io.write(os.getenv("QUERY_STRING"))

ser_out:close()

print [[

Content-Type: text/plain

]]

监听串口接收下位机上传的数据(开机后台运行)

单片机发起:

单片机发送指令,路由器接收指令,执行指令

单片机可以发起的动作有:

1、存储数据到路由器  指令形式:1:key,value/r/n

2、访问互联网网络    指令形式:2:url/r/n

3、执行指令       指令形式  3:code/r/n
#!/usr/bin/lua

    #!/usr/bin/lua

    ser_in=io.input("/dev/ttyATH0")

    while 1

    do

        ser_char=io.read(1)

        if ser_char=="1" then

            do

                print("1")

            end

        elseif ser_char=="2" then

            do

                print("2")

            end

        elseif ser_char=="3" then

            do

                print("3")

            end

        else

            print("others")

        end

    end

PHP请求http(file_get_content)

1.GET方式请求

 <?php



       $data = array('sParam1'=>'test1','sParam2'=>101,'isAuto'=>1);  //定义参数



       $data = @http_build_query($data);  //把参数转换成URL数据



       $aContext = array('http' => array('method' => 'GET',



         'header'  => 'Content-type: application/x-www-form-urlencoded',



          'content' => $data ));



       $cxContext  = stream_context_create($aContext);



       $sUrl = 'http://www.mytest.com/test.php'; //此处必须为完整路径



       $d = @file_get_contents($sUrl,false,$cxContext);



       print_r($d);



?>

2.POST方式请求

 <?php



       $data = array('sParam1'=>'test1','sParam2'=>101,'isAuto'=>1);  //定义参数



       $data = @http_build_query($data);  //把参数转换成URL数据



       $aContext = array('http' => array('method' => 'POST',



        'header'  => 'Content-type: application/x-www-form-urlencoded',



        'content' => $data ));



       $cxContext  = stream_context_create($aContext);



       $sUrl = 'http://www.mytest.com/test.php'; //此处必须为完整路径



       $d = @file_get_contents($sUrl,false,$cxContext);



       print_r($d);



?>

 


文章参考:

OpenWrt与Arduino的USB直接通信 By 船长@第一次的硬软件结合 :http://www.sl088.com/voyage/2012/03/3411.slboat

LUA操作系统库:http://www.cnblogs.com/hwblog/articles/2080721.html

WR703N 开发集锦(亲测成功) :http://blog.chinaunix.net/uid-27194309-id-3287712.html

PHP请求http(file_get_content): http://blog.csdn.net/loveruguo/article/details/6737635

网页控制Openwrt串口:http://see.sl088.com/wiki/%E7%BD%91%E9%A1%B5%E6%8E%A7%E5%88%B6Openwrt%E4%B8%B2%E5%8F%A3

原文出处:http://rmingwang.com/720n-openwrt-stty-uart.html

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值