Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: PwmIn IONMcMotor MPU6050 Eigen ROVER
Fork of Hyfliers_Completo_testato by
Diff: Ethernet_tcp_comunication/Eth_tcp.cpp
- Revision:
- 3:fc26045926d9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Ethernet_tcp_comunication/Eth_tcp.cpp Wed Nov 06 10:57:51 2019 +0000
@@ -0,0 +1,225 @@
+#include "Eth_tcp.h"
+
+Eth_tcp::Eth_tcp(int port_, int sock_timeout_){
+
+ port = port_;
+ sock_timeout = sock_timeout_;
+}
+
+void Eth_tcp::connect(){
+ eth.set_network("192.168.1.160", "255.255.255.0", "192.168.1.1");
+
+ eth.connect();
+ printf("Connected: the target IP address is '%s'\r\n", eth.get_ip_address());
+
+ //Open the server on ethernet stack
+ if(srv.open(ð)==0){printf("SERVER OPENED\r\n");}
+
+ //Bind the HTTP port (TCP 80) to the server
+ srv.bind(eth.get_ip_address(), port);
+ srv.set_blocking(false);
+ clt_sock.set_blocking(false);
+ //clt_sock.set_timeout(sock_timeout);
+ //Can handle 1 simultaneous connections
+ srv.listen(5);
+
+ comunicationTimer.start();
+
+ }
+
+
+int Eth_tcp::recv_int(int time_out) {
+
+ int32_t recv_value;
+ char recv_msg[4];
+
+ double t_p = comunicationTimer.read();
+ double t = t_p;
+
+ int msg_size = 0;
+
+ while(msg_size!=4 && (t-t_p)<=(float)time_out/1000){
+ msg_size = clt_sock.recv(&recv_msg,4);
+ t = comunicationTimer.read();
+
+ //printf("Int: %f \r\n", t-t_p);
+
+ }
+ //printf("int: %d\r\n",msg_size);
+ recv_value = (uint16_t)recv_msg[0]<<24;
+ recv_value |= (uint16_t)recv_msg[1]<<16;
+ recv_value |= (uint16_t)recv_msg[2]<<8;
+ recv_value |= (uint16_t)recv_msg[3];
+
+ if((t-t_p)>(float)time_out/1000){
+ printf("int time out..\r\n");
+ recv_value = 0;
+ }
+
+
+
+ return recv_value;
+
+}
+
+
+bool Eth_tcp::recv_pkt(char& cmd, MyBuffer <int> &values, int& n_of_int) {
+
+ values.clear();
+ int32_t recv_value;
+ char recv_msg[255];
+
+ int msg_size = -1;
+
+ msg_size = clt_sock.recv(&recv_msg,255);
+
+ if(msg_size > 0){
+ if(recv_msg[msg_size-1] == '@'){
+ cmd = recv_msg[0];
+ n_of_int = (msg_size-2)/4;
+ int k = 1;
+ for(int i = 0; i< n_of_int; i++){
+ recv_value = (uint16_t)recv_msg[k]<<24;
+ recv_value |= (uint16_t)recv_msg[k+1]<<16;
+ recv_value |= (uint16_t)recv_msg[k+2]<<8;
+ recv_value |= (uint16_t)recv_msg[k+3];
+
+ k = k+4;
+
+
+ values.put(recv_value);
+ }
+ return true;
+ }else{
+ //printf("Terminatore non valido!\r\n");
+ n_of_int = 0;
+ return false;
+ }
+ }else{
+ //printf("Time out...\r\n");
+ n_of_int = 0;
+ return false;
+ }
+
+
+}
+
+char Eth_tcp::recv_char(int time_out) {
+
+
+ char recv_msg[1];
+
+ double t_p = comunicationTimer.read();
+ double t = t_p;
+
+ int msg_size = 0;
+
+ while(msg_size!=1 && (t-t_p)<=(float)time_out/1000){
+ msg_size = clt_sock.recv(&recv_msg,1);
+ t = comunicationTimer.read();
+
+ }
+ if((t-t_p)>(float)time_out/1000){
+ printf("time out..\r\n");
+ recv_msg[0] = 'x';
+ }
+
+
+ return recv_msg[0];
+
+}
+
+char* Eth_tcp::recv_cmd(int time_out) {
+
+
+ char recv_msg[2];
+
+ double t_p = comunicationTimer.read();
+ double t = t_p;
+
+ int msg_size = 0;
+
+ while(msg_size!=2 && (t-t_p)<=(float)time_out/1000){
+ msg_size = clt_sock.recv(&recv_msg,2);
+ t = comunicationTimer.read();
+ //printf("Cmd: %f \r\n", t-t_p);
+
+ }
+ //printf("cmd: %d\r\n",msg_size);
+ if((t-t_p)>(float)time_out/1000){
+ printf("cmd time out..\r\n");
+ recv_msg[0] = ' ';
+ recv_msg[1] = ' ';
+ }
+
+
+ return recv_msg;
+
+}
+
+
+void Eth_tcp::StuffIntIntoChar4(char* pIntoChar4, int32_t val)
+{
+ pIntoChar4[0] = val>>24;
+ pIntoChar4[1] = val>>16;
+ pIntoChar4[2] = val>>8;
+ pIntoChar4[3] = val;
+}
+
+
+void Eth_tcp::send_vec_of_int(MyBuffer <int> vec_of_int) {
+
+ int value_to_send;
+
+ char send_msg[255];
+ char temp[4];
+ int k=0;
+ while(vec_of_int.available()){
+ value_to_send = vec_of_int.get();
+
+ StuffIntIntoChar4(temp,value_to_send);
+
+ send_msg[k] = temp[0];
+ send_msg[k+1] = temp[1];
+ send_msg[k+2] = temp[2];
+ send_msg[k+3] = temp[3];
+ k= k+4;
+
+
+ }
+
+ clt_sock.send(send_msg, k);
+
+
+}
+
+
+void Eth_tcp::sendAck(char c) {
+
+ char send_msg[1];
+
+ send_msg[0] = c;
+
+
+ clt_sock.send(send_msg, 1);
+
+
+}
+
+bool Eth_tcp::is_connected() {
+
+ if ( srv.accept(&clt_sock)==0 ){
+ return true;
+ }else{
+ return false;
+ }
+
+}
+
+void Eth_tcp::reset_connection(){
+ if(clt_sock.close()==0){printf("SOCHET CLOSED\r\n");}
+
+}
+
+
+
\ No newline at end of file
