File: modem_check2.rb

package info (click to toggle)
libtermios-ruby 0.9.5-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 124 kB
  • ctags: 59
  • sloc: ansic: 917; ruby: 374; makefile: 3
file content (69 lines) | stat: -rw-r--r-- 1,606 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'fcntl'
require 'termios'

DEVICE = '/dev/modem'
BAUDRATE = Termios::B115200

def dev_open(path)
  dev = open(DEVICE, File::RDWR | File::NONBLOCK)
  mode = dev.fcntl(Fcntl::F_GETFL, 0)
  dev.fcntl(Fcntl::F_SETFL, mode & ~File::NONBLOCK)
  dev
end

def dump_termios(tio, banner)
  puts banner
  puts "  ispeed = #{Termios::BAUDS[tio.ispeed]}, ospeed = #{Termios::BAUDS[tio.ospeed]}"
  ["iflag", "oflag", "cflag", "lflag"].each do |x|
    flag = tio.send(x)
    flags = []
    eval("Termios::#{x.upcase}S").each do |f, sym|
      flags << sym.to_s if flag & f != 0
    end
    puts "   #{x} = #{flags.sort.join(' | ')}"
  end
  print "      cc ="
  cc = tio.cc
  cc.each_with_index do |x, idx|
    print " #{Termios::CCINDEX[idx]}=#{x}" if Termios::CCINDEX.include?(idx)
  end
  puts
end

dev = dev_open(DEVICE)
dev.extend Termios

oldtio = dev.tcgetattr
dump_termios(oldtio, "current tio:")

newtio = Termios::new_termios()
newtio.iflag = Termios::IGNPAR
newtio.oflag = 0
newtio.cflag = (Termios::CRTSCTS | Termios::CS8 | Termios::CREAD)
newtio.lflag = 0
newtio.cc[Termios::VTIME] = 0
newtio.cc[Termios::VMIN] = 1
newtio.ispeed = BAUDRATE
newtio.ospeed = BAUDRATE
dump_termios(newtio, "new tio:")

dev.tcflush(Termios::TCIOFLUSH)
dev.tcsetattr(Termios::TCSANOW, newtio)
dump_termios(dev.tcgetattr, "current tio:")

"AT\x0d".each_byte {|c|
  c = c.chr
  p [:write_char, c]
  dev.putc c
  d = dev.getc
  p [:echo_back, d && d.chr || nil]
}

r = ''
while /OK\x0d\x0a/o !~ r
  r << dev.getc.chr
  p [:response, r]
end

dev.tcsetattr(Termios::TCSANOW, oldtio)
dump_termios(dev.tcgetattr, "current tio:")