Changes between Initial Version and Version 1 of Docs/Prog/Manual/ApplicationLibraries/lib825ev/Comm/CComm


Ignore:
Timestamp:
04/09/18 14:13:20 (7 years ago)
Author:
Don Wilson
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Docs/Prog/Manual/ApplicationLibraries/lib825ev/Comm/CComm

    v1 v1  
     1[[TOC]]
     2
     3= CComm =
     4
     5CComm 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).
     6
     7=== Member Functions ===
     8
     9{{{
     10void AddRcvEvent(uint16 startchr, uint16 endchr, int (*pDataEvent)(CComm* pComm, CConnection *pConnnection, int nData, void* pUser), int nRawCnt = 0, void* pUser = NULL)
     11void 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)
     12void AddRcvEvent(uint16 startchr, int (*pDataEvent)(CComm* pComm, CConnection *pConnnection, int nData, void* pUser), int nRawCnt = 0, void* pUser = NULL)
     13}}}
     14
     15==== Description ====
     16
     17Specifies 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.
     18
     19
     20
     21
     22==== Example ====
     23
     24{{{
     25CComm *pComm = NULL;
     26// pComm is a pointer that can point to a CCommSerial, CCommClient, or CCommServer instance
     27
     28#define SERIAL 1
     29#define CLIENT 2
     30#define SERVER 3
     31
     32void StartCommunications(int commTypeSelection)
     33{
     34   if(commTypeSelection == SERIAL)
     35   {
     36      int commPort = 2;
     37      CCommSerial *pSerial = new CCommSerial(commPort);
     38      pComm = pSerial;
     39   }
     40   else if(commTypeSelection == CLIENT)
     41   {
     42      string svrIP = "192.168.1.25";
     43      int svrPort = 8001;
     44      CCommClient *pClient = new CCommClient(svrIP, svrPort);
     45      pComm = pClient;
     46   }
     47   else if(commTypeSelection == SERVER)
     48   {
     49      int svrPort = 8001;
     50      CCommServer *pServer = new CCommServer(svrPort);
     51      pComm = pServer;
     52   }
     53
     54   pComm->Open();
     55
     56   // Now the app can call pComm->Send to send data without regard for whether it is serial or TCP/IP.
     57
     58   pComm->AddRcvEvent(CHR_STX, CHR_ETX, CommEventDataMsg, 0, NULL);
     59
     60}
     61
     62void StopCommunications(void)
     63{
     64   if(pComm != NULL)
     65   {
     66       pComm->Close();
     67       delete pComm;
     68       pComm = NULL;
     69   }
     70}
     71
     72COMM_EVENT(DataMsg)
     73{
     74   uint8* buf = pConnection->GetRcvBuffer();
     75   buf[pConnection->GetRcvPos() - 1] = '\0';
     76
     77   printf("rcvd [%s]\r\n", buf);
     78
     79   if(buf[1] == 'T') // Received <STX>T<ETX> return timeval
     80   {
     81       time_t tmval;
     82       time(&tmval);  // Get timeval
     83
     84       char response[12];
     85       sprintf(response, "%d\n", tmval);
     86       // pConnection isn't important in the case of serial port, but if TCP/IP server it is important to send
     87       // the response to the connection that the message was received from.
     88       pComm->Send(pConnection, response, strlen(response);
     89    }
     90
     91
     92   return 0;
     93}
     94
     95
     96}}}