Debian Codegraph @

src/hardware/serialport/libserial.cpp

raw
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: libserial.cpp,v 1.4 2009-11-02 09:51:02 h-a-l-9000 Exp $ */
20
21 #include "libserial.h"
22
23 #include "config.h"
24
25 #ifdef WIN32
26
27 #include <windows.h>
28 #include <stdio.h>
29
30 struct _COMPORT {
31 HANDLE porthandle;
32 bool breakstatus;
33 DCB orig_dcb;
34 };
35
36 bool SERIAL_open(const char* portname, COMPORT* port) {
37 // allocate COMPORT structure
38 COMPORT cp = (_COMPORT*)malloc(sizeof(_COMPORT));
39 if(cp == NULL) return false;
40
41 cp->breakstatus=false;
42
43 // open the port in NT object space (recommended by Microsoft)
44 // allows the user to open COM10+ and custom port names.
45 size_t len = strlen(portname);
46 if(len > 240) {
47 SetLastError(ERROR_BUFFER_OVERFLOW);
48 return false;
49 }
50 char extended_portname[256] = "\\\\.\\";
51 memcpy(extended_portname+4,portname,len+1);
52
53 cp->porthandle = CreateFile (extended_portname,
54 GENERIC_READ | GENERIC_WRITE, 0,
55 // must be opened with exclusive-access
56 NULL, // no security attributes
57 OPEN_EXISTING, // must use OPEN_EXISTING
58 0, // non overlapped I/O
59 NULL // hTemplate must be NULL for comm devices
60 );
61
62 if (cp->porthandle == INVALID_HANDLE_VALUE) goto cleanup_error;
63
64 cp->orig_dcb.DCBlength=sizeof(DCB);
65
66 if(!GetCommState(cp->porthandle, &cp->orig_dcb)) {
67 goto cleanup_error;
68 }
69
70 // configure the port for polling
71 DCB newdcb;
72 memcpy(&newdcb,&cp->orig_dcb,sizeof(DCB));
73
74 newdcb.fBinary=true;
75 newdcb.fParity=true;
76 newdcb.fOutxCtsFlow=false;
77 newdcb.fOutxDsrFlow=false;
78 newdcb.fDtrControl=DTR_CONTROL_DISABLE;
79 newdcb.fDsrSensitivity=false;
80
81 newdcb.fOutX=false;
82 newdcb.fInX=false;
83 newdcb.fErrorChar=0;
84 newdcb.fNull=false;
85 newdcb.fRtsControl=RTS_CONTROL_DISABLE;
86 newdcb.fAbortOnError=false;
87
88 if(!SetCommState(cp->porthandle, &newdcb)) {
89 goto cleanup_error;
90 }
91
92 // Configure timeouts to effectively use polling
93 COMMTIMEOUTS ct;
94 ct.ReadIntervalTimeout = MAXDWORD;
95 ct.ReadTotalTimeoutConstant = 0;
96 ct.ReadTotalTimeoutMultiplier = 0;
97 ct.WriteTotalTimeoutConstant = 0;
98 ct.WriteTotalTimeoutMultiplier = 0;
99 if(!SetCommTimeouts(cp->porthandle, &ct)) {
100 goto cleanup_error;
101 }
102 if(!ClearCommBreak(cp->porthandle)) {
103 // Bluetooth Bluesoleil seems to not implement it
104 //goto cleanup_error;
105 }
106 DWORD errors;
107 if(!ClearCommError(cp->porthandle, &errors, NULL)) {
108 goto cleanup_error;
109 }
110 *port = cp;
111 return true;
112
113 cleanup_error:
114 if (cp->porthandle != INVALID_HANDLE_VALUE) CloseHandle(cp->porthandle);
115 free(cp);
116 return false;
117 }
118
119 void SERIAL_close(COMPORT port) {
120 // restore original DCB, close handle, free the COMPORT struct
121 if (port->porthandle != INVALID_HANDLE_VALUE) {
122 SetCommState(port->porthandle, &port->orig_dcb);
123 CloseHandle(port->porthandle);
124 }
125 free(port);
126 }
127
128 void SERIAL_getErrorString(char* buffer, size_t length) {
129 int error = GetLastError();
130 if(length < 50) return;
131 memset(buffer,0,length);
132 // get the error message text from the operating system
133 LPVOID sysmessagebuffer;
134 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
135 NULL,
136 error,
137 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
138 (LPTSTR) &sysmessagebuffer,
139 0,NULL);
140
141 const char* err5text = "The specified port is already in use.\n";
142 const char* err2text = "The specified port does not exist.\n";
143
144 size_t sysmsg_offset = 0;
145
146 if(error == 5) {
147 sysmsg_offset = strlen(err5text);
148 memcpy(buffer,err5text,sysmsg_offset);
149
150 } else if(error == 2) {
151 sysmsg_offset = strlen(err2text);
152 memcpy(buffer,err2text,sysmsg_offset);
153 }
154
155 if((length - sysmsg_offset - strlen((const char*)sysmessagebuffer)) >= 0)
156 memcpy(buffer + sysmsg_offset, sysmessagebuffer,
157 strlen((const char*)sysmessagebuffer));
158
159 LocalFree(sysmessagebuffer);
160 }
161
162
163 void SERIAL_setDTR(COMPORT port, bool value) {
164 EscapeCommFunction(port->porthandle, value ? SETDTR:CLRDTR);
165 }
166
167 void SERIAL_setRTS(COMPORT port, bool value) {
168 EscapeCommFunction(port->porthandle, value ? SETRTS:CLRRTS);
169 }
170
171 void SERIAL_setBREAK(COMPORT port, bool value) {
172 EscapeCommFunction(port->porthandle, value ? SETBREAK:CLRBREAK);
173 port->breakstatus = value;
174 }
175
176 int SERIAL_getmodemstatus(COMPORT port) {
177 DWORD retval = 0;
178 GetCommModemStatus (port->porthandle, &retval);
179 return (int)retval;
180 }
181
182 bool SERIAL_sendchar(COMPORT port, char data) {
183 DWORD bytesWritten;
184
185 // mean bug: with break = 1, WriteFile will never return.
186 if(port->breakstatus) return true; // true or false?!
187
188 WriteFile (port->porthandle, &data, 1, &bytesWritten, NULL);
189 if(bytesWritten==1) return true;
190 else return false;
191 }
192
193 // 0-7 char data, higher=flags
194 int SERIAL_getextchar(COMPORT port) {
195 DWORD errors = 0; // errors from API
196 DWORD dwRead = 0; // Number of chars read
197 char chRead;
198
199 int retval = 0;
200 // receive a byte; TODO communicate faliure
201 if (ReadFile (port->porthandle, &chRead, 1, &dwRead, NULL)) {
202 if (dwRead) {
203 // check for errors
204 ClearCommError(port->porthandle, &errors, NULL);
205 // mask bits are identical
206 errors &= CE_BREAK|CE_FRAME|CE_RXPARITY|CE_OVERRUN;
207 retval |= (errors<<8);
208 retval |= (chRead & 0xff);
209 retval |= 0x10000;
210 }
211 }
212 return retval;
213 }
214
215 bool SERIAL_setCommParameters(COMPORT port,
216 int baudrate, char parity, int stopbits, int length) {
217
218 DCB dcb;
219 dcb.DCBlength=sizeof(dcb);
220 GetCommState(port->porthandle,&dcb);
221
222 // parity
223 switch (parity) {
224 case 'n': dcb.Parity = NOPARITY; break;
225 case 'o': dcb.Parity = ODDPARITY; break;
226 case 'e': dcb.Parity = EVENPARITY; break;
227 case 'm': dcb.Parity = MARKPARITY; break;
228 case 's': dcb.Parity = SPACEPARITY; break;
229 default:
230 SetLastError(ERROR_INVALID_PARAMETER);
231 return false;
232 }
233
234 // stopbits
235 switch(stopbits) {
236 case SERIAL_1STOP: dcb.StopBits = ONESTOPBIT; break;
237 case SERIAL_2STOP: dcb.StopBits = TWOSTOPBITS; break;
238 case SERIAL_15STOP: dcb.StopBits = ONE5STOPBITS; break;
239 default:
240 SetLastError(ERROR_INVALID_PARAMETER);
241 return false;
242 }
243
244 // byte length
245 if(length > 8 || length < 5) {
246 SetLastError(ERROR_INVALID_PARAMETER);
247 return false;
248 }
249 dcb.ByteSize = length;
250 dcb.BaudRate = baudrate;
251
252 if (!SetCommState (port->porthandle, &dcb)) return false;
253 return true;
254 }
255 #endif
256
257 #if defined (LINUX) || defined (MACOSX) || defined (BSD)
258
259 #include <string.h> // strlen
260 #include <stdlib.h>
261
262 #include <termios.h>
263 #include <unistd.h>
264
265 #include <sys/types.h>
266 #include <sys/stat.h>
267 #include <sys/ioctl.h>
268
269 #include <errno.h>
270 #include <fcntl.h>
271 #include <stdio.h> // sprinf
272
273 struct _COMPORT {
274 int porthandle;
275 bool breakstatus;
276 termios backup;
277 };
278
279 bool SERIAL_open(const char* portname, COMPORT* port) {
280 int result;
281 // allocate COMPORT structure
282 COMPORT cp = (_COMPORT*)malloc(sizeof(_COMPORT));
283 if(cp == NULL) return false;
284
285 cp->breakstatus=false;
286
287 size_t len = strlen(portname);
288 if(len > 240) {
289 ///////////////////////////////////SetLastError(ERROR_BUFFER_OVERFLOW);
290 return false;
291 }
292 char extended_portname[256] = "/dev/";
293 memcpy(extended_portname+5,portname,len);
294
295 cp->porthandle = open (extended_portname, O_RDWR | O_NOCTTY | O_NONBLOCK);
296 if (cp->porthandle < 0) goto cleanup_error;
297
298 result = tcgetattr(cp->porthandle,&cp->backup);
299 if (result == -1) goto cleanup_error;
300
301 // get port settings
302 termios termInfo;
303 memcpy(&termInfo,&cp->backup,sizeof(termios));
304
305 // initialize the port
306 termInfo.c_cflag = CS8 | CREAD | CLOCAL; // noparity, 1 stopbit
307 termInfo.c_iflag = PARMRK | INPCK;
308 termInfo.c_oflag = 0;
309 termInfo.c_lflag = 0;
310 termInfo.c_cc[VMIN] = 0;
311 termInfo.c_cc[VTIME] = 0;
312
313 tcflush (cp->porthandle, TCIFLUSH);
314 tcsetattr (cp->porthandle, TCSANOW, &termInfo);
315
316 *port = cp;
317 return true;
318
319 cleanup_error:
320 if (cp->porthandle != 0) close(cp->porthandle);
321 free(cp);
322 return false;
323 }
324
325 void SERIAL_close(COMPORT port) {
326 // restore original termios, close handle, free the COMPORT struct
327 if (port->porthandle >= 0) {
328 tcsetattr(port->porthandle, TCSANOW, &port->backup);
329 close(port->porthandle);
330 }
331 free(port);
332 }
333
334 void SERIAL_getErrorString(char* buffer, size_t length) {
335 int error = errno;
336 if(length < 50) return;
337 memset(buffer,0,length);
338 // get the error message text from the operating system
339 // TODO (or not)
340
341 const char* err5text = "The specified port is already in use.\n";
342 const char* err2text = "The specified port does not exist.\n";
343
344 size_t sysmsg_offset = 0;
345
346 if(error == EBUSY) {
347 sysmsg_offset = strlen(err5text);
348 memcpy(buffer,err5text,sysmsg_offset);
349
350 } else if(error == 2) {
351 sysmsg_offset = strlen(err2text);
352 memcpy(buffer,err2text,sysmsg_offset);
353 }
354
355 sprintf(buffer + sysmsg_offset, "System error %d.",error);
356
357 }
358
359 int SERIAL_getmodemstatus(COMPORT port) {
360 long flags = 0;
361 ioctl (port->porthandle, TIOCMGET, &flags);
362 int retval = 0;
363 if (flags & TIOCM_CTS) retval |= SERIAL_CTS;
364 if (flags & TIOCM_DSR) retval |= SERIAL_DSR;
365 if (flags & TIOCM_RI) retval |= SERIAL_RI;
366 if (flags & TIOCM_CD) retval |= SERIAL_CD;
367 return retval;
368 }
369
370 bool SERIAL_sendchar(COMPORT port, char data) {
371 if(port->breakstatus) return true; // true or false?!; Does POSIX need this check?
372 int bytesWritten = write(port->porthandle, &data, 1);
373 if(bytesWritten==1) return true;
374 else return false;
375 }
376
377 int SERIAL_getextchar(COMPORT port) {
378 unsigned char chRead = 0;
379 int dwRead = 0;
380 unsigned char error = 0;
381 int retval = 0;
382
383 dwRead=read(port->porthandle,&chRead,1);
384 if (dwRead==1) {
385 if(chRead==0xff) // error escape
386 {
387 dwRead=read(port->porthandle,&chRead,1);
388 if(chRead==0x00) // an error
389 {
390 dwRead=read(port->porthandle,&chRead,1);
391 if(chRead==0x0) error=SERIAL_BREAK_ERR;
392 else error=SERIAL_FRAMING_ERR;
393 }
394 }
395 retval |= (error<<8);
396 retval |= chRead;
397 retval |= 0x10000;
398 }
399 return retval;
400 }
401
402 bool SERIAL_setCommParameters(COMPORT port,
403 int baudrate, char parity, int stopbits, int length) {
404
405 termios termInfo;
406 int result = tcgetattr(port->porthandle, &termInfo);
407 if (result==-1) return false;
408 termInfo.c_cflag = CREAD | CLOCAL;
409
410 // parity
411 // "works on many systems"
412 #define CMSPAR 010000000000
413 switch (parity) {
414 case 'n': break;
415 case 'o': termInfo.c_cflag |= (PARODD | PARENB); break;
416 case 'e': termInfo.c_cflag |= PARENB; break;
417 case 'm': termInfo.c_cflag |= (PARENB | CMSPAR | PARODD); break;
418 case 's': termInfo.c_cflag |= (PARENB | CMSPAR); break;
419 default:
420 return false;
421 }
422 // stopbits
423 switch(stopbits) {
424 case SERIAL_1STOP: break;
425 case SERIAL_2STOP:
426 case SERIAL_15STOP: termInfo.c_cflag |= CSTOPB; break;
427 default:
428 return false;
429 }
430 // byte length
431 if(length > 8 || length < 5) return false;
432 switch (length) {
433 case 5: termInfo.c_cflag |= CS5; break;
434 case 6: termInfo.c_cflag |= CS6; break;
435 case 7: termInfo.c_cflag |= CS7; break;
436 case 8: termInfo.c_cflag |= CS8; break;
437 }
438 // baudrate
439 int posix_baudrate=0;
440 switch(baudrate) {
441 case 115200: posix_baudrate = B115200; break;
442 case 57600: posix_baudrate = B57600; break;
443 case 38400: posix_baudrate = B38400; break;
444 case 19200: posix_baudrate = B19200; break;
445 case 9600: posix_baudrate = B9600; break;
446 case 4800: posix_baudrate = B4800; break;
447 case 2400: posix_baudrate = B2400; break;
448 case 1200: posix_baudrate = B1200; break;
449 case 600: posix_baudrate = B600; break;
450 case 300: posix_baudrate = B300; break;
451 case 110: posix_baudrate = B110; break;
452 default: return false;
453 }
454 cfsetospeed (&termInfo, posix_baudrate);
455 cfsetispeed (&termInfo, posix_baudrate);
456
457 int retval = tcsetattr(port->porthandle, TCSANOW, &termInfo);
458 if(retval==-1) return false;
459 return true;
460 }
461
462 void SERIAL_setBREAK(COMPORT port, bool value) {
463 ioctl(port->porthandle, value?TIOCSBRK:TIOCCBRK);
464 }
465
466 void SERIAL_setDTR(COMPORT port, bool value) {
467 long flag = TIOCM_DTR;
468 ioctl(port->porthandle, value?TIOCMBIS:TIOCMBIC, &flag);
469 }
470
471 void SERIAL_setRTS(COMPORT port, bool value) {
472 long flag = TIOCM_RTS;
473 ioctl(port->porthandle, value?TIOCMBIS:TIOCMBIC, &flag);
474 }
475 #endif
476
477 #ifdef OS2
478 // OS/2 related headers
479 #define INCL_DOSFILEMGR
480 #define INCL_DOSERRORS
481 #define INCL_DOSDEVICES
482 #define INCL_DOSDEVIOCTL
483 #define INCL_DOSPROCESS
484 #include <os2.h>
485 #include <malloc.h>
486 #include <string.h>
487 #include <stdio.h>
488
489 struct _COMPORT {
490 HFILE porthandle;
491 DCBINFO orig_dcb;
492 };
493 // TODO: THIS IS INCOMPLETE and UNTESTED.
494
495 bool SERIAL_open(const char* portname, COMPORT* port) {
496 // allocate COMPORT structure
497 COMPORT cp = (_COMPORT*)malloc(sizeof(_COMPORT));
498 if(cp == NULL) return false;
499 cp->porthandle=0;
500
501 USHORT errors = 0;
502 ULONG ulAction = 0;
503 ULONG ulParmLen = sizeof(DCBINFO);
504 APIRET rc = DosOpen((PSZ)portname, &cp->porthandle,
505 &ulAction, 0L, FILE_NORMAL, FILE_OPEN,
506 OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE | OPEN_FLAGS_SEQUENTIAL, 0L);
507 if (rc != NO_ERROR) {
508 goto cleanup_error;
509 }
510
511 rc = DosDevIOCtl(cp->porthandle, IOCTL_ASYNC, ASYNC_GETDCBINFO,
512 0, 0, 0, &cp->orig_dcb, ulParmLen, &ulParmLen);
513 if ( rc != NO_ERROR) {
514 goto cleanup_error;
515 }
516 // configure the port for polling
517 DCBINFO newdcb;
518 memcpy(&newdcb,&cp->orig_dcb,sizeof(DCBINFO));
519
520 newdcb.usWriteTimeout = 0;
521 newdcb.usReadTimeout = 0; //65535;
522 newdcb.fbCtlHndShake = cp->orig_dcb.fbFlowReplace = 0;
523 newdcb.fbTimeout = 6;
524
525 rc = DosDevIOCtl(cp->porthandle, IOCTL_ASYNC, ASYNC_SETDCBINFO,
526 &newdcb, ulParmLen, &ulParmLen, 0, 0, 0);
527 if ( rc != NO_ERROR) {
528 goto cleanup_error;
529 }
530
531 ulParmLen = sizeof(errors);
532 rc = DosDevIOCtl(cp->porthandle, IOCTL_ASYNC, ASYNC_GETCOMMERROR,
533 0, 0, 0, &errors, ulParmLen, &ulParmLen);
534 if ( rc != NO_ERROR) {
535 goto cleanup_error;
536 }
537
538 *port = cp;
539 return true;
540
541 cleanup_error:
542 // TODO error string - rc value
543 if (cp->porthandle != 0) DosClose(cp->porthandle);
544 free(cp);
545 return false;
546 }
547
548 void SERIAL_getErrorString(char* buffer, size_t length) {
549 sprintf(buffer, "TODO: error handling is not fun");
550 }
551 void SERIAL_close(COMPORT port) {
552 ULONG ulParmLen = sizeof(DCBINFO);
553 // restore original DCB, close handle, free the COMPORT struct
554 if (port->porthandle != 0) {
555 DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_SETDCBINFO,
556 &port->orig_dcb, ulParmLen, &ulParmLen, 0, 0, 0);
557 DosClose (port->porthandle);
558 }
559 free(port);
560 }
561 bool SERIAL_sendchar(COMPORT port, char data) {
562 ULONG bytesWritten = 0;
563
564 APIRET rc = DosWrite(port->porthandle, &data, 1, &bytesWritten);
565 if (rc == NO_ERROR && bytesWritten > 0) return true;
566 else return false;
567 }
568
569 void SERIAL_setBREAK(COMPORT port, bool value) {
570 USHORT error;
571 ULONG ulParmLen = sizeof(error);
572 DosDevIOCtl(port->porthandle, IOCTL_ASYNC,
573 value ? ASYNC_SETBREAKON : ASYNC_SETBREAKOFF,
574 0,0,0, &error, ulParmLen, &ulParmLen);
575 }
576
577 int SERIAL_getextchar(COMPORT port) {
578 ULONG dwRead = 0; // Number of chars read
579 char chRead;
580
581 int retval = 0;
582 // receive a byte; TODO communicate faliure
583 if (DosRead(port->porthandle, &chRead, 1, &dwRead) == NO_ERROR) {
584 if (dwRead) {
585 // check for errors; will OS/2 clear the error on reading its data?
586 // if yes then this is in wrong order
587 USHORT errors = 0, event = 0;
588 ULONG ulParmLen = sizeof(errors);
589 DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_GETCOMMEVENT,
590 0, 0, 0, &event, ulParmLen, &ulParmLen);
591 if (event & (64 + 128) ) { // Break (Bit 6) or Frame or Parity (Bit 7) error
592 Bit8u errreg = 0;
593 if (event & 64) retval |= SERIAL_BREAK_ERR;
594 if (event & 128) {
595 DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_GETCOMMERROR,
596 0, 0, 0, &errors, ulParmLen, &ulParmLen);
597 if (errors & 8) retval |= SERIAL_FRAMING_ERR;
598 if (errors & 4) retval |= SERIAL_PARITY_ERR;
599 }
600 }
601 retval |= (chRead & 0xff);
602 retval |= 0x10000;
603 }
604 }
605 return retval;
606 }
607
608
609 int SERIAL_getmodemstatus(COMPORT port) {
610 UCHAR dptr = 0;
611 ULONG ulParmLen = sizeof(dptr);
612 DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_GETMODEMINPUT,
613 0, 0, 0, &dptr, ulParmLen, &ulParmLen);
614 // bits are the same as return value
615 return (int)dptr;
616 }
617 void SERIAL_setDTR(COMPORT port, bool value) {
618 UCHAR masks[2];
619 ULONG ulParmLen = sizeof(masks);
620 if(value) {
621 masks[0]=0x01;
622 masks[1]=0xFF;
623 } else {
624 masks[0]=0x00;
625 masks[1]=0xFE;
626 }
627 DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_SETMODEMCTRL,
628 0,0,0, &masks, ulParmLen, &ulParmLen);
629 }
630
631 void SERIAL_setRTS(COMPORT port, bool value) {
632 UCHAR masks[2];
633 ULONG ulParmLen = sizeof(masks);
634 if(value) {
635 masks[0]=0x02;
636 masks[1]=0xFF;
637 } else {
638 masks[0]=0x00;
639 masks[1]=0xFD;
640 }
641 DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_SETMODEMCTRL,
642 0,0,0, &masks, ulParmLen, &ulParmLen);
643 }
644
645
646
647 bool SERIAL_setCommParameters(COMPORT port,
648 int baudrate, char parity, int stopbits, int length) {
649 // baud
650 struct {
651 ULONG baud;
652 BYTE fraction;
653 } setbaud;
654
655 setbaud.baud = baudrate;
656 setbaud.fraction = 0;
657 ULONG ulParmLen = sizeof(setbaud);
658 APIRET rc = DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_EXTSETBAUDRATE,
659 &setbaud, ulParmLen, &ulParmLen, 0, 0, 0);
660 if (rc != NO_ERROR) {
661 return false;
662 }
663
664 struct {
665 UCHAR data;
666 UCHAR parity;
667 UCHAR stop;
668 } paramline;
669
670 // byte length
671 if(length > 8 || length < 5) {
672 // TODO SetLastError(ERROR_INVALID_PARAMETER);
673 return false;
674 }
675 paramline.data = length;
676
677 // parity
678 switch (parity) {
679 case 'n': paramline.parity = 0; break;
680 case 'o': paramline.parity = 1; break;
681 case 'e': paramline.parity = 2; break;
682 case 'm': paramline.parity = 3; break;
683 case 's': paramline.parity = 4; break;
684 default:
685 // TODO SetLastError(ERROR_INVALID_PARAMETER);
686 return false;
687 }
688 // stopbits
689 switch(stopbits) {
690 case SERIAL_1STOP: paramline.stop = 0; break;
691 case SERIAL_2STOP: paramline.stop = 2; break;
692 case SERIAL_15STOP: paramline.stop = 1; break;
693 default:
694 // TODO SetLastError(ERROR_INVALID_PARAMETER);
695 return false;
696 }
697 // set it
698 ulParmLen = sizeof(paramline);
699 rc = DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_SETLINECTRL,
700 &paramline, ulParmLen, &ulParmLen, 0, 0, 0);
701 if ( rc != NO_ERROR)
702 return false;
703 return true;
704 }
705 #endif