wiki:Docs/Prog/Manual/ApplicationLibraries/lib825ev/ccomm

Version 9 (modified by Don Wilson, 6 years ago) ( diff )

--

CComm

CComm is a C++ class that is provided to simplify 825 serial and ethernet operations. CComm is a base class for the inherited classes CCommSerial (serial ports), CCommClient (TCP/IP client), and CCommServer (TCP/IP Server).

Member Functions

void AddRcvEvent(uint16 startchr, uint16 endchr, int (*pDataEvent)(CComm* pComm, CConnection *pConnnection, int nData, void* pUser), int nRawCnt = 0, void* pUser = NULL)
void AddRcvEvent(uint8 startchrs[], int startchrcnt, uint8 endchrs[], int endchrcnt, int (*pDataEvent)(CComm* pComm, CConnection *pConnnection, int nData, void* pUser), int nRawCnt = 0, void* pUser = NULL)
void AddRcvEvent(uint16 startchr, int (*pDataEvent)(CComm* pComm, CConnection *pConnnection, int nData, void* pUser), int nRawCnt = 0, void* pUser = NULL)

Description

Specifies a receive event (callback function) when after data is received based on the specified start, end chars, data count. A COMM_EVENT preprocessor define is provided to simply the event function declarations.

Example

CComm *pComm = NULL;
// pComm is a pointer that can point to a CCommSerial, CCommClient, or CCommServer instance

#define SERIAL 1
#define CLIENT 2
#define SERVER 3

int commTypeSelection = CLIENT;

void StartCommunications(void)
{
   if(commTypeSelection == SERIAL)
   {
      int commPort = 2;
      CCommSerial *pSerial = new CCommSerial(commPort);
      pComm = pSerial;
   }
   else if(commTypeSelection == CLIENT)
   {
      string svrIP = "192.168.1.25";
      int svrPort = 8001;
      CCommClient *pClient = new CCommClient(svrIP, svrPort);
      pComm = pClient;
   }
   else if(commTypeSelection == SERVER)
   {
      int svrPort = 8001;
      CCommServer *pServer = new CCommServer(svrPort);
      pComm = pServer;
   }

   pComm->Open();

   // Now the app can call pComm->Send to send data without regard for whether it is serial or TCP/IP.

   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;
}


Note: See TracWiki for help on using the wiki.