author | Claus Gittinger <cg@exept.de> |
Wed, 31 Aug 2016 16:23:05 +0200 | |
changeset 4056 | de0810b2be05 |
parent 3846 | 7c9889bed912 |
child 4057 | 9df077908336 |
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 |
||
3846 | 29 |
#ifdef __win32__ |
1593 | 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 |
||
3846 | 48 |
#else /* not __win32__ */ |
1593 | 49 |
|
50 |
# define PORT_FROM_FD(fd) (fd) |
|
51 |
# define closePort(port) close(port) |
|
52 |
# define SERIALPORT int |
|
53 |
||
3846 | 54 |
#endif /* __win32__ */ |
1593 | 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 |
|
3846 | 87 |
#ifndef __win32__ |
1593 | 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 |
||
4056 | 121 |
Before opening, the port should be configured, unless the default settings |
122 |
are not correct. |
|
123 |
i.e. |
|
124 |
port := SerialPort new. |
|
125 |
port setBaudrate:19200 |
|
126 |
port open. |
|
127 |
||
1598 | 128 |
Arguments/Parameters/Instvars: |
129 |
||
4056 | 130 |
portName String device name |
131 |
WIN32: COM0, COM1, ... |
|
132 |
Unix: /dev/ttyS0, ... |
|
133 |
/dev/cua0, ... |
|
1598 | 134 |
|
4056 | 135 |
baudRate Integer 9600, 19200, ... |
136 |
stopBitsType Symbol #stop1, #stop1_5 or #stop2 |
|
137 |
parityType Symbol #odd, #even or #none |
|
138 |
dataBits Integer 5, 6, 7, 8 |
|
139 |
inFlowCtrlType Symbol #xOnOff, #hardware or #none |
|
140 |
outFlowCtrlType Symbol #xOnOff, #hardware or #none |
|
141 |
xOnChar Integer | Character |
|
142 |
xOffChar' Integer | Character |
|
1593 | 143 |
" |
144 |
! |
|
145 |
||
146 |
examples |
|
147 |
" |
|
148 |
example (get help info from an nntp server): |
|
1598 | 149 |
[exBegin] |
1595 | 150 |
|serialPort| |
151 |
||
152 |
serialPort := SerialPort new setPortName:(SerialPort defaultPortName). |
|
153 |
serialPort open. |
|
154 |
serialPort close. |
|
1598 | 155 |
[exEnd] |
1596 | 156 |
|
1598 | 157 |
[exBegin] |
1596 | 158 |
|serialPort| |
159 |
||
160 |
serialPort := SerialPort new setPortName:'COM5'. |
|
161 |
serialPort open. |
|
162 |
serialPort nextPutAll:'hello world'. |
|
163 |
serialPort cr. |
|
164 |
serialPort close. |
|
1598 | 165 |
[exEnd] |
1597 | 166 |
|
1598 | 167 |
[exBegin] |
1597 | 168 |
|serialPort| |
169 |
||
170 |
serialPort := SerialPort new setPortName:'COM5'. |
|
171 |
serialPort open. |
|
172 |
Transcript showCR:(serialPort nextAvailable:100). |
|
173 |
serialPort close. |
|
1598 | 174 |
[exEnd] |
1593 | 175 |
" |
176 |
! ! |
|
177 |
||
1595 | 178 |
!SerialPort class methodsFor:'instance creation'! |
1593 | 179 |
|
180 |
portName:portName |
|
181 |
baudRate:baudRateOrNil stopBitsType:stopBitsTypeOrNil |
|
182 |
parityType:parityTypeOrNil dataBits:dataBitsOrNil |
|
183 |
inFlowCtrlType:inFlowCtrlTypeOrNil outFlowCtrlType:outFlowCtrlTypeOrNil |
|
184 |
xOnChar:xOnCharOrNil xOffChar:xOffCharOrNil |
|
185 |
||
4056 | 186 |
"arguments are self describing; nil values mean: leave setting as is. |
187 |
||
188 |
baudrate is a number i.e. 9600, 19200... |
|
189 |
stopBitsType must be one of: #stop1, #stop1_5 or #stop2 |
|
190 |
parityType must be one of: #odd, #even or #none |
|
191 |
dataBits must be 5, 6, 7 or 8 |
|
192 |
inFlowCtrlType must be one of: #xOnOff, #hardware or #none (or nil) |
|
193 |
outFlowCtrlType must be one of: #xOnOff, #hardware or #none (or nil) |
|
194 |
xOnChar must be nil or a character or a small byte valued integer |
|
195 |
xOffChar must be nil or a character or a small byte valued integer |
|
196 |
" |
|
1593 | 197 |
|
1599 | 198 |
^ self new |
4056 | 199 |
setPortName:portName |
200 |
baudRate:baudRateOrNil stopBitsType:stopBitsTypeOrNil |
|
201 |
parityType:parityTypeOrNil dataBits:dataBitsOrNil |
|
202 |
inFlowCtrlType:inFlowCtrlTypeOrNil outFlowCtrlType:outFlowCtrlTypeOrNil |
|
203 |
xOnChar:xOnCharOrNil xOffChar:xOffCharOrNil |
|
1593 | 204 |
! ! |
205 |
||
206 |
!SerialPort class methodsFor:'debugging'! |
|
207 |
||
208 |
debug:aBoolean |
|
209 |
"turn on/off internal debugprints. |
|
210 |
This method is for ST/X debugging only and |
|
211 |
may be removed in later versions" |
|
212 |
||
213 |
%{ /* NOCONTEXT */ |
|
214 |
||
215 |
__debugging__ = (aBoolean == true); |
|
216 |
%} |
|
217 |
" |
|
218 |
SerialPort debug:true |
|
219 |
SerialPort debug:false |
|
220 |
" |
|
221 |
! ! |
|
222 |
||
1595 | 223 |
!SerialPort class methodsFor:'defaults'! |
224 |
||
225 |
defaultPortName |
|
226 |
DefaultPortName notNil ifTrue:[^ DefaultPortName ]. |
|
227 |
||
228 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
3846 | 229 |
^ 'COM0' |
1595 | 230 |
]. |
3220 | 231 |
^ '/dev/ttyS0' |
4056 | 232 |
! |
233 |
||
234 |
defaultPortName:aString |
|
235 |
DefaultPortName := aString |
|
1595 | 236 |
! ! |
237 |
||
1593 | 238 |
!SerialPort methodsFor:'accessing'! |
239 |
||
3220 | 240 |
pathName |
241 |
"answer the port name - compatibilty with FileStream" |
|
242 |
||
243 |
^ portName |
|
244 |
! |
|
245 |
||
1595 | 246 |
setBaudRate:baudRateOrNil |
4056 | 247 |
"baudrate is a number i.e. 9600, 19200.... |
248 |
Must be set before opening." |
|
249 |
||
1595 | 250 |
baudRate := baudRateOrNil. |
251 |
! |
|
252 |
||
1598 | 253 |
setDataBits:dataBitsOrNil |
4056 | 254 |
"dataBits must be 5, 6, 7 or 8. |
255 |
Must be set before opening." |
|
256 |
||
1598 | 257 |
dataBits := dataBitsOrNil. |
258 |
! |
|
259 |
||
4056 | 260 |
setInFlowCtrlType:inFlowCtrlTypeOrNil |
261 |
"inFlowCtrlType must be one of: #xOnOff, #hardware or #none (or nil) |
|
262 |
Must be set before opening." |
|
263 |
||
264 |
inFlowCtrlType := inFlowCtrlTypeOrNil. |
|
265 |
! |
|
266 |
||
267 |
setOutFlowCtrlType:outFlowCtrlTypeOrNil |
|
268 |
"outFlowCtrlType must be one of: #xOnOff, #hardware or #none (or nil) |
|
269 |
Must be set before opening." |
|
270 |
||
271 |
outFlowCtrlType := outFlowCtrlTypeOrNil. |
|
272 |
! |
|
273 |
||
1598 | 274 |
setParityType:parityTypeOrNil |
4056 | 275 |
"parityType must be one of: #odd, #even or #none. |
276 |
Must be set before opening." |
|
277 |
||
1598 | 278 |
parityType := parityTypeOrNil. |
279 |
! |
|
280 |
||
1595 | 281 |
setPortName:portNameArg |
4056 | 282 |
"Must be set before opening." |
283 |
||
2020
f47ac2710db3
Change port name COM* to \\.\COM* to support com ports > 9
ab
parents:
1903
diff
changeset
|
284 |
(portNameArg startsWith:'C') ifTrue:[ |
4056 | 285 |
"/ add special port name to support COM ports > 9 |
286 |
portName := '\\.\', portNameArg. |
|
287 |
^ self |
|
2020
f47ac2710db3
Change port name COM* to \\.\COM* to support com ports > 9
ab
parents:
1903
diff
changeset
|
288 |
]. |
1595 | 289 |
portName := portNameArg. |
290 |
! |
|
291 |
||
1593 | 292 |
setPortName:portNameArg |
4056 | 293 |
baudRate:baudRateOrNil stopBitsType:stopBitsTypeOrNil |
294 |
parityType:parityTypeOrNil dataBits:dataBitsOrNil |
|
295 |
inFlowCtrlType:inFlowCtrlTypeOrNil outFlowCtrlType:outFlowCtrlTypeOrNil |
|
296 |
xOnChar:xOnCharOrNil xOffChar:xOffCharOrNil |
|
1593 | 297 |
|
4056 | 298 |
"these must be set before opening. |
299 |
baudrate is a number i.e. 9600, 19200... |
|
300 |
stopBitsType must be one of: #stop1, #stop1_5 or #stop2 |
|
301 |
parityType must be one of: #odd, #even or #none |
|
302 |
dataBits must be 5, 6, 7 or 8 |
|
303 |
inFlowCtrlType must be one of: #xOnOff, #hardware or #none (or nil) |
|
304 |
outFlowCtrlType must be one of: #xOnOff, #hardware or #none (or nil) |
|
305 |
xOnChar must be nil or a character or a small byte valued integer |
|
306 |
xOffChar must be nil or a character or a small byte valued integer |
|
307 |
" |
|
308 |
||
2020
f47ac2710db3
Change port name COM* to \\.\COM* to support com ports > 9
ab
parents:
1903
diff
changeset
|
309 |
self setPortName:portNameArg. |
1593 | 310 |
baudRate := baudRateOrNil. |
311 |
stopBitsType := stopBitsTypeOrNil. |
|
312 |
parityType := parityTypeOrNil. |
|
313 |
dataBits := dataBitsOrNil. |
|
314 |
inFlowCtrlType := inFlowCtrlTypeOrNil. |
|
315 |
outFlowCtrlType := outFlowCtrlTypeOrNil. |
|
316 |
xOnChar := xOnCharOrNil. |
|
317 |
xOffChar := xOffCharOrNil. |
|
1846 | 318 |
! |
319 |
||
320 |
setStopBitsType:stopBitsTypeOrNil |
|
4056 | 321 |
"stopBitsType must be one of: #stop1, #stop1_5 or #stop2. |
322 |
Must be set before opening." |
|
323 |
||
1846 | 324 |
stopBitsType := stopBitsTypeOrNil. |
4056 | 325 |
! |
326 |
||
327 |
setXOffChar:xOffCharOrNil |
|
328 |
"xOffChar must be nil or a character or a small byte valued integer. |
|
329 |
Must be set before opening." |
|
330 |
||
331 |
xOnChar := xOffCharOrNil. |
|
332 |
! |
|
333 |
||
334 |
setXOnChar:xOnCharOrNil |
|
335 |
"xOnChar must be nil or a character or a small byte valued integer. |
|
336 |
Must be set before opening." |
|
337 |
||
338 |
xOnChar := xOnCharOrNil. |
|
1593 | 339 |
! ! |
340 |
||
3483 | 341 |
!SerialPort methodsFor:'initialization'! |
342 |
||
343 |
initialize |
|
344 |
super initialize. |
|
345 |
"/ transparent |
|
346 |
eolMode := nil. |
|
347 |
! ! |
|
348 |
||
1595 | 349 |
!SerialPort methodsFor:'low level'! |
350 |
||
351 |
baudRate:newRate |
|
4056 | 352 |
"allows change of the baudrate while being open. |
353 |
Currently, only works for windows. |
|
354 |
Unix users should close, reconfigure and reopen." |
|
355 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
356 |
handle isNil ifTrue:[ |
4056 | 357 |
"not open" |
358 |
baudRate := newRate. |
|
359 |
^ self |
|
1598 | 360 |
]. |
1595 | 361 |
%{ |
362 |
OBJ fp; |
|
363 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
364 |
fp = __INST(handle); |
1598 | 365 |
if (__isSmallInteger(newRate)) { |
4056 | 366 |
SERIALPORT port; |
367 |
int ret; |
|
1595 | 368 |
|
4056 | 369 |
port = PORT_FROM_FILE_OBJECT(fp); |
3846 | 370 |
#ifdef __win32__ |
4056 | 371 |
{ |
372 |
DCB dcb; |
|
1595 | 373 |
|
4056 | 374 |
ZeroMemory(&dcb, sizeof(dcb)); |
375 |
dcb.DCBlength = sizeof(dcb); |
|
376 |
GetCommState(port, &dcb); |
|
1595 | 377 |
|
4056 | 378 |
dcb.BaudRate = __intVal(newRate); |
1595 | 379 |
|
4056 | 380 |
if (! SetCommState(port, &dcb)) { |
381 |
RETURN(false); |
|
382 |
} |
|
383 |
__INST(baudRate) = newRate; |
|
384 |
RETURN(true); |
|
385 |
} |
|
3846 | 386 |
# else /* ! __win32__ */ |
4056 | 387 |
/* add code for unix ioctl here ... */ |
3846 | 388 |
# endif /* __win32__ */ |
1595 | 389 |
} |
390 |
%}. |
|
391 |
self primitiveFailed. |
|
3220 | 392 |
! ! |
393 |
||
394 |
!SerialPort protectedMethodsFor:'low level'! |
|
1593 | 395 |
|
396 |
closeFile |
|
397 |
"low level close" |
|
398 |
||
399 |
%{ |
|
3219 | 400 |
OBJ t = __INST(handle); |
1593 | 401 |
if (t != nil) { |
3846 | 402 |
FILE *fp = __FILEVal(t); |
1593 | 403 |
|
3846 | 404 |
__INST(handle) = nil; |
1593 | 405 |
|
3846 | 406 |
#if defined(__win32__) && defined(xxxDO_WRAP_CALLS) |
407 |
{ |
|
408 |
int ret; |
|
1593 | 409 |
|
3846 | 410 |
do { |
411 |
__threadErrno = 0; |
|
412 |
ret = STX_C_CALL1("fclose", fclose, fp); |
|
413 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
|
414 |
} |
|
2818 | 415 |
#else |
3846 | 416 |
fclose(fp); |
2610 | 417 |
#endif |
1593 | 418 |
} |
419 |
%} |
|
2610 | 420 |
|
421 |
"Modified: / 05-08-2011 / 14:13:35 / cg" |
|
1593 | 422 |
! ! |
423 |
||
424 |
!SerialPort methodsFor:'opening'! |
|
425 |
||
426 |
open |
|
4056 | 427 |
"open the OS-port. |
428 |
Notice: all parameters (baudrate, stopbits, etc.) must have been set before" |
|
429 |
||
1596 | 430 |
|errorSymbol errorNumber| |
1593 | 431 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
432 |
handle notNil ifTrue:[ |
4056 | 433 |
^ self errorAlreadyOpen |
1593 | 434 |
]. |
435 |
%{ |
|
436 |
FILE *fp; |
|
437 |
SERIALPORT port; |
|
438 |
char *__portName; |
|
439 |
int __setBaudRate = 1, |
|
4056 | 440 |
__setDataBits = 1, |
441 |
__setXOnChar = 1, |
|
442 |
__setXOffChar = 1, |
|
443 |
__setInFlowCtrl = 1, |
|
444 |
__setOutFlowCtrl = 1, |
|
445 |
__setStopBits = 1, |
|
446 |
__setParityType = 1; |
|
1593 | 447 |
int __baudRate, __dataBits; |
448 |
int __xOnChar, __xOffChar; |
|
449 |
int __inFlowCtrl, __outFlowCtrl; |
|
450 |
int __stopBits, __parityType; |
|
3846 | 451 |
#ifdef __win32__ |
1593 | 452 |
COMMTIMEOUTS timeouts; |
453 |
DCB dcb; |
|
3846 | 454 |
#endif /* __win32__ */ |
1593 | 455 |
|
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
|
456 |
# 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
|
457 |
# 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
|
458 |
# 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
|
459 |
# 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
|
460 |
# 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
|
461 |
# 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
|
462 |
# 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
|
463 |
# 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
|
464 |
# define FLG_PARITY_NONE 3 |
1593 | 465 |
|
2751 | 466 |
if (__isStringLike(__INST(portName))) { |
4056 | 467 |
__portName = __stringVal(__INST(portName)); |
1593 | 468 |
} else { |
4056 | 469 |
errorSymbol = @symbol(portName); |
470 |
goto getOutOfhere; |
|
1593 | 471 |
} |
472 |
||
473 |
if (__isSmallInteger(__INST(baudRate))) { |
|
4056 | 474 |
__baudRate = __intVal(__INST(baudRate)); |
1593 | 475 |
} else if (__INST(baudRate) == nil) { |
4056 | 476 |
__setBaudRate = 0; |
1593 | 477 |
} else { |
4056 | 478 |
errorSymbol = @symbol(baudRate); |
479 |
goto getOutOfhere; |
|
1593 | 480 |
} |
481 |
||
482 |
if (__isSmallInteger(__INST(dataBits))) { |
|
4056 | 483 |
__dataBits = __intVal(__INST(dataBits)); |
1596 | 484 |
} else if (__INST(dataBits) == nil) { |
4056 | 485 |
__setDataBits = 0; |
1593 | 486 |
} else { |
4056 | 487 |
errorSymbol = @symbol(dataBits); |
488 |
goto getOutOfhere; |
|
1593 | 489 |
} |
490 |
||
491 |
if (__isSmallInteger(__INST(xOnChar))) { |
|
4056 | 492 |
__xOnChar = __intVal(__INST(xOnChar)); |
1593 | 493 |
} else if (__isCharacter(__INST(xOnChar))) { |
4056 | 494 |
__xOnChar = __intVal(__characterVal(__INST(xOnChar))); |
1593 | 495 |
} else if (__INST(xOnChar) == nil) { |
4056 | 496 |
__setXOnChar = 0; |
1593 | 497 |
} else { |
4056 | 498 |
errorSymbol = @symbol(xOnChar); |
499 |
goto getOutOfhere; |
|
1593 | 500 |
} |
501 |
||
502 |
if (__isSmallInteger(__INST(xOffChar))) { |
|
4056 | 503 |
__xOffChar = __intVal(__INST(xOffChar)); |
1593 | 504 |
} else if (__isCharacter(__INST(xOffChar))) { |
4056 | 505 |
__xOffChar = __intVal(__characterVal(__INST(xOffChar))); |
1593 | 506 |
} else if (__INST(xOffChar) == nil) { |
4056 | 507 |
__setXOffChar = 0; |
1593 | 508 |
} else { |
4056 | 509 |
errorSymbol = @symbol(xOffChar); |
510 |
goto getOutOfhere; |
|
1593 | 511 |
} |
512 |
||
513 |
if (__INST(inFlowCtrlType) == @symbol(xOnOff)) { |
|
4056 | 514 |
__inFlowCtrl = FLG_FLOW_XONOFF; |
1593 | 515 |
} else if (__INST(inFlowCtrlType) == @symbol(hardware)) { |
4056 | 516 |
__inFlowCtrl = FLG_FLOW_HARDWARE; |
1599 | 517 |
} else if (__INST(inFlowCtrlType) == @symbol(none)) { |
4056 | 518 |
__inFlowCtrl = FLG_FLOW_NONE; |
1593 | 519 |
} else if (__INST(inFlowCtrlType) == nil) { |
4056 | 520 |
__setInFlowCtrl = 0; |
1593 | 521 |
} else { |
4056 | 522 |
errorSymbol = @symbol(inFlowCtrlType); |
523 |
goto getOutOfhere; |
|
1593 | 524 |
} |
525 |
||
526 |
if (__INST(outFlowCtrlType) == @symbol(xOnOff)) { |
|
4056 | 527 |
__outFlowCtrl = FLG_FLOW_XONOFF; |
1593 | 528 |
} else if (__INST(outFlowCtrlType) == @symbol(hardware)) { |
4056 | 529 |
__outFlowCtrl = FLG_FLOW_HARDWARE; |
1599 | 530 |
} else if (__INST(outFlowCtrlType) == @symbol(none)) { |
4056 | 531 |
__outFlowCtrl = FLG_FLOW_NONE; |
1593 | 532 |
} else if (__INST(outFlowCtrlType) == nil) { |
4056 | 533 |
__setOutFlowCtrl = 0; |
1593 | 534 |
} else { |
4056 | 535 |
errorSymbol = @symbol(outFlowCtrlType); |
536 |
goto getOutOfhere; |
|
1593 | 537 |
} |
538 |
||
539 |
if (__INST(stopBitsType) == @symbol(stop1)) { |
|
4056 | 540 |
__stopBits = FLG_STOP_1; |
1593 | 541 |
} else if (__INST(stopBitsType) == @symbol(stop2)) { |
4056 | 542 |
__stopBits = FLG_STOP_2; |
1593 | 543 |
} else if (__INST(stopBitsType) == @symbol(stop1_5)) { |
4056 | 544 |
__stopBits = FLG_STOP_1_5; |
1593 | 545 |
} else if (__INST(stopBitsType) == nil) { |
4056 | 546 |
__setStopBits = 0; |
1593 | 547 |
} else { |
4056 | 548 |
errorSymbol = @symbol(stopBitsType); |
549 |
goto getOutOfhere; |
|
1596 | 550 |
} |
551 |
||
552 |
if (__INST(parityType) == @symbol(odd)) { |
|
4056 | 553 |
__parityType = FLG_PARITY_ODD; |
1596 | 554 |
} else if (__INST(parityType) == @symbol(even)) { |
4056 | 555 |
__parityType = FLG_PARITY_EVEN; |
1596 | 556 |
} else if (__INST(parityType) == @symbol(none)) { |
4056 | 557 |
__parityType = FLG_PARITY_NONE; |
1596 | 558 |
} else if (__INST(parityType) == nil) { |
4056 | 559 |
__setParityType = 0; |
1596 | 560 |
} else { |
4056 | 561 |
errorSymbol = @symbol(parityType); |
562 |
goto getOutOfhere; |
|
1593 | 563 |
} |
564 |
||
3846 | 565 |
#ifdef __win32__ |
1593 | 566 |
port = CreateFile(__portName, |
4056 | 567 |
GENERIC_READ | GENERIC_WRITE, |
568 |
0, /* comm devices must be opened with exclusive access */ |
|
569 |
NULL, /* no security attrs */ |
|
570 |
OPEN_EXISTING, /* comm devices must use OPEN_EXISTING */ |
|
571 |
0, /* no overlapped I/O */ |
|
572 |
NULL /* hTemplate must be NULL for comm devices */ |
|
573 |
); |
|
1593 | 574 |
|
575 |
if (port == INVALID_HANDLE_VALUE) { |
|
4056 | 576 |
console_fprintf(stderr, "Win32OS [info]: serial port open failed\n"); |
577 |
errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) ); |
|
578 |
errorSymbol = @symbol(openFailed); |
|
579 |
goto getOutOfhere; |
|
1593 | 580 |
} |
581 |
||
582 |
/* Flush the driver */ |
|
583 |
PurgeComm( port, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); |
|
584 |
||
585 |
/* Set driver buffer sizes */ |
|
586 |
SetupComm( port, 4096 /*SERIAL_IN_QUEUE_SIZE*/, 4096 /*SERIAL_OUT_QUEUE_SIZE*/); |
|
587 |
||
588 |
/* Reset timeout constants */ |
|
589 |
timeouts.ReadIntervalTimeout= 0xFFFFFFFF; |
|
590 |
timeouts.ReadTotalTimeoutMultiplier = 0; |
|
591 |
timeouts.ReadTotalTimeoutConstant = 0; |
|
592 |
timeouts.WriteTotalTimeoutMultiplier = 0; |
|
593 |
timeouts.WriteTotalTimeoutConstant = 0; |
|
594 |
SetCommTimeouts( port, &timeouts ); |
|
595 |
||
596 |
/* Set communication parameters */ |
|
597 |
ZeroMemory(&dcb, sizeof(dcb)); |
|
598 |
dcb.DCBlength = sizeof(dcb); |
|
599 |
GetCommState(port, &dcb); |
|
600 |
||
601 |
if (__setBaudRate) dcb.BaudRate = __baudRate; |
|
602 |
if (__setDataBits) dcb.ByteSize = __dataBits; |
|
603 |
if (__setXOnChar) dcb.XonChar = __xOnChar; |
|
604 |
if (__setXOffChar) dcb.XoffChar = __xOffChar; |
|
605 |
||
606 |
if (__setStopBits) { |
|
4056 | 607 |
/* set stop bits */ |
608 |
switch(__stopBits) { |
|
609 |
case FLG_STOP_1_5: dcb.StopBits = 1; break; /* 1.5 stop bits */ |
|
610 |
case FLG_STOP_1: dcb.StopBits = 0; break; /* 1 stop bit */ |
|
611 |
case FLG_STOP_2: dcb.StopBits = 2; break; /* 2 stop bits */ |
|
612 |
default: |
|
613 |
errorSymbol = @symbol(stopBits); |
|
614 |
goto errExit; |
|
615 |
} |
|
1593 | 616 |
} |
617 |
||
618 |
if (__setParityType) { |
|
4056 | 619 |
/* set parity */ |
620 |
switch(__parityType) { |
|
621 |
case FLG_PARITY_NONE: dcb.Parity = NOPARITY; break; |
|
622 |
case FLG_PARITY_ODD: dcb.Parity = ODDPARITY; break; |
|
623 |
case FLG_PARITY_EVEN: dcb.Parity = EVENPARITY; break; |
|
624 |
default: |
|
625 |
errorSymbol = @symbol(parityType); |
|
626 |
goto errExit; |
|
627 |
} |
|
1593 | 628 |
} |
629 |
||
630 |
if (__setInFlowCtrl) { |
|
4056 | 631 |
/* set control flow */ |
632 |
dcb.fInX = FALSE; |
|
633 |
dcb.fDtrControl = FALSE; |
|
634 |
if (__inFlowCtrl == FLG_FLOW_XONOFF) dcb.fInX = TRUE; /* XOn/XOff handshaking */ |
|
635 |
if (__inFlowCtrl == FLG_FLOW_HARDWARE) dcb.fDtrControl = TRUE; /* hardware handshaking */ |
|
1593 | 636 |
} |
637 |
if (__setOutFlowCtrl) { |
|
4056 | 638 |
dcb.fOutX = FALSE; |
639 |
dcb.fOutxCtsFlow = FALSE; |
|
1593 | 640 |
|
4056 | 641 |
if (__outFlowCtrl == FLG_FLOW_XONOFF) dcb.fOutX = TRUE; /* XOn/XOff handshaking */ |
642 |
if (__outFlowCtrl == FLG_FLOW_HARDWARE) dcb.fOutxCtsFlow = TRUE; /* hardware handshaking */ |
|
1593 | 643 |
} |
644 |
||
645 |
if (! SetCommState(port, &dcb)) { |
|
4056 | 646 |
console_fprintf(stderr, "Win32OS [info]: serial port comm-setup failed\n"); |
647 |
errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) ); |
|
648 |
goto errExit; |
|
1593 | 649 |
} |
650 |
||
651 |
/* |
|
652 |
* make it a FILE * |
|
653 |
*/ |
|
654 |
{ |
|
4056 | 655 |
int _fd; |
656 |
__stxWrapApiEnterCritical(); |
|
657 |
_fd = _open_osfhandle((long)port, 0); |
|
658 |
fp = fdopen(_fd, "r+"); |
|
659 |
__stxWrapApiLeaveCritical(); |
|
1593 | 660 |
} |
3846 | 661 |
# else /* ! __win32__ */ |
3220 | 662 |
fp = fopen(__portName, "r+"); |
663 |
/* add code for unix ioctl here ... */ |
|
3846 | 664 |
# endif /* !__win32__ */ |
3220 | 665 |
|
1593 | 666 |
if (! fp) { |
4056 | 667 |
console_fprintf(stderr, "SerialPort [info]: fdopen failed\n"); |
668 |
errorNumber = __MKSMALLINT(errno); |
|
3220 | 669 |
|
1596 | 670 |
errExit: ; |
3846 | 671 |
# ifdef __win32__ |
4056 | 672 |
CloseHandle(port); |
2610 | 673 |
# endif |
4056 | 674 |
goto getOutOfhere; |
1593 | 675 |
} |
676 |
||
677 |
{ |
|
4056 | 678 |
OBJ t = __MKEXTERNALADDRESS(fp); |
679 |
__INST(handle) = t; |
|
680 |
__STORE(self, t); |
|
1593 | 681 |
} |
682 |
||
1596 | 683 |
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
|
684 |
# 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
|
685 |
# 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
|
686 |
# 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
|
687 |
# 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
|
688 |
# 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
|
689 |
# 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
|
690 |
# 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
|
691 |
# 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
|
692 |
# undef FLG_PARITY_NONE |
1593 | 693 |
|
694 |
%}. |
|
695 |
"all ok?" |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
696 |
handle notNil ifTrue:[ |
4056 | 697 |
handleType := #filePointer. |
698 |
self registerForFinalization. |
|
1593 | 699 |
] ifFalse:[ |
4056 | 700 |
self openError:(errorNumber ? 'invalid argument(s)'). |
1593 | 701 |
]. |
1846 | 702 |
|
2610 | 703 |
"Modified: / 05-08-2011 / 14:14:08 / cg" |
1593 | 704 |
! ! |
705 |
||
1595 | 706 |
!SerialPort methodsFor:'printing & storing'! |
707 |
||
708 |
printOn:aStream |
|
1903
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
709 |
aStream nextPutAll:'SerialPort('. |
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
710 |
portName printOn:aStream. |
3220 | 711 |
baudRate notNil ifTrue:[ |
3846 | 712 |
aStream nextPut:$/. |
713 |
baudRate printOn:aStream. |
|
3220 | 714 |
]. |
1903
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
715 |
aStream nextPut:$). |
1595 | 716 |
! ! |
717 |
||
718 |
!SerialPort methodsFor:'queries'! |
|
719 |
||
4056 | 720 |
getBaudrate |
721 |
"return the baudRate" |
|
722 |
||
723 |
^ baudRate |
|
724 |
! |
|
725 |
||
726 |
getDataBits |
|
727 |
"return the nr. of dataBits" |
|
728 |
||
729 |
^ dataBits |
|
730 |
! |
|
731 |
||
732 |
getInFlowCtrlType |
|
733 |
"return the inFlowCtrlType" |
|
734 |
||
735 |
^ inFlowCtrlType |
|
736 |
! |
|
737 |
||
1595 | 738 |
getName |
739 |
"return the name; here, we return the devices name" |
|
740 |
||
741 |
^ portName |
|
4056 | 742 |
! |
743 |
||
744 |
getOutFlowCtrlType |
|
745 |
"return the outFlowCtrlType" |
|
746 |
||
747 |
^ outFlowCtrlType |
|
748 |
! |
|
749 |
||
750 |
getParityType |
|
751 |
"return the parityType" |
|
752 |
||
753 |
^ parityType |
|
754 |
! |
|
755 |
||
756 |
getStopBitsType |
|
757 |
"return the stopBitsType" |
|
758 |
||
759 |
^ stopBitsType |
|
760 |
! |
|
761 |
||
762 |
getXOffChar |
|
763 |
"return the xOffChar" |
|
764 |
||
765 |
^ xOffChar |
|
766 |
! |
|
767 |
||
768 |
getXOnChar |
|
769 |
"return the xOnChar" |
|
770 |
||
771 |
^ xOnChar |
|
1595 | 772 |
! ! |
773 |
||
1593 | 774 |
!SerialPort class methodsFor:'documentation'! |
775 |
||
776 |
version |
|
3621 | 777 |
^ '$Header$' |
2419
8ea9b41e7b4b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
778 |
! |
8ea9b41e7b4b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
779 |
|
8ea9b41e7b4b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
780 |
version_CVS |
3621 | 781 |
^ '$Header$' |
1593 | 782 |
! ! |
3153
eff2380e918e
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
2998
diff
changeset
|
783 |