Frameworks > Serial Framework

rs232 OnData problem

<< < (2/2)

Mike Petrichenko:
Aha, ok. I will recheck it once again and fix possible issue.

abadon:
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.

Mike Petrichenko:
Hello,

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

Navigation

[0] Message Index

[*] Previous page

Sitemap 1 2 3 4 5 6 7 
Go to full version