TCP communication sample for mbed classic by using 3 GR-PEACHs.
Dependencies: EthernetInterface mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "EthernetInterface.h" 00004 #include "TCPPacket.h" 00005 00006 /**** Selection of PEACH *****/ 00007 #define USE_PEACH (1) /* 1(PEACH No1) or 2(PEACH No2) or 3(PEACH No3) */ 00008 /*****************************/ 00009 00010 #if USE_PEACH == 1 /* in case of PEACH No1 */ 00011 Thread TCPTask; 00012 EthernetInterface eth; 00013 DigitalOut led1(LED1); 00014 00015 static tcp_packet send_packet; 00016 00017 int TCP_send(tcp_packet* send_data, int length, const char* adress, int port) { 00018 int ret; 00019 int retry_cnt = 0; 00020 TCPSocketConnection socket; 00021 00022 printf("Now connecting...\n"); 00023 while (socket.connect(adress, port) < 0) { 00024 printf("Unable to connect to (%s) on port (%d)\n", adress, port); 00025 retry_cnt++; 00026 if (retry_cnt >= TCPSEND_CONN_RETRY_MAX) { 00027 socket.close(); 00028 return -1; 00029 } 00030 } 00031 ret = socket.send_all((char*)send_data, length); 00032 socket.close(); 00033 00034 return ret; 00035 } 00036 00037 void tcp_comm_task(void) { 00038 int ret; 00039 00040 memset(&send_packet, 0, sizeof(send_packet)); 00041 eth.init(PEACH_1_IP_ADDRESS, SUB_NET_MASK, DEFAULT_GATEWAY); 00042 ret = eth.connect(); 00043 if (ret == 0) { 00044 printf("IP Address is %s\n", eth.getIPAddress()); 00045 00046 send_packet.data = 0; 00047 while (1) { 00048 // create data 00049 if (send_packet.data < 0xFF) { 00050 send_packet.data++; 00051 } else { 00052 send_packet.data = 0; 00053 } 00054 // Send message 00055 ret = TCP_send(&send_packet, sizeof(send_packet), PEACH_2_IP_ADDRESS, PEACH_2_PORT); 00056 if (ret >= 0) { 00057 printf("Send message : %d\n", send_packet.data); 00058 led1 = !led1; 00059 } else { 00060 printf("send failed!!\n"); 00061 } 00062 Thread::wait(1000); 00063 } 00064 } else { 00065 printf("Network Connect Error \n"); 00066 } 00067 } 00068 00069 /****** main ******/ 00070 int main(void) { 00071 printf("GR-PEACH No1 sample start!!\n"); 00072 00073 /* Start TCP communicationl processing */ 00074 TCPTask.start(callback(tcp_comm_task)); 00075 00076 /* Wait for the threads to finish */ 00077 TCPTask.join(); 00078 } 00079 00080 // set mac address 00081 void mbed_mac_address(char *mac) { 00082 mac[0] = 0x74; 00083 mac[1] = 0x90; 00084 mac[2] = 0x50; 00085 mac[3] = 0x00; 00086 mac[4] = 0x56; 00087 mac[5] = 0xA1; 00088 } 00089 00090 #elif USE_PEACH == 2 /* in case of PEACH No2 */ 00091 Thread TCPTask; 00092 EthernetInterface eth; 00093 TCPSocketServer server; 00094 DigitalOut led2(LED2); 00095 00096 #define CONNECT_PENDING_NUM (2) 00097 static tcp_packet send_packet; 00098 static tcp_packet recv_packet; 00099 00100 int TCP_send(tcp_packet* send_data, int length, const char* adress, int port) { 00101 int ret; 00102 int retry_cnt = 0; 00103 TCPSocketConnection socket; 00104 00105 printf("Now connecting...\n"); 00106 while (socket.connect(adress, port) < 0) { 00107 printf("Unable to connect to (%s) on port (%d)\n", adress, port); 00108 retry_cnt++; 00109 if (retry_cnt >= TCPSEND_CONN_RETRY_MAX) { 00110 socket.close(); 00111 return -1; 00112 } 00113 } 00114 ret = socket.send_all((char*)send_data, length); 00115 socket.close(); 00116 00117 return ret; 00118 } 00119 00120 static int TCP_receive(tcp_packet* recv_data, int length) { 00121 int ret; 00122 TCPSocketConnection client; 00123 00124 ret = server.accept(client); 00125 if (ret == 0) { 00126 client.set_blocking(false, 1500); // Timeout after 1.5s 00127 00128 ret = client.receive((char*)recv_data, length); 00129 client.close(); 00130 } 00131 00132 return ret; 00133 } 00134 00135 void tcp_comm_task(void) { 00136 int ret; 00137 00138 memset(&send_packet, 0, sizeof(send_packet)); 00139 memset(&recv_packet, 0, sizeof(recv_packet)); 00140 eth.init(PEACH_2_IP_ADDRESS, SUB_NET_MASK, DEFAULT_GATEWAY); 00141 ret = eth.connect(); 00142 if (ret == 0) { 00143 printf("IP Address is %s\n", eth.getIPAddress()); 00144 00145 server.bind(PEACH_2_PORT); 00146 server.listen(CONNECT_PENDING_NUM); // number of pending connections 00147 while (1) { 00148 // Receive message 00149 ret = TCP_receive(&recv_packet, sizeof(recv_packet)); 00150 if (ret >= 0) { 00151 printf("Received message : %d\n", recv_packet.data); 00152 // Send message 00153 memcpy(&send_packet, &recv_packet, sizeof(send_packet)); 00154 ret = TCP_send(&send_packet, sizeof(send_packet), PEACH_3_IP_ADDRESS, PEACH_3_PORT); 00155 if (ret >= 0) { 00156 printf("Send message : %d\n", send_packet.data); 00157 led2 = !led2; 00158 } else { 00159 printf("send failed!!\n"); 00160 } 00161 } else { 00162 printf("receive failed!!\n"); 00163 } 00164 Thread::wait(100); 00165 } 00166 } else { 00167 printf("Network Connect Error \n"); 00168 } 00169 } 00170 00171 /****** main ******/ 00172 int main(void) { 00173 printf("GR-PEACH No2 sample start!!\n"); 00174 00175 /* Start TCP communicationl processing */ 00176 TCPTask.start(callback(tcp_comm_task)); 00177 00178 /* Wait for the threads to finish */ 00179 TCPTask.join(); 00180 } 00181 00182 // set mac address 00183 void mbed_mac_address(char *mac) { 00184 mac[0] = 0x74; 00185 mac[1] = 0x90; 00186 mac[2] = 0x50; 00187 mac[3] = 0x00; 00188 mac[4] = 0x56; 00189 mac[5] = 0xA2; 00190 } 00191 00192 #elif USE_PEACH == 3 /* in case of PEACH No3 */ 00193 Thread TCPTask; 00194 EthernetInterface eth; 00195 TCPSocketServer server; 00196 DigitalOut led3(LED3); 00197 00198 #define CONNECT_PENDING_NUM (2) 00199 static tcp_packet recv_packet; 00200 00201 static int TCP_receive(tcp_packet* recv_data, int length) { 00202 int ret; 00203 TCPSocketConnection client; 00204 00205 ret = server.accept(client); 00206 if (ret == 0) { 00207 client.set_blocking(false, 1500); // Timeout after 1.5s 00208 00209 ret = client.receive((char*)recv_data, length); 00210 client.close(); 00211 } 00212 00213 return ret; 00214 } 00215 00216 void tcp_comm_task(void) { 00217 int ret; 00218 00219 memset(&recv_packet, 0, sizeof(recv_packet)); 00220 eth.init(PEACH_3_IP_ADDRESS, SUB_NET_MASK, DEFAULT_GATEWAY); 00221 ret = eth.connect(); 00222 if (ret == 0) { 00223 printf("IP Address is %s\n", eth.getIPAddress()); 00224 00225 server.bind(PEACH_3_PORT); 00226 server.listen(CONNECT_PENDING_NUM); // number of pending connections 00227 while (1) { 00228 // Receive message 00229 ret = TCP_receive(&recv_packet, sizeof(recv_packet)); 00230 if (ret >= 0) { 00231 printf("Received message : %d\n", recv_packet.data); 00232 led3 = !led3; 00233 } else { 00234 printf("receive failed!!\n"); 00235 } 00236 Thread::wait(100); 00237 } 00238 } else { 00239 printf("Network Connect Error \n"); 00240 } 00241 } 00242 00243 /****** main ******/ 00244 int main(void) { 00245 printf("GR-PEACH No3 sample start!!\n"); 00246 00247 /* Start TCP communicationl processing */ 00248 TCPTask.start(callback(tcp_comm_task)); 00249 00250 /* Wait for the threads to finish */ 00251 TCPTask.join(); 00252 } 00253 00254 // set mac address 00255 void mbed_mac_address(char *mac) { 00256 mac[0] = 0x74; 00257 mac[1] = 0x90; 00258 mac[2] = 0x50; 00259 mac[3] = 0x00; 00260 mac[4] = 0x56; 00261 mac[5] = 0xA3; 00262 } 00263 00264 #endif
Generated on Fri Jul 15 2022 19:13:56 by 1.7.2