浏览 4372 次
锁定老帖子 主题:用Ruby与串口交互
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-02-07
最后修改:2009-02-07
rubynroll 写道 仅通过File IO缺乏设置串口通讯参数的能力. 串口通讯建议用ruby-serialport这个gem,功能齐全, Linux/Windows均可工作.
总结一下: 串口模块 ruby: ruby-serialport <gem install serial-port, this needs nmake> perl: Win32::SerialPort Python: serial 代码如下: def execute_cmd(chan, cmd) print "Executing #{cmd} " chan.write("#{cmd}\r") begin result = chan.readlines result = "Command not support\n" if result.include?("ERROR\n") rescue result = "Writing serial port error\n" end print result end # My modem on serial port 3 File.open('com3:', 'r+') do |file| execute_cmd(file, "AT") execute_cmd(file, "AT&FE0V1S0=0&C1&D2+MR=2;+DR=1;+ER=1;W2") execute_cmd(file, "ATQ0V1E0") execute_cmd(file, "AT+GMM") execute_cmd(file, "AT+FCLASS") execute_cmd(file, "AT#CLS=?") execute_cmd(file, "AT+GCI?") execute_cmd(file, "AT+GCI=?") 1.upto(7) do |i| execute_cmd(file, "ATI#{i}") end end 用perl: open FH, '+>', "com3:" ||die "can not open this port"; print FH "AT+GMM\r"; print $s while($s=<FH>); print FH "ATI1\r"; print $s while($s=<FH>); close FH; Python 有个 serial 的模块 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-02-07
仅通过File IO缺乏设置串口通讯参数的能力. 串口通讯建议用ruby-serialport这个gem,功能齐全, Linux/Windows均可工作.
|
|
返回顶楼 | |
发表时间:2009-02-07
rubynroll 写道 仅通过File IO缺乏设置串口通讯参数的能力. 串口通讯建议用ruby-serialport这个gem,功能齐全, Linux/Windows均可工作. 谢了 ^_^ 我马上更新一下。 |
|
返回顶楼 | |
发表时间:2009-05-19
楼主,你的脚本在我这里行不通。
# My modem on serial port 6 File.open('com6:', 'r+') do |file| execute_cmd(file, "AT") execute_cmd(file, "AT+CPBS=?") execute_cmd(file, "AT+CPBS?") execute_cmd(file, "AT+CIMI?") execute_cmd(file, "AT+CSQ?") execute_cmd(file, "AT+CREG?") execute_cmd(file, "AT+VSPST?") execute_cmd(file, "AT+CMGF=?") end 结果是: Executing AT Executing AT+CPBS=? OK Executing AT+CPBS? +CPBS:("ME","SM") OK Executing AT+CIMI? +CPBS:"ME",0,200 OK Executing AT+CSQ? 460036380892074 OK Executing AT+CREG? +CSQ: 0,99 OK Executing AT+VSPST? +CREG:2,2,0x0,0x0 OK Executing AT+CMGF=? +VSPST:1,1 OK 发现错位,并少了最后一个AT响应,请指教。 |
|
返回顶楼 | |
发表时间:2009-05-21
错位的问题应该和rubynroll提到的“串口通讯参数的能力”有关。
请看下面这个帖子 http://magixyu.iteye.com/blog/325315 |
|
返回顶楼 | |
发表时间:2009-05-22
magixyu 写道
用perl:
open FH, '+>', "com3:" ||die "can not open this port"; print FH "AT+GMM\r"; print $s while($s=<FH>); print FH "ATI1\r"; print $s while($s=<FH>); close FH; 用PERL看着确实方便,怀念PERL的年代。
|
|
返回顶楼 | |