ModBusTCP with some fixes
Diff: ModBus-TCP.cpp
- Revision:
- 3:ebea8e061ae6
- Parent:
- 2:fcd20e2cd110
--- a/ModBus-TCP.cpp Thu Jun 29 10:17:29 2017 +0000 +++ b/ModBus-TCP.cpp Fri Jun 30 08:17:01 2017 +0000 @@ -26,13 +26,13 @@ _handler_count = 0; } -int Modbus::start (NetworkStack *ns, int port) { +int Modbus::start (EthernetInterface *ns, int port) { int i; m_ns = ns; for (i = 0; i < MODBUS_MAX_CLIENTS; i ++) { - _state[i].buf = new CircBuffer<char>(MODBUS_BUF_SIZE); + // _state[i].buf = new CircBuffer<char>(MODBUS_BUF_SIZE); _state[i].thread = new Thread(osPriorityNormal, MODBUS_STACK_SIZE); _state[i].client = new TCPSocket(); _state[i].thread->start(callback(child, (void*)i)); @@ -46,7 +46,7 @@ _server.open(m_ns); _server.bind(port); _server.set_blocking(true); - _server.listen(); + _server.listen(MODBUS_MAX_CLIENTS); _daemon = new Thread(osPriorityNormal, MODBUS_STACK_SIZE); _daemon->start(Modbus::daemon); return 0; @@ -56,25 +56,27 @@ Modbus *modbus = Modbus::getInstance(); int i, t = 0; - INFO("Wait for new connection...\r\n"); + for (;;) { + //DBG("Wait for new connection... child %i",t); if (t >= 0) { if (modbus->_server.accept(modbus->_state[t].client) == 0) { INFO("accept %d\r\n", t); - modbus->_state[t].thread->signal_set(1); + modbus->_state[t].thread->signal_set(0x1); } } else { #ifdef HTTPD_ENABLE_CLOSER if (modbus->_server.accept(modbus->_state[MODBUS_MAX_CLIENTS].client) == 0) { INFO("accept x\r\n"); - modbus->_state[MODBUS_MAX_CLIENTS].thread->signal_set(1); + modbus->_state[MODBUS_MAX_CLIENTS].thread->signal_set(0x1); } #endif } t = -1; for (i = 0; i < MODBUS_MAX_CLIENTS; i ++) { - if (modbus->_state[i].thread->get_state() == Thread::WaitingAnd) { + //DBG("Child %i in State : %u", i, modbus->_state[i].thread->get_state()); + if ( modbus->_state[i].thread->get_state() == Thread::WaitingThreadFlag) { if (t < 0) t = i; // next empty thread } } @@ -82,42 +84,52 @@ } void Modbus::child (void const *arg) { - Modbus *modbus = ModBus::getInstance(); + Modbus *modbus = Modbus::getInstance(); int id = (int)arg; int i, n; - char buf[ModBus_BUF_SIZE]; + char buf[MODBUS_BUF_SIZE]; for (;;) { - Thread::signal_wait(1); - modbus->_state[id].mode = MODE_REQUEST; - modbus->_state[id].buf->flush(); - modbus->_state[id].keepalive = 0; - INFO("Connection from client\r\n"); + //DBG("child %i waiting for connection",id); + Thread::signal_wait(0x1); + + + + INFO("Connection from client on child %i", id); // INFO("Connection from %s\r\n", httpd->_state[id].client->get_ip_address()); - modbus->_state[id].client->set_blocking(false); - modbus->_state[id].client->set_timeout(HTTPD_TIMEOUT); - + modbus->_state[id].client->set_blocking(true); + modbus->_state[id].client->set_timeout(15000); + time_t t1 = time(NULL); for (;;) { //if (! httpd->_state[id].client->is_connected()) break; - + modbus->_state[id].client->set_blocking(true); + modbus->_state[id].client->set_timeout(15000); n = modbus->_state[id].client->recv(buf, sizeof(buf)); if (n < 0 ) { - printf("HTTPD::child breaking n = %d\r\n", n); + printf("Modbus::child breaking n = %d\r\n", n); break; } - buf[n] = 0; - //DBG("Recv %d ", n); - DBG("Recv %d '%s'", n, buf); - - for (i = 0; i < n; i ++) { - modbus->recvData(id, buf[i]); + + if( n > 0 ) { // We received something + + t1 = time(NULL); + + //DBG("Recv %d bytes Content: %x", n, buf) + + modbus->recvData(id, buf, n); } + + if(abs((int)(time(NULL) - t1))> 15) { + DBG("Timeout in child %i",id); + break; + } + } modbus->_state[id].client->close(); // Needs to bere moved - INFO("Closed client connection\r\n"); + INFO("Closed client connection"); //INFO("Close %s\r\n", httpd->_state[id].client->get_ip_address()); } }