Machine Vision Status TCP Server
Dependencies: C12832 EthernetInterface mbed-rtos mbed ConfigFile
Revision 2:a8eebf64cd3e, committed 2015-03-05
- Comitter:
- dwini
- Date:
- Thu Mar 05 15:32:22 2015 +0000
- Parent:
- 1:8efef658d90b
- Child:
- 3:254a2671a8e3
- Commit message:
- Refactored message receival and added partial message timeout mechanism.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/StatusIndicator.cpp Thu Mar 05 15:32:22 2015 +0000 @@ -0,0 +1,1 @@ +#include "StatusIndicator.h" \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/StatusIndicator.h Thu Mar 05 15:32:22 2015 +0000 @@ -0,0 +1,22 @@ +#ifndef STATUS_INDICATOR_HEADER +#define STATUS_INDICATOR_HEADER + +#include "mbed.h" + +namespace MachineVision{ + + class StatusIndicator{ + public: + + private: + + + public: + + + private: + + }; +} + +#endif
--- a/TcpDaemon.cpp Thu Mar 05 13:18:28 2015 +0000 +++ b/TcpDaemon.cpp Thu Mar 05 15:32:22 2015 +0000 @@ -1,7 +1,7 @@ #include "TcpDaemon.h" #include "Log.h" -// Incomming message end +// Incoming message end #define END_MSG_SEQUENCE "\r\n" namespace MachineVision{ @@ -10,9 +10,11 @@ * * @server_port the port the daemon will be listening on */ - TcpDaemon::TcpDaemon(int server_port) { + TcpDaemon::TcpDaemon(int server_port, PinName receive_led_pin) + : receive_led(receive_led_pin) { this->server_port = server_port; this->keepListening = true; + this->receive_led = 0; } /* @@ -67,35 +69,40 @@ int read_size = 0; int total_read_size = 0; char * end = NULL; + int i_read = 0; - while((read_size = client.receive(buffer, BUFFER_SIZE)) > 0) { - total_read_size = read_size; + int buffer_size = BUFFER_SIZE; + char * buffer_offset = buffer; + + // Read full message from client + Log::v("Waiting for message\r\n"); + do { + read_size = client.receive(buffer_offset, buffer_size); + receive_led = !receive_led; - // Null-terminate string - this->buffer[total_read_size] = '\0'; - Log::v("Received partial: %s\r\n", this->buffer); + if (read_size > 0) { + total_read_size += read_size; + + // Null-terminate string + buffer[total_read_size] = '\0'; + Log::v("Received partial: %s\r\n", buffer); + + // Search for END + end = strstr(buffer, END_MSG_SEQUENCE); + + buffer_size = BUFFER_SIZE-total_read_size; + buffer_offset = buffer+total_read_size; + } else if (total_read_size > 0) { // Increment i_read (max timeouts for message) + i_read++; + } + } while (total_read_size < BUFFER_SIZE && end == NULL && i_read < MAX_READ_TIMEOUTS); - // Keep reading until full message is received - end = strstr(this->buffer, END_MSG_SEQUENCE); // Find END_MSG_SEQUENCE - while (total_read_size < BUFFER_SIZE && end == NULL) { - read_size = client.receive(this->buffer+total_read_size, BUFFER_SIZE-total_read_size); - if (read_size != -1) { - total_read_size += read_size; - end = strstr(this->buffer, END_MSG_SEQUENCE); - } - Log::v("Waiting for rest of message\r\n"); - } - } - - if (total_read_size > 0) { + if (total_read_size > 0 && i_read < MAX_READ_TIMEOUTS) { if (end == NULL) { Log::w("Buffer full\r\n"); - } else { - // Null-terminate string - this->buffer[total_read_size] = '\0'; - + } else { // Full string received, lets do our thing - Log::v("Received: %s", this->buffer); + Log::v("Received: %s\r\n", buffer); } } }
--- a/TcpDaemon.h Thu Mar 05 13:18:28 2015 +0000 +++ b/TcpDaemon.h Thu Mar 05 15:32:22 2015 +0000 @@ -3,6 +3,8 @@ #ifndef TCP_DAEMON_HEADER #define TCP_DAEMON_HEADER +#include "mbed.h" + #define MAX_BACKLOG 1 namespace MachineVision{ @@ -10,7 +12,8 @@ class TcpDaemon{ public: const static int BUFFER_SIZE = 512; - const static int TCP_TIMEOUT = 1000; + const static int TCP_TIMEOUT = 250; + const static int MAX_READ_TIMEOUTS = 10; // Maximum number of times to retry to read for rest of message private: int server_port; @@ -19,6 +22,8 @@ char buffer[BUFFER_SIZE+1]; bool keepListening; + + DigitalOut receive_led; public: /* @@ -26,7 +31,7 @@ * * @server_port the port the daemon will be listening on */ - TcpDaemon(int server_port); + TcpDaemon(int server_port, PinName receive_led_pin); /* * Make the daemon start listening for incoming connections
--- a/main.cpp Thu Mar 05 13:18:28 2015 +0000 +++ b/main.cpp Thu Mar 05 15:32:22 2015 +0000 @@ -85,7 +85,7 @@ setLcdServerInfo(eth.getIPAddress()); // Start the daemon - TcpDaemon daemon(TCP_SERVER_PORT); + TcpDaemon daemon(TCP_SERVER_PORT, LED2); Log::v("TCP daemon listening @ TCP_SERVER_PORT = %d\r\n", TCP_SERVER_PORT); daemon.startListening(); }