Tserial_event.cpp
上传用户:shoubosun
上传日期:2015-09-13
资源大小:1801k
文件大小:29k
源码类别:

驱动编程

开发平台:

Visual C++

  1. /* ------------------------------------------------------------------------ --
  2. --                                                                          --
  3. --                        PC serial port connection object                  --
  4. --                           for  event-driven programs                     --
  5. --                                                                          --
  6. --                                                                          --
  7. m                         --
  8. --                                                                          --
  9. --                                                                          --
  10. --                                                                          --
  11. -- ------------------------------------------------------------------------ --
  12. --                                                                          --
  13. --  Filename : Tserial_event.cpp                                            --
  14. --  Author   : Wang Bin                                            --
  15. --  Created  : 3rd Aug 2007                                           --
  16. --  Modified :                                             --
  17. --  Plateform: Windows 95, 98, NT, 2000 (Win32)                             --
  18. -- ------------------------------------------------------------------------ --
  19. -- ------------------------------------------------------------------------ --
  20. --                                                                          --
  21. --    Note to Visual C++ users:  Don't forget to compile with the           --
  22. --     "Multithreaded" option in your project settings                      --
  23. --                                                                          --
  24. --         See   Project settings                                           --
  25. --                   |                                                      --
  26. --                   *--- C/C++                                             --
  27. --                          |                                               --
  28. --                          *--- Code generation                            --
  29. --                                       |                                  --
  30. --                                       *---- Use run-time library         --
  31. --                                                     |                    --
  32. --                                                     *---- Multithreaded  --
  33. --                                                                          --
  34. --                                                                          --
  35. --                                                                          --
  36. -- ------------------------------------------------------------------------ */
  37. /* ---------------------------------------------------------------------- */
  38. #define STRICT
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <process.h>
  43. #include <conio.h>
  44. #include <windows.h>
  45. #include "Tserial_event.h"
  46. #define SIG_POWER_DOWN     0
  47. #define SIG_READER         1
  48. #define SIG_READ_DONE      2    // data received has been read
  49. #define SIG_WRITER         3
  50. #define SIG_DATA_TO_TX     4    // data waiting to be sent
  51. #define SIG_MODEM_EVENTS   5
  52. #define SIG_MODEM_CHECKED  6
  53. void Tserial_event_thread_start(void *arg);
  54. typedef unsigned (WINAPI *PBEGINTHREADEX_THREADFUNC) (LPVOID lpThreadParameter);
  55. typedef unsigned *PBEGINTHREADEX_THREADID;
  56. /* ---------------------------------------------------------------------- */
  57. /* ---------------------  Tserial_event_thread_start  ------------------- */
  58. /* ---------------------------------------------------------------------- */
  59. /**
  60.     This function is not part of the Tserial_event object. It is simply used
  61.     to start the thread from an external point of the object.
  62. */
  63. void Tserial_event_thread_start(void *arg)
  64. {
  65.     class Tserial_event *serial_unit;
  66.     serial_unit = (Tserial_event *) arg;
  67.     
  68.     if (serial_unit!=0)
  69.         serial_unit->run();
  70. }
  71. /* -------------------------------------------------------------------- */
  72. /* -------------------------    Tserial_event ------------------------- */
  73. /* -------------------------------------------------------------------- */
  74. Tserial_event::Tserial_event()
  75. {
  76.     int i;
  77.     ready            = false;
  78.     parityMode       = SERIAL_PARITY_NONE;
  79.     port[0]          = 0;
  80.     rate             = 0;
  81.     threadid         = 0;
  82.     serial_handle    = INVALID_HANDLE_VALUE;
  83.     tx_in_progress   = 0;
  84.     rx_in_progress   = 0;
  85.     max_rx_size      = 1;
  86.     tx_size          = 0;
  87.     received_size    = 0;
  88.     check_modem      = false;
  89.     manager          = 0;
  90.     /* -------------------------------------------------------------- */
  91.     // creating Events for the different sources
  92.     for (i=0; i<SERIAL_SIGNAL_NBR; i++)
  93.     {
  94.         if ((i==SIG_READER) || (i==SIG_WRITER) || (i==SIG_MODEM_EVENTS))
  95.             serial_events[i] = CreateEvent(NULL, TRUE, FALSE, NULL);  // Manual Reset
  96.         else
  97.             serial_events[i] = CreateEvent(NULL, FALSE, FALSE, NULL); // Auto reset
  98.     }
  99. }
  100. /* -------------------------------------------------------------------- */
  101. /* --------------------------    ~Tserial_event ----------------------- */
  102. /* -------------------------------------------------------------------- */
  103. Tserial_event::~Tserial_event()
  104. {
  105.     int i;
  106.     /* -------------------------------------------------------- */
  107.     for (i=0; i<SERIAL_SIGNAL_NBR; i++)         // deleting the events
  108.     {
  109.         if (serial_events[i]!=INVALID_HANDLE_VALUE)
  110.             CloseHandle(serial_events[i]);
  111.         serial_events[i] = INVALID_HANDLE_VALUE;
  112.     }
  113.     if (serial_handle!=INVALID_HANDLE_VALUE)
  114.         CloseHandle(serial_handle);
  115.     serial_handle = INVALID_HANDLE_VALUE;
  116. }
  117. /* -------------------------------------------------------------------- */
  118. /* --------------------------    disconnect   ------------------------- */
  119. /* -------------------------------------------------------------------- */
  120. void Tserial_event::disconnect(void)
  121. {
  122.     ready = false;
  123.     SetEvent(serial_events[SIG_POWER_DOWN]);
  124. }
  125. /* -------------------------------------------------------------------- */
  126. /* --------------------------    connect      ------------------------- */
  127. /* -------------------------------------------------------------------- */
  128. /**
  129.      Serial port, file and overlapped structures initialization
  130. */
  131. int  Tserial_event::connect (char *port_arg, int  rate_arg,  int parity_arg,
  132.                              char ByteSize , bool modem_events)
  133. {
  134.     int  erreur;
  135.     DCB  dcb;
  136.     int  i;
  137.     COMMTIMEOUTS cto = { 0, 0, 0, 0, 0 };
  138.     /* --------------------------------------------- */
  139.     if (serial_handle!=INVALID_HANDLE_VALUE)
  140.         CloseHandle(serial_handle);
  141.     serial_handle = INVALID_HANDLE_VALUE;
  142.     if (port_arg!=0)
  143.     {
  144.         strncpy(port, port_arg, 10);
  145.         rate        = rate_arg;
  146.         parityMode  = parity_arg;
  147.         check_modem = modem_events;
  148.         erreur      = 0;
  149.         ZeroMemory(&ovReader   ,sizeof(ovReader)   );  // clearing the overlapped
  150.         ZeroMemory(&ovWriter   ,sizeof(ovWriter)   );
  151.         ZeroMemory(&ovWaitEvent,sizeof(ovWaitEvent));
  152.         memset(&dcb,0,sizeof(dcb));
  153.         /* -------------------------------------------------------------------- */
  154.         // set DCB to configure the serial port
  155.         dcb.DCBlength       = sizeof(dcb);                   
  156.         
  157.         /* ---------- Serial Port Config ------- */
  158.         dcb.BaudRate        = rate;
  159.         switch(parityMode)
  160.         {
  161.             case SERIAL_PARITY_NONE:
  162.                             dcb.Parity      = NOPARITY;
  163.                             dcb.fParity     = 0;
  164.                             break;
  165.             case SERIAL_PARITY_EVEN:
  166.                             dcb.Parity      = EVENPARITY;
  167.                             dcb.fParity     = 1;
  168.                             break;
  169.             case SERIAL_PARITY_ODD:
  170.                             dcb.Parity      = ODDPARITY;
  171.                             dcb.fParity     = 1;
  172.                             break;
  173.         }
  174.         dcb.StopBits        = ONESTOPBIT;
  175.         dcb.ByteSize        = (BYTE) ByteSize;
  176.         dcb.fOutxCtsFlow    = 0;
  177.         dcb.fOutxDsrFlow    = 0;
  178.         dcb.fDtrControl     = DTR_CONTROL_DISABLE;
  179.         dcb.fDsrSensitivity = 0;
  180.         dcb.fRtsControl     = RTS_CONTROL_DISABLE;
  181.         dcb.fOutX           = 0;
  182.         dcb.fInX            = 0;
  183.         
  184.         /* ----------------- misc parameters ----- */
  185.         dcb.fErrorChar      = 0;
  186.         dcb.fBinary         = 1;
  187.         dcb.fNull           = 0;
  188.         dcb.fAbortOnError   = 0;
  189.         dcb.wReserved       = 0;
  190.         dcb.XonLim          = 2;
  191.         dcb.XoffLim         = 4;
  192.         dcb.XonChar         = 0x13;
  193.         dcb.XoffChar        = 0x19;
  194.         dcb.EvtChar         = 0;
  195.         
  196.         /* -------------------------------------------------------------------- */
  197.         serial_handle    = CreateFile(port, GENERIC_READ | GENERIC_WRITE,
  198.                    0, NULL, OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
  199.                    // opening serial port
  200.         ovReader.hEvent    = serial_events[SIG_READER];
  201.         ovWriter.hEvent    = serial_events[SIG_WRITER];
  202.         ovWaitEvent.hEvent = serial_events[SIG_MODEM_EVENTS];
  203.         if (serial_handle    != INVALID_HANDLE_VALUE)
  204.         {
  205.             if (check_modem)
  206.             {
  207.                 if(!SetCommMask(serial_handle, EV_RING | EV_RLSD))
  208.                     erreur = 1;
  209.             }
  210.             else
  211.             {
  212.                 if(!SetCommMask(serial_handle, 0))
  213.                     erreur = 1;
  214.             }
  215.                 
  216.             // set timeouts
  217.             if(!SetCommTimeouts(serial_handle,&cto))
  218.                 erreur = 2;
  219.             // set DCB
  220.             if(!SetCommState(serial_handle,&dcb))
  221.                 erreur = 4;
  222.         }
  223.         else
  224.             erreur = 8;
  225.     }
  226.     else
  227.         erreur = 16;
  228.     /* --------------------------------------------- */
  229.     for (i=0; i<SERIAL_SIGNAL_NBR; i++)
  230.     {
  231.         if (serial_events[i]==INVALID_HANDLE_VALUE)
  232.             erreur = 32;
  233.     }
  234.     /* --------------------------------------------- */
  235.     if (erreur!=0)
  236.     {
  237.         CloseHandle(serial_handle);
  238.         serial_handle = INVALID_HANDLE_VALUE;
  239.     }
  240.     else
  241.     {
  242.         // start thread
  243.         _beginthreadex(NULL,0,(PBEGINTHREADEX_THREADFUNC) Tserial_event_thread_start,
  244.                   (void *) this, 0, &threadid);
  245.     }
  246.     /* --------------------------------------------- */
  247.     return(erreur);
  248. }
  249. /* -------------------------------------------------------------------- */
  250. /* ---------------------           setManager     --------------------- */
  251. /* -------------------------------------------------------------------- */
  252. void         Tserial_event::setManager(type_myCallBack manager_arg)
  253. {
  254.         manager = manager_arg;
  255. }
  256. /* -------------------------------------------------------------------- */
  257. /* ---------------------           setRxSize      --------------------- */
  258. /* -------------------------------------------------------------------- */
  259. void         Tserial_event::setRxSize(int size)
  260. {
  261.         max_rx_size = size;
  262.         if (max_rx_size>SERIAL_MAX_RX)
  263.             max_rx_size = SERIAL_MAX_RX;
  264. }
  265. /* -------------------------------------------------------------------- */
  266. /* ---------------------           setManager     --------------------- */
  267. /* -------------------------------------------------------------------- */
  268. char *      Tserial_event::getDataInBuffer(void)
  269. {
  270.     return(rxBuffer);
  271. }
  272. /* -------------------------------------------------------------------- */
  273. /* ---------------------           setManager     --------------------- */
  274. /* -------------------------------------------------------------------- */
  275. int      Tserial_event::getDataInSize(void)
  276. {
  277.     return(received_size);
  278. }
  279. /* -------------------------------------------------------------------- */
  280. /* ---------------------           setManager     --------------------- */
  281. /* -------------------------------------------------------------------- */
  282. void   Tserial_event::dataHasBeenRead(void)
  283. {
  284.     SetEvent(serial_events[SIG_READ_DONE]);
  285. }
  286. /* -------------------------------------------------------------------- */
  287. /* -----------------------   getNbrOfBytes  --------------------------- */
  288. /* -------------------------------------------------------------------- */
  289. int Tserial_event::getNbrOfBytes    (void)
  290. {
  291.     struct _COMSTAT status;
  292.     int             n;
  293.     unsigned long   etat;
  294.     n = 0;
  295.     if (serial_handle!=INVALID_HANDLE_VALUE)
  296.     {
  297.         ClearCommError(serial_handle, &etat, &status);
  298.         n = status.cbInQue;
  299.     }
  300.     return(n);
  301. }
  302. /* -------------------------------------------------------------------- */
  303. /* --------------------------    sendData     ------------------------- */
  304. /* -------------------------------------------------------------------- */
  305. void Tserial_event::sendData (char *buffer, int size)
  306. {
  307.     if ((!tx_in_progress) && (size<SERIAL_MAX_TX) && (buffer!=0))
  308.     {
  309.         tx_in_progress = 1;
  310.         memcpy(txBuffer, buffer, size);
  311.         tx_size = size;
  312.         SetEvent(serial_events[SIG_DATA_TO_TX]);
  313.         // indicating data to be sent
  314.     }
  315. }
  316. /* -------------------------------------------------------------------- */
  317. /* --------------------------    OnEvent      ------------------------- */
  318. /* -------------------------------------------------------------------- */
  319. void Tserial_event::OnEvent (unsigned long events)
  320. {
  321.     unsigned long ModemStat;
  322.     GetCommModemStatus(serial_handle, &ModemStat);
  323.     if ((events & EV_RING)!=0)
  324.     {
  325.         if ((ModemStat &  MS_RING_ON)!= 0)
  326.         {
  327.             if (manager!=0)
  328.                 manager((uint32) this, SERIAL_RING);
  329.         }
  330.     }
  331.     if ((events & EV_RLSD)!=0)
  332.     {
  333.         if ((ModemStat &  MS_RLSD_ON)!= 0)
  334.         {
  335.             if (manager!=0)
  336.                 manager((uint32) this, SERIAL_CD_ON);
  337.         }
  338.         else
  339.         {
  340.             if (manager!=0)
  341.                 manager((uint32) this, SERIAL_CD_OFF);
  342.         }
  343.     }
  344. }
  345. /* -------------------------------------------------------------------- */
  346. /* --------------------------       run       ------------------------- */
  347. /* -------------------------------------------------------------------- */
  348. /**
  349.  this function is the main loop of the Tserial object. There is a
  350.  do while() loop executed until either an error or a PowerDown is 
  351.  received.
  352.  this is not a busy wait since we use the WaitForMultipleObject function
  353. */
  354. /* * /
  355. #define DEBUG_EVENTS
  356. /* */
  357. void Tserial_event::run(void)
  358. {
  359.     bool          done;
  360.     long          status;
  361.     unsigned long read_nbr, result_nbr;
  362.     char          success;
  363.     ready                   = true;
  364.     done                    = false;
  365.     tx_in_progress          = 0;
  366.     rx_in_progress          = 0;
  367.     WaitCommEventInProgress = 0;
  368.     if (manager!=0)
  369.         manager((uint32) this, SERIAL_CONNECTED);
  370.     GetLastError();               // just to clear any pending error
  371.     SetEvent(serial_events[SIG_READ_DONE]);
  372.     if (check_modem)
  373.         SetEvent(serial_events[SIG_MODEM_CHECKED]);
  374.     /* ----------------------------------------------------------- */
  375.     while(!done)
  376.     {
  377.         /* ------------------------------------------------------------------ */
  378.         /*                                                                    */
  379.         /*                                                                    */
  380.         /*                                                                    */
  381.         /*                          Waiting  for signals                      */
  382.         /*                                                                    */
  383.         /*                                                                    */
  384.         /*                                                                    */
  385.         /* ------------------------------------------------------------------ */
  386.         // Main wait function. Waiting for something to happen.
  387.         // This may be either the completion of a Read or a Write or
  388.         // the reception of modem events, Power Down, new Tx
  389.         //
  390.         status = WaitForMultipleObjects(SERIAL_SIGNAL_NBR, serial_events,
  391.                                         FALSE, INFINITE);
  392.         // processing answer to filter other failures
  393.         status = status - WAIT_OBJECT_0;
  394.         if ((status<0) || (status>=SERIAL_SIGNAL_NBR))
  395.             done=true;   // error
  396.         else
  397.         {
  398.             /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  399.             /* ++++++++++++++++++++ EVENT DISPATCHER ++++++++++++++++++ */
  400.             /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  401.             switch(status)
  402.             {
  403.                 /* ######################################################## */
  404.                 case SIG_POWER_DOWN:
  405.                     // receiving a POWER down signal. Stopping the thread
  406.                     done = true;
  407.                     break;
  408.                 /* ######################################################## */
  409.                 /* #                                                      # */
  410.                 /* #                                                      # */
  411.                 /* #                       RX                             # */
  412.                 /* #                                                      # */
  413.                 /* #                                                      # */
  414.                 /* ######################################################## */
  415.                 case SIG_READ_DONE:
  416.                     // previous reading is finished
  417.                     // I start a new one here
  418.                     if (!rx_in_progress)
  419.                     {
  420.                         // locking reading
  421.                         rx_in_progress = 1;
  422.                         // starting a new read
  423.                         success = (char) ReadFile(serial_handle,&rxBuffer,
  424.                                          max_rx_size,&read_nbr,&ovReader);
  425.                         if (!success)
  426.                         {
  427.                             // failure
  428.                             if(GetLastError() != ERROR_IO_PENDING )
  429.                             {
  430.                                 // real failure => quiting
  431.                                 done = true;
  432.                                 #ifdef DEBUG_EVENTS
  433.                                 printf("Readfile error (not pending)n");
  434.                                 #endif DEBUG_EVENTS
  435.                             }
  436.                             #ifdef DEBUG_EVENTS
  437.                             else
  438.                                 printf("ReadFile pendingn");
  439.                             #endif DEBUG_EVENTS
  440.                         }
  441.                         #ifdef DEBUG_EVENTS
  442.                         else
  443.                         {
  444.                             // I make nothing here since the overlapped
  445.                             // will be signaled anyway, so I'll make
  446.                             // the processing there
  447.                             printf("ReadFile immediate successn");
  448.                         }
  449.                         #endif
  450.                     }
  451.                     break;
  452.                 /* ######################################################## */
  453.                 case SIG_READER:
  454.                     // reading the result of the terminated read
  455.                     //BOOL GetOverlappedResult(
  456.                     //    HANDLE hFile, // handle of file, pipe, or communications device
  457.                     //    LPOVERLAPPED lpOverlapped, // address of overlapped structure
  458.                     //    LPDWORD lpNumberOfBytesTransferred, // address of actual bytes count
  459.                     //    BOOL bWait  // wait flag
  460.                     //   );
  461.                     //
  462.                     if (GetOverlappedResult(serial_handle, &ovReader,
  463.                         &result_nbr, FALSE))
  464.                     {
  465.                         #ifdef DEBUG_EVENTS
  466.                             printf("ReadFile => GetOverlappedResult donen");
  467.                         #endif DEBUG_EVENTS
  468.                         // no error => OK
  469.                         // Read operation completed successfully
  470.                         ResetEvent(serial_events[SIG_READER]);
  471.                         // Write operation completed successfully
  472.                         received_size  = result_nbr;
  473.                         rx_in_progress = 0; // read has ended
  474.                         // if incoming data, I process them
  475.                         if ((result_nbr!=0) &&(manager!=0))
  476.                             manager((uint32) this, SERIAL_DATA_ARRIVAL);
  477.                         // I automatically restart a new read once the
  478.                         // previous is completed.
  479.                         SetEvent(serial_events[SIG_READ_DONE]);
  480.                     }
  481.                     else
  482.                     {
  483.                         // GetOverlapped didn't succeed !
  484.                         // What's the reason ?
  485.                         if(GetLastError()!= ERROR_IO_PENDING )
  486.                             done = 1;  // failure
  487.                     }
  488.                     break;
  489.                 /* ######################################################## */
  490.                 /* #                                                      # */
  491.                 /* #                                                      # */
  492.                 /* #                       TX                             # */
  493.                 /* #                                                      # */
  494.                 /* #                                                      # */
  495.                 /* ######################################################## */
  496.                 case SIG_DATA_TO_TX:
  497.                     // Signal asserted that there is a new valid message
  498.                     // in the "txBuffer" variable
  499.                     // sending data to the port
  500.                     success = (char) WriteFile(serial_handle, txBuffer, tx_size,
  501.                                         &result_nbr, &ovWriter);
  502.                         if (!success)
  503.                         {
  504.                             // ouups, failure
  505.                             if(GetLastError() != ERROR_IO_PENDING )
  506.                             {
  507.                                 // real failure => quiting
  508.                                 done = true;
  509.                                 #ifdef DEBUG_EVENTS
  510.                                 printf("WriteFile error (not pending)n");
  511.                                 #endif DEBUG_EVENTS
  512.                             }
  513.                             #ifdef DEBUG_EVENTS
  514.                             else
  515.                                 printf("WriteFile pendingn");
  516.                             #endif DEBUG_EVENTS
  517.                         }
  518.                         #ifdef DEBUG_EVENTS
  519.                         else
  520.                         {
  521.                             // I make nothing here since the overlapped
  522.                             // will be signaled anyway, so I'll make
  523.                             // the processing there
  524.                             printf("WriteFile immediate successn");
  525.                         }
  526.                         #endif
  527.                     break;
  528.                 /* ######################################################## */
  529.                 case SIG_WRITER:
  530.                     // WriteFile has terminated
  531.                     // checking the result of the operation
  532.                     if (GetOverlappedResult(serial_handle, &ovWriter,
  533.                         &result_nbr, FALSE))
  534.                     {
  535.                         // Write operation completed successfully
  536.                         ResetEvent(serial_events[SIG_WRITER]);
  537.                         // further write are now allowed
  538.                         tx_in_progress = 0;
  539.                         // telling it to the manager
  540.                         if (manager!=0)
  541.                             manager((uint32) this, SERIAL_DATA_SENT);
  542.                     }
  543.                     else
  544.                     {
  545.                         // GetOverlapped didn't succeed !
  546.                         // What's the reason ?
  547.                         if(GetLastError() != ERROR_IO_PENDING )
  548.                             done = 1;  // failure
  549.                     }
  550.                     break;
  551.                 /* ######################################################## */
  552.                 /* #                                                      # */
  553.                 /* #                                                      # */
  554.                 /* #                    MODEM_EVENTS EVENTS                      # */
  555.                 /* #                                                      # */
  556.                 /* #                                                      # */
  557.                 /* ######################################################## */
  558.                 case SIG_MODEM_CHECKED:
  559.                     if ((!WaitCommEventInProgress) && check_modem)
  560.                     // if no wait is in progress I start a new one
  561.                     {            
  562.                         WaitCommEventInProgress=1;
  563.                         success = (char) WaitCommEvent(serial_handle,&dwCommEvent,
  564.                                                        &ovWaitEvent);
  565.                         // reading one byte only to have immediate answer on each byte
  566.                         if (!success)
  567.                         {
  568.                             // ouups, failure
  569.                             if(GetLastError() != ERROR_IO_PENDING )
  570.                             {
  571.                                 // real failure => quiting
  572.                                 done = true;
  573.                                 #ifdef DEBUG_EVENTS
  574.                                 printf("WaitCommEvent error (not pending)n");
  575.                                 #endif DEBUG_EVENTS
  576.                             }
  577.                             #ifdef DEBUG_EVENTS
  578.                             else
  579.                                 printf("WaitCommEvent pendingn");
  580.                             #endif DEBUG_EVENTS
  581.                         }
  582.                         #ifdef DEBUG_EVENTS
  583.                         else
  584.                         {
  585.                             // I make nothing here since the overlapped
  586.                             // will be signaled anyway, so I'll make
  587.                             // the processing there
  588.                             printf("WaitCommEvent immediate successn");
  589.                         }
  590.                         #endif
  591.                     }
  592.                     break;
  593.                 /* ######################################################## */
  594.                 case SIG_MODEM_EVENTS:
  595.                     // reading the result of the terminated wait
  596.                     if (GetOverlappedResult(serial_handle, &ovWaitEvent,
  597.                         &result_nbr, FALSE))
  598.                     {
  599.                         // Wait operation completed successfully
  600.                         ResetEvent(serial_events[SIG_MODEM_EVENTS]);
  601.                         WaitCommEventInProgress = 0;
  602.                         // if incoming data, I process them
  603.                         OnEvent(dwCommEvent);
  604.                         // automatically starting a new check
  605.                         SetEvent(serial_events[SIG_MODEM_CHECKED]);
  606.                     }
  607.                     else
  608.                     {
  609.                         // GetOverlapped didn't succeed !
  610.                         // What's the reason ?
  611.                         if(GetLastError() != ERROR_IO_PENDING )
  612.                             done = 1;  // failure
  613.                     }
  614.                     break;
  615.                 /* ######################################################## */
  616.             }
  617.             /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  618.             /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  619.             /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  620.         }
  621.     };
  622.     // --------------------- Disconnecting ----------------
  623.     ready = false;
  624.     if (serial_handle!=INVALID_HANDLE_VALUE)
  625.         CloseHandle(serial_handle);
  626.     serial_handle = INVALID_HANDLE_VALUE;
  627.     if (manager!=0)
  628.         manager((uint32) this, SERIAL_DISCONNECTED);
  629. }
  630. /* -------------------------------------------------------------------- */
  631. /* -------------------------------------------------------------------- */