diff --git a/extra/io/serial/serial.factor b/extra/io/serial/serial.factor index f7324acd05..293d0e8ad6 100644 --- a/extra/io/serial/serial.factor +++ b/extra/io/serial/serial.factor @@ -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 ; + +: ( 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" ] } diff --git a/extra/io/serial/unix/unix-tests.factor b/extra/io/serial/unix/unix-tests.factor index 422844ab82..f9b047c87b 100644 --- a/extra/io/serial/unix/unix-tests.factor +++ b/extra/io/serial/unix/unix-tests.factor @@ -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 ) + "/dev/ttyS0" 19200 ; + +: with-serial-port-test ( quot -- ) + [ ] dip with-serial-port ; inline + +! [ ] [ + ! [ "hello" over stream-write stream-flush ] with-serial-port-test +! ] unit-test diff --git a/extra/io/serial/unix/unix.factor b/extra/io/serial/unix/unix.factor index f2dec1972e..6bf7a85e16 100644 --- a/extra/io/serial/unix/unix.factor +++ b/extra/io/serial/unix/unix.factor @@ -30,19 +30,23 @@ FUNCTION: int cfsetspeed ( termios* t, speed_t s ) ; [ ] [ ] bi ; : open-rw ( path -- fd ) O_RDWR file-mode open-file ; + : ( 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 [ tcgetattr io-error ] keep ; +: set-termios ( serial -- ) + [ + serial-fd + termios [ 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 ;