Author Topic: rs232 OnData problem  (Read 4346 times)

Offline abadon

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
rs232 OnData problem
« on: March 30, 2010, 12:11:49 PM »
Hi,
I have a problem with OnData event.
When I receive a short messages all is OK. But when device sends me a big message, i don't have all message in buffor. I'm sure that meaasge was sending by device because i see that on serial monitor.

My OnData function :
void CCSerialAdapter::OnData(LPVOID  lpBuffer, DWORD dwSize)
{
   const char cSeparator = CHAR(10);
   char* Buf;
   Buf = (char*)LocalAlloc(LPTR, dwSize + 1);
   ZeroMemory(Buf, dwSize + 1);
   CopyMemory(Buf, lpBuffer, dwSize);

        ...

   LocalFree((HLOCAL)Buf);
}

My connection proprtites :

m_pClient->SetTransport( wcl::ctSerial );
m_pClient->GetSerialParams()->SetPort(iPort);
m_pClient->GetSerialParams()->SetBaudRate( wcl::br115200 );
wcl::CwclBuffers buffers( m_pClient );
buffers.SetReadBuffer( 4126 );
buffers.SetWriteBuffer( 4126 );
m_pClient->SetBuffers( buffers );

This is my firs time to use WCL so forgive me if my question is stupid :)

Thx,

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: rs232 OnData problem
« Reply #1 on: March 30, 2010, 12:46:16 PM »
Hi,

Ondata event fires when intenal PCs buffer is full. Your long message from device can be splitted to more than on OnData even.

Offline abadon

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: rs232 OnData problem
« Reply #2 on: March 30, 2010, 01:03:40 PM »
That's correct, i have more than one event and I add all to my queue.

Thats my all OnData function:

void CCSerialAdapter::OnData(LPVOID  lpBuffer, DWORD dwSize)
{
   const char cSeparator = CHAR(10);
   char* Buf;
   Buf = (char*)LocalAlloc(LPTR, dwSize + 1);
   ZeroMemory(Buf, dwSize + 1);
   CopyMemory(Buf, lpBuffer, dwSize);
   QStringList strList = QStringList::split( cSeparator , Buf, false );

   for ( int i = 0 ; i< strList.count() ; i++ )
   {
      if ( strList[ i ].length() >= 1 )
      {
      m_QuaueMutex.lock();
      m_AnswerList.push_back( strList[ i ] );
      m_QuaueMutex.unlock();
      }
   }
   LocalFree( ( HLOCAL ) Buf );
}
All messages i have in m_AnswerList.
I found funny thing , when i add sleep(300) in the end of the function, everything works almost good ( i loose only one line ).

Thx

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: rs232 OnData problem
« Reply #3 on: March 30, 2010, 02:09:35 PM »
What is BT driver you use? I have to check it and add Sleep (or somthing) into WCL code to prevent such issue in the future :)

Offline abadon

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: rs232 OnData problem
« Reply #4 on: March 30, 2010, 02:23:08 PM »
I don't use any blueTooth , i have only rs232 ports.

Thx.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: rs232 OnData problem
« Reply #5 on: March 30, 2010, 02:27:30 PM »
Aha, ok. I will recheck it once again and fix possible issue.

Offline abadon

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: rs232 OnData problem
« Reply #6 on: April 01, 2010, 02:53:33 PM »
Hi,
I was trying to solve this problem by opening classs which use wcl i other thread, but this not work. Proble is still the same: all work good until events with messages from rs stop coming.
If You have a time, check my code, maybe there is some stupid mistake


#include <afxtempl.h>                 
#include "cserial.h"                  
#include "WCL\\source\\wclAPI.h"
#include "WCL\\source\\wclSerialMonitor.h"
#include "WCL\\source\\wclConnections.h"
#include "WCL\\source\\wclErrors.h"
#include <qstring.h>



CCSerialAdapter::CCSerialAdapter()
{
   Init();
}


CCSerialAdapter::~CCSerialAdapter()
{
   
}

void CCSerialAdapter::Init()
{
   
   m_pDisc = new wcl::CwclSerialDiscovery();
   m_pClient =new wcl::CwclClient();
   m_API = new wcl::CwclAPI();
   m_pAnswerBuffer = "";
   m_API->Load();
   m_AnswerQueue.clear();
   m_AnswerList.clear();
   
   
   
   int iPort = 1;
   m_pClient->SetTransport( wcl::ctSerial );
   m_pClient->GetSerialParams()->SetPort(iPort);
   m_pClient->GetSerialParams()->SetBaudRate( wcl::br115200 );
}


bool CCSerialAdapter::Connect()
{
   
   m_pClient->Disconnect();
   wcl::CwclSerialDevices Devices;
   m_API->Load();
   if ( !wcl::wclShowError( m_pDisc->EnumDevices( Devices ) )  )
   {
      if ( !IsOpen() )
      {   
                wcl::wclShowError(m_pClient->Connect());
      }
   }
   
   return true;
}
bool CCSerialAdapter::Write( unsigned char* a_pBuffer, int a_iSize  )
{
   if ( !IsOpen() )
   {
      wcl::wclShowError(m_pClient->Connect());
   }
      
   int result = wcl::wclShowError( m_pClient->Write( a_pBuffer , a_iSize ) );
   return true;
   
}

bool CCSerialAdapter::IsOpen()
{
   
   if ( m_pClient->GetState() == wcl::csConnected )
   {
      return true;
   }
   else
   {
      return false;
   }

}



QString CCSerialAdapter::GetLastAnswer()
{
   QString strAns("");
   m_BufforMutex.lock();
   strAns = m_pAnswerBuffer.section( CHAR(13) , 0 ,0);
   m_pAnswerBuffer.remove( 0 , strAns.length() + 1 );
   m_BufforMutex.unlock();
   if ( strAns.length() > 1 )

   {
      return strAns;
   }
   else
   {
         return "";
   }

}


void CCSerialAdapter::OnData(LPVOID  lpBuffer, DWORD dwSize)
{
   
   char* Buf;
   Buf = (char*)LocalAlloc(LPTR, dwSize + 1);
   ZeroMemory(Buf, dwSize + 1);
   CopyMemory(Buf, lpBuffer, dwSize);
   QString strNew( Buf );
   m_BufforMutex.lock();
   m_pAnswerBuffer += strNew;
   m_BufforMutex.unlock();

        // when i add this sleep, my buffer has more info
   //Sleep( dwSize );   

}

void CCSerialAdapter::run()
{
// stupid but should be working
   __hook(&wcl::CwclClient::OnData, m_pClient, &CCSerialAdapter::OnData);
   while( 1 )
   {
      Sleep( 1 );
   }
}


Thx.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: rs232 OnData problem
« Reply #7 on: April 01, 2010, 04:16:31 PM »
Hello,

your code looks ok, but while() in run() function locks all events.

 

Sitemap 1 2 3 4 5 6 7 8 9