author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jun 2019 14:28:51 +0200 | |
changeset 5050 | 44fa8672d102 |
parent 4058 | aa7a38bb1376 |
child 5446 | f802bbaa5b0a |
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 |
||
4057 | 349 |
!SerialPort methodsFor:'inspecting'! |
350 |
||
351 |
inspectorExtraMenuOperations |
|
352 |
"extra operation-menu entries to be shown in an inspector. |
|
353 |
Answers a collection of pairs contining aString and action aBlock. |
|
354 |
aString is the label of the menu item. |
|
355 |
aBlock is evaluated when the menu item is selected. |
|
356 |
To be redefined in objects which think that it makes sense to offer |
|
357 |
often used operations in an inspector. |
|
358 |
See SerialPort as an example." |
|
359 |
||
360 |
^ super inspectorExtraMenuOperations |
|
361 |
, { |
|
4058 | 362 |
{ 'Close SerialPort' . [self close] } |
4057 | 363 |
} |
364 |
! ! |
|
365 |
||
1595 | 366 |
!SerialPort methodsFor:'low level'! |
367 |
||
368 |
baudRate:newRate |
|
4056 | 369 |
"allows change of the baudrate while being open. |
370 |
Currently, only works for windows. |
|
371 |
Unix users should close, reconfigure and reopen." |
|
372 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
373 |
handle isNil ifTrue:[ |
4056 | 374 |
"not open" |
375 |
baudRate := newRate. |
|
376 |
^ self |
|
1598 | 377 |
]. |
1595 | 378 |
%{ |
379 |
OBJ fp; |
|
380 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
381 |
fp = __INST(handle); |
1598 | 382 |
if (__isSmallInteger(newRate)) { |
4056 | 383 |
SERIALPORT port; |
384 |
int ret; |
|
1595 | 385 |
|
4056 | 386 |
port = PORT_FROM_FILE_OBJECT(fp); |
3846 | 387 |
#ifdef __win32__ |
4056 | 388 |
{ |
389 |
DCB dcb; |
|
1595 | 390 |
|
4056 | 391 |
ZeroMemory(&dcb, sizeof(dcb)); |
392 |
dcb.DCBlength = sizeof(dcb); |
|
393 |
GetCommState(port, &dcb); |
|
1595 | 394 |
|
4056 | 395 |
dcb.BaudRate = __intVal(newRate); |
1595 | 396 |
|
4056 | 397 |
if (! SetCommState(port, &dcb)) { |
398 |
RETURN(false); |
|
399 |
} |
|
400 |
__INST(baudRate) = newRate; |
|
401 |
RETURN(true); |
|
402 |
} |
|
3846 | 403 |
# else /* ! __win32__ */ |
4056 | 404 |
/* add code for unix ioctl here ... */ |
3846 | 405 |
# endif /* __win32__ */ |
1595 | 406 |
} |
407 |
%}. |
|
408 |
self primitiveFailed. |
|
3220 | 409 |
! ! |
410 |
||
411 |
!SerialPort protectedMethodsFor:'low level'! |
|
1593 | 412 |
|
413 |
closeFile |
|
414 |
"low level close" |
|
415 |
||
416 |
%{ |
|
3219 | 417 |
OBJ t = __INST(handle); |
1593 | 418 |
if (t != nil) { |
3846 | 419 |
FILE *fp = __FILEVal(t); |
1593 | 420 |
|
3846 | 421 |
__INST(handle) = nil; |
1593 | 422 |
|
3846 | 423 |
#if defined(__win32__) && defined(xxxDO_WRAP_CALLS) |
424 |
{ |
|
425 |
int ret; |
|
1593 | 426 |
|
3846 | 427 |
do { |
428 |
__threadErrno = 0; |
|
429 |
ret = STX_C_CALL1("fclose", fclose, fp); |
|
430 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
|
431 |
} |
|
2818 | 432 |
#else |
3846 | 433 |
fclose(fp); |
2610 | 434 |
#endif |
1593 | 435 |
} |
436 |
%} |
|
2610 | 437 |
|
438 |
"Modified: / 05-08-2011 / 14:13:35 / cg" |
|
1593 | 439 |
! ! |
440 |
||
441 |
!SerialPort methodsFor:'opening'! |
|
442 |
||
443 |
open |
|
4056 | 444 |
"open the OS-port. |
445 |
Notice: all parameters (baudrate, stopbits, etc.) must have been set before" |
|
446 |
||
1596 | 447 |
|errorSymbol errorNumber| |
1593 | 448 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
449 |
handle notNil ifTrue:[ |
4056 | 450 |
^ self errorAlreadyOpen |
1593 | 451 |
]. |
452 |
%{ |
|
453 |
FILE *fp; |
|
454 |
SERIALPORT port; |
|
455 |
char *__portName; |
|
456 |
int __setBaudRate = 1, |
|
4056 | 457 |
__setDataBits = 1, |
458 |
__setXOnChar = 1, |
|
459 |
__setXOffChar = 1, |
|
460 |
__setInFlowCtrl = 1, |
|
461 |
__setOutFlowCtrl = 1, |
|
462 |
__setStopBits = 1, |
|
463 |
__setParityType = 1; |
|
1593 | 464 |
int __baudRate, __dataBits; |
465 |
int __xOnChar, __xOffChar; |
|
466 |
int __inFlowCtrl, __outFlowCtrl; |
|
467 |
int __stopBits, __parityType; |
|
3846 | 468 |
#ifdef __win32__ |
1593 | 469 |
COMMTIMEOUTS timeouts; |
470 |
DCB dcb; |
|
3846 | 471 |
#endif /* __win32__ */ |
1593 | 472 |
|
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 |
# 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
|
474 |
# 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
|
475 |
# 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
|
476 |
# 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
|
477 |
# 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
|
478 |
# 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
|
479 |
# 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
|
480 |
# 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
|
481 |
# define FLG_PARITY_NONE 3 |
1593 | 482 |
|
2751 | 483 |
if (__isStringLike(__INST(portName))) { |
4056 | 484 |
__portName = __stringVal(__INST(portName)); |
1593 | 485 |
} else { |
4056 | 486 |
errorSymbol = @symbol(portName); |
487 |
goto getOutOfhere; |
|
1593 | 488 |
} |
489 |
||
490 |
if (__isSmallInteger(__INST(baudRate))) { |
|
4056 | 491 |
__baudRate = __intVal(__INST(baudRate)); |
1593 | 492 |
} else if (__INST(baudRate) == nil) { |
4056 | 493 |
__setBaudRate = 0; |
1593 | 494 |
} else { |
4056 | 495 |
errorSymbol = @symbol(baudRate); |
496 |
goto getOutOfhere; |
|
1593 | 497 |
} |
498 |
||
499 |
if (__isSmallInteger(__INST(dataBits))) { |
|
4056 | 500 |
__dataBits = __intVal(__INST(dataBits)); |
1596 | 501 |
} else if (__INST(dataBits) == nil) { |
4056 | 502 |
__setDataBits = 0; |
1593 | 503 |
} else { |
4056 | 504 |
errorSymbol = @symbol(dataBits); |
505 |
goto getOutOfhere; |
|
1593 | 506 |
} |
507 |
||
508 |
if (__isSmallInteger(__INST(xOnChar))) { |
|
4056 | 509 |
__xOnChar = __intVal(__INST(xOnChar)); |
1593 | 510 |
} else if (__isCharacter(__INST(xOnChar))) { |
4056 | 511 |
__xOnChar = __intVal(__characterVal(__INST(xOnChar))); |
1593 | 512 |
} else if (__INST(xOnChar) == nil) { |
4056 | 513 |
__setXOnChar = 0; |
1593 | 514 |
} else { |
4056 | 515 |
errorSymbol = @symbol(xOnChar); |
516 |
goto getOutOfhere; |
|
1593 | 517 |
} |
518 |
||
519 |
if (__isSmallInteger(__INST(xOffChar))) { |
|
4056 | 520 |
__xOffChar = __intVal(__INST(xOffChar)); |
1593 | 521 |
} else if (__isCharacter(__INST(xOffChar))) { |
4056 | 522 |
__xOffChar = __intVal(__characterVal(__INST(xOffChar))); |
1593 | 523 |
} else if (__INST(xOffChar) == nil) { |
4056 | 524 |
__setXOffChar = 0; |
1593 | 525 |
} else { |
4056 | 526 |
errorSymbol = @symbol(xOffChar); |
527 |
goto getOutOfhere; |
|
1593 | 528 |
} |
529 |
||
530 |
if (__INST(inFlowCtrlType) == @symbol(xOnOff)) { |
|
4056 | 531 |
__inFlowCtrl = FLG_FLOW_XONOFF; |
1593 | 532 |
} else if (__INST(inFlowCtrlType) == @symbol(hardware)) { |
4056 | 533 |
__inFlowCtrl = FLG_FLOW_HARDWARE; |
1599 | 534 |
} else if (__INST(inFlowCtrlType) == @symbol(none)) { |
4056 | 535 |
__inFlowCtrl = FLG_FLOW_NONE; |
1593 | 536 |
} else if (__INST(inFlowCtrlType) == nil) { |
4056 | 537 |
__setInFlowCtrl = 0; |
1593 | 538 |
} else { |
4056 | 539 |
errorSymbol = @symbol(inFlowCtrlType); |
540 |
goto getOutOfhere; |
|
1593 | 541 |
} |
542 |
||
543 |
if (__INST(outFlowCtrlType) == @symbol(xOnOff)) { |
|
4056 | 544 |
__outFlowCtrl = FLG_FLOW_XONOFF; |
1593 | 545 |
} else if (__INST(outFlowCtrlType) == @symbol(hardware)) { |
4056 | 546 |
__outFlowCtrl = FLG_FLOW_HARDWARE; |
1599 | 547 |
} else if (__INST(outFlowCtrlType) == @symbol(none)) { |
4056 | 548 |
__outFlowCtrl = FLG_FLOW_NONE; |
1593 | 549 |
} else if (__INST(outFlowCtrlType) == nil) { |
4056 | 550 |
__setOutFlowCtrl = 0; |
1593 | 551 |
} else { |
4056 | 552 |
errorSymbol = @symbol(outFlowCtrlType); |
553 |
goto getOutOfhere; |
|
1593 | 554 |
} |
555 |
||
556 |
if (__INST(stopBitsType) == @symbol(stop1)) { |
|
4056 | 557 |
__stopBits = FLG_STOP_1; |
1593 | 558 |
} else if (__INST(stopBitsType) == @symbol(stop2)) { |
4056 | 559 |
__stopBits = FLG_STOP_2; |
1593 | 560 |
} else if (__INST(stopBitsType) == @symbol(stop1_5)) { |
4056 | 561 |
__stopBits = FLG_STOP_1_5; |
1593 | 562 |
} else if (__INST(stopBitsType) == nil) { |
4056 | 563 |
__setStopBits = 0; |
1593 | 564 |
} else { |
4056 | 565 |
errorSymbol = @symbol(stopBitsType); |
566 |
goto getOutOfhere; |
|
1596 | 567 |
} |
568 |
||
569 |
if (__INST(parityType) == @symbol(odd)) { |
|
4056 | 570 |
__parityType = FLG_PARITY_ODD; |
1596 | 571 |
} else if (__INST(parityType) == @symbol(even)) { |
4056 | 572 |
__parityType = FLG_PARITY_EVEN; |
1596 | 573 |
} else if (__INST(parityType) == @symbol(none)) { |
4056 | 574 |
__parityType = FLG_PARITY_NONE; |
1596 | 575 |
} else if (__INST(parityType) == nil) { |
4056 | 576 |
__setParityType = 0; |
1596 | 577 |
} else { |
4056 | 578 |
errorSymbol = @symbol(parityType); |
579 |
goto getOutOfhere; |
|
1593 | 580 |
} |
581 |
||
3846 | 582 |
#ifdef __win32__ |
1593 | 583 |
port = CreateFile(__portName, |
4056 | 584 |
GENERIC_READ | GENERIC_WRITE, |
585 |
0, /* comm devices must be opened with exclusive access */ |
|
586 |
NULL, /* no security attrs */ |
|
587 |
OPEN_EXISTING, /* comm devices must use OPEN_EXISTING */ |
|
588 |
0, /* no overlapped I/O */ |
|
589 |
NULL /* hTemplate must be NULL for comm devices */ |
|
590 |
); |
|
1593 | 591 |
|
592 |
if (port == INVALID_HANDLE_VALUE) { |
|
4056 | 593 |
console_fprintf(stderr, "Win32OS [info]: serial port open failed\n"); |
594 |
errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) ); |
|
595 |
errorSymbol = @symbol(openFailed); |
|
596 |
goto getOutOfhere; |
|
1593 | 597 |
} |
598 |
||
599 |
/* Flush the driver */ |
|
600 |
PurgeComm( port, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); |
|
601 |
||
602 |
/* Set driver buffer sizes */ |
|
603 |
SetupComm( port, 4096 /*SERIAL_IN_QUEUE_SIZE*/, 4096 /*SERIAL_OUT_QUEUE_SIZE*/); |
|
604 |
||
605 |
/* Reset timeout constants */ |
|
606 |
timeouts.ReadIntervalTimeout= 0xFFFFFFFF; |
|
607 |
timeouts.ReadTotalTimeoutMultiplier = 0; |
|
608 |
timeouts.ReadTotalTimeoutConstant = 0; |
|
609 |
timeouts.WriteTotalTimeoutMultiplier = 0; |
|
610 |
timeouts.WriteTotalTimeoutConstant = 0; |
|
611 |
SetCommTimeouts( port, &timeouts ); |
|
612 |
||
613 |
/* Set communication parameters */ |
|
614 |
ZeroMemory(&dcb, sizeof(dcb)); |
|
615 |
dcb.DCBlength = sizeof(dcb); |
|
616 |
GetCommState(port, &dcb); |
|
617 |
||
618 |
if (__setBaudRate) dcb.BaudRate = __baudRate; |
|
619 |
if (__setDataBits) dcb.ByteSize = __dataBits; |
|
620 |
if (__setXOnChar) dcb.XonChar = __xOnChar; |
|
621 |
if (__setXOffChar) dcb.XoffChar = __xOffChar; |
|
622 |
||
623 |
if (__setStopBits) { |
|
4056 | 624 |
/* set stop bits */ |
625 |
switch(__stopBits) { |
|
626 |
case FLG_STOP_1_5: dcb.StopBits = 1; break; /* 1.5 stop bits */ |
|
627 |
case FLG_STOP_1: dcb.StopBits = 0; break; /* 1 stop bit */ |
|
628 |
case FLG_STOP_2: dcb.StopBits = 2; break; /* 2 stop bits */ |
|
629 |
default: |
|
630 |
errorSymbol = @symbol(stopBits); |
|
631 |
goto errExit; |
|
632 |
} |
|
1593 | 633 |
} |
634 |
||
635 |
if (__setParityType) { |
|
4056 | 636 |
/* set parity */ |
637 |
switch(__parityType) { |
|
638 |
case FLG_PARITY_NONE: dcb.Parity = NOPARITY; break; |
|
639 |
case FLG_PARITY_ODD: dcb.Parity = ODDPARITY; break; |
|
640 |
case FLG_PARITY_EVEN: dcb.Parity = EVENPARITY; break; |
|
641 |
default: |
|
642 |
errorSymbol = @symbol(parityType); |
|
643 |
goto errExit; |
|
644 |
} |
|
1593 | 645 |
} |
646 |
||
647 |
if (__setInFlowCtrl) { |
|
4056 | 648 |
/* set control flow */ |
649 |
dcb.fInX = FALSE; |
|
650 |
dcb.fDtrControl = FALSE; |
|
651 |
if (__inFlowCtrl == FLG_FLOW_XONOFF) dcb.fInX = TRUE; /* XOn/XOff handshaking */ |
|
652 |
if (__inFlowCtrl == FLG_FLOW_HARDWARE) dcb.fDtrControl = TRUE; /* hardware handshaking */ |
|
1593 | 653 |
} |
654 |
if (__setOutFlowCtrl) { |
|
4056 | 655 |
dcb.fOutX = FALSE; |
656 |
dcb.fOutxCtsFlow = FALSE; |
|
1593 | 657 |
|
4056 | 658 |
if (__outFlowCtrl == FLG_FLOW_XONOFF) dcb.fOutX = TRUE; /* XOn/XOff handshaking */ |
659 |
if (__outFlowCtrl == FLG_FLOW_HARDWARE) dcb.fOutxCtsFlow = TRUE; /* hardware handshaking */ |
|
1593 | 660 |
} |
661 |
||
662 |
if (! SetCommState(port, &dcb)) { |
|
4056 | 663 |
console_fprintf(stderr, "Win32OS [info]: serial port comm-setup failed\n"); |
664 |
errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) ); |
|
665 |
goto errExit; |
|
1593 | 666 |
} |
667 |
||
668 |
/* |
|
669 |
* make it a FILE * |
|
670 |
*/ |
|
671 |
{ |
|
4056 | 672 |
int _fd; |
673 |
__stxWrapApiEnterCritical(); |
|
674 |
_fd = _open_osfhandle((long)port, 0); |
|
675 |
fp = fdopen(_fd, "r+"); |
|
676 |
__stxWrapApiLeaveCritical(); |
|
1593 | 677 |
} |
3846 | 678 |
# else /* ! __win32__ */ |
3220 | 679 |
fp = fopen(__portName, "r+"); |
680 |
/* add code for unix ioctl here ... */ |
|
3846 | 681 |
# endif /* !__win32__ */ |
3220 | 682 |
|
1593 | 683 |
if (! fp) { |
4056 | 684 |
console_fprintf(stderr, "SerialPort [info]: fdopen failed\n"); |
685 |
errorNumber = __MKSMALLINT(errno); |
|
3220 | 686 |
|
1596 | 687 |
errExit: ; |
3846 | 688 |
# ifdef __win32__ |
4056 | 689 |
CloseHandle(port); |
2610 | 690 |
# endif |
4056 | 691 |
goto getOutOfhere; |
1593 | 692 |
} |
693 |
||
694 |
{ |
|
4056 | 695 |
OBJ t = __MKEXTERNALADDRESS(fp); |
696 |
__INST(handle) = t; |
|
697 |
__STORE(self, t); |
|
1593 | 698 |
} |
699 |
||
1596 | 700 |
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
|
701 |
# 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
|
702 |
# 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
|
703 |
# 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
|
704 |
# 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
|
705 |
# 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
|
706 |
# 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
|
707 |
# 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
|
708 |
# 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
|
709 |
# undef FLG_PARITY_NONE |
1593 | 710 |
|
711 |
%}. |
|
712 |
"all ok?" |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2020
diff
changeset
|
713 |
handle notNil ifTrue:[ |
4056 | 714 |
handleType := #filePointer. |
715 |
self registerForFinalization. |
|
1593 | 716 |
] ifFalse:[ |
4056 | 717 |
self openError:(errorNumber ? 'invalid argument(s)'). |
1593 | 718 |
]. |
1846 | 719 |
|
2610 | 720 |
"Modified: / 05-08-2011 / 14:14:08 / cg" |
1593 | 721 |
! ! |
722 |
||
1595 | 723 |
!SerialPort methodsFor:'printing & storing'! |
724 |
||
725 |
printOn:aStream |
|
1903
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
726 |
aStream nextPutAll:'SerialPort('. |
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
727 |
portName printOn:aStream. |
3220 | 728 |
baudRate notNil ifTrue:[ |
3846 | 729 |
aStream nextPut:$/. |
730 |
baudRate printOn:aStream. |
|
3220 | 731 |
]. |
1903
d62268dffe6b
changed #printOn: print port name (COMx)
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
732 |
aStream nextPut:$). |
1595 | 733 |
! ! |
734 |
||
735 |
!SerialPort methodsFor:'queries'! |
|
736 |
||
4056 | 737 |
getBaudrate |
738 |
"return the baudRate" |
|
739 |
||
740 |
^ baudRate |
|
741 |
! |
|
742 |
||
743 |
getDataBits |
|
744 |
"return the nr. of dataBits" |
|
745 |
||
746 |
^ dataBits |
|
747 |
! |
|
748 |
||
749 |
getInFlowCtrlType |
|
750 |
"return the inFlowCtrlType" |
|
751 |
||
752 |
^ inFlowCtrlType |
|
753 |
! |
|
754 |
||
1595 | 755 |
getName |
756 |
"return the name; here, we return the devices name" |
|
757 |
||
758 |
^ portName |
|
4056 | 759 |
! |
760 |
||
761 |
getOutFlowCtrlType |
|
762 |
"return the outFlowCtrlType" |
|
763 |
||
764 |
^ outFlowCtrlType |
|
765 |
! |
|
766 |
||
767 |
getParityType |
|
768 |
"return the parityType" |
|
769 |
||
770 |
^ parityType |
|
771 |
! |
|
772 |
||
773 |
getStopBitsType |
|
774 |
"return the stopBitsType" |
|
775 |
||
776 |
^ stopBitsType |
|
777 |
! |
|
778 |
||
779 |
getXOffChar |
|
780 |
"return the xOffChar" |
|
781 |
||
782 |
^ xOffChar |
|
783 |
! |
|
784 |
||
785 |
getXOnChar |
|
786 |
"return the xOnChar" |
|
787 |
||
788 |
^ xOnChar |
|
1595 | 789 |
! ! |
790 |
||
1593 | 791 |
!SerialPort class methodsFor:'documentation'! |
792 |
||
793 |
version |
|
3621 | 794 |
^ '$Header$' |
2419
8ea9b41e7b4b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
795 |
! |
8ea9b41e7b4b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
796 |
|
8ea9b41e7b4b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
797 |
version_CVS |
3621 | 798 |
^ '$Header$' |
1593 | 799 |
! ! |
3153
eff2380e918e
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
2998
diff
changeset
|
800 |