Update serial library so it's usable.

db4
Doug Coleman 2011-10-08 11:38:43 -07:00
parent 95ef4d90a9
commit 97fa79d7fa
3 changed files with 58 additions and 30 deletions

View File

@ -2,10 +2,10 @@
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.c-types assocs combinators destructors
kernel math math.bitwise math.parser sequences summary system
vocabs.loader ;
vocabs.loader io ;
IN: io.serial
TUPLE: serial stream path baud
TUPLE: serial-port < disposable stream path baud
termios iflag oflag cflag lflag ;
ERROR: invalid-baud baud ;
@ -15,7 +15,20 @@ M: invalid-baud summary ( invalid-baud -- string )
HOOK: lookup-baud os ( m -- n )
HOOK: open-serial os ( serial -- serial' )
M: serial dispose ( serial -- ) stream>> dispose ;
HOOK: default-serial-flags os ( m -- n )
M: serial-port dispose* ( serial -- ) stream>> dispose ;
: <serial-port> ( path baud -- obj )
serial-port new
swap >>baud
swap >>path
default-serial-flags ;
: with-duplex-stream ( duplex-stream quot -- )
[ [ in>> ] [ out>> ] bi ] dip with-streams ; inline
: with-serial-port ( serial-port quot -- )
[ open-serial ] dip with-duplex-stream ; inline
{
{ [ os unix? ] [ "io.serial.unix" ] }

View File

@ -1,25 +1,20 @@
! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel math.bitwise io.serial io.serial.unix
literals ;
USING: accessors io io.serial io.serial.unix kernel literals
math.bitwise tools.test ;
IN: io.serial.unix
: serial-obj ( -- obj )
serial new
"/dev/ttyS0" >>path ! linux
! "/dev/dty00" >>path ! netbsd
! "/dev/ttyd0" >>path ! freebsd
! "/dev/ttyU0" >>path ! openbsd
19200 >>baud
flags{ IGNPAR ICRNL } >>iflag
flags{ } >>oflag
flags{ CS8 CLOCAL CREAD } >>cflag
flags{ ICANON } >>lflag ;
! "/dev/ttyS0" ! netbsd
! "/dev/dty00" ! netbsd
! "/dev/ttyd0" ! freebsd
! "/dev/ttyU0" ! openbsd
: serial-test ( -- serial )
serial-obj
open-serial
dup get-termios >>termios
dup configure-termios
dup tciflush
dup apply-termios ;
: <serial-port-test> ( -- serial-port )
"/dev/ttyS0" 19200 <serial-port> ;
: with-serial-port-test ( quot -- )
[ <serial-port-test> ] dip with-serial-port ; inline
! [ ] [
! [ "hello" over stream-write stream-flush ] with-serial-port-test
! ] unit-test

View File

@ -30,19 +30,23 @@ FUNCTION: int cfsetspeed ( termios* t, speed_t s ) ;
[ <input-port> ] [ <output-port> ] bi <duplex-stream> ;
: open-rw ( path -- fd ) O_RDWR file-mode open-file ;
: <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
M: unix open-serial ( serial -- serial' )
dup
path>> flags{ O_RDWR O_NOCTTY O_NDELAY } file-mode open-file
fd>duplex-stream >>stream ;
: open-unix-serial-port ( serial-port -- )
[
path>> flags{ O_RDWR O_NOCTTY O_NDELAY } file-mode open-file
fd>duplex-stream
] keep stream<< ;
: serial-fd ( serial -- fd )
stream>> in>> handle>> fd>> ;
: get-termios ( serial -- termios )
serial-fd
termios <struct> [ tcgetattr io-error ] keep ;
: set-termios ( serial -- )
[
serial-fd
termios <struct> [ tcgetattr io-error ] keep
] keep termios<< ;
: configure-termios ( serial -- )
dup termios>>
@ -63,3 +67,19 @@ M: unix open-serial ( serial -- serial' )
: apply-termios ( serial -- )
[ serial-fd TCSANOW ]
[ termios>> ] bi tcsetattr io-error ;
M: unix open-serial ( serial -- serial' )
{
[ open-unix-serial-port ]
[ set-termios ]
[ configure-termios ]
[ tciflush ]
[ apply-termios ]
[ ]
} cleave ;
M: unix default-serial-flags
flags{ IGNPAR ICRNL } >>iflag
flags{ } >>oflag
flags{ CS8 CLOCAL CREAD } >>cflag
flags{ ICANON } >>lflag ;