From: Ned Konz Date: 2001-12-20T02:14:07+09:00 Subject: [ruby-talk:28964] Re: Can't obtain data from Serial Port. On Wednesday 19 December 2001 06:47 am, Vlad wrote: > I try to write the simple program allowing exchanging packages through > Serial Port with POS Terminal. > The code "ENQ" really comes in POS and it answers in Serial Port, but I > can not receive it. Whether I can use here for this purpose IOCTL? > If not it is difficult, write an example of use of function IOCTL c > FIONREAD and TIOCMGET in parameters. Look at termios (in the RAA). Here's a program I recently wrote (note the need to open it and reopen it after setting the parameters): require 'termios' DEVICE = '/dev/ttyS1' BAUDRATE = Termios::B19200 # first open non-blocking to set the modes dev = File.open(DEVICE, File::RDWR | File::NONBLOCK) # set up the modes we want newtio = Termios::new_termios() newtio.c_iflag = Termios::IGNPAR newtio.c_cflag = Termios::CS8 | Termios::CREAD newtio.c_cc[Termios::VTIME] = 0 # timeout in deciseconds newtio.c_cc[Termios::VMIN] = 1 # 1 char min for read (block) newtio.c_ispeed = newtio.c_ospeed = BAUDRATE Termios::setattr(dev, Termios::TCSANOW, newtio) # then reopen dev.close dev = File.open(DEVICE, File::RDWR) Termios::flush(dev, Termios::TCIOFLUSH) $stdout.sync = 1 loop { dev.print("C\r") line = dev.readline # CS=3 CT=10 V=13.175 %=100 bl= 0 SS=1 ST=14 SD=0 or= 0 unless line =~ /CS=(\S+) CT=(\S+) V=(\S+) %=(\S+) bl=\s*(\S+)/ $stderr.puts("bad line: " + line) end printf("\"%s\",%s,%s,%s,%s,%s\n", Time.now.to_s, $1, $2, $3, $4, $5) sleep 60 } -- Ned Konz currently: Stanwood, WA email: ned@bike-nomad.com homepage: http://bike-nomad.com