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