Author Topic: Use Read instead OnData  (Read 20210 times)

gaorio

  • Guest
Use Read instead OnData
« on: November 09, 2012, 12:44:15 PM »
In my DLL event OnData stops after the main application window changes.

To run the processes use the function

void BlueSerial :: DoEvents (void)
{
     MSG msg;

     while (PeekMessage (& msg, NULL, 0, 0, PM_REMOVE))
     {
         TranslateMessage (& msg);
         DispatchMessage (& msg);
     }
}

you can directly read the receive buffer? Because the event OnData stops?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Use Read instead OnData
« Reply #1 on: November 09, 2012, 12:45:59 PM »
Hello,

Yes, WCL has Read methods. But in any case WCL requires windows message loop to work. However, Read method has message processing inside so shoudl work in your enveronment.

gaorio

  • Guest
Re: Use Read instead OnData
« Reply #2 on: November 09, 2012, 01:13:17 PM »
I use CwclClient, but I can not find the Read Method

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Use Read instead OnData
« Reply #3 on: November 09, 2012, 01:24:37 PM »
You have to use wclSyncClient. It is the same as wclClient except OnData event and Read method.

gaorio

  • Guest
Re: Use Read instead OnData
« Reply #4 on: November 12, 2012, 10:44:00 AM »
My problem is still present.

I think the main application message queue and eat my PeekMessage does not read the message read. The same procedures work well using the serial port (CreateFile)

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Use Read instead OnData
« Reply #5 on: November 12, 2012, 12:17:16 PM »
Hello,

Can you please send me a code which reproduces the issue so we can run it here and find where problem is.

gaorio

  • Guest
Re: Use Read instead OnData
« Reply #6 on: November 12, 2012, 12:46:43 PM »
Attached the class bluetooth

The function that checks data is

CommandStatus __FASTCALLCLASSFAST Chk_Answer(unsigned char command)
{
   unsigned short  buffer_len;
   short         n_car;
   unsigned char   car;
   CommandStatus   status;
   unsigned char    rx_bcc;
   unsigned char    n_bcc_car;
   unsigned long    tim;
   unsigned char    rx_status;
   unsigned char   buffer[280];
   short          rx_data_len;
   short          rx_buffer_ptr;

   // Inizializzazione attesa risposta
   SerialPort->StartTimer(RX_INTERCAR_TIMER, EXECUTE_CMD_MASTER_TIMEOUT);
   rx_status = RX_WAITSTX;
   do
   {

      MSG msg;
   
      while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
       {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
      }

        buffer_len = SerialPort->ReadData(buffer, usLenBufferRX);

      for(n_car=0; n_car<buffer_len; n_car++)
      {
         car = buffer[n_car];
         switch(rx_status)
         {
            case RX_WAITSTX:   if(car == STX)
                              rx_status = RX_WAITCMD;
               break;
            case RX_WAITCMD:   rx_buffer_ptr = 0;
                           if(rx_buffer_ptr == usLenBufferRX)
                              rx_status = RX_WAITSTX;
                           else
                           {
                              ucBufferRX[rx_buffer_ptr++] = car;
                              rx_status = RX_WAITCMDSTATUS;
                           }
               break;
            case RX_WAITCMDSTATUS:
                           if(rx_buffer_ptr == usLenBufferRX)
                              rx_status = RX_WAITSTX;
                           else
                           {
                              ucBufferRX[rx_buffer_ptr++] = car;
                              rx_status = RX_WAITLEN;
                              status = (CommandStatus)car;
                              if(status!=StatusCMD_DONE)
                              {
                                 rx_status = RX_WAITBCC;
                                 rx_data_len = 1;
                              }
                           }
               break;
            case RX_WAITLEN:    if(rx_buffer_ptr == usLenBufferRX)
                              rx_status = RX_WAITSTX;
                           else
                           {
                              ucBufferRX[rx_buffer_ptr++] = car;
                              rx_data_len = car;
                              if(rx_data_len)
                                 rx_status = RX_DATA;
                              else
                                 rx_status = RX_WAITBCC;
                           }
               break;
            case RX_DATA:       if(rx_buffer_ptr == usLenBufferRX)
                              rx_status = RX_WAITSTX;
                           else
                           {
                              ucBufferRX[rx_buffer_ptr++] = car;
                              rx_data_len--;
                              if(!rx_data_len)
                                 rx_status = RX_WAITBCC;
                           }
               break;
            case RX_WAITBCC:   rx_bcc = 0;
                           for(n_bcc_car=0; n_bcc_car<rx_buffer_ptr; n_bcc_car++)
                              rx_bcc += ucBufferRX[n_bcc_car];
                           if(car == rx_bcc)
                              rx_status = RX_WAITETX;
                           else
                              rx_status = RX_WAITSTX;
               break;
            case RX_WAITETX:   if(car == ETX)
                              rx_status = RX_END;
                           else
                              rx_status = RX_WAITSTX;
               break;
         }
      }
      if(buffer_len)
         SerialPort->StartTimer(RX_INTERCAR_TIMER, RX_INTERCAR_TIMEOUT);
        if(rx_status!=RX_END)
         tim=SerialPort->CheckTimer(RX_INTERCAR_TIMER);
        else
           tim = 1;
   }while(tim && rx_status!=RX_END);

   // time out
   if(rx_status!=RX_END)
      status = StatusCMD_ERROR;

   gEndAnswer = status;
   return (status);
}


[attachment deleted by admin]

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Use Read instead OnData
« Reply #7 on: November 12, 2012, 05:32:17 PM »
Hello,

What is SerialPort? ReadData? SetTimer? WCL does not have such methods.

gaorio

  • Guest
Re: Use Read instead OnData
« Reply #8 on: November 13, 2012, 11:34:48 AM »
attached to the previous message is the class "BlueSerial.zip" with metod ReadData, SetTimer ....

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Use Read instead OnData
« Reply #9 on: November 13, 2012, 03:23:59 PM »
Hello,

Ok, thank you. I am going to check that out.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Use Read instead OnData
« Reply #10 on: November 15, 2012, 11:49:38 AM »
Hello,

I have checked your code. If you would like to use Read method intead of OnData event then you do not need message loop. But I do recomend to use asynchronous calls and OnData event. WCL has serial port support as well so you can have one code for Serial and for Bluetooth communication.

 

Sitemap 1 2 3 4 5 6 7