sqefqsdf

Dependencies:   C12832 EthernetInterface LM75B mbed-rtos mbed

Fork of app-board-LM75B by Chris Styles

Committer:
gimohd
Date:
Thu Mar 23 12:51:27 2017 +0000
Revision:
6:77a4c45f6416
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gimohd 6:77a4c45f6416 1 #include "Communication.h"
gimohd 6:77a4c45f6416 2
gimohd 6:77a4c45f6416 3 Communication::Communication()
gimohd 6:77a4c45f6416 4 {
gimohd 6:77a4c45f6416 5
gimohd 6:77a4c45f6416 6 this->ownID = 113;
gimohd 6:77a4c45f6416 7 eth = new EthernetInterface();
gimohd 6:77a4c45f6416 8 printf("Ethernet initialized\r\n");
gimohd 6:77a4c45f6416 9 }
gimohd 6:77a4c45f6416 10
gimohd 6:77a4c45f6416 11 Communication::~Communication()
gimohd 6:77a4c45f6416 12 {
gimohd 6:77a4c45f6416 13 delete eth;
gimohd 6:77a4c45f6416 14 delete server;
gimohd 6:77a4c45f6416 15 }
gimohd 6:77a4c45f6416 16
gimohd 6:77a4c45f6416 17
gimohd 6:77a4c45f6416 18 void Communication::connect()
gimohd 6:77a4c45f6416 19 {
gimohd 6:77a4c45f6416 20 //printf("Initiating Ethernet\r\n");
gimohd 6:77a4c45f6416 21 //eth->init();
gimohd 6:77a4c45f6416 22 char IPAddr[512];
gimohd 6:77a4c45f6416 23 sprintf(IPAddr,"192.168.0.%d",ownID);
gimohd 6:77a4c45f6416 24 eth->init(IPAddr, "255.255.255.0", "192.168.0.1"); //Use DHCP
gimohd 6:77a4c45f6416 25 //eth->init("10.182.34.113", "255.255.255.0", "10.182.34.1"); //Use DHCP
gimohd 6:77a4c45f6416 26 printf("Connecting Ethernet\r\n");
gimohd 6:77a4c45f6416 27 eth->connect();
gimohd 6:77a4c45f6416 28 //printf("Server IP Address is %s\r\n", eth->getIPAddress());
gimohd 6:77a4c45f6416 29 server = new TCPSocketServer();
gimohd 6:77a4c45f6416 30 //printf("Binding server port\r\n");
gimohd 6:77a4c45f6416 31 server->bind(SERVER_PORT);
gimohd 6:77a4c45f6416 32 //printf("Listen to port\r\n");
gimohd 6:77a4c45f6416 33 server->listen();
gimohd 6:77a4c45f6416 34 }
gimohd 6:77a4c45f6416 35
gimohd 6:77a4c45f6416 36 void Communication::sendDataPacket(std::string data)
gimohd 6:77a4c45f6416 37 {
gimohd 6:77a4c45f6416 38 printf("-------------SENDING------------\r\n");
gimohd 6:77a4c45f6416 39 char IPAddr[512];
gimohd 6:77a4c45f6416 40 char buffer[512];
gimohd 6:77a4c45f6416 41 //strcpy(buffer, data.c_str());
gimohd 6:77a4c45f6416 42 //data.copy(buffer,0,data.size());
gimohd 6:77a4c45f6416 43 printf("Sending data chars:\r\n");
gimohd 6:77a4c45f6416 44
gimohd 6:77a4c45f6416 45 printf("DATA: ");
gimohd 6:77a4c45f6416 46 for (int a = 0; a<data.size(); a++) {
gimohd 6:77a4c45f6416 47 buffer[a] = data.at(a);
gimohd 6:77a4c45f6416 48 printf("%d ",buffer[a]);
gimohd 6:77a4c45f6416 49 }
gimohd 6:77a4c45f6416 50 printf("\r\n");
gimohd 6:77a4c45f6416 51 TCPSocketConnection client;
gimohd 6:77a4c45f6416 52 //server->accept(client);
gimohd 6:77a4c45f6416 53 //sprintf(buf,"10.182.34.%d",packet.getIDD());
gimohd 6:77a4c45f6416 54 sprintf(IPAddr,"192.168.0.%d",data[3]);
gimohd 6:77a4c45f6416 55 while (client.connect(IPAddr, SERVER_PORT) < 0) {
gimohd 6:77a4c45f6416 56 printf("Unable to connect to (%s) on port (%d)\r\n", IPAddr, SERVER_PORT);
gimohd 6:77a4c45f6416 57 wait(1);
gimohd 6:77a4c45f6416 58 }
gimohd 6:77a4c45f6416 59 client.send_all(buffer, data.size());
gimohd 6:77a4c45f6416 60
gimohd 6:77a4c45f6416 61 client.close();
gimohd 6:77a4c45f6416 62
gimohd 6:77a4c45f6416 63 }
gimohd 6:77a4c45f6416 64
gimohd 6:77a4c45f6416 65 std::string Communication::getData()
gimohd 6:77a4c45f6416 66 {
gimohd 6:77a4c45f6416 67 printf("-------------RECEIVING------------\r\n");
gimohd 6:77a4c45f6416 68 //printf("Wait for new connection...\r\n");
gimohd 6:77a4c45f6416 69 TCPSocketConnection client;
gimohd 6:77a4c45f6416 70 std::string str;
gimohd 6:77a4c45f6416 71 server->accept(client);
gimohd 6:77a4c45f6416 72 client.set_blocking(false, 5000); // Timeout after (1.5)s
gimohd 6:77a4c45f6416 73 //printf("Connection from: %s\r\n", client.get_address());
gimohd 6:77a4c45f6416 74 char buffer[512];
gimohd 6:77a4c45f6416 75 while (true) {
gimohd 6:77a4c45f6416 76 int n = client.receive_all(buffer, 512);
gimohd 6:77a4c45f6416 77
gimohd 6:77a4c45f6416 78 //printf("size :'%d'\r\n",n);
gimohd 6:77a4c45f6416 79 if (n <= 0) break;
gimohd 6:77a4c45f6416 80
gimohd 6:77a4c45f6416 81 // print received message to terminal
gimohd 6:77a4c45f6416 82 buffer[n] = '\0';
gimohd 6:77a4c45f6416 83 //printf("Received message from Client :'%s'\r\n",buf);
gimohd 6:77a4c45f6416 84
gimohd 6:77a4c45f6416 85
gimohd 6:77a4c45f6416 86 //str = buffer;
gimohd 6:77a4c45f6416 87 printf("Received data:\r\n");
gimohd 6:77a4c45f6416 88 for (int a = 0; a<n; a++) {
gimohd 6:77a4c45f6416 89 if (buffer[a] == 0){
gimohd 6:77a4c45f6416 90 str.push_back('\0');
gimohd 6:77a4c45f6416 91 }else{
gimohd 6:77a4c45f6416 92 str.push_back(buffer[a]);
gimohd 6:77a4c45f6416 93 }
gimohd 6:77a4c45f6416 94 printf("%d : %d",str.at(a), buffer[a]);
gimohd 6:77a4c45f6416 95 }
gimohd 6:77a4c45f6416 96 printf("\r\n");
gimohd 6:77a4c45f6416 97 if (n <= 0) break;
gimohd 6:77a4c45f6416 98 }
gimohd 6:77a4c45f6416 99 client.close();
gimohd 6:77a4c45f6416 100 return str;
gimohd 6:77a4c45f6416 101
gimohd 6:77a4c45f6416 102 }
gimohd 6:77a4c45f6416 103
gimohd 6:77a4c45f6416 104 void Communication::setOwnID(int i)
gimohd 6:77a4c45f6416 105 {
gimohd 6:77a4c45f6416 106 this->ownID = i;
gimohd 6:77a4c45f6416 107 }
gimohd 6:77a4c45f6416 108
gimohd 6:77a4c45f6416 109 int Communication::getOwnID()
gimohd 6:77a4c45f6416 110 {
gimohd 6:77a4c45f6416 111 return this->ownID;
gimohd 6:77a4c45f6416 112 }
gimohd 6:77a4c45f6416 113
gimohd 6:77a4c45f6416 114 /*
gimohd 6:77a4c45f6416 115 void Communication::sendDataTest(int8_t IDD)
gimohd 6:77a4c45f6416 116 {
gimohd 6:77a4c45f6416 117 TCPSocketConnection client;
gimohd 6:77a4c45f6416 118 //server->accept(client);
gimohd 6:77a4c45f6416 119 char buf[13];
gimohd 6:77a4c45f6416 120 //sprintf(buf,"10.182.34.%d",IDD);
gimohd 6:77a4c45f6416 121 sprintf(buf,"192.168.0.%d",IDD);
gimohd 6:77a4c45f6416 122 while (client.connect(buf, SERVER_PORT) < 0) {
gimohd 6:77a4c45f6416 123 printf("Unable to connect to (%s) on port (%d)\n", buf, SERVER_PORT);
gimohd 6:77a4c45f6416 124 wait(1);
gimohd 6:77a4c45f6416 125 }
gimohd 6:77a4c45f6416 126 char str[512] = "THIS IS A TEST";
gimohd 6:77a4c45f6416 127 client.send_all(str, sizeof(str) - 1);
gimohd 6:77a4c45f6416 128 client.close();
gimohd 6:77a4c45f6416 129 }
gimohd 6:77a4c45f6416 130
gimohd 6:77a4c45f6416 131 void Communication::sendData(int8_t NUM, int8_t IDD, int8_t PWM, int8_t IDN[] , int16_t TMP[] ){
gimohd 6:77a4c45f6416 132 TCPSocketConnection client;
gimohd 6:77a4c45f6416 133 //server->accept(client);
gimohd 6:77a4c45f6416 134 char buf[13];
gimohd 6:77a4c45f6416 135 sprintf(buf,"192.168.0.%d",IDD);
gimohd 6:77a4c45f6416 136 while (client.connect(buf, SERVER_PORT) < 0) {
gimohd 6:77a4c45f6416 137 printf("Unable to connect to (%s) on port (%d)\r\n", buf, SERVER_PORT);
gimohd 6:77a4c45f6416 138 wait(1);
gimohd 6:77a4c45f6416 139 }
gimohd 6:77a4c45f6416 140 printf("Connected to Server at %s\n",buf);
gimohd 6:77a4c45f6416 141 char str[512];
gimohd 6:77a4c45f6416 142 str[0] = FRAME_BYTE_1;
gimohd 6:77a4c45f6416 143 str[1] = FRAME_BYTE_2;
gimohd 6:77a4c45f6416 144 str[2] = NUM;
gimohd 6:77a4c45f6416 145 str[3] = IDD;
gimohd 6:77a4c45f6416 146 str[4] = PWM;
gimohd 6:77a4c45f6416 147
gimohd 6:77a4c45f6416 148 int frameNumber = 5;
gimohd 6:77a4c45f6416 149 for( int i = 0; i < IDN.size(); i++ ) {
gimohd 6:77a4c45f6416 150 str[frameNumber] = IDN[i];
gimohd 6:77a4c45f6416 151 frameNumber++;
gimohd 6:77a4c45f6416 152 }
gimohd 6:77a4c45f6416 153 for( int i = 0; i < TMP.size(); i++ ) {
gimohd 6:77a4c45f6416 154 int16_t data = TMP[i];
gimohd 6:77a4c45f6416 155 for(int i = 0; i < 2; i++)
gimohd 6:77a4c45f6416 156 {
gimohd 6:77a4c45f6416 157 str[frameNumber] = data & 0xff;
gimohd 6:77a4c45f6416 158 data >>= 8;
gimohd 6:77a4c45f6416 159 frameNumber++;
gimohd 6:77a4c45f6416 160 }
gimohd 6:77a4c45f6416 161 }
gimohd 6:77a4c45f6416 162 str[frameNumber] = FRAME_BYTE_2;
gimohd 6:77a4c45f6416 163 str[frameNumber+1] = FRAME_BYTE_1;
gimohd 6:77a4c45f6416 164
gimohd 6:77a4c45f6416 165 client.send_all(str, sizeof(str) - 1);
gimohd 6:77a4c45f6416 166 client.close();
gimohd 6:77a4c45f6416 167
gimohd 6:77a4c45f6416 168
gimohd 6:77a4c45f6416 169 }*/