| | 1 | [[TOC]] |
| | 2 | |
| | 3 | = CComm = |
| | 4 | |
| | 5 | 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). |
| | 6 | |
| | 7 | === Member Functions === |
| | 8 | |
| | 9 | {{{ |
| | 10 | void AddRcvEvent(uint16 startchr, uint16 endchr, int (*pDataEvent)(CComm* pComm, CConnection *pConnnection, int nData, void* pUser), int nRawCnt = 0, void* pUser = NULL) |
| | 11 | 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) |
| | 12 | void AddRcvEvent(uint16 startchr, int (*pDataEvent)(CComm* pComm, CConnection *pConnnection, int nData, void* pUser), int nRawCnt = 0, void* pUser = NULL) |
| | 13 | }}} |
| | 14 | |
| | 15 | ==== Description ==== |
| | 16 | |
| | 17 | 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. |
| | 18 | |
| | 19 | |
| | 20 | |
| | 21 | |
| | 22 | ==== Example ==== |
| | 23 | |
| | 24 | {{{ |
| | 25 | CComm *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 | |
| | 32 | void 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 | |
| | 62 | void StopCommunications(void) |
| | 63 | { |
| | 64 | if(pComm != NULL) |
| | 65 | { |
| | 66 | pComm->Close(); |
| | 67 | delete pComm; |
| | 68 | pComm = NULL; |
| | 69 | } |
| | 70 | } |
| | 71 | |
| | 72 | COMM_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 | }}} |