Dependencies: ros_lib_kinetic
Diff: HLComms.cpp
- Revision:
- 12:595ed862e52f
- Parent:
- 11:7029367a1840
- Child:
- 13:a373dfc57b89
--- a/HLComms.cpp Fri Aug 10 10:40:18 2018 +0000 +++ b/HLComms.cpp Mon Aug 13 09:16:29 2018 +0000 @@ -11,43 +11,43 @@ int HLComms::setup_server(void) { int error_code; - printf("Starting TCP server...\n\r"); + if(IS_PRINT_OUTPUT) printf("Starting TCP server...\n\r"); if( !IS_DHCP ) { const char* ip = "10.101.81.135"; //"10.101.79.125"; // Get first 3 parts from "ifconfig" on PC const char* mask = "255.255.255.0"; // Get from "ifconfig" on PC const char* gateway = "10.101.81.1"; //"10.101.79.1"; // Get from "route -n" on PC error_code = interfaces.eth.set_network(ip, mask, gateway); // Dont use DHCP if( error_code < 0 ) { - printf("Error %i. Could not network interface to use a static IP address. " + if(IS_PRINT_OUTPUT) printf("Error %i. Could not network interface to use a static IP address. " "Perhaps the network is already connected?\n\r", error_code); return -1; } } error_code = interfaces.eth.connect(); if( error_code < 0 ) { - printf("Error %i. Could not start the Ethernet interface.\n\r", error_code); + if(IS_PRINT_OUTPUT) printf("Error %i. Could not start the Ethernet interface.\n\r", error_code); return -1; } const char* server_ip = interfaces.eth.get_ip_address(); if( server_ip == NULL ) { error_code = -1; - printf("Error %i. Ethernet interface is not yet connected.\n\r", error_code); + if(IS_PRINT_OUTPUT) printf("Error %i. Ethernet interface is not yet connected.\n\r", error_code); return -1; } - printf("The target IP address is '%s'\n\r", server_ip); + if(IS_PRINT_OUTPUT) printf("The target IP address is '%s'\n\r", server_ip); error_code = interfaces.srv.open(&interfaces.eth); // Open the server on ethernet stack if( error_code < 0 ) { - printf("Error %i. Could not open server socket.\n\r", error_code); + if(IS_PRINT_OUTPUT) printf("Error %i. Could not open server socket.\n\r", error_code); return -1; } error_code = interfaces.srv.bind(interfaces.eth.get_ip_address(), _port); // Bind the HTTP port (TCP 80) to the server if( error_code < 0 ) { - printf("Error %i. Could not bind server socket to port '%d'.\n\r", error_code, _port); + if(IS_PRINT_OUTPUT) printf("Error %i. Could not bind server socket to port '%d'.\n\r", error_code, _port); return -1; } error_code = interfaces.srv.listen(1); // Listen for 1 simultaneous connection if( error_code < 0 ) { - printf("Error %i. Could not put server socket into listening mode.\n\r", error_code); + if(IS_PRINT_OUTPUT) printf("Error %i. Could not put server socket into listening mode.\n\r", error_code); return -1; } return 0; @@ -55,21 +55,21 @@ int HLComms::accept_connection(void) { - printf("Waiting to accept a connection on the TCP socket.\n\r"); + if(IS_PRINT_OUTPUT) printf("Waiting to accept a connection on the TCP socket.\n\r"); int error_code; error_code = interfaces.srv.accept(&interfaces.clt_sock, &interfaces.clt_addr); // Blocks until data is sent if( error_code < 0 ) { - printf("Error %i. Could not create a network socket using the specified socket instance. " + if(IS_PRINT_OUTPUT) printf("Error %i. Could not create a network socket using the specified socket instance. " "Perhaps the socket is set to non-blocking or timed out?\n\r", error_code); return -1; } - printf("Accepted %s:%d\n\r", interfaces.clt_addr.get_ip_address(), interfaces.clt_addr.get_port()); + if(IS_PRINT_OUTPUT) printf("Accepted %s:%d\n\r", interfaces.clt_addr.get_ip_address(), interfaces.clt_addr.get_port()); return 0; } void HLComms::close_server(void) { - printf("Closing server...\n\r"); + if(IS_PRINT_OUTPUT) printf("Closing server...\n\r"); interfaces.clt_sock.close(); interfaces.srv.close(); interfaces.eth.disconnect(); @@ -80,6 +80,7 @@ int HLComms::receive_message(void) { memset(recv_buffer, 0, sizeof(recv_buffer)); int error_code = interfaces.clt_sock.recv(recv_buffer, 1024); // Blocks until data is received + if(IS_PRINT_OUTPUT) printf("Message received.\r\n"); return error_code; } @@ -92,7 +93,7 @@ char * token = strtok((char *)msg, delim); while(token != NULL) { tokens.push_back(string(token)); - //printf("%s\n\r",token); + //if(IS_PRINT_OUTPUT) printf("%s\n\r",token); token = strtok(NULL, delim); } // Cast into doubles and assign to variables @@ -119,7 +120,7 @@ if(input.psi[2][2] < 0.0) input.psi[2][2] = 0.0; if(input.duration < 0.0) input.duration = 0.0; /*if( input.psi[2][0] > 0.14 ) { - printf("Hi\n\r"); + if(IS_PRINT_OUTPUT) printf("Hi\n\r"); }*/ //throw std::invalid_argument( "received negative value" ); //return 0; @@ -132,7 +133,9 @@ } int HLComms::send_message(void) { + if(IS_PRINT_OUTPUT) printf("Sending message...\r\n"); char * msg = send_buffer; int error_code = interfaces.clt_sock.send(msg, strlen(msg)); + if(IS_PRINT_OUTPUT) printf("Message sent.\r\n"); return error_code; } \ No newline at end of file