wiki:Docs/Prog/Manual/ApplicationLibraries/lib825ev/Comm/CCommClient

Version 5 (modified by Don Wilson, 3 years ago) ( diff )

--

CCommClient

CCommClient is a C++ class that is provided to simplify 825 TCP/IP client communications. CCommClient is inherited from the CComm base class.

Constructor

CCommClient(string& strIP, int nPort, int nOpenTimeoutSecs = 0, int nReconnectTimeSecs = 30);

The constructor creates a CCommClient object without opening a communications channel.

Parameters

  • strIP - IP address of server to connect to.
  • nPort - IP port of server to connect to.
  • nOpenTimeoutSecs - optional timeout in seconds for connection to server.
  • nReconnectTimeSecs - Optional time in seconds to reconnect after attempted communications if connection is lost.

Member Functions

Description

The Process method must be called regularly for the communication channel to be serviced allowing any receive events to occur. This also allows the reconnect option to be performed. This should usually be done in the app background event such as:

BACKGROUND_EVENT(Comm)
{
   if(pComm != NULL)
   {
      pComm->Process();
   }
}

Example

   if(commTypeSelection == CLIENT)
   {
      string svrIP = "192.168.1.25";
      int svrPort = 8001;
      CCommClient *pClient = new CCommClient(svrIP, svrPort);
      pComm = pClient;
   }
   pComm->Open();

   pComm->AddRcvEvent(CHR_STX, CHR_ETX, CommEventDataMsg, 0, NULL);

}

void StopCommunications(void)
{ 
   if(pComm != NULL)
   {
       pComm->Close();
       delete pComm;
       pComm = NULL;
   }
}

COMM_EVENT(DataMsg)
{
   uint8* buf = pConnection->GetRcvBuffer();
   buf[pConnection->GetRcvPos() - 1] = '\0';

   printf("rcvd [%s]\r\n", buf);

   if(buf[1] == 'T') // Received <STX>T<ETX> return timeval
   {
       time_t tmval;
       time(&tmval);  // Get timeval

       char response[12];
       sprintf(response, "%d\n", tmval);
       // pConnection isn't important in the case of serial port, but if TCP/IP server it is important to send
       // the response to the connection that the message was received from.
       pComm->Send(pConnection, response, strlen(response);
    }


   return 0;
}


See Also

Note: See TracWiki for help on using the wiki.