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