author | Stefan Vogel <sv@exept.de> |
Tue, 28 Jan 2014 23:20:58 +0100 | |
changeset 3153 | eff2380e918e |
parent 2998 | f3b0c290b01b |
child 3219 | 65db912c5ff8 |
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 |
|
171 |
portName:portName |
|
172 |
baudRate:baudRateOrNil stopBitsType:stopBitsTypeOrNil |
|
173 |
parityType:parityTypeOrNil dataBits:dataBitsOrNil |
|
174 |
inFlowCtrlType:inFlowCtrlTypeOrNil outFlowCtrlType:outFlowCtrlTypeOrNil |
|
175 |
xOnChar:xOnCharOrNil xOffChar:xOffCharOrNil |
|
176 |
||
177 |
"arguments are self describing; nil values mean: leave setting as is" |
|
178 |
||
1599 | 179 |
^ self new |
1593 | 180 |
setPortName:portName |
181 |
baudRate:baudRateOrNil stopBitsType:stopBitsTypeOrNil |
|
182 |
parityType:parityTypeOrNil dataBits:dataBitsOrNil |
|
183 |
inFlowCtrlType:inFlowCtrlTypeOrNil outFlowCtrlType:outFlowCtrlTypeOrNil |
|
1599 | 184 |
xOnChar:xOnCharOrNil xOffChar:xOffCharOrNil |
1593 | 185 |
! ! |
186 |
||
187 |
!SerialPort class methodsFor:'debugging'! |
|
188 |
||
189 |
debug:aBoolean |
|
190 |
"turn on/off internal debugprints. |
|
191 |
This method is for ST/X debugging only and |
|
192 |
may be removed in later versions" |
|
193 |
||
194 |
%{ /* NOCONTEXT */ |
|
195 |
||
196 |
__debugging__ = (aBoolean == true); |
|
197 |
%} |
|
198 |
" |
|
199 |
SerialPort debug:true |
|
200 |
SerialPort debug:false |
|
201 |
" |
|
202 |
! ! |
|
203 |
||
1595 | 204 |
!SerialPort class methodsFor:'defaults'! |
205 |
||
206 |
defaultPortName |
|
207 |
DefaultPortName notNil ifTrue:[^ DefaultPortName ]. |
|
208 |
||
209 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
1596 | 210 |
^ 'COM0' |
1595 | 211 |
]. |
212 |
^ '/dev/cua0' |
|
213 |
! ! |
|
214 |
||
1593 | 215 |
!SerialPort methodsFor:'accessing'! |
216 |
||
1595 | 217 |
setBaudRate:baudRateOrNil |
218 |
baudRate := baudRateOrNil. |
|
219 |
! |
|
220 |
||
1598 | 221 |
setDataBits:dataBitsOrNil |
222 |
dataBits := dataBitsOrNil. |
|
223 |
! |
|
224 |
||
225 |
setParityType:parityTypeOrNil |
|
226 |
parityType := parityTypeOrNil. |
|
227 |
! |
|
228 |
||
1595 | 229 |
setPortName:portNameArg |
2020
f47ac2710db3
Change port name COM* to \\.\COM* to support com ports > 9
ab
parents:
1903
diff
changeset
|
230 |
(portNameArg startsWith:'C') ifTrue:[ |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
231 |
"/ add special port name to support COM ports > 9 |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
232 |
portName := '\\.\', portNameArg. |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
233 |
^ self |
2020
f47ac2710db3
Change port name COM* to \\.\COM* to support com ports > 9
ab
parents:
1903
diff
changeset
|
234 |
]. |
1595 | 235 |
portName := portNameArg. |
236 |
! |
|
237 |
||
1593 | 238 |
setPortName:portNameArg |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
239 |
baudRate:baudRateOrNil stopBitsType:stopBitsTypeOrNil |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
240 |
parityType:parityTypeOrNil dataBits:dataBitsOrNil |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
241 |
inFlowCtrlType:inFlowCtrlTypeOrNil outFlowCtrlType:outFlowCtrlTypeOrNil |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
242 |
xOnChar:xOnCharOrNil xOffChar:xOffCharOrNil |
1593 | 243 |
|
2020
f47ac2710db3
Change port name COM* to \\.\COM* to support com ports > 9
ab
parents:
1903
diff
changeset
|
244 |
self setPortName:portNameArg. |
1593 | 245 |
baudRate := baudRateOrNil. |
246 |
stopBitsType := stopBitsTypeOrNil. |
|
247 |
parityType := parityTypeOrNil. |
|
248 |
dataBits := dataBitsOrNil. |
|
249 |
inFlowCtrlType := inFlowCtrlTypeOrNil. |
|
250 |
outFlowCtrlType := outFlowCtrlTypeOrNil. |
|
251 |
xOnChar := xOnCharOrNil. |
|
252 |
xOffChar := xOffCharOrNil. |
|
1846 | 253 |
! |
254 |
||
255 |
setStopBitsType:stopBitsTypeOrNil |
|
256 |
stopBitsType := stopBitsTypeOrNil. |
|
1593 | 257 |
! ! |
258 |
||
1595 | 259 |
!SerialPort methodsFor:'low level'! |
260 |
||
261 |
baudRate:newRate |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
262 |
handle isNil ifTrue:[ |
1598 | 263 |
"not open" |
264 |
baudRate := newRate. |
|
265 |
^ self |
|
266 |
]. |
|
1595 | 267 |
%{ |
268 |
OBJ fp; |
|
269 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
270 |
fp = __INST(handle); |
1598 | 271 |
if (__isSmallInteger(newRate)) { |
1595 | 272 |
SERIALPORT port; |
273 |
int ret; |
|
274 |
||
275 |
port = PORT_FROM_FILE_OBJECT(fp); |
|
276 |
#ifdef WIN32 |
|
277 |
{ |
|
278 |
DCB dcb; |
|
279 |
||
280 |
ZeroMemory(&dcb, sizeof(dcb)); |
|
281 |
dcb.DCBlength = sizeof(dcb); |
|
282 |
GetCommState(port, &dcb); |
|
283 |
||
284 |
dcb.BaudRate = __intVal(newRate); |
|
285 |
||
286 |
if (! SetCommState(port, &dcb)) { |
|
287 |
RETURN(false); |
|
288 |
} |
|
1598 | 289 |
__INST(baudRate) = newRate; |
1595 | 290 |
RETURN(true); |
291 |
} |
|
292 |
# else /* ! WIN32 */ |
|
293 |
/* add code for unix ioctl here ... */ |
|
294 |
# endif /* WIN32 */ |
|
295 |
} |
|
296 |
%}. |
|
297 |
self primitiveFailed. |
|
2692 | 298 |
! |
1593 | 299 |
|
300 |
closeFile |
|
301 |
"low level close" |
|
302 |
||
303 |
%{ |
|
304 |
OBJ t; |
|
305 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
306 |
t = __INST(handle); |
1593 | 307 |
if (t != nil) { |
2818 | 308 |
FILE *fp; |
1593 | 309 |
|
2818 | 310 |
__INST(handle) = nil; |
311 |
fp = __FILEVal(t); |
|
2610 | 312 |
#ifdef WIN32 |
2818 | 313 |
{ |
314 |
int ret; |
|
315 |
SERIALPORT port; |
|
1593 | 316 |
|
2818 | 317 |
port = PORT_FROM_FILE(fp); |
2610 | 318 |
# ifdef xxxDO_WRAP_CALLS |
2818 | 319 |
do { |
320 |
__threadErrno = 0; |
|
321 |
ret = STX_C_CALL1("fclose", fclose, fp); |
|
322 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
|
1593 | 323 |
|
2818 | 324 |
do { |
325 |
__threadErrno = 0; |
|
326 |
ret = STX_WSA_CALL1("CloseHandle", CloseHandle, port); |
|
327 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
|
2610 | 328 |
# else |
2818 | 329 |
fclose(fp); |
330 |
/* In WIN32, apparently the fclose() does not close the SerialPort hadle, |
|
331 |
* so we have to do it here! |
|
332 |
*/ |
|
333 |
CloseHandle(port); |
|
2610 | 334 |
# endif /* DO_WRAP_CALLS */ |
2818 | 335 |
} |
336 |
#else |
|
337 |
fclose(fp); |
|
2610 | 338 |
#endif |
1593 | 339 |
} |
340 |
%} |
|
2610 | 341 |
|
342 |
"Modified: / 05-08-2011 / 14:13:35 / cg" |
|
1593 | 343 |
! ! |
344 |
||
345 |
!SerialPort methodsFor:'opening'! |
|
346 |
||
347 |
open |
|
1596 | 348 |
|errorSymbol errorNumber| |
1593 | 349 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
350 |
handle notNil ifTrue:[ |
2818 | 351 |
^ self errorAlreadyOpen |
1593 | 352 |
]. |
353 |
%{ |
|
354 |
FILE *fp; |
|
355 |
SERIALPORT port; |
|
356 |
char *__portName; |
|
357 |
int __setBaudRate = 1, |
|
2818 | 358 |
__setDataBits = 1, |
359 |
__setXOnChar = 1, |
|
360 |
__setXOffChar = 1, |
|
361 |
__setInFlowCtrl = 1, |
|
362 |
__setOutFlowCtrl = 1, |
|
363 |
__setStopBits = 1, |
|
364 |
__setParityType = 1; |
|
1593 | 365 |
int __baudRate, __dataBits; |
366 |
int __xOnChar, __xOffChar; |
|
367 |
int __inFlowCtrl, __outFlowCtrl; |
|
368 |
int __stopBits, __parityType; |
|
369 |
#ifdef WIN32 |
|
370 |
COMMTIMEOUTS timeouts; |
|
371 |
DCB dcb; |
|
372 |
#endif /* WIN32 */ |
|
373 |
||
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
|
374 |
# 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
|
375 |
# 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
|
376 |
# 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
|
377 |
# 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
|
378 |
# 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
|
379 |
# 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
|
380 |
# 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
|
381 |
# 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
|
382 |
# define FLG_PARITY_NONE 3 |
1593 | 383 |
|
2751 | 384 |
if (__isStringLike(__INST(portName))) { |
2818 | 385 |
__portName = __stringVal(__INST(portName)); |
1593 | 386 |
} else { |
2818 | 387 |
errorSymbol = @symbol(portName); |
388 |
goto getOutOfhere; |
|
1593 | 389 |
} |
390 |
||
391 |
if (__isSmallInteger(__INST(baudRate))) { |
|
2818 | 392 |
__baudRate = __intVal(__INST(baudRate)); |
1593 | 393 |
} else if (__INST(baudRate) == nil) { |
2818 | 394 |
__setBaudRate = 0; |
1593 | 395 |
} else { |
2818 | 396 |
errorSymbol = @symbol(baudRate); |
397 |
goto getOutOfhere; |
|
1593 | 398 |
} |
399 |
||
400 |
if (__isSmallInteger(__INST(dataBits))) { |
|
2818 | 401 |
__dataBits = __intVal(__INST(dataBits)); |
1596 | 402 |
} else if (__INST(dataBits) == nil) { |
2818 | 403 |
__setDataBits = 0; |
1593 | 404 |
} else { |
2818 | 405 |
errorSymbol = @symbol(dataBits); |
406 |
goto getOutOfhere; |
|
1593 | 407 |
} |
408 |
||
409 |
if (__isSmallInteger(__INST(xOnChar))) { |
|
2818 | 410 |
__xOnChar = __intVal(__INST(xOnChar)); |
1593 | 411 |
} else if (__isCharacter(__INST(xOnChar))) { |
2998
f3b0c290b01b
stx macros begin with 2 underlines
Claus Gittinger <cg@exept.de>
parents:
2820
diff
changeset
|
412 |
__xOnChar = __intVal(__characterVal(__INST(xOnChar))); |
1593 | 413 |
} else if (__INST(xOnChar) == nil) { |
2818 | 414 |
__setXOnChar = 0; |
1593 | 415 |
} else { |
2818 | 416 |
errorSymbol = @symbol(xOnChar); |
417 |
goto getOutOfhere; |
|
1593 | 418 |
} |
419 |
||
420 |
if (__isSmallInteger(__INST(xOffChar))) { |
|
2818 | 421 |
__xOffChar = __intVal(__INST(xOffChar)); |
1593 | 422 |
} else if (__isCharacter(__INST(xOffChar))) { |
2818 | 423 |
__xOffChar = __intVal(__characterVal(__INST(xOffChar))); |
1593 | 424 |
} else if (__INST(xOffChar) == nil) { |
2818 | 425 |
__setXOffChar = 0; |
1593 | 426 |
} else { |
2818 | 427 |
errorSymbol = @symbol(xOffChar); |
428 |
goto getOutOfhere; |
|
1593 | 429 |
} |
430 |
||
431 |
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
|
432 |
__inFlowCtrl = FLG_FLOW_XONOFF; |
1593 | 433 |
} 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
|
434 |
__inFlowCtrl = FLG_FLOW_HARDWARE; |
1599 | 435 |
} 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
|
436 |
__inFlowCtrl = FLG_FLOW_NONE; |
1593 | 437 |
} else if (__INST(inFlowCtrlType) == nil) { |
2818 | 438 |
__setInFlowCtrl = 0; |
1593 | 439 |
} else { |
2818 | 440 |
errorSymbol = @symbol(inFlowCtrlType); |
441 |
goto getOutOfhere; |
|
1593 | 442 |
} |
443 |
||
444 |
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
|
445 |
__outFlowCtrl = FLG_FLOW_XONOFF; |
1593 | 446 |
} 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
|
447 |
__outFlowCtrl = FLG_FLOW_HARDWARE; |
1599 | 448 |
} 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
|
449 |
__outFlowCtrl = FLG_FLOW_NONE; |
1593 | 450 |
} else if (__INST(outFlowCtrlType) == nil) { |
2818 | 451 |
__setOutFlowCtrl = 0; |
1593 | 452 |
} else { |
2818 | 453 |
errorSymbol = @symbol(outFlowCtrlType); |
454 |
goto getOutOfhere; |
|
1593 | 455 |
} |
456 |
||
457 |
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
|
458 |
__stopBits = FLG_STOP_1; |
1593 | 459 |
} 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
|
460 |
__stopBits = FLG_STOP_2; |
1593 | 461 |
} 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
|
462 |
__stopBits = FLG_STOP_1_5; |
1593 | 463 |
} else if (__INST(stopBitsType) == nil) { |
2818 | 464 |
__setStopBits = 0; |
1593 | 465 |
} else { |
2818 | 466 |
errorSymbol = @symbol(stopBitsType); |
467 |
goto getOutOfhere; |
|
1596 | 468 |
} |
469 |
||
470 |
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
|
471 |
__parityType = FLG_PARITY_ODD; |
1596 | 472 |
} 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
|
473 |
__parityType = FLG_PARITY_EVEN; |
1596 | 474 |
} 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
|
475 |
__parityType = FLG_PARITY_NONE; |
1596 | 476 |
} else if (__INST(parityType) == nil) { |
2818 | 477 |
__setParityType = 0; |
1596 | 478 |
} else { |
2818 | 479 |
errorSymbol = @symbol(parityType); |
480 |
goto getOutOfhere; |
|
1593 | 481 |
} |
482 |
||
483 |
#ifdef WIN32 |
|
484 |
port = CreateFile(__portName, |
|
2818 | 485 |
GENERIC_READ | GENERIC_WRITE, |
486 |
0, /* comm devices must be opened with exclusive access */ |
|
487 |
NULL, /* no security attrs */ |
|
488 |
OPEN_EXISTING, /* comm devices must use OPEN_EXISTING */ |
|
489 |
0, /* no overlapped I/O */ |
|
490 |
NULL /* hTemplate must be NULL for comm devices */ |
|
491 |
); |
|
1593 | 492 |
|
493 |
if (port == INVALID_HANDLE_VALUE) { |
|
2818 | 494 |
console_fprintf(stderr, "Win32OS [info]: serial port open failed\n"); |
495 |
errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) ); |
|
496 |
errorSymbol = @symbol(openFailed); |
|
497 |
goto getOutOfhere; |
|
1593 | 498 |
} |
499 |
||
500 |
/* Flush the driver */ |
|
501 |
PurgeComm( port, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); |
|
502 |
||
503 |
/* Set driver buffer sizes */ |
|
504 |
SetupComm( port, 4096 /*SERIAL_IN_QUEUE_SIZE*/, 4096 /*SERIAL_OUT_QUEUE_SIZE*/); |
|
505 |
||
506 |
/* Reset timeout constants */ |
|
507 |
timeouts.ReadIntervalTimeout= 0xFFFFFFFF; |
|
508 |
timeouts.ReadTotalTimeoutMultiplier = 0; |
|
509 |
timeouts.ReadTotalTimeoutConstant = 0; |
|
510 |
timeouts.WriteTotalTimeoutMultiplier = 0; |
|
511 |
timeouts.WriteTotalTimeoutConstant = 0; |
|
512 |
SetCommTimeouts( port, &timeouts ); |
|
513 |
||
514 |
/* Set communication parameters */ |
|
515 |
ZeroMemory(&dcb, sizeof(dcb)); |
|
516 |
dcb.DCBlength = sizeof(dcb); |
|
517 |
GetCommState(port, &dcb); |
|
518 |
||
519 |
if (__setBaudRate) dcb.BaudRate = __baudRate; |
|
520 |
if (__setDataBits) dcb.ByteSize = __dataBits; |
|
521 |
if (__setXOnChar) dcb.XonChar = __xOnChar; |
|
522 |
if (__setXOffChar) dcb.XoffChar = __xOffChar; |
|
523 |
||
524 |
if (__setStopBits) { |
|
2818 | 525 |
/* set stop bits */ |
526 |
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
|
527 |
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
|
528 |
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
|
529 |
case FLG_STOP_2: dcb.StopBits = 2; break; /* 2 stop bits */ |
2818 | 530 |
default: |
531 |
errorSymbol = @symbol(stopBits); |
|
532 |
goto errExit; |
|
533 |
} |
|
1593 | 534 |
} |
535 |
||
536 |
if (__setParityType) { |
|
2818 | 537 |
/* set parity */ |
538 |
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
|
539 |
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
|
540 |
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
|
541 |
case FLG_PARITY_EVEN: dcb.Parity = EVENPARITY; break; |
2818 | 542 |
default: |
543 |
errorSymbol = @symbol(parityType); |
|
544 |
goto errExit; |
|
545 |
} |
|
1593 | 546 |
} |
547 |
||
548 |
if (__setInFlowCtrl) { |
|
2818 | 549 |
/* set control flow */ |
550 |
dcb.fInX = FALSE; |
|
551 |
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
|
552 |
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
|
553 |
if (__inFlowCtrl == FLG_FLOW_HARDWARE) dcb.fDtrControl = TRUE; /* hardware handshaking */ |
1593 | 554 |
} |
555 |
if (__setOutFlowCtrl) { |
|
2818 | 556 |
dcb.fOutX = FALSE; |
557 |
dcb.fOutxCtsFlow = FALSE; |
|
1593 | 558 |
|
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
|
559 |
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
|
560 |
if (__outFlowCtrl == FLG_FLOW_HARDWARE) dcb.fOutxCtsFlow = TRUE; /* hardware handshaking */ |
1593 | 561 |
} |
562 |
||
563 |
if (! SetCommState(port, &dcb)) { |
|
2818 | 564 |
console_fprintf(stderr, "Win32OS [info]: serial port comm-setup failed\n"); |
565 |
errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) ); |
|
566 |
goto errExit; |
|
1593 | 567 |
} |
568 |
# endif /* WIN32 */ |
|
569 |
||
570 |
/* |
|
571 |
* make it a FILE * |
|
572 |
*/ |
|
573 |
# ifdef WIN32 |
|
574 |
{ |
|
2818 | 575 |
int _fd; |
576 |
__stxWrapApiEnterCritical(); |
|
577 |
_fd = _open_osfhandle((long)port, 0); |
|
578 |
fp = fdopen(_fd, "r+"); |
|
579 |
__stxWrapApiLeaveCritical(); |
|
1593 | 580 |
} |
581 |
# else |
|
582 |
fp = fdopen(port, "r+"); |
|
583 |
# endif |
|
584 |
if (! fp) { |
|
2818 | 585 |
console_fprintf(stderr, "Win32OS [info]: fdopen failed\n"); |
586 |
errorNumber = __MKSMALLINT(errno); |
|
1596 | 587 |
errExit: ; |
2610 | 588 |
# ifdef WIN32 |
2818 | 589 |
CloseHandle(port); |
2610 | 590 |
# endif |
2818 | 591 |
goto getOutOfhere; |
1593 | 592 |
} |
593 |
||
594 |
{ |
|
2818 | 595 |
OBJ t; |
1593 | 596 |
|
2818 | 597 |
t = __MKEXTERNALADDRESS(fp); |
598 |
__INST(handle) = t; |
|
599 |
__STORE(self, t); |
|
1593 | 600 |
} |
601 |
||
1596 | 602 |
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
|
603 |
# 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
|
604 |
# 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
|
605 |
# 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
|
606 |
# 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
|
607 |
# 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
|
608 |
# 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
|
609 |
# 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
|
610 |
# 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
|
611 |
# undef FLG_PARITY_NONE |
1593 | 612 |
|
613 |
%}. |
|
614 |
"all ok?" |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
615 |
handle notNil ifTrue:[ |
2818 | 616 |
Lobby register:self. |
1593 | 617 |
] ifFalse:[ |
2818 | 618 |
errorNumber isNil ifTrue:[ |
619 |
self error:'invalid argument(s)'. |
|
620 |
] ifFalse:[ |
|
621 |
((OperatingSystem errorHolderForNumber:errorNumber) parameter:portName) reportError |
|
622 |
]. |
|
1593 | 623 |
]. |
1846 | 624 |
|
2610 | 625 |
"Modified: / 05-08-2011 / 14:14:08 / cg" |
1593 | 626 |
! ! |
627 |
||
1595 | 628 |
!SerialPort methodsFor:'printing & storing'! |
629 |
||
630 |
printOn:aStream |
|
1903
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
631 |
aStream nextPutAll:'SerialPort('. |
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
632 |
portName printOn:aStream. |
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
633 |
aStream nextPut:$/. |
1595 | 634 |
baudRate printOn:aStream. |
1903
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
635 |
aStream nextPut:$). |
1595 | 636 |
! ! |
637 |
||
638 |
!SerialPort methodsFor:'queries'! |
|
639 |
||
640 |
getName |
|
641 |
"return the name; here, we return the devices name" |
|
642 |
||
643 |
^ portName |
|
644 |
! ! |
|
645 |
||
1593 | 646 |
!SerialPort class methodsFor:'documentation'! |
647 |
||
648 |
version |
|
3153
eff2380e918e
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
2998
diff
changeset
|
649 |
^ '$Header: /cvs/stx/stx/libbasic2/SerialPort.st,v 1.23 2014-01-28 22:20:58 stefan Exp $' |
2419
8ea9b41e7b4b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
650 |
! |
8ea9b41e7b4b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
651 |
|
8ea9b41e7b4b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
652 |
version_CVS |
3153
eff2380e918e
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
2998
diff
changeset
|
653 |
^ '$Header: /cvs/stx/stx/libbasic2/SerialPort.st,v 1.23 2014-01-28 22:20:58 stefan Exp $' |
1593 | 654 |
! ! |
3153
eff2380e918e
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
2998
diff
changeset
|
655 |