DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/hardware/serialport/libserial.cpp
00001 /*
00002  *  Copyright (C) 2002-2013  The DOSBox Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 
00020 #include "libserial.h"
00021 
00022 #include "config.h"
00023 
00024 #ifdef WIN32
00025 
00026 #include <windows.h>
00027 #include <stdio.h>
00028 
00029 struct _COMPORT {
00030         HANDLE porthandle;
00031         bool breakstatus;
00032         DCB orig_dcb;
00033 };
00034 
00035 bool SERIAL_open(const char* portname, COMPORT* port) {
00036         // allocate COMPORT structure
00037         COMPORT cp = (_COMPORT*)malloc(sizeof(_COMPORT));
00038         if(cp == NULL) return false;
00039         
00040         cp->breakstatus=false;
00041 
00042         // open the port in NT object space (recommended by Microsoft)
00043         // allows the user to open COM10+ and custom port names.
00044         int len = (int)strlen(portname);
00045         if(len > 240) {
00046                 SetLastError(ERROR_BUFFER_OVERFLOW);
00047                 free(cp);
00048                 return false;
00049         }
00050         char extended_portname[256] = "\\\\.\\";
00051         memcpy(extended_portname+4,portname,len+1);
00052         
00053         cp->porthandle = CreateFile (extended_portname,
00054                                            GENERIC_READ | GENERIC_WRITE, 0,
00055                                                                           // must be opened with exclusive-access
00056                            NULL,          // no security attributes
00057                            OPEN_EXISTING, // must use OPEN_EXISTING
00058                            0,             // non overlapped I/O
00059                            NULL           // hTemplate must be NULL for comm devices
00060                           );
00061 
00062         if (cp->porthandle == INVALID_HANDLE_VALUE) goto cleanup_error;
00063         
00064         cp->orig_dcb.DCBlength=sizeof(DCB);
00065 
00066         if(!GetCommState(cp->porthandle, &cp->orig_dcb)) {
00067                 goto cleanup_error;
00068         }
00069 
00070         // configure the port for polling
00071         DCB newdcb;
00072         memcpy(&newdcb,&cp->orig_dcb,sizeof(DCB));
00073 
00074         newdcb.fBinary=true;
00075         newdcb.fParity=true;
00076         newdcb.fOutxCtsFlow=false;
00077         newdcb.fOutxDsrFlow=false;
00078         newdcb.fDtrControl=DTR_CONTROL_DISABLE;
00079         newdcb.fDsrSensitivity=false;
00080         
00081         newdcb.fOutX=false;
00082         newdcb.fInX=false;
00083         newdcb.fErrorChar=0;
00084         newdcb.fNull=false;
00085         newdcb.fRtsControl=RTS_CONTROL_DISABLE;
00086         newdcb.fAbortOnError=false;
00087 
00088         if(!SetCommState(cp->porthandle, &newdcb)) {
00089                 goto cleanup_error;
00090         }
00091 
00092         // Configure timeouts to effectively use polling
00093         COMMTIMEOUTS ct;
00094         ct.ReadIntervalTimeout = MAXDWORD;
00095         ct.ReadTotalTimeoutConstant = 0;
00096         ct.ReadTotalTimeoutMultiplier = 0;
00097         ct.WriteTotalTimeoutConstant = 0;
00098         ct.WriteTotalTimeoutMultiplier = 0;
00099         if(!SetCommTimeouts(cp->porthandle, &ct)) {
00100                 goto cleanup_error;
00101         }
00102         if(!ClearCommBreak(cp->porthandle)) {
00103                 // Bluetooth Bluesoleil seems to not implement it
00104                 //goto cleanup_error;
00105         }
00106         DWORD errors;
00107         if(!ClearCommError(cp->porthandle, &errors, NULL)) {
00108                 goto cleanup_error;
00109         }
00110         *port = cp;
00111         return true;
00112 
00113 cleanup_error:
00114         if (cp->porthandle != INVALID_HANDLE_VALUE) CloseHandle(cp->porthandle);
00115         free(cp);
00116         return false;
00117 }
00118 
00119 void SERIAL_close(COMPORT port) {
00120         // restore original DCB, close handle, free the COMPORT struct
00121         if (port->porthandle != INVALID_HANDLE_VALUE) {
00122                 SetCommState(port->porthandle, &port->orig_dcb);
00123                 CloseHandle(port->porthandle);
00124         }
00125         free(port);
00126 }
00127 
00128 void SERIAL_getErrorString(char* buffer, int length) {
00129         int error = GetLastError();
00130         if(length < 50) return;
00131         memset(buffer,0,length);
00132         // get the error message text from the operating system
00133         LPVOID sysmessagebuffer;
00134         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
00135                 NULL,
00136                 error,
00137                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00138                 (LPTSTR) &sysmessagebuffer,
00139                 0,NULL);
00140 
00141         const char* err5text = "The specified port is already in use.\n";
00142         const char* err2text = "The specified port does not exist.\n";
00143 
00144         int sysmsg_offset = 0;
00145 
00146         if(error == 5) {
00147                 sysmsg_offset = (int)strlen(err5text);
00148                 memcpy(buffer,err5text,sysmsg_offset);
00149 
00150         } else if(error == 2) {
00151                 sysmsg_offset = (int)strlen(err2text);
00152                 memcpy(buffer,err2text,sysmsg_offset);
00153         }
00154 
00155         if((length - sysmsg_offset - (int)strlen((const char*)sysmessagebuffer)) >= 0)
00156                 memcpy(buffer + sysmsg_offset, sysmessagebuffer,
00157                 strlen((const char*)sysmessagebuffer));
00158                 
00159         LocalFree(sysmessagebuffer);
00160 }
00161 
00162 
00163 void SERIAL_setDTR(COMPORT port, bool value) {
00164         EscapeCommFunction(port->porthandle, value ? SETDTR:CLRDTR);
00165 }
00166 
00167 void SERIAL_setRTS(COMPORT port, bool value) {
00168         EscapeCommFunction(port->porthandle, value ? SETRTS:CLRRTS);
00169 }
00170 
00171 void SERIAL_setBREAK(COMPORT port, bool value) {
00172         EscapeCommFunction(port->porthandle, value ? SETBREAK:CLRBREAK);
00173         port->breakstatus = value;
00174 }
00175 
00176 int SERIAL_getmodemstatus(COMPORT port) {
00177         DWORD retval = 0;
00178         GetCommModemStatus (port->porthandle, &retval);
00179         return (int)retval;
00180 }
00181 
00182 bool SERIAL_sendchar(COMPORT port, char data) {
00183         DWORD bytesWritten;
00184 
00185         // mean bug: with break = 1, WriteFile will never return.
00186         if(port->breakstatus) return true; // true or false?!
00187 
00188         WriteFile (port->porthandle, &data, 1, &bytesWritten, NULL);
00189         if(bytesWritten==1) return true;
00190         else return false;
00191 }
00192 
00193 // 0-7 char data, higher=flags
00194 int SERIAL_getextchar(COMPORT port) {
00195         DWORD errors = 0;       // errors from API
00196         DWORD dwRead = 0;       // Number of chars read
00197         char chRead;
00198 
00199         int retval = 0;
00200         // receive a byte; TODO communicate failure
00201         if (ReadFile (port->porthandle, &chRead, 1, &dwRead, NULL)) {
00202                 if (dwRead) {
00203                         // check for errors
00204                         ClearCommError(port->porthandle, &errors, NULL);
00205                         // mask bits are identical
00206                         errors &= CE_BREAK|CE_FRAME|CE_RXPARITY|CE_OVERRUN;
00207                         retval |= (errors<<8);
00208                         retval |= (chRead & 0xff);
00209                         retval |= 0x10000; 
00210                 }
00211         }
00212         return retval;
00213 }
00214 
00215 bool SERIAL_setCommParameters(COMPORT port,
00216                         int baudrate, char parity, int stopbits, int length) {
00217         
00218         DCB dcb;
00219         dcb.DCBlength=sizeof(dcb);
00220         GetCommState(port->porthandle,&dcb);
00221 
00222         // parity
00223         switch (parity) {
00224         case 'n': dcb.Parity = NOPARITY; break;
00225         case 'o': dcb.Parity = ODDPARITY; break;
00226         case 'e': dcb.Parity = EVENPARITY; break;
00227         case 'm': dcb.Parity = MARKPARITY; break;
00228         case 's': dcb.Parity = SPACEPARITY;     break;
00229         default:
00230                 SetLastError(ERROR_INVALID_PARAMETER);
00231                 return false;
00232         }
00233 
00234         // stopbits
00235         switch(stopbits) {
00236         case SERIAL_1STOP: dcb.StopBits = ONESTOPBIT; break;
00237         case SERIAL_2STOP: dcb.StopBits = TWOSTOPBITS; break;
00238         case SERIAL_15STOP: dcb.StopBits = ONE5STOPBITS; break;
00239         default:
00240                 SetLastError(ERROR_INVALID_PARAMETER);
00241                 return false;
00242         }
00243 
00244         // byte length
00245         if(length > 8 || length < 5) {
00246                 SetLastError(ERROR_INVALID_PARAMETER);
00247                 return false;
00248         }
00249         dcb.ByteSize = length;
00250         dcb.BaudRate = baudrate;
00251 
00252         if (!SetCommState (port->porthandle, &dcb)) return false;
00253         return true;
00254 }
00255 #endif
00256 
00257 #if defined (LINUX) || defined (MACOSX) || defined (BSD)
00258 
00259 #include <string.h> // strlen
00260 #include <stdlib.h>
00261 
00262 #include <termios.h>
00263 #include <unistd.h>
00264 
00265 #include <sys/types.h>
00266 #include <sys/stat.h>
00267 #include <sys/ioctl.h>
00268 
00269 #include <errno.h>
00270 #include <fcntl.h>
00271 #include <stdio.h> // sprinf
00272 
00273 struct _COMPORT {
00274         int porthandle;
00275         bool breakstatus;
00276         termios backup;
00277 };
00278 
00279 bool SERIAL_open(const char* portname, COMPORT* port) {
00280         int result;
00281         // allocate COMPORT structure
00282         COMPORT cp = (_COMPORT*)malloc(sizeof(_COMPORT));
00283         if(cp == NULL) return false;
00284 
00285         cp->breakstatus=false;
00286 
00287         int len = strlen(portname);
00288         if(len > 240) {
00290                 return false;
00291         }
00292         char extended_portname[256] = "/dev/";
00293         memcpy(extended_portname+5,portname,(size_t)len);
00294 
00295         cp->porthandle = open (extended_portname, O_RDWR | O_NOCTTY | O_NONBLOCK);
00296         if (cp->porthandle < 0) goto cleanup_error;
00297 
00298         result = tcgetattr(cp->porthandle,&cp->backup);
00299         if (result==-1) goto cleanup_error;
00300 
00301         // get port settings
00302         termios termInfo;
00303         memcpy(&termInfo,&cp->backup,sizeof(termios));
00304 
00305         // initialize the port
00306         termInfo.c_cflag = CS8 | CREAD | CLOCAL; // noparity, 1 stopbit
00307         termInfo.c_iflag = PARMRK | INPCK;
00308         termInfo.c_oflag = 0;
00309         termInfo.c_lflag = 0;
00310         termInfo.c_cc[VMIN] = 0;
00311         termInfo.c_cc[VTIME] = 0;
00312 
00313         tcflush (cp->porthandle, TCIFLUSH);
00314         tcsetattr (cp->porthandle, TCSANOW, &termInfo);
00315 
00316         *port = cp;
00317         return true;
00318 
00319 cleanup_error:
00320         if (cp->porthandle != 0) close(cp->porthandle);
00321         free(cp);
00322         return false;
00323 }
00324 
00325 void SERIAL_close(COMPORT port) {
00326         // restore original termios, close handle, free the COMPORT struct
00327         if (port->porthandle >= 0) {
00328                 tcsetattr(port->porthandle, TCSANOW, &port->backup);
00329                 close(port->porthandle);
00330         }
00331         free(port);
00332 }
00333 
00334 void SERIAL_getErrorString(char* buffer, int length) {
00335         int error = errno;
00336         if(length < 50) return;
00337         memset(buffer,0,(size_t)length);
00338         // get the error message text from the operating system
00339         // TODO (or not)
00340         
00341         const char* err5text = "The specified port is already in use.\n";
00342         const char* err2text = "The specified port does not exist.\n";
00343         
00344         int sysmsg_offset = 0;
00345 
00346         if(error == EBUSY) {
00347                 sysmsg_offset = strlen(err5text);
00348                 memcpy(buffer,err5text,(size_t)sysmsg_offset);
00349 
00350         } else if(error == 2) {
00351                 sysmsg_offset = strlen(err2text);
00352                 memcpy(buffer,err2text,(size_t)sysmsg_offset);
00353         }
00354         
00355         sprintf(buffer + sysmsg_offset, "System error %d.",error);
00356         
00357 }
00358 
00359 int SERIAL_getmodemstatus(COMPORT port) {
00360         long flags = 0;
00361         ioctl (port->porthandle, TIOCMGET, &flags);
00362         int retval = 0;
00363         if (flags & TIOCM_CTS) retval |= SERIAL_CTS;
00364         if (flags & TIOCM_DSR) retval |= SERIAL_DSR;
00365         if (flags & TIOCM_RI) retval |= SERIAL_RI;
00366         if (flags & TIOCM_CD) retval |= SERIAL_CD;
00367         return retval;
00368 }
00369 
00370 bool SERIAL_sendchar(COMPORT port, char data) {
00371         if(port->breakstatus) return true; // true or false?!; Does POSIX need this check?
00372         int bytesWritten = write(port->porthandle, &data, 1);
00373         if(bytesWritten==1) return true;
00374         else return false;
00375 }
00376 
00377 int SERIAL_getextchar(COMPORT port) {
00378         unsigned char chRead = 0;
00379         int dwRead = 0;
00380         unsigned char error = 0;
00381         int retval = 0;
00382 
00383         dwRead=read(port->porthandle,&chRead,1);
00384         if (dwRead==1) {
00385                 if(chRead==0xff) // error escape
00386                 {
00387                         dwRead=read(port->porthandle,&chRead,1);
00388                         if(chRead==0x00) // an error 
00389                         {
00390                                 dwRead=read(port->porthandle,&chRead,1);
00391                                 if(chRead==0x0) error=SERIAL_BREAK_ERR;
00392                                 else error=SERIAL_FRAMING_ERR;
00393                         }
00394                 }
00395                 retval |= (error<<8);
00396                 retval |= chRead;
00397                 retval |= 0x10000; 
00398         }
00399         return retval;
00400 }
00401 
00402 bool SERIAL_setCommParameters(COMPORT port,
00403                         int baudrate, char parity, int stopbits, int length) {
00404         
00405         termios termInfo;
00406         int result = tcgetattr(port->porthandle, &termInfo);
00407         if (result==-1) return false;
00408         termInfo.c_cflag = CREAD | CLOCAL;
00409 
00410         // parity
00411         // "works on many systems"
00412         #define CMSPAR 010000000000
00413         switch (parity) {
00414         case 'n': break;
00415         case 'o': termInfo.c_cflag |= (PARODD | PARENB); break;
00416         case 'e': termInfo.c_cflag |= PARENB; break;
00417         case 'm': termInfo.c_cflag |= (PARENB | CMSPAR | PARODD); break;
00418         case 's': termInfo.c_cflag |= (PARENB | CMSPAR); break;
00419         default:
00420                 return false;
00421         }
00422         // stopbits
00423         switch(stopbits) {
00424         case SERIAL_1STOP: break;
00425         case SERIAL_2STOP: 
00426         case SERIAL_15STOP: termInfo.c_cflag |= CSTOPB; break;
00427         default:
00428                 return false;
00429         }
00430         // byte length
00431         if(length > 8 || length < 5) return false;
00432         switch (length) {
00433         case 5: termInfo.c_cflag |= CS5; break;
00434         case 6: termInfo.c_cflag |= CS6; break;
00435         case 7: termInfo.c_cflag |= CS7; break;
00436         case 8: termInfo.c_cflag |= CS8; break;
00437         }
00438         // baudrate
00439         int posix_baudrate=0;
00440         switch(baudrate) {
00441                 case 115200: posix_baudrate = B115200; break;
00442                 case  57600: posix_baudrate = B57600; break;
00443                 case  38400: posix_baudrate = B38400; break;
00444                 case  19200: posix_baudrate = B19200; break;
00445                 case   9600: posix_baudrate = B9600; break;
00446                 case   4800: posix_baudrate = B4800; break;
00447                 case   2400: posix_baudrate = B2400; break;
00448                 case   1200: posix_baudrate = B1200; break;
00449                 case    600: posix_baudrate = B600; break;
00450                 case    300: posix_baudrate = B300; break;
00451                 case    110: posix_baudrate = B110; break;
00452                 default: return false;
00453         }
00454         cfsetospeed (&termInfo, (unsigned int)posix_baudrate);
00455         cfsetispeed (&termInfo, (unsigned int)posix_baudrate);
00456 
00457         int retval = tcsetattr(port->porthandle, TCSANOW, &termInfo);
00458         if(retval==-1) return false;
00459         return true;
00460 }
00461 
00462 void SERIAL_setBREAK(COMPORT port, bool value) {
00463         ioctl(port->porthandle, value?TIOCSBRK:TIOCCBRK);
00464 }
00465 
00466 void SERIAL_setDTR(COMPORT port, bool value) {
00467         long flag = TIOCM_DTR;
00468         ioctl(port->porthandle, value?TIOCMBIS:TIOCMBIC, &flag);
00469 }
00470 
00471 void SERIAL_setRTS(COMPORT port, bool value) {
00472         long flag = TIOCM_RTS;
00473         ioctl(port->porthandle, value?TIOCMBIS:TIOCMBIC, &flag);
00474 }
00475 #endif
00476 
00477 #ifdef OS2
00478 // OS/2 related headers
00479 #define INCL_DOSFILEMGR
00480 #define INCL_DOSERRORS
00481 #define INCL_DOSDEVICES
00482 #define INCL_DOSDEVIOCTL
00483 #define INCL_DOSPROCESS
00484 #include <os2.h>
00485 
00486 struct _COMPORT {
00487         HFILE porthandle;
00488         bool breakstatus;
00489         DCBINFO backup;
00490 };
00491 // TODO: THIS IS INCOMPLETE and UNTESTED.
00492 
00493 bool SERIAL_open(const char* portname, COMPORT* port) {
00494         // allocate COMPORT structure
00495         COMPORT cp = (_COMPORT*)malloc(sizeof(_COMPORT));
00496         if(cp == NULL) return false;
00497         cp->porthandle=0;
00498         cp->breakstatus=false;
00499 
00500         ULONG ulAction = 0;
00501         APIRET rc = DosOpen(portname, &cp->porthandle,
00502                 &ulAction, 0L, FILE_NORMAL, FILE_OPEN,
00503                 OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE | OPEN_FLAGS_SEQUENTIAL, 0L);
00504         if (rc != NO_ERROR) {
00505                 goto cleanup_error;
00506         }
00507 
00508         ULONG ulParmLen = sizeof(DCBINFO);
00509         rc = DosDevIOCtl(hCom, IOCTL_ASYNC, ASYNC_GETDCBINFO,
00510                 0, 0, 0, &cp->orig_dcb, ulParmLen, &ulParmLen);
00511         if ( rc != NO_ERROR) {
00512                 goto cleanup_error;
00513         }
00514         // configure the port for polling
00515         DCBINFO newdcb;
00516         memcpy(&newdcb,&cp->orig_dcb,sizeof(DCBINFO));
00517 
00518         newdcb.usWriteTimeout = 0;
00519         newdcb.usReadTimeout = 0; //65535;
00520         newdcb.fbCtlHndShake = dcb.fbFlowReplace = 0;
00521         newdcb.fbTimeout = 6;
00522 
00523         rc = DosDevIOCtl(hCom, IOCTL_ASYNC, ASYNC_SETDCBINFO,
00524                 &newdcb, ulParmLen, &ulParmLen, 0, 0, 0);
00525         if ( rc != NO_ERROR) {
00526                 goto cleanup_error;
00527         }
00528 
00529         USHORT errors = 0;
00530         ulParmLen = sizeof(errors);
00531         rc = DosDevIOCtl(hCom, IOCTL_ASYNC, ASYNC_GETCOMMERROR,
00532                 0, 0, 0, &errors, ulParmLen, &ulParmLen);
00533         if ( rc != NO_ERROR) {
00534                 goto cleanup_error;
00535         }
00536 
00537         *port = cp;
00538         return true;
00539 
00540 cleanup_error:
00541         // TODO error string - rc value
00542         if (cp->porthandle != 0) CloseHandle(cp->porthandle);
00543         free(cp);
00544         return false;
00545 }
00546 
00547 void SERIAL_getErrorString(char* buffer, int length) {
00548         sprintf(buffer, "TODO: error handling is not fun");
00549 }
00550 void SERIAL_close(COMPORT port) {
00551         ULONG ulParmLen = sizeof(DCBINFO);
00552         // restore original DCB, close handle, free the COMPORT struct
00553         if (port->porthandle != 0) {
00554                 DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_SETDCBINFO,
00555                         &port->orig_dcb, ulParmLen, &ulParmLen, 0, 0, 0);
00556                 SetCmmState(port->porthandle, &port->orig_dcb);
00557                 DosClose (port->porthandle);
00558         }
00559         free(port);
00560 }
00561 bool SERIAL_sendchar(COMPORT port, char data) {
00562         ULONG bytesWritten = 0;
00563         if(port->breakstatus) return true; // does OS/2 need this?
00564 
00565         APIRET rc = DosWrite(port->porthandle, &data, 1, &bytesWritten);
00566         if (rc == NO_ERROR && bytesWritten > 0) return true;
00567         else return false;
00568 }
00569 
00570 void SERIAL_setBREAK(COMPORT port, bool value) {
00571         USHORT error;
00572         ULONG ulParmLen = sizeof(error);
00573         DosDevIOCtl(port->porthandle, IOCTL_ASYNC,
00574                 value? ASYNC_SETBREAKON:ASYNC_SETBREAKOFF,
00575                 0,0,0, &error, ulParmLen, &ulParmLen);
00576 }
00577 
00578 int SERIAL_getextchar(COMPORT port) {
00579         ULONG dwRead = 0;       // Number of chars read
00580         char chRead;
00581 
00582         int retval = 0;
00583         // receive a byte; TODO communicate failure
00584         if (DosRead(port->porthandle, &chRead, 1, &dwRead) == NO_ERROR) {
00585                 if (dwRead) {
00586                         // check for errors; will OS/2 clear the error on reading its data?
00587                         // if yes then this is in wrong order
00588                         USHORT errors = 0, event = 0;
00589                         ULONG ulParmLen = sizeof(errors);
00590                         DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_GETCOMMEVENT,
00591                                 0, 0, 0, &event, ulParmLen, &ulParmLen);
00592                         if (event & (64 + 128) ) { // Break (Bit 6) or Frame or Parity (Bit 7) error
00593                                 Bit8u errreg = 0;
00594                                 if (event & 64) retval |= SERIAL_BREAK_ERR;
00595                                 if (event & 128) {
00596                                         DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_GETCOMMERROR,
00597                                                 0, 0, 0, &errors, ulParmLen, &ulParmLen);
00598                                         if (errors & 8) retval |= SERIAL_FRAMING_ERR;
00599                                         if (errors & 4) retval |= SERIAL_PARITY_ERR;
00600                                 }
00601                         }
00602                         retval |= (chRead & 0xff);
00603                         retval |= 0x10000; 
00604                 }
00605         }
00606         return retval;
00607 }
00608 
00609 
00610 int SERIAL_getmodemstatus(COMPORT port) {
00611         UCHAR dptr = 0;
00612         ULONG ulParmLen = sizeof(dptr);
00613         DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_GETMODEMINPUT,
00614                 0, 0, 0, &dptr, ulParmLen, &ulParmLen);
00615         // bits are the same as return value
00616         return (int)dptr;
00617 }
00618 void SERIAL_setDTR(COMPORT port, bool value) {
00619         UCHAR masks[2];
00620         ULONG ulParmLen = sizeof(masks);
00621         if(value) {
00622                 masks[0]=0x01;
00623                 masks[1]=0xFF;
00624         } else {
00625                 masks[0]=0x00;
00626                 masks[1]=0xFE;
00627         }
00628         DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_SETMODEMCTRL,
00629                 0,0,0, &masks, ulParmLen, &ulParmLen);
00630 }
00631 
00632 void SERIAL_setRTS(COMPORT port, bool value) {
00633         UCHAR masks[2];
00634         ULONG ulParmLen = sizeof(masks);
00635         if(value) {
00636                 masks[0]=0x02;
00637                 masks[1]=0xFF;
00638         } else {
00639                 masks[0]=0x00;
00640                 masks[1]=0xFD;
00641         }
00642         DosDevIOCtl(port->porthandle, IOCTL_ASYNC, ASYNC_SETMODEMCTRL,
00643                 0,0,0, &masks, ulParmLen, &ulParmLen);
00644 }
00645 
00646 
00647 
00648 bool SERIAL_setCommParameters(COMPORT port,
00649                         int baudrate, char parity, int stopbits, int length) {
00650         // baud
00651         struct {
00652                 ULONG baud;
00653                 BYTE fraction;
00654         } setbaud;
00655 
00656         setbaud.baud = baudrate;
00657         setbaud.fraction = 0;
00658         ULONG ulParmLen = sizeof(setbaud);
00659         APIRET rc = DosDevIOCtl(hCom, IOCTL_ASYNC, ASYNC_EXTSETBAUDRATE,
00660                 &setbaud, ulParmLen, &ulParmLen, 0, 0, 0);
00661         if (rc != NO_ERROR) {
00662                 return false;
00663         }
00664 
00665         struct {
00666                 UCHAR data;
00667                 UCHAR parity;
00668                 UCHAR stop;
00669         } paramline;
00670 
00671         // byte length
00672         if(length > 8 || length < 5) {
00673                 // TODO SetLastError(ERROR_INVALID_PARAMETER);
00674                 return false;
00675         }
00676         paramline.data = length;
00677 
00678         // parity
00679         switch (parity) {
00680         case 'n': paramline.parity = 0; break;
00681         case 'o': paramline.parity = 1; break;
00682         case 'e': paramline.parity = 2; break;
00683         case 'm': paramline.parity = 3; break;
00684         case 's': paramline.parity = 4; break;
00685         default:
00686                 // TODO SetLastError(ERROR_INVALID_PARAMETER);
00687                 return false;
00688         }
00689         // stopbits
00690         switch(stopbits) {
00691         case SERIAL_1STOP: paramline.stop = 0; break;
00692         case SERIAL_2STOP: paramline.stop = 2; break;
00693         case SERIAL_15STOP: paramline.stop = 1; break;
00694         default:
00695                 // TODO SetLastError(ERROR_INVALID_PARAMETER);
00696                 return false;
00697         }
00698         // set it
00699         ulParmLen = sizeof(paramline);
00700         rc = DosDevIOCtl(hCom, IOCTL_ASYNC, ASYNC_SETLINECTRL,
00701                 &paramline, ulParmLen, &ulParmLen, 0, 0, 0);
00702         if ( rc != NO_ERROR)
00703                 return false;
00704         return true;
00705 }
00706 #endif