From: kjana@... (YANAGAWA Kazuhisa) Date: 2001-06-21T21:44:53+09:00 Subject: [ruby-talk:16700] Re: Serial Communications In message matju@sympatico.ca writes: > Well, you'd only need to go back for a few pages of C. However it's better > to know beforehand, and know some of the Ruby<->C bridge. > > After that you'll have to make decisions, like how struct termios maps to > Termios objects: copy upon crossing C/Ruby limit; or make Termios wrap > struct termios; or just don't make a Termios object, and handle everything > through methods... FYI there's already termios extension library by Akira YAMADA . # I'm not sure but it is already imported into CVS repository.... Ugh, # I forgot the name of the module, sorry. That is a straightforward wrapper of the POSIX termios interface. See attatched example which is included the tarball. -- kjana@os.xaxon.ne.jp June 21, 2001 Shortest way may be longest way. require 'termios' DEVICE = '/dev/ttyS3' BAUDRATE = Termios::B38400 RESCUE = Termios::new_termios(1280, 5, 3261, 35387, [3, 28, 127, 21, 4, 0, 1, 0, 17, 19, 26, 0, 18, 15, 23, 22, 0, 0, 115, 80, 138, 6, 8, 48, 236, 255, 191, 85, 97, 0, 64, 3], 13, 13) #p 1 dev = open(DEVICE, 'r+') #p 1 #sleep 30 #p Termios::getattr(dev) #Termios::setattr(dev, Termios::TCSANOW, RESCUE) #p Termios::getattr(dev) #p 1 #exit oldtio = Termios::getattr(dev) p oldtio newtio = Termios::new_termios() newtio.c_iflag = Termios::IGNPAR newtio.c_oflag = 0 newtio.c_cflag = (BAUDRATE | Termios::CRTSCTS | Termios::CS8 | Termios::CREAD) newtio.c_lflag = 0 newtio.c_cc[Termios::VTIME] = 0 newtio.c_cc[Termios::VMIN] = 1 Termios::flush(dev, Termios::TCIOFLUSH) Termios::setattr(dev, Termios::TCSANOW, newtio) p Termios::getattr(dev) "AT\x0d\x0a".each_byte {|c| c = c.chr dev.putc c d = dev.getc.chr p [c, d] } i = '' while /OK\x0d\x0a/o !~ i i << dev.getc.chr p i end Termios::setattr(dev, Termios::TCSANOW, oldtio) p Termios::getattr(dev)