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