| 1 | [[TOC]] |
| 2 | |
| 3 | = CCommClient = |
| 4 | |
| 5 | CCommClient is a C++ class that is provided to simplify 825 TCP/IP client communications. CCommClient is inherited from the CComm base class. |
| 6 | |
| 7 | === Constructor === |
| 8 | |
| 9 | === Member Functions === |
| 10 | |
| 11 | |
| 12 | ==== Description ==== |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | ==== Example ==== |
| 18 | |
| 19 | {{{ |
| 20 | if(commTypeSelection == CLIENT) |
| 21 | { |
| 22 | string svrIP = "192.168.1.25"; |
| 23 | int svrPort = 8001; |
| 24 | CCommClient *pClient = new CCommClient(svrIP, svrPort); |
| 25 | pComm = pClient; |
| 26 | } |
| 27 | pComm->Open(); |
| 28 | |
| 29 | pComm->AddRcvEvent(CHR_STX, CHR_ETX, CommEventDataMsg, 0, NULL); |
| 30 | |
| 31 | } |
| 32 | |
| 33 | void StopCommunications(void) |
| 34 | { |
| 35 | if(pComm != NULL) |
| 36 | { |
| 37 | pComm->Close(); |
| 38 | delete pComm; |
| 39 | pComm = NULL; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | COMM_EVENT(DataMsg) |
| 44 | { |
| 45 | uint8* buf = pConnection->GetRcvBuffer(); |
| 46 | buf[pConnection->GetRcvPos() - 1] = '\0'; |
| 47 | |
| 48 | printf("rcvd [%s]\r\n", buf); |
| 49 | |
| 50 | if(buf[1] == 'T') // Received <STX>T<ETX> return timeval |
| 51 | { |
| 52 | time_t tmval; |
| 53 | time(&tmval); // Get timeval |
| 54 | |
| 55 | char response[12]; |
| 56 | sprintf(response, "%d\n", tmval); |
| 57 | // pConnection isn't important in the case of serial port, but if TCP/IP server it is important to send |
| 58 | // the response to the connection that the message was received from. |
| 59 | pComm->Send(pConnection, response, strlen(response); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | }}} |