File:  [gforth] / gforth / contrib / serial.fs
Revision 1.1: download - view: text, annotated - select for diffs
Fri Nov 5 14:27:32 2004 UTC (15 years, 4 months ago) by anton
Branches: MAIN
CVS tags: v0-7-0, HEAD
added Krishna Myneni's serial.fs, terminal.fs, and support stuff to contrib

    1: \ serial.fs
    2: \
    3: \ gforth interface words for Linux serial communcations.
    4: \
    5: \ Copyright (c) 2000--2004 David P. Wallace, Krishna Myneni
    6: \ Provided under the terms of the GNU General Public License
    7: \
    8: \ Requires:
    9: \
   10: \	strings.fs
   11: \	syscalls386.fs
   12: \
   13: \ Revisions:
   14: \
   15: \	3-13-2000  first working version
   16: \	6-03-2001  modified serial_open to disable XON/XOFF flow control,
   17: \			added bit constants for c_iflag, etc.,
   18: \			modified serial_setparams for readability  KM
   19: \      12-13-2001  modified serial_open to disable CR to NL translation.  KM
   20: \       9-17-2004  ported from kForth  KM
   21: 
   22: \ termios structure
   23: 
   24: create termios 4 4 + 4 + 4 + 2 + 64 + 4 + 4 + allot
   25: 
   26: 0 constant C_IFLAG
   27: 4 constant C_OFLAG
   28: 8 constant C_CFLAG
   29: 12 constant C_LFLAG
   30: 16 constant C_LINE
   31: 18 constant C__CC
   32: 82 constant C_ISPEED
   33: 86 constant C_OSPEED
   34: 
   35: \ c_iflag bits
   36: 
   37:    1 constant IGNBRK
   38:    2 constant BRKINT
   39:    4 constant IGNPAR
   40:    8 constant PARMRK
   41:   16 constant INPCK
   42:   32 constant ISTRIP
   43:   64 constant INLCR
   44:  128 constant IGNCR
   45:  256 constant ICRNL
   46:  512 constant IUCLC
   47: 1024 constant IXON
   48: 2048 constant IXANY
   49: 4096 constant IXOFF
   50: 8192 constant IMAXBEL
   51: 
   52: \ c_oflag bits
   53: 
   54:    1 constant OPOST
   55:    2 constant OLCUC
   56:    4 constant ONLCR
   57:    8 constant OCRNL
   58:   16 constant ONOCR
   59:   32 constant ONLRET
   60:   64 constant OFILL
   61:  128 constant OFDEL
   62:  256 constant NLDLY
   63: 
   64: \ c_cflag bits
   65: 			\ baud rates constants
   66: 4111 constant CBAUD
   67:    0 constant B0
   68:    7 constant B300
   69:    9 constant B1200
   70:   11 constant B2400
   71:   12 constant B4800
   72:   13 constant B9600
   73:   14 constant B19200
   74:   15 constant B38400
   75: 4097 constant B57600
   76: 4098 constant B115200
   77: 			\ character size constants
   78:   48 constant CSIZE
   79:    0 constant CS5
   80:   16 constant CS6
   81:   32 constant CS7
   82:   48 constant CS8
   83: 
   84: \ parity constants
   85: 
   86: 768 constant CPARITY
   87: 0 constant PARNONE
   88: 256 constant PAREVEN
   89: 768 constant PARODD
   90: 
   91: \ stop bits constants
   92: 
   93: 64 constant CSTOPB
   94: 0 constant ONESTOPB
   95: 64 constant TWOSTOPB
   96: 
   97: \ c_lflag bits
   98: 
   99:    1 constant ISIG
  100:    2 constant ICANON
  101:    4 constant XCASE
  102:    8 constant ECHO
  103:   16 constant ECHOE
  104:   32 constant ECHOK
  105:   64 constant ECHONL
  106:  128 constant NOFLSH
  107:  256 constant TOSTOP
  108:  512 constant ECHOCTL
  109: 1024 constant ECHOPRT
  110: 2048 constant ECHOKE
  111: 4096 constant FLUSHO
  112: 16384 constant PENDIN
  113: 32768 constant IEXTEN
  114: 
  115: \ com port constants
  116: 
  117: 0 constant COM1
  118: 1 constant COM2
  119: 2 constant COM3
  120: 3 constant COM4
  121: 
  122: 
  123: \ ioctl request constants
  124: 
  125: hex
  126: 5401 constant TCGETS
  127: 5402 constant TCSETS
  128: 540B constant TCFLSH
  129: 541B constant FIONREAD
  130: decimal
  131: 
  132: \ file control constants
  133: 
  134: hex
  135: 800 constant O_NDELAY
  136: 100 constant O_NOCTTY
  137: 002 constant O_RDWR
  138: decimal
  139: 
  140: 
  141: : serial_getoptions ( handle -- | read serial port options into termios )
  142: 	TCGETS termios ioctl drop ;
  143: 	
  144: : serial_setoptions ( handle -- | write termios into serial port options )
  145: 	TCSETS termios ioctl drop ;
  146: 	
  147: : serial_open ( port -- handle | opens the serial port for communcation )
  148: 	\ port is the serial port to open
  149: 	\ 0 = ttyS0 (COM1)
  150: 	\ 1 = ttyS1 (COM2)
  151: 	\ 2 = ttyS2 (COM3)
  152: 	\ 3 = ttyS3 (COM4)
  153: 	\ handle is a handle to the open serial port
  154: 	\ if handle < 0 there was an error opening the port
  155: 	dup
  156: 	0 >=
  157: 	if
  158: 		s>string count
  159: 		s" /dev/ttyS"
  160: 		2swap
  161: 		strcat
  162: 		strpck
  163: 		O_RDWR O_NOCTTY  O_NDELAY or or
  164: 		open
  165: 		dup
  166: 		dup
  167: 		serial_getoptions
  168: 	
  169: 		\ Disable XON/XOFF flow control and CR to NL mapping
  170: 
  171: 		termios C_IFLAG + @
  172: 		IXON IXOFF or IXANY or ICRNL or ( not) invert and
  173: 		termios C_IFLAG + !
  174: 
  175: 		\ Open for raw input
  176: 
  177: 		termios C_LFLAG + @
  178: 		ISIG ICANON or ECHO or ECHOE or ( not) invert
  179: 		and
  180: 		termios C_LFLAG + !
  181: 
  182: 		\ Open for raw output
  183: 
  184: 		termios C_OFLAG + @
  185: 		OPOST ( not) invert
  186: 		and
  187: 		termios C_OFLAG + !
  188: 		serial_setoptions
  189: 	then ;
  190: 	
  191: : serial_close ( handle -- | closes the port )
  192: 	\ handle = serial port handle received from serial_open
  193: 	
  194: 	close ;
  195: 
  196: : serial_write ( handle buf num_to_write -- num_written )
  197: 	\ handle = serial port handle received from serial_open
  198: 	\ buf = address to buffer that holds chars to be written
  199: 	\ num_to_write = number of chars to write
  200: 	\ num_written = number of chars actually written
  201: 	write ;
  202: 	
  203: : serial_read ( handle buf num_to_read -- num_read )
  204: 	\ handle = serial port handle received from serial_open
  205: 	\ buf = address to buffer to hold chars to being read
  206: 	\ num_to_read = number of chars to read
  207: 	\ num_read = number of chars actually read
  208: 	read ;
  209: 	
  210: : serial_setbaud ( handle baud -- )
  211: 	\ handle = serial port handle received from serial_open
  212: 	\ baud = desired baud rate ( use constants defined above )
  213: 	swap
  214: 	dup
  215: 	serial_getoptions
  216: 	\ set the baud rate
  217: 	swap
  218: 	termios C_CFLAG + @
  219: 	CBAUD ( not) invert and
  220: 	or
  221: 	termios C_CFLAG + !
  222: 	serial_setoptions ;
  223: 
  224: : serial_setparity ( handle parity -- )
  225: 	\ handle = serial port handle received from serial_open
  226: 	\ parity = desired parity ( use constants defined above )
  227:  	swap
  228: 	dup
  229: 	serial_getoptions
  230: 	\ set the parity
  231: 	swap
  232: 	termios C_CFLAG + @
  233: 	CPARITY ( not) invert and
  234: 	or
  235: 	termios C_CFLAG + !
  236: 	serial_setoptions ;
  237: 
  238: : serial_setstopbits ( handle stopbits -- )
  239: 	\ handle = serial port handle received from serial_open
  240: 	\ stopbits = desired number of stopbits ( constants above )
  241:  	swap
  242: 	dup
  243: 	serial_getoptions
  244: 	\ set the stop_bit
  245: 	swap
  246: 	termios C_CFLAG + @
  247: 	CSTOPB ( not) invert and
  248: 	or
  249: 	termios C_CFLAG + !
  250: 	serial_setoptions ;
  251: 
  252: : serial_setdatabits ( handle databits -- )
  253: 	\ handle = serial port handle received from serial_open
  254: 	\ databits = desired number of databits ( constants above )
  255: 	swap
  256: 	dup
  257: 	serial_getoptions
  258: 	\ set the data size
  259: 	swap
  260: 	termios C_CFLAG + @
  261: 	CSIZE ( not) invert and
  262: 	or
  263: 	termios C_CFLAG + !
  264: 	serial_setoptions ;
  265: 
  266: : serial_flush ( handle -- )
  267: 	\ handle = serial port handle received from serial_open
  268: 	TCFLSH 2 ioctl drop ;
  269: 
  270: variable inque
  271: 	
  272: : serial_lenrx ( handle -- rx_len)
  273: 	\ handle = serial port handle received from serial_open
  274: 	\ rx_len = number of chars in recieve que
  275: 	FIONREAD inque ioctl drop
  276: 	inque @ ;	 	
  277: 
  278: : serial_setparams ( handle ^str -- )
  279: 	\ ^str examples are 8N1, 7E1, etc.
  280: 	swap >r
  281: 	dup
  282: 	1+ c@
  283: 	case
  284: 	  [char] 8 of r@ CS8 serial_setdatabits endof
  285: 	  [char] 7 of r@ CS7 serial_setdatabits endof
  286: 	endcase
  287: 
  288: 	dup
  289: 	2 + c@
  290: 	case
  291: 	  [char] N of r@ PARNONE serial_setparity endof
  292: 	  [char] E of r@ PAREVEN serial_setparity endof
  293: 	  [char] O of r@ PARODD serial_setparity endof
  294: 	endcase
  295: 
  296: 	3 + c@
  297: 	case
  298: 	  [char] 1 of r@ ONESTOPB serial_setstopbits endof
  299: 	  [char] 2 of r@ TWOSTOPB serial_setstopbits endof
  300: 	endcase
  301: 	
  302: 	r> drop ;
  303: 	
  304: 
  305: 		
  306: 			
  307: 		
  308: 		

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>