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