author | Claus Gittinger <cg@exept.de> |
Wed, 05 Sep 2012 11:32:10 +0200 | |
changeset 2820 | 45ee5437c35b |
parent 2818 | 94d67df5c00d |
child 2998 | f3b0c290b01b |
permissions | -rw-r--r-- |
1593 | 1 |
" |
2 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
"{ Package: 'stx:libbasic2' }" |
|
13 |
||
14 |
NonPositionableExternalStream subclass:#SerialPort |
|
1595 | 15 |
instanceVariableNames:'portName baudRate stopBitsType parityType dataBits inFlowCtrlType |
16 |
outFlowCtrlType xOnChar xOffChar' |
|
1593 | 17 |
classVariableNames:'DefaultPortName' |
18 |
poolDictionaries:'' |
|
19 |
category:'Streams-External' |
|
20 |
! |
|
21 |
||
22 |
!SerialPort primitiveDefinitions! |
|
23 |
%{ |
|
24 |
||
25 |
#include "stxOSDefs.h" |
|
26 |
||
27 |
#ifdef WIN32 |
|
28 |
||
2818 | 29 |
/* this is to catch uses of those - there should be none */ |
1593 | 30 |
# undef __BEGIN_INTERRUPTABLE__ |
31 |
# undef __END_INTERRUPTABLE__ |
|
2818 | 32 |
# ifdef __TCC__ |
33 |
# define __BEGIN_INTERRUPTABLE__ xxxx |
|
34 |
# define __END_INTERRUPTABLE__ yyyy |
|
35 |
# else |
|
36 |
# define __BEGIN_INTERRUPTABLE__ .. |
|
37 |
# define __END_INTERRUPTABLE__ .. |
|
38 |
# endif |
|
1593 | 39 |
|
40 |
# define WRAP_STDIO |
|
41 |
||
42 |
# define PORT_FROM_FD(fd) (_get_osfhandle(fd)) |
|
43 |
# define closePort(port) closeHandle(port) |
|
44 |
# define SERIALPORT HANDLE |
|
45 |
||
46 |
#else /* not WIN32 */ |
|
47 |
||
48 |
# define PORT_FROM_FD(fd) (fd) |
|
49 |
# define closePort(port) close(port) |
|
50 |
# define SERIALPORT int |
|
51 |
||
52 |
#endif /* WIN32 */ |
|
53 |
||
54 |
# define PORT_FROM_FILE(f) (PORT_FROM_FD(fileno(f))) |
|
55 |
# define PORT_FROM_FILE_OBJECT(f) (PORT_FROM_FILE(__FILEVal(f))) |
|
56 |
||
57 |
#include <stdio.h> |
|
58 |
#include <errno.h> |
|
59 |
||
60 |
/* |
|
61 |
* on some systems errno is a macro ... check for it here |
|
62 |
*/ |
|
63 |
#ifndef errno |
|
64 |
extern errno; |
|
65 |
#endif |
|
66 |
||
67 |
#ifdef USE_H_ERRNO |
|
68 |
# ifndef h_errno |
|
69 |
extern h_errno; |
|
70 |
# endif |
|
71 |
#endif |
|
72 |
||
73 |
#ifdef DEBUG |
|
1839 | 74 |
# define DBGPRINTF(x) { if (__debugging__) console_printf x; } |
1593 | 75 |
#else |
76 |
# define DBGPRINTF(x) /* as nothing */ |
|
77 |
#endif |
|
78 |
||
79 |
#ifndef TRUE |
|
80 |
# define TRUE 1 |
|
81 |
#endif |
|
82 |
#ifndef FALSE |
|
83 |
# define FALSE 0 |
|
84 |
#endif |
|
85 |
#ifndef WIN32 |
|
86 |
typedef int BOOL; |
|
87 |
#endif |
|
88 |
||
89 |
||
90 |
%} |
|
91 |
! ! |
|
92 |
||
93 |
!SerialPort primitiveVariables! |
|
94 |
%{ |
|
95 |
static int __debugging__ = 0; |
|
96 |
%} |
|
97 |
! ! |
|
98 |
||
99 |
!SerialPort class methodsFor:'documentation'! |
|
100 |
||
101 |
copyright |
|
102 |
" |
|
103 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
104 |
All Rights Reserved |
|
105 |
||
106 |
This software is furnished under a license and may be used |
|
107 |
only in accordance with the terms of that license and with the |
|
108 |
inclusion of the above copyright notice. This software may not |
|
109 |
be provided or otherwise made available to, or used by, any |
|
110 |
other person. No title to or ownership of the software is |
|
111 |
hereby transferred. |
|
112 |
" |
|
113 |
! |
|
114 |
||
115 |
documentation |
|
116 |
" |
|
1598 | 117 |
This class provides access to serial ports. |
118 |
||
119 |
Arguments/Parameters/Instvars: |
|
120 |
||
121 |
portName String device name |
|
122 |
WIN32: COM0, COM1, ... |
|
123 |
Unix: /dev/cua0, ... |
|
124 |
||
125 |
||
126 |
baudRate Integer 9600, 19200, ... |
|
127 |
stopBitsType Symbol #stop1, #stop1_5 or #stop2 |
|
128 |
parityType Symbol #odd, #even or #none |
|
129 |
dataBits Integer 5, 6, 7, 8 |
|
1599 | 130 |
inFlowCtrlType Symbol #xOnOff, #hardware or #none |
131 |
outFlowCtrlType Symbol #xOnOff, #hardware or #none |
|
1598 | 132 |
xOnChar Integer | Character |
133 |
xOffChar' Integer | Character |
|
1593 | 134 |
" |
135 |
! |
|
136 |
||
137 |
examples |
|
138 |
" |
|
139 |
example (get help info from an nntp server): |
|
1598 | 140 |
[exBegin] |
1595 | 141 |
|serialPort| |
142 |
||
143 |
serialPort := SerialPort new setPortName:(SerialPort defaultPortName). |
|
144 |
serialPort open. |
|
145 |
serialPort close. |
|
1598 | 146 |
[exEnd] |
1596 | 147 |
|
1598 | 148 |
[exBegin] |
1596 | 149 |
|serialPort| |
150 |
||
151 |
serialPort := SerialPort new setPortName:'COM5'. |
|
152 |
serialPort open. |
|
153 |
serialPort nextPutAll:'hello world'. |
|
154 |
serialPort cr. |
|
155 |
serialPort close. |
|
1598 | 156 |
[exEnd] |
1597 | 157 |
|
1598 | 158 |
[exBegin] |
1597 | 159 |
|serialPort| |
160 |
||
161 |
serialPort := SerialPort new setPortName:'COM5'. |
|
162 |
serialPort open. |
|
163 |
Transcript showCR:(serialPort nextAvailable:100). |
|
164 |
serialPort close. |
|
1598 | 165 |
[exEnd] |
1593 | 166 |
" |
167 |
! ! |
|
168 |
||
1595 | 169 |
!SerialPort class methodsFor:'instance creation'! |
1593 | 170 |
|
1595 | 171 |
new |
172 |
"create a serial port" |
|
1593 | 173 |
|
1595 | 174 |
^ super new buffered:false |
175 |
! |
|
1593 | 176 |
|
177 |
portName:portName |
|
178 |
baudRate:baudRateOrNil stopBitsType:stopBitsTypeOrNil |
|
179 |
parityType:parityTypeOrNil dataBits:dataBitsOrNil |
|
180 |
inFlowCtrlType:inFlowCtrlTypeOrNil outFlowCtrlType:outFlowCtrlTypeOrNil |
|
181 |
xOnChar:xOnCharOrNil xOffChar:xOffCharOrNil |
|
182 |
||
183 |
"arguments are self describing; nil values mean: leave setting as is" |
|
184 |
||
1599 | 185 |
^ self new |
1593 | 186 |
setPortName:portName |
187 |
baudRate:baudRateOrNil stopBitsType:stopBitsTypeOrNil |
|
188 |
parityType:parityTypeOrNil dataBits:dataBitsOrNil |
|
189 |
inFlowCtrlType:inFlowCtrlTypeOrNil outFlowCtrlType:outFlowCtrlTypeOrNil |
|
1599 | 190 |
xOnChar:xOnCharOrNil xOffChar:xOffCharOrNil |
1593 | 191 |
! ! |
192 |
||
193 |
!SerialPort class methodsFor:'debugging'! |
|
194 |
||
195 |
debug:aBoolean |
|
196 |
"turn on/off internal debugprints. |
|
197 |
This method is for ST/X debugging only and |
|
198 |
may be removed in later versions" |
|
199 |
||
200 |
%{ /* NOCONTEXT */ |
|
201 |
||
202 |
__debugging__ = (aBoolean == true); |
|
203 |
%} |
|
204 |
" |
|
205 |
SerialPort debug:true |
|
206 |
SerialPort debug:false |
|
207 |
" |
|
208 |
! ! |
|
209 |
||
1595 | 210 |
!SerialPort class methodsFor:'defaults'! |
211 |
||
212 |
defaultPortName |
|
213 |
DefaultPortName notNil ifTrue:[^ DefaultPortName ]. |
|
214 |
||
215 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
1596 | 216 |
^ 'COM0' |
1595 | 217 |
]. |
218 |
^ '/dev/cua0' |
|
219 |
! ! |
|
220 |
||
1593 | 221 |
!SerialPort methodsFor:'accessing'! |
222 |
||
1595 | 223 |
setBaudRate:baudRateOrNil |
224 |
baudRate := baudRateOrNil. |
|
225 |
! |
|
226 |
||
1598 | 227 |
setDataBits:dataBitsOrNil |
228 |
dataBits := dataBitsOrNil. |
|
229 |
! |
|
230 |
||
231 |
setParityType:parityTypeOrNil |
|
232 |
parityType := parityTypeOrNil. |
|
233 |
! |
|
234 |
||
1595 | 235 |
setPortName:portNameArg |
2020
f47ac2710db3
Change port name COM* to \\.\COM* to support com ports > 9
ab
parents:
1903
diff
changeset
|
236 |
(portNameArg startsWith:'C') ifTrue:[ |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
237 |
"/ add special port name to support COM ports > 9 |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
238 |
portName := '\\.\', portNameArg. |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
239 |
^ self |
2020
f47ac2710db3
Change port name COM* to \\.\COM* to support com ports > 9
ab
parents:
1903
diff
changeset
|
240 |
]. |
1595 | 241 |
portName := portNameArg. |
242 |
! |
|
243 |
||
1593 | 244 |
setPortName:portNameArg |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
245 |
baudRate:baudRateOrNil stopBitsType:stopBitsTypeOrNil |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
246 |
parityType:parityTypeOrNil dataBits:dataBitsOrNil |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
247 |
inFlowCtrlType:inFlowCtrlTypeOrNil outFlowCtrlType:outFlowCtrlTypeOrNil |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
248 |
xOnChar:xOnCharOrNil xOffChar:xOffCharOrNil |
1593 | 249 |
|
2020
f47ac2710db3
Change port name COM* to \\.\COM* to support com ports > 9
ab
parents:
1903
diff
changeset
|
250 |
self setPortName:portNameArg. |
1593 | 251 |
baudRate := baudRateOrNil. |
252 |
stopBitsType := stopBitsTypeOrNil. |
|
253 |
parityType := parityTypeOrNil. |
|
254 |
dataBits := dataBitsOrNil. |
|
255 |
inFlowCtrlType := inFlowCtrlTypeOrNil. |
|
256 |
outFlowCtrlType := outFlowCtrlTypeOrNil. |
|
257 |
xOnChar := xOnCharOrNil. |
|
258 |
xOffChar := xOffCharOrNil. |
|
1846 | 259 |
! |
260 |
||
261 |
setStopBitsType:stopBitsTypeOrNil |
|
262 |
stopBitsType := stopBitsTypeOrNil. |
|
1593 | 263 |
! ! |
264 |
||
1595 | 265 |
!SerialPort methodsFor:'low level'! |
266 |
||
267 |
baudRate:newRate |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
268 |
handle isNil ifTrue:[ |
1598 | 269 |
"not open" |
270 |
baudRate := newRate. |
|
271 |
^ self |
|
272 |
]. |
|
1595 | 273 |
%{ |
274 |
OBJ fp; |
|
275 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
276 |
fp = __INST(handle); |
1598 | 277 |
if (__isSmallInteger(newRate)) { |
1595 | 278 |
SERIALPORT port; |
279 |
int ret; |
|
280 |
||
281 |
port = PORT_FROM_FILE_OBJECT(fp); |
|
282 |
#ifdef WIN32 |
|
283 |
{ |
|
284 |
DCB dcb; |
|
285 |
||
286 |
ZeroMemory(&dcb, sizeof(dcb)); |
|
287 |
dcb.DCBlength = sizeof(dcb); |
|
288 |
GetCommState(port, &dcb); |
|
289 |
||
290 |
dcb.BaudRate = __intVal(newRate); |
|
291 |
||
292 |
if (! SetCommState(port, &dcb)) { |
|
293 |
RETURN(false); |
|
294 |
} |
|
1598 | 295 |
__INST(baudRate) = newRate; |
1595 | 296 |
RETURN(true); |
297 |
} |
|
298 |
# else /* ! WIN32 */ |
|
299 |
/* add code for unix ioctl here ... */ |
|
300 |
# endif /* WIN32 */ |
|
301 |
} |
|
302 |
%}. |
|
303 |
self primitiveFailed. |
|
2692 | 304 |
! |
1593 | 305 |
|
306 |
closeFile |
|
307 |
"low level close" |
|
308 |
||
309 |
%{ |
|
310 |
OBJ t; |
|
311 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
312 |
t = __INST(handle); |
1593 | 313 |
if (t != nil) { |
2818 | 314 |
FILE *fp; |
1593 | 315 |
|
2818 | 316 |
__INST(handle) = nil; |
317 |
fp = __FILEVal(t); |
|
2610 | 318 |
#ifdef WIN32 |
2818 | 319 |
{ |
320 |
int ret; |
|
321 |
SERIALPORT port; |
|
1593 | 322 |
|
2818 | 323 |
port = PORT_FROM_FILE(fp); |
2610 | 324 |
# ifdef xxxDO_WRAP_CALLS |
2818 | 325 |
do { |
326 |
__threadErrno = 0; |
|
327 |
ret = STX_C_CALL1("fclose", fclose, fp); |
|
328 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
|
1593 | 329 |
|
2818 | 330 |
do { |
331 |
__threadErrno = 0; |
|
332 |
ret = STX_WSA_CALL1("CloseHandle", CloseHandle, port); |
|
333 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
|
2610 | 334 |
# else |
2818 | 335 |
fclose(fp); |
336 |
/* In WIN32, apparently the fclose() does not close the SerialPort hadle, |
|
337 |
* so we have to do it here! |
|
338 |
*/ |
|
339 |
CloseHandle(port); |
|
2610 | 340 |
# endif /* DO_WRAP_CALLS */ |
2818 | 341 |
} |
342 |
#else |
|
343 |
fclose(fp); |
|
2610 | 344 |
#endif |
1593 | 345 |
} |
346 |
%} |
|
2610 | 347 |
|
348 |
"Modified: / 05-08-2011 / 14:13:35 / cg" |
|
1593 | 349 |
! ! |
350 |
||
351 |
!SerialPort methodsFor:'opening'! |
|
352 |
||
353 |
open |
|
1596 | 354 |
|errorSymbol errorNumber| |
1593 | 355 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
356 |
handle notNil ifTrue:[ |
2818 | 357 |
^ self errorAlreadyOpen |
1593 | 358 |
]. |
359 |
%{ |
|
360 |
FILE *fp; |
|
361 |
SERIALPORT port; |
|
362 |
char *__portName; |
|
363 |
int __setBaudRate = 1, |
|
2818 | 364 |
__setDataBits = 1, |
365 |
__setXOnChar = 1, |
|
366 |
__setXOffChar = 1, |
|
367 |
__setInFlowCtrl = 1, |
|
368 |
__setOutFlowCtrl = 1, |
|
369 |
__setStopBits = 1, |
|
370 |
__setParityType = 1; |
|
1593 | 371 |
int __baudRate, __dataBits; |
372 |
int __xOnChar, __xOffChar; |
|
373 |
int __inFlowCtrl, __outFlowCtrl; |
|
374 |
int __stopBits, __parityType; |
|
375 |
#ifdef WIN32 |
|
376 |
COMMTIMEOUTS timeouts; |
|
377 |
DCB dcb; |
|
378 |
#endif /* WIN32 */ |
|
379 |
||
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
380 |
# define FLG_FLOW_XONOFF 1 |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
381 |
# define FLG_FLOW_HARDWARE 2 |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
382 |
# define FLG_FLOW_NONE 3 |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
383 |
# define FLG_STOP_1 1 |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
384 |
# define FLG_STOP_2 2 |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
385 |
# define FLG_STOP_1_5 3 |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
386 |
# define FLG_PARITY_ODD 1 |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
387 |
# define FLG_PARITY_EVEN 2 |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
388 |
# define FLG_PARITY_NONE 3 |
1593 | 389 |
|
2751 | 390 |
if (__isStringLike(__INST(portName))) { |
2818 | 391 |
__portName = __stringVal(__INST(portName)); |
1593 | 392 |
} else { |
2818 | 393 |
errorSymbol = @symbol(portName); |
394 |
goto getOutOfhere; |
|
1593 | 395 |
} |
396 |
||
397 |
if (__isSmallInteger(__INST(baudRate))) { |
|
2818 | 398 |
__baudRate = __intVal(__INST(baudRate)); |
1593 | 399 |
} else if (__INST(baudRate) == nil) { |
2818 | 400 |
__setBaudRate = 0; |
1593 | 401 |
} else { |
2818 | 402 |
errorSymbol = @symbol(baudRate); |
403 |
goto getOutOfhere; |
|
1593 | 404 |
} |
405 |
||
406 |
if (__isSmallInteger(__INST(dataBits))) { |
|
2818 | 407 |
__dataBits = __intVal(__INST(dataBits)); |
1596 | 408 |
} else if (__INST(dataBits) == nil) { |
2818 | 409 |
__setDataBits = 0; |
1593 | 410 |
} else { |
2818 | 411 |
errorSymbol = @symbol(dataBits); |
412 |
goto getOutOfhere; |
|
1593 | 413 |
} |
414 |
||
415 |
if (__isSmallInteger(__INST(xOnChar))) { |
|
2818 | 416 |
__xOnChar = __intVal(__INST(xOnChar)); |
1593 | 417 |
} else if (__isCharacter(__INST(xOnChar))) { |
2818 | 418 |
__xOnChar = __intVal(_characterVal(__INST(xOnChar))); |
1593 | 419 |
} else if (__INST(xOnChar) == nil) { |
2818 | 420 |
__setXOnChar = 0; |
1593 | 421 |
} else { |
2818 | 422 |
errorSymbol = @symbol(xOnChar); |
423 |
goto getOutOfhere; |
|
1593 | 424 |
} |
425 |
||
426 |
if (__isSmallInteger(__INST(xOffChar))) { |
|
2818 | 427 |
__xOffChar = __intVal(__INST(xOffChar)); |
1593 | 428 |
} else if (__isCharacter(__INST(xOffChar))) { |
2818 | 429 |
__xOffChar = __intVal(__characterVal(__INST(xOffChar))); |
1593 | 430 |
} else if (__INST(xOffChar) == nil) { |
2818 | 431 |
__setXOffChar = 0; |
1593 | 432 |
} else { |
2818 | 433 |
errorSymbol = @symbol(xOffChar); |
434 |
goto getOutOfhere; |
|
1593 | 435 |
} |
436 |
||
437 |
if (__INST(inFlowCtrlType) == @symbol(xOnOff)) { |
|
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
438 |
__inFlowCtrl = FLG_FLOW_XONOFF; |
1593 | 439 |
} else if (__INST(inFlowCtrlType) == @symbol(hardware)) { |
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
440 |
__inFlowCtrl = FLG_FLOW_HARDWARE; |
1599 | 441 |
} else if (__INST(inFlowCtrlType) == @symbol(none)) { |
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
442 |
__inFlowCtrl = FLG_FLOW_NONE; |
1593 | 443 |
} else if (__INST(inFlowCtrlType) == nil) { |
2818 | 444 |
__setInFlowCtrl = 0; |
1593 | 445 |
} else { |
2818 | 446 |
errorSymbol = @symbol(inFlowCtrlType); |
447 |
goto getOutOfhere; |
|
1593 | 448 |
} |
449 |
||
450 |
if (__INST(outFlowCtrlType) == @symbol(xOnOff)) { |
|
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
451 |
__outFlowCtrl = FLG_FLOW_XONOFF; |
1593 | 452 |
} else if (__INST(outFlowCtrlType) == @symbol(hardware)) { |
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
453 |
__outFlowCtrl = FLG_FLOW_HARDWARE; |
1599 | 454 |
} else if (__INST(outFlowCtrlType) == @symbol(none)) { |
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
455 |
__outFlowCtrl = FLG_FLOW_NONE; |
1593 | 456 |
} else if (__INST(outFlowCtrlType) == nil) { |
2818 | 457 |
__setOutFlowCtrl = 0; |
1593 | 458 |
} else { |
2818 | 459 |
errorSymbol = @symbol(outFlowCtrlType); |
460 |
goto getOutOfhere; |
|
1593 | 461 |
} |
462 |
||
463 |
if (__INST(stopBitsType) == @symbol(stop1)) { |
|
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
464 |
__stopBits = FLG_STOP_1; |
1593 | 465 |
} else if (__INST(stopBitsType) == @symbol(stop2)) { |
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
466 |
__stopBits = FLG_STOP_2; |
1593 | 467 |
} else if (__INST(stopBitsType) == @symbol(stop1_5)) { |
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
468 |
__stopBits = FLG_STOP_1_5; |
1593 | 469 |
} else if (__INST(stopBitsType) == nil) { |
2818 | 470 |
__setStopBits = 0; |
1593 | 471 |
} else { |
2818 | 472 |
errorSymbol = @symbol(stopBitsType); |
473 |
goto getOutOfhere; |
|
1596 | 474 |
} |
475 |
||
476 |
if (__INST(parityType) == @symbol(odd)) { |
|
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
477 |
__parityType = FLG_PARITY_ODD; |
1596 | 478 |
} else if (__INST(parityType) == @symbol(even)) { |
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
479 |
__parityType = FLG_PARITY_EVEN; |
1596 | 480 |
} else if (__INST(parityType) == @symbol(none)) { |
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
481 |
__parityType = FLG_PARITY_NONE; |
1596 | 482 |
} else if (__INST(parityType) == nil) { |
2818 | 483 |
__setParityType = 0; |
1596 | 484 |
} else { |
2818 | 485 |
errorSymbol = @symbol(parityType); |
486 |
goto getOutOfhere; |
|
1593 | 487 |
} |
488 |
||
489 |
#ifdef WIN32 |
|
490 |
port = CreateFile(__portName, |
|
2818 | 491 |
GENERIC_READ | GENERIC_WRITE, |
492 |
0, /* comm devices must be opened with exclusive access */ |
|
493 |
NULL, /* no security attrs */ |
|
494 |
OPEN_EXISTING, /* comm devices must use OPEN_EXISTING */ |
|
495 |
0, /* no overlapped I/O */ |
|
496 |
NULL /* hTemplate must be NULL for comm devices */ |
|
497 |
); |
|
1593 | 498 |
|
499 |
if (port == INVALID_HANDLE_VALUE) { |
|
2818 | 500 |
console_fprintf(stderr, "Win32OS [info]: serial port open failed\n"); |
501 |
errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) ); |
|
502 |
errorSymbol = @symbol(openFailed); |
|
503 |
goto getOutOfhere; |
|
1593 | 504 |
} |
505 |
||
506 |
/* Flush the driver */ |
|
507 |
PurgeComm( port, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); |
|
508 |
||
509 |
/* Set driver buffer sizes */ |
|
510 |
SetupComm( port, 4096 /*SERIAL_IN_QUEUE_SIZE*/, 4096 /*SERIAL_OUT_QUEUE_SIZE*/); |
|
511 |
||
512 |
/* Reset timeout constants */ |
|
513 |
timeouts.ReadIntervalTimeout= 0xFFFFFFFF; |
|
514 |
timeouts.ReadTotalTimeoutMultiplier = 0; |
|
515 |
timeouts.ReadTotalTimeoutConstant = 0; |
|
516 |
timeouts.WriteTotalTimeoutMultiplier = 0; |
|
517 |
timeouts.WriteTotalTimeoutConstant = 0; |
|
518 |
SetCommTimeouts( port, &timeouts ); |
|
519 |
||
520 |
/* Set communication parameters */ |
|
521 |
ZeroMemory(&dcb, sizeof(dcb)); |
|
522 |
dcb.DCBlength = sizeof(dcb); |
|
523 |
GetCommState(port, &dcb); |
|
524 |
||
525 |
if (__setBaudRate) dcb.BaudRate = __baudRate; |
|
526 |
if (__setDataBits) dcb.ByteSize = __dataBits; |
|
527 |
if (__setXOnChar) dcb.XonChar = __xOnChar; |
|
528 |
if (__setXOffChar) dcb.XoffChar = __xOffChar; |
|
529 |
||
530 |
if (__setStopBits) { |
|
2818 | 531 |
/* set stop bits */ |
532 |
switch(__stopBits) { |
|
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
533 |
case FLG_STOP_1_5: dcb.StopBits = 1; break; /* 1.5 stop bits */ |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
534 |
case FLG_STOP_1: dcb.StopBits = 0; break; /* 1 stop bit */ |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
535 |
case FLG_STOP_2: dcb.StopBits = 2; break; /* 2 stop bits */ |
2818 | 536 |
default: |
537 |
errorSymbol = @symbol(stopBits); |
|
538 |
goto errExit; |
|
539 |
} |
|
1593 | 540 |
} |
541 |
||
542 |
if (__setParityType) { |
|
2818 | 543 |
/* set parity */ |
544 |
switch(__parityType) { |
|
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
545 |
case FLG_PARITY_NONE: dcb.Parity = NOPARITY; break; |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
546 |
case FLG_PARITY_ODD: dcb.Parity = ODDPARITY; break; |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
547 |
case FLG_PARITY_EVEN: dcb.Parity = EVENPARITY; break; |
2818 | 548 |
default: |
549 |
errorSymbol = @symbol(parityType); |
|
550 |
goto errExit; |
|
551 |
} |
|
1593 | 552 |
} |
553 |
||
554 |
if (__setInFlowCtrl) { |
|
2818 | 555 |
/* set control flow */ |
556 |
dcb.fInX = FALSE; |
|
557 |
dcb.fDtrControl = FALSE; |
|
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
558 |
if (__inFlowCtrl == FLG_FLOW_XONOFF) dcb.fInX = TRUE; /* XOn/XOff handshaking */ |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
559 |
if (__inFlowCtrl == FLG_FLOW_HARDWARE) dcb.fDtrControl = TRUE; /* hardware handshaking */ |
1593 | 560 |
} |
561 |
if (__setOutFlowCtrl) { |
|
2818 | 562 |
dcb.fOutX = FALSE; |
563 |
dcb.fOutxCtsFlow = FALSE; |
|
1593 | 564 |
|
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
565 |
if (__outFlowCtrl == FLG_FLOW_XONOFF) dcb.fOutX = TRUE; /* XOn/XOff handshaking */ |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
566 |
if (__outFlowCtrl == FLG_FLOW_HARDWARE) dcb.fOutxCtsFlow = TRUE; /* hardware handshaking */ |
1593 | 567 |
} |
568 |
||
569 |
if (! SetCommState(port, &dcb)) { |
|
2818 | 570 |
console_fprintf(stderr, "Win32OS [info]: serial port comm-setup failed\n"); |
571 |
errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) ); |
|
572 |
goto errExit; |
|
1593 | 573 |
} |
574 |
# endif /* WIN32 */ |
|
575 |
||
576 |
/* |
|
577 |
* make it a FILE * |
|
578 |
*/ |
|
579 |
# ifdef WIN32 |
|
580 |
{ |
|
2818 | 581 |
int _fd; |
582 |
__stxWrapApiEnterCritical(); |
|
583 |
_fd = _open_osfhandle((long)port, 0); |
|
584 |
fp = fdopen(_fd, "r+"); |
|
585 |
__stxWrapApiLeaveCritical(); |
|
1593 | 586 |
} |
587 |
# else |
|
588 |
fp = fdopen(port, "r+"); |
|
589 |
# endif |
|
590 |
if (! fp) { |
|
2818 | 591 |
console_fprintf(stderr, "Win32OS [info]: fdopen failed\n"); |
592 |
errorNumber = __MKSMALLINT(errno); |
|
1596 | 593 |
errExit: ; |
2610 | 594 |
# ifdef WIN32 |
2818 | 595 |
CloseHandle(port); |
2610 | 596 |
# endif |
2818 | 597 |
goto getOutOfhere; |
1593 | 598 |
} |
599 |
||
600 |
{ |
|
2818 | 601 |
OBJ t; |
1593 | 602 |
|
2818 | 603 |
t = __MKEXTERNALADDRESS(fp); |
604 |
__INST(handle) = t; |
|
605 |
__STORE(self, t); |
|
1593 | 606 |
} |
607 |
||
1596 | 608 |
getOutOfhere: ; |
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
609 |
# undef FLG_FLOW_XONOFF |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
610 |
# undef FLG_FLOW_HARDWARE |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
611 |
# undef FLG_FLOW_NONE |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
612 |
# undef FLG_STOP_1 |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
613 |
# undef FLG_STOP_2 |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
614 |
# undef FLG_STOP_1_5 |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
615 |
# undef FLG_PARITY_ODD |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
616 |
# undef FLG_PARITY_EVEN |
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
617 |
# undef FLG_PARITY_NONE |
1593 | 618 |
|
619 |
%}. |
|
620 |
"all ok?" |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
621 |
handle notNil ifTrue:[ |
2818 | 622 |
Lobby register:self. |
1593 | 623 |
] ifFalse:[ |
2818 | 624 |
errorNumber isNil ifTrue:[ |
625 |
self error:'invalid argument(s)'. |
|
626 |
] ifFalse:[ |
|
627 |
((OperatingSystem errorHolderForNumber:errorNumber) parameter:portName) reportError |
|
628 |
]. |
|
1593 | 629 |
]. |
1846 | 630 |
|
2610 | 631 |
"Modified: / 05-08-2011 / 14:14:08 / cg" |
1593 | 632 |
! ! |
633 |
||
1595 | 634 |
!SerialPort methodsFor:'printing & storing'! |
635 |
||
636 |
printOn:aStream |
|
1903
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
637 |
aStream nextPutAll:'SerialPort('. |
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
638 |
portName printOn:aStream. |
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
639 |
aStream nextPut:$/. |
1595 | 640 |
baudRate printOn:aStream. |
1903
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
641 |
aStream nextPut:$). |
1595 | 642 |
! ! |
643 |
||
644 |
!SerialPort methodsFor:'queries'! |
|
645 |
||
646 |
getName |
|
647 |
"return the name; here, we return the devices name" |
|
648 |
||
649 |
^ portName |
|
650 |
! ! |
|
651 |
||
1593 | 652 |
!SerialPort class methodsFor:'documentation'! |
653 |
||
654 |
version |
|
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
655 |
^ '$Header: /cvs/stx/stx/libbasic2/SerialPort.st,v 1.21 2012-09-05 09:32:10 cg Exp $' |
2419
8ea9b41e7b4b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
656 |
! |
8ea9b41e7b4b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
657 |
|
8ea9b41e7b4b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
658 |
version_CVS |
2820
45ee5437c35b
flag names changed; they conflict with a header file under some win32 compilers (lcc)
Claus Gittinger <cg@exept.de>
parents:
2818
diff
changeset
|
659 |
^ '$Header: /cvs/stx/stx/libbasic2/SerialPort.st,v 1.21 2012-09-05 09:32:10 cg Exp $' |
1593 | 660 |
! ! |