home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / SERIAL.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  19KB  |  792 lines

  1.  
  2. // LoraBBS Version 2.99 Free Edition
  3. // Copyright (C) 1987-98 Marco Maccaferri
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include "_ldefs.h"
  20. #include "combase.h"
  21.  
  22. #if defined(__OS2__)
  23. #define ASYNC_EXTSETBAUDRATE              0x0043
  24.  
  25. USHORT DevIOCtl (PVOID pData, USHORT cbData, PVOID pParms, USHORT cbParms, USHORT usFunction, USHORT usCategory, HFILE hDevice)
  26. {
  27.    ULONG ulParmLengthInOut = cbParms, ulDataLengthInOut = cbData;
  28.    return ((USHORT)DosDevIOCtl (hDevice, usCategory, usFunction, pParms, cbParms, &ulParmLengthInOut, pData, cbData, &ulDataLengthInOut));
  29. }
  30. #endif
  31.  
  32. TSerial::TSerial (void)
  33. {
  34. #if defined(__OS2__)
  35.    hFile = NULLHANDLE;
  36. #elif defined(__NT__)
  37.    hFile = INVALID_HANDLE_VALUE;
  38. #elif defined(__LINUX__)
  39.    hFile = -1;
  40. #else
  41.    hPort = NULL;
  42. #endif
  43.  
  44. #if defined(__OS2__) || defined(__NT__)
  45.    strcpy (Device, "COM2");
  46. #elif defined(__LINUX__)
  47.    strcpy (Device, "/dev/modem");
  48. #else
  49.    Com = 2;
  50. #endif
  51.    Speed = 19200L;
  52.    DataBits = 8;
  53.    StopBits = 1;
  54.    Parity = 'N';
  55.  
  56.    EndRun = FALSE;
  57.    TxBytes = RxBytes = 0;
  58. }
  59.  
  60. TSerial::~TSerial (void)
  61. {
  62. #if defined(__OS2__)
  63.    if (hFile != NULLHANDLE)
  64.       DosClose (hFile);
  65. #elif defined(__NT__)
  66.    if (hFile != INVALID_HANDLE_VALUE)
  67.       CloseHandle (hFile);
  68. #elif defined(__LINUX__)
  69.    if (hFile >= 0) {
  70.       ioctl (hFile, TCSETSW, &old_termio);
  71.       close (hFile);
  72.    }
  73. #else
  74.    if (hPort != NULL)
  75.       PortClose (hPort);
  76. #endif
  77. }
  78.  
  79. VOID TSerial::BufferByte (UCHAR byte)
  80. {
  81.    TxBuffer[TxBytes++] = byte;
  82.    if (TxBytes >= TSIZE)
  83.       UnbufferBytes ();
  84. }
  85.  
  86. VOID TSerial::BufferBytes (UCHAR *bytes, USHORT len)
  87. {
  88.    USHORT ToCopy;
  89.  
  90.    if (len > 0 && EndRun == FALSE) {
  91.       do {
  92.          if (TxBytes < TSIZE) {
  93.             ToCopy = len;
  94.             if (ToCopy > TSIZE - TxBytes)
  95.                ToCopy = (USHORT)(TSIZE - TxBytes);
  96.             memcpy (&TxBuffer[TxBytes], bytes, ToCopy);
  97.             bytes += ToCopy;
  98.             TxBytes += ToCopy;
  99.             len -= ToCopy;
  100.          }
  101.          if (TxBytes >= TSIZE)
  102.             UnbufferBytes ();
  103.       } while (len > 0 && EndRun == FALSE);
  104.    }
  105. }
  106.  
  107. USHORT TSerial::BytesReady (VOID)
  108. {
  109.    USHORT RetVal = FALSE;
  110.  
  111. #if defined(__OS2__)
  112.    UINT data = 0;
  113.  
  114.    if (hFile != NULLHANDLE) {
  115.       if (RxBytes > 0)
  116.          RetVal = TRUE;
  117.       else {
  118.          DevIOCtl ((VOID *)&data, sizeof (UINT), NULL, 0, ASYNC_GETINQUECOUNT, IOCTL_ASYNC, hFile);
  119.          if ((data & 0xFFFF) > 0)
  120.             RetVal = TRUE;
  121.       }
  122.    }
  123.  
  124.    if (RetVal == FALSE)
  125.       DosSleep (1L);
  126. #elif defined(__NT__)
  127.    ULONG Available;
  128.  
  129.    if (hFile != INVALID_HANDLE_VALUE) {
  130.       if (RxBytes > 0)
  131.          RetVal = TRUE;
  132.       else {
  133.          ReadFile (hFile, (PVOID)RxBuffer, sizeof (RxBuffer), &Available, NULL);
  134.          if (Available > 0) {
  135.             RxBytes = (USHORT)Available;
  136.             NextByte = RxBuffer;
  137.             RetVal = TRUE;
  138.          }
  139.       }
  140.    }
  141.  
  142.    if (RetVal == FALSE)
  143.       Sleep (1L);
  144. #elif defined(__LINUX__)
  145.    int i;
  146.  
  147.    if (hFile >= 0) {
  148.       if (RxBytes > 0)
  149.          RetVal = TRUE;
  150.       else {
  151.          if ((i = read (hFile, RxBuffer, sizeof (RxBuffer))) > 0) {
  152.             RxBytes = (USHORT)i;
  153.             NextByte = RxBuffer;
  154.             RetVal = TRUE;
  155.          }
  156.       }
  157.    }
  158. #else
  159.    if (hPort != NULL) {
  160.       if (RxBytes > 0)
  161.          RetVal = TRUE;
  162.       else {
  163.          if (PeekChar (hPort) >= 0)
  164.             RetVal = TRUE;
  165.       }
  166.    }
  167. #endif
  168.  
  169.    return (RetVal);
  170. }
  171.  
  172. USHORT TSerial::Carrier (VOID)
  173. {
  174. #if defined(__OS2__)
  175.    UCHAR data = 0;
  176.  
  177.    if (hFile != NULLHANDLE && EndRun == FALSE)
  178.       DevIOCtl ((VOID *)&data, sizeof (UCHAR), NULL, 0, ASYNC_GETMODEMINPUT, IOCTL_ASYNC, hFile);
  179.  
  180.    return ((USHORT)((data & DCD) ? TRUE : FALSE));
  181. #elif defined(__NT__)
  182.    DWORD Status = 0L;
  183.  
  184.    if (hFile != INVALID_HANDLE_VALUE && EndRun == FALSE)
  185.       GetCommModemStatus (hFile, &Status);
  186.  
  187.    return ((Status & MS_RLSD_ON) ? TRUE : FALSE);
  188. #elif defined(__LINUX__)
  189.    int mcs;
  190.  
  191.    ioctl (hFile, TIOCMGET, &mcs);
  192.    return ((mcs & TIOCM_CAR) ? TRUE : FALSE);
  193. #else
  194.    int data = 0;
  195.  
  196.    if (hPort != NULL)
  197.       data = GetModemStatus (hPort);
  198.  
  199.    return ((data & CD_SET) ? TRUE : FALSE);
  200. #endif
  201. }
  202.  
  203. VOID TSerial::ClearInbound (VOID)
  204. {
  205. #if defined(__OS2__)
  206.    UINT data;
  207.    CHAR parm = 0;
  208.  
  209.    if (hFile != NULLHANDLE) {
  210.       RxBytes = 0;
  211.       DevIOCtl (&data, sizeof (data), &parm, sizeof (parm), DEV_FLUSHINPUT, IOCTL_GENERAL, hFile);
  212.    }
  213. #elif defined(__NT__)
  214.    if (hFile != INVALID_HANDLE_VALUE) {
  215.       RxBytes = 0;
  216.       PurgeComm (hFile, PURGE_RXCLEAR);
  217.    }
  218. #elif defined(__LINUX__)
  219.    RxBytes = 0;
  220. #else
  221.    if (hPort != NULL) {
  222.       RxBytes = 0;
  223.       ClearRXBuffer (hPort);
  224.    }
  225. #endif
  226. }
  227.  
  228. VOID TSerial::ClearOutbound (VOID)
  229. {
  230. #if defined(__OS2__)
  231.    UINT data;
  232.    CHAR parm = 0;
  233.  
  234.    if (hFile != NULLHANDLE) {
  235.       TxBytes = 0;
  236.       DevIOCtl (&data, sizeof (data), &parm, sizeof (parm), DEV_FLUSHOUTPUT, IOCTL_GENERAL, hFile);
  237.    }
  238. #elif defined(__NT__)
  239.    if (hFile != INVALID_HANDLE_VALUE) {
  240.       TxBytes = 0;
  241.       PurgeComm (hFile, PURGE_TXCLEAR);
  242.    }
  243. #elif defined(__LINUX__)
  244.    TxBytes = 0;
  245. #else
  246.    if (hPort != NULL) {
  247.       TxBytes = 0;
  248.       ClearTXBuffer (hPort);
  249.    }
  250. #endif
  251. }
  252.  
  253. USHORT TSerial::Initialize (VOID)
  254. {
  255.    USHORT RetVal = FALSE;
  256.  
  257. #if defined(__OS2__)
  258.    UINT data;
  259.    ULONG action;
  260.    MODEMSTATUS ms;
  261.    DCBINFO dcbCom;
  262.  
  263.    hFile = NULLHANDLE;
  264.  
  265.    if (DosOpen (Device, &hFile, &action, 0L, FILE_NORMAL, FILE_OPEN, OPEN_ACCESS_READWRITE|OPEN_SHARE_DENYNONE, 0L) == 0) {
  266.       DevIOCtl ((VOID *)&dcbCom, sizeof (DCBINFO), NULL, 0, ASYNC_GETDCBINFO, IOCTL_ASYNC, hFile);
  267.       dcbCom.fbCtlHndShake &= ~(MODE_RTS_HANDSHAKE);
  268.       dcbCom.fbCtlHndShake |= MODE_DTR_CONTROL|MODE_CTS_HANDSHAKE;
  269.       dcbCom.fbFlowReplace &= ~(MODE_AUTO_TRANSMIT|MODE_AUTO_RECEIVE|MODE_ERROR_CHAR|MODE_NULL_STRIPPING|MODE_BREAK_CHAR);
  270.       dcbCom.fbTimeout |= MODE_NOWAIT_READ_TIMEOUT;
  271.       dcbCom.fbTimeout &= ~(MODE_NO_WRITE_TIMEOUT);
  272.       dcbCom.usReadTimeout = 1;
  273.       dcbCom.usWriteTimeout = 1;
  274.       DevIOCtl (NULL, 0, (VOID *)&dcbCom, sizeof (DCBINFO), ASYNC_SETDCBINFO, IOCTL_ASYNC, hFile);
  275.  
  276.       ms.fbModemOn = RTS_ON|DTR_ON;
  277.       ms.fbModemOff = 255;
  278.       DevIOCtl ((VOID *)&data, sizeof (data), (VOID *)&ms, sizeof (ms), ASYNC_SETMODEMCTRL, IOCTL_ASYNC, hFile);
  279.  
  280.       SetParameters (Speed, DataBits, Parity, StopBits);
  281.       RetVal = TRUE;
  282.    }
  283. #elif defined(__NT__)
  284.    COMMTIMEOUTS cto;
  285.  
  286.    if ((hFile = CreateFile (Device, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) {
  287.       GetCommTimeouts (hFile, &cto);
  288.       cto.ReadIntervalTimeout = MAXDWORD;
  289.       cto.ReadTotalTimeoutMultiplier = 0;
  290.       cto.ReadTotalTimeoutConstant = 0;
  291.       SetCommTimeouts (hFile, &cto);
  292.  
  293.       SetParameters (Speed, DataBits, Parity, StopBits);
  294.       RetVal = TRUE;
  295.    }
  296. #elif defined(__LINUX__)
  297.    FILE *fpd;
  298.  
  299.    if ((hFile = open (Device, O_RDWR|O_BINARY)) >= 0) {
  300.       fcntl (hFile, F_SETFL, O_NONBLOCK);
  301.  
  302.       ioctl (hFile, TCGETS, &old_termio);
  303. //      new_termio = old_termio;
  304.       memcpy (&new_termio, &old_termio, sizeof (new_termio));
  305. fpd=fopen("serial.dbg", "wt");
  306. fprintf (fpd, "Before\n");
  307. fprintf (fpd, "  c_iflag=%04o\n", new_termio.c_iflag);
  308. fprintf (fpd, "  c_oflag=%04o\n", new_termio.c_oflag);
  309. fprintf (fpd, "  c_cflag=%04o\n", new_termio.c_cflag);
  310. fprintf (fpd, "  c_lflag=%04o\n", new_termio.c_lflag);
  311.       new_termio.c_iflag = 0;
  312.       new_termio.c_oflag = 0;
  313.       new_termio.c_lflag = 0;
  314.       new_termio.c_cflag = CRTSCTS;
  315.       ioctl (hFile, TCSETSW, &new_termio);
  316. fprintf (fpd, "After\n");
  317. fprintf (fpd, "  c_iflag=%04o\n", new_termio.c_iflag);
  318. fprintf (fpd, "  c_oflag=%04o\n", new_termio.c_oflag);
  319. fprintf (fpd, "  c_cflag=%04o\n", new_termio.c_cflag);
  320. fprintf (fpd, "  c_lflag=%04o\n", new_termio.c_lflag);
  321. fclose (fpd);
  322.  
  323.       SetParameters (Speed, DataBits, Parity, StopBits);
  324.       RetVal = TRUE;
  325.    }
  326.  
  327. //   ioctl (tty_fd, KDSKBMODE, K_RAW);
  328. //   ioctl (tty_fd, VT_GETMODE, &vtm);
  329. //   vtm.mode = VT_PROCESS;
  330. //   vtm.relsig = SIGUSR1;
  331. //   vtm.acqsig = SIGUSR2;
  332. //   ioctl (tty_fd, VT_SETMODE, &vtm);
  333. #else
  334.    breakchk (0);
  335.  
  336.    if ((hPort = PortOpenGreenleafFast ((USHORT)(Com - 1), Speed, Parity, DataBits, StopBits)) != NULL) {
  337.       if (hPort->status != ASSUCCESS) {
  338.          PortClose (hPort);
  339.          hPort = NULL;
  340.          RetVal = FALSE;
  341.       }
  342.    }
  343. #endif
  344.  
  345.    return (RetVal);
  346. }
  347.  
  348. UCHAR TSerial::ReadByte (VOID)
  349. {
  350.    UCHAR c = 0;
  351.  
  352. #if defined(__OS2__)
  353.    ULONG readed;
  354.  
  355.    if (hFile != NULLHANDLE) {
  356.       if (RxBytes == 0) {
  357.          do {
  358.             DosRead (hFile, (PVOID)RxBuffer, sizeof (RxBuffer), &readed);
  359.          } while (readed == 0L && EndRun == FALSE);
  360.          RxBytes = (USHORT)readed;
  361.          NextByte = RxBuffer;
  362.       }
  363.    }
  364. #elif defined(__NT__)
  365.    ULONG readed;
  366.  
  367.    if (hFile != INVALID_HANDLE_VALUE) {
  368.       if (RxBytes == 0) {
  369.          do {
  370.             ReadFile (hFile, (PVOID)RxBuffer, sizeof (RxBuffer), &readed, NULL);
  371.          } while (readed == 0L && EndRun == FALSE);
  372.          RxBytes = (USHORT)readed;
  373.          NextByte = RxBuffer;
  374.       }
  375.    }
  376. #elif defined(__LINUX__)
  377.    int i;
  378.  
  379.    if (hFile >= 0) {
  380.       if (RxBytes == 0) {
  381.          while ((i = read (hFile, RxBuffer, sizeof (RxBuffer))) == 0)
  382.             ;
  383.          RxBytes = (USHORT)i;
  384.          NextByte = RxBuffer;
  385.       }
  386.    }
  387. #else
  388.    if (hPort != NULL) {
  389.       if (RxBytes == 0) {
  390.          do {
  391.             ReadBuffer (hPort, (char *)RxBuffer, sizeof (RxBuffer));
  392.          } while (hPort->count <= 0 && EndRun == FALSE);
  393.          RxBytes = (USHORT)hPort->count;
  394.          NextByte = RxBuffer;
  395.       }
  396.    }
  397. #endif
  398.  
  399.    if (RxBytes > 0) {
  400.       c = *NextByte++;
  401.       RxBytes--;
  402.    }
  403.  
  404.    return (c);
  405. }
  406.  
  407. USHORT TSerial::ReadBytes (UCHAR *bytes, USHORT len)
  408. {
  409.    USHORT Max;
  410.  
  411. #if defined(__OS2__)
  412.    ULONG readed;
  413.  
  414.    if (hFile != NULLHANDLE) {
  415.       if (RxBytes == 0) {
  416.          do {
  417.             DosRead (hFile, (PVOID)RxBuffer, sizeof (RxBuffer), &readed);
  418.          } while (readed == 0L && EndRun == FALSE);
  419.          RxBytes = (USHORT)readed;
  420.          NextByte = RxBuffer;
  421.       }
  422.    }
  423. #elif defined(__NT__)
  424.    ULONG readed;
  425.  
  426.    if (hFile != INVALID_HANDLE_VALUE) {
  427.       if (RxBytes == 0) {
  428.          do {
  429.             ReadFile (hFile, (PVOID)RxBuffer, sizeof (RxBuffer), &readed, NULL);
  430.          } while (readed == 0L && EndRun == FALSE);
  431.          RxBytes = (USHORT)readed;
  432.          NextByte = RxBuffer;
  433.       }
  434.    }
  435. #elif defined(__LINUX__)
  436.    int i;
  437.  
  438.    if (hFile >= 0) {
  439.       if (RxBytes == 0) {
  440.          while ((i = read (hFile, RxBuffer, sizeof (RxBuffer))) == 0)
  441.             ;
  442.          RxBytes = (USHORT)i;
  443.          NextByte = RxBuffer;
  444.       }
  445.    }
  446. #else
  447.    if (hPort != NULL) {
  448.       if (RxBytes == 0) {
  449.          do {
  450.             ReadBuffer (hPort, (char *)RxBuffer, sizeof (RxBuffer));
  451.          } while (hPort->count <= 0 && EndRun == FALSE);
  452.          RxBytes = (USHORT)hPort->count;
  453.          NextByte = RxBuffer;
  454.       }
  455.    }
  456. #endif
  457.  
  458.    if (RxBytes > 0) {
  459.       if ((Max = len) > RxBytes)
  460.          Max = RxBytes;
  461.       memcpy (bytes, NextByte, Max);
  462.       RxBytes -= Max;
  463.       NextByte += Max;
  464.    }
  465.  
  466.    return (Max);
  467. }
  468.  
  469. VOID TSerial::SetDTR (USHORT fStatus)
  470. {
  471. #if defined(__OS2__)
  472.    MODEMSTATUS ms;
  473.    UINT data;
  474.  
  475.    if (hFile != NULLHANDLE) {
  476.       if (fStatus == TRUE) {
  477.          ms.fbModemOn = 0x01;
  478.          ms.fbModemOff = 0xFF;
  479.       }
  480.       else {
  481.          ms.fbModemOn = 0x00;
  482.          ms.fbModemOff = 0xFE;
  483.       }
  484.       DevIOCtl ((VOID *)&data, sizeof (data), (VOID *)&ms, sizeof (ms), ASYNC_SETMODEMCTRL, IOCTL_ASYNC, hFile);
  485.    }
  486. #elif defined(__NT__)
  487.    if (hFile != INVALID_HANDLE_VALUE)
  488.       EscapeCommFunction (hFile, (fStatus == TRUE) ? SETDTR : CLRDTR);
  489. #elif defined(__LINUX__)
  490.    int mcs;
  491.  
  492.    ioctl (hFile, TIOCMGET, &mcs);
  493.    if (fStatus == FALSE)
  494.       mcs &= ~TIOCM_DTR;
  495.    else
  496.       mcs |= TIOCM_DTR;
  497.    ioctl (hFile, TIOCMSET, &mcs);
  498. #else
  499.    if (hPort != NULL)
  500.       SetDtr (hPort, (fStatus == TRUE) ? ON : OFF);
  501. #endif
  502. }
  503.  
  504. VOID TSerial::SetRTS (USHORT fStatus)
  505. {
  506. #if defined(__OS2__)
  507.    MODEMSTATUS ms;
  508.    UINT data;
  509.  
  510.    if (hFile != NULLHANDLE) {
  511.       if (fStatus == TRUE) {
  512.          ms.fbModemOn = 0x02;
  513.          ms.fbModemOff = 0xFF;
  514.       }
  515.       else {
  516.          ms.fbModemOn = 0x00;
  517.          ms.fbModemOff = 0xFD;
  518.       }
  519.       DevIOCtl ((VOID *)&data, sizeof (data), (VOID *)&ms, sizeof (ms), ASYNC_SETMODEMCTRL, IOCTL_ASYNC, hFile);
  520.    }
  521. #elif defined(__NT__)
  522.    if (hFile != INVALID_HANDLE_VALUE)
  523.       EscapeCommFunction (hFile, (fStatus == TRUE) ? SETRTS : CLRRTS);
  524. #elif defined(__LINUX__)
  525.    int mcs;
  526.  
  527.    ioctl (hFile, TIOCMGET, &mcs);
  528.    if (fStatus == FALSE)
  529.       mcs &= ~TIOCM_RTS;
  530.    else
  531.       mcs |= TIOCM_RTS;
  532.    ioctl (hFile, TIOCMSET, &mcs);
  533. #else
  534.    if (hPort != NULL)
  535.       SetRts (hPort, (fStatus == TRUE) ? ON : OFF);
  536. #endif
  537. }
  538.  
  539. VOID TSerial::SetParameters (ULONG ulSpeed, USHORT nData, UCHAR nParity, USHORT nStop)
  540. {
  541. #if defined(__OS2__)
  542.    ULONG Param[2];
  543.    LINECONTROL lc;
  544.  
  545.    if (hFile != NULLHANDLE) {
  546.       Param[0] = ulSpeed;
  547.       Param[1] = 0L;
  548.       DevIOCtl (NULL, 0, (VOID *)&Param[0], sizeof (ULONG) + 1, ASYNC_EXTSETBAUDRATE, IOCTL_ASYNC, hFile);
  549.  
  550.       lc.bDataBits = (BYTE)nData;
  551.       lc.bStopBits = (BYTE)nStop;
  552.       lc.bParity = (BYTE)nParity;
  553.  
  554.       DevIOCtl (NULL, 0, (VOID *)&lc, sizeof (LINECONTROL), ASYNC_SETLINECTRL, IOCTL_ASYNC, hFile);
  555.    }
  556. #elif defined(__NT__)
  557.    DCB dcb;
  558.  
  559.    if (hFile != INVALID_HANDLE_VALUE) {
  560.       dcb.DCBlength = sizeof (DCB);
  561.       GetCommState (hFile, &dcb);
  562.       dcb.BaudRate = ulSpeed;
  563.       dcb.ByteSize = (BYTE)nData;
  564.       if (nParity == 'N')
  565.          dcb.Parity = NOPARITY;
  566.       else if (nParity == 'E')
  567.          dcb.Parity = EVENPARITY;
  568.       else if (nParity == 'O')
  569.          dcb.Parity = ODDPARITY;
  570.       else if (nParity == 'M')
  571.          dcb.Parity = MARKPARITY;
  572.       if (nStop == 1)
  573.          dcb.StopBits = ONESTOPBIT;
  574.       dcb.fBinary = TRUE;
  575.       dcb.fOutxCtsFlow = TRUE;
  576.       dcb.fOutxDsrFlow = FALSE;
  577.       dcb.fDtrControl = DTR_CONTROL_ENABLE;
  578.       dcb.fRtsControl = RTS_CONTROL_ENABLE;
  579.       SetCommState (hFile, &dcb);
  580.    }
  581. #elif defined(__LINUX__)
  582.    speed_t speed;
  583.  
  584.    tcgetattr (hFile, &tty);
  585.  
  586.    switch (ulSpeed) {
  587.       case 300:
  588.          speed = B300;
  589.          break;
  590.       case 1200:
  591.          speed = B1200;
  592.          break;
  593.       case 2400:
  594.          speed = B2400;
  595.          break;
  596.       case 4800:
  597.          speed = B4800;
  598.          break;
  599.       case 9600:
  600.          speed = B9600;
  601.          break;
  602.       case 19200:
  603.          speed = B19200;
  604.          break;
  605.       case 38400:
  606.          speed = B38400;
  607.          break;
  608.       case 57600L:
  609.          speed = B57600;
  610.          break;
  611.       case 115200L:
  612.          speed = B115200;
  613.          break;
  614.    }
  615.  
  616.    cfsetospeed (&tty, speed);
  617.    cfsetispeed (&tty, speed);
  618.  
  619.    tty.c_cflag &= ~(CSIZE);
  620.    if (nData == 5)
  621.       tty.c_cflag |= CS5;
  622.    else if (nData == 6)
  623.       tty.c_cflag |= CS6;
  624.    else if (nData == 7)
  625.       tty.c_cflag |= CS7;
  626.    else
  627.       tty.c_cflag |= CS8;
  628.  
  629.    tty.c_cflag |= CRTSCTS;
  630.    tty.c_cflag |= IXON;
  631.  
  632.    tty.c_cflag &= ~(PARENB|PARODD);
  633.    if (nParity == 'E')
  634.       tty.c_cflag |= PARENB;
  635.    else if (nParity == 'O')
  636.       tty.c_cflag |= PARODD;
  637.  
  638.    tcsetattr (hFile, TCSANOW, &tty);
  639. #else
  640.    if (hPort != NULL)
  641.       PortSet (hPort, ulSpeed, nParity, nData, nStop);
  642. #endif
  643. }
  644.  
  645. VOID TSerial::SendByte (UCHAR byte)
  646. {
  647. #if defined(__OS2__)
  648.    ULONG written;
  649.  
  650.    if (hFile != NULLHANDLE) {
  651.       do {
  652.          DosWrite (hFile, (PVOID)&byte, 1L, &written);
  653.       } while (written != 1L && EndRun == FALSE && Carrier () == TRUE);
  654.    }
  655. #elif defined(__NT__)
  656.    ULONG written;
  657.  
  658.    if (hFile != INVALID_HANDLE_VALUE && EndRun == FALSE)
  659.       do {
  660.          WriteFile (hFile, (LPCVOID)&byte, 1L, &written, NULL);
  661.       } while (written != 1L && EndRun == FALSE && Carrier () == TRUE);
  662. #elif defined(__LINUX__)
  663.    while (write (hFile, &byte, 1) != 1)
  664.       ;
  665. #else
  666.    int retval;
  667.  
  668.    if (hPort != NULL) {
  669.       do {
  670.          retval = WriteChar (hPort, byte);
  671.       } while (retval != ASSUCCESS && hPort->status == ASBUFRFULL && Carrier () == TRUE);
  672.    }
  673. #endif
  674. }
  675.  
  676. VOID TSerial::SendBytes (UCHAR *bytes, USHORT len)
  677. {
  678. #if defined(__OS2__)
  679.    ULONG written;
  680.  
  681.    if (hFile != NULLHANDLE) {
  682.       do {
  683.          DosWrite (hFile, (PVOID)bytes, (long)len, &written);
  684.          bytes += written;
  685.          len -= (USHORT)written;
  686.       } while (len > 0 && EndRun == FALSE && Carrier () == TRUE);
  687.    }
  688. #elif defined(__NT__)
  689.    ULONG written;
  690.  
  691.    if (hFile != INVALID_HANDLE_VALUE && EndRun == FALSE)
  692.       do {
  693.          WriteFile (hFile, (LPCVOID)bytes, (long)len, &written, NULL);
  694.          bytes += written;
  695.          len -= (USHORT)written;
  696.       } while (len > 0 && EndRun == FALSE && Carrier () == TRUE);
  697. #elif defined(__LINUX__)
  698.    write (hFile, bytes, len);
  699. #else
  700.    int retval;
  701.  
  702.    if (hPort != NULL) {
  703.       do {
  704.          retval = WriteBuffer (hPort, (char *)bytes, len);
  705.          bytes += hPort->count;
  706.          len -= hPort->count;
  707.       } while (retval != ASSUCCESS && hPort->status == ASBUFRFULL && Carrier () == TRUE);
  708.    }
  709. #endif
  710. }
  711.  
  712. VOID TSerial::UnbufferBytes (VOID)
  713. {
  714. #if defined(__OS2__)
  715.    ULONG Written;
  716.    UCHAR *p;
  717.  
  718.    while (hFile != NULLHANDLE && TxBytes > 0 && EndRun == FALSE) {
  719.       p = TxBuffer;
  720.       do {
  721.          Written = 0L;
  722.          DosWrite (hFile, (PVOID)p, TxBytes, &Written);
  723.          p += Written;
  724.          TxBytes -= (USHORT)Written;
  725.       } while (TxBytes > 0 && EndRun == FALSE && Carrier () == TRUE);
  726.       if (TxBytes > 0 && Written != 0L)
  727.          memcpy (TxBuffer, p, TxBytes);
  728.    }
  729. #elif defined(__NT__)
  730.    ULONG written;
  731.    UCHAR *p;
  732.  
  733.    while (hFile != INVALID_HANDLE_VALUE && EndRun == FALSE && TxBytes > 0) {
  734.       p = TxBuffer;
  735.       do {
  736.          WriteFile (hFile, (LPCVOID)p, (long)TxBytes, &written, NULL);
  737.          p += written;
  738.          TxBytes -= (USHORT)written;
  739.       } while (TxBytes > 0 && EndRun == FALSE && Carrier () == TRUE);
  740.    }
  741. #elif defined(__LINUX__)
  742.    int i;
  743.  
  744.    if (TxBytes > 0) {
  745.       fcntl (hFile, F_SETFL, 0);
  746.       do {
  747.          i = write (hFile, TxBuffer, TxBytes);
  748.          TxBytes -= (USHORT)i;
  749.       } while (TxBytes > 0);
  750.       fcntl (hFile, F_SETFL, O_NONBLOCK);
  751.    }
  752. #else
  753.    char *p;
  754.  
  755.    while (hPort != NULL && TxBytes > 0) {
  756.       p = (char *)TxBuffer;
  757.       do {
  758.          WriteBuffer (hPort, p, TxBytes);
  759.          p += hPort->count;
  760.          TxBytes -= hPort->count;
  761.       } while (TxBytes > 0 && EndRun == FALSE && Carrier () == TRUE);
  762.    }
  763. #endif
  764. }
  765.  
  766. VOID TSerial::SetName (PSZ name)
  767. {
  768.    name = name;
  769. }
  770.  
  771. VOID TSerial::SetCity (PSZ name)
  772. {
  773.    name = name;
  774. }
  775.  
  776. VOID TSerial::SetLevel (PSZ level)
  777. {
  778.    level = level;
  779. }
  780.  
  781. VOID TSerial::SetTimeLeft (ULONG seconds)
  782. {
  783.    seconds = seconds;
  784. }
  785.  
  786. VOID TSerial::SetTime (ULONG seconds)
  787. {
  788.    seconds = seconds;
  789. }
  790.  
  791.  
  792.