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