ModBusTCP with some fixes
Diff: ModBus-TCP.h
- Revision:
- 3:ebea8e061ae6
- Parent:
- 2:fcd20e2cd110
- Child:
- 4:41ee166a0e6c
--- a/ModBus-TCP.h Thu Jun 29 10:17:29 2017 +0000 +++ b/ModBus-TCP.h Fri Jun 30 08:17:01 2017 +0000 @@ -22,21 +22,24 @@ #include "mbed.h" #include "rtos.h" #include "NetworkStack.h" -#include "EthInterface.h" -#include "CBuffer.h" +#include "EthernetInterface.h" +//#include "CBuffer.h" -#define DEBUG +//#define DEBUG #define MODBUS_TCP_PORT 502 -#define MODBUS_MAX_CLIENTS 5 +#define MODBUS_MAX_CLIENTS 3 #define MODBUS_KEEPALIVE 10 -#define MODBUS_TIMEOUT 15000 -#define MODBUS_MAX_HANDLES 10 +#define MODBUS_TIMEOUT 50000 +#define MODBUS_MAX_HANDLES 2 + +#define MODBUS_MAX_REGISTER 256 + #define MODBUS_CMD_SIZE 100 -#define MODBUS_BUF_SIZE 256 +#define MODBUS_BUF_SIZE 128 #define MODBUS_STACK_SIZE (1024 * 6) -//#define HTTPD_ENABLE_CLOSER +//efine HTTPD_ENABLE_CLOSER //Debug is disabled by default #if defined(DEBUG) and (!defined(TARGET_LPC11U24)) @@ -55,58 +58,32 @@ class Modbus { public: - enum Mode { - MODE_REQUEST, - MODE_REQSTR, - MODE_HEADER, - MODE_BODY, - MODE_ENTER, - MODE_ERROR, - MODE_WEBSOCKET, - MODE_WEBSOCKET_MASK, - MODE_WEBSOCKET_BODY, - MODE_WEBSOCKET_ENTER, - }; - struct STATE { Thread *thread; TCPSocket *client; - volatile Request req; - volatile Mode mode; - CircBuffer <char>*buf; + //volatile Request req; + //CircBuffer <char>*buf; - - char *querystring; - int enter; - int length; - int n; - int keepalive; + char start_message[8]; }; Modbus (); - int start (NetworkStack *ns, int port = MODBUS_TCP_PORT); + int start (EthernetInterface *ns, int port = MODBUS_TCP_PORT); // --- HTTPD_req.cpp --- void httpdError (int id, int err); // --- HTTPD_util.cpp --- - - - TCPSocket *getClientSocket(int id) { - if (id >= HTTPD_MAX_CLIENTS) return NULL; + if (id >= MODBUS_MAX_CLIENTS) return NULL; return _state[id].client; } - int send (int id, const char *body, int len, const char *header = NULL); - int sendstr (int id, const char *buf); - int hprintf(int id, const char* format, ...); - int receive (int id, char *buf, int len); - int attach (const char *uri, const char *dir); - int attach (const char *uri, void (*funcCgi)(int id)); - + int send (int id, unsigned short buffer[], int len); + + int attachRead (void (*funcCgi)(int id, unsigned short start_address, unsigned short length), int startRegister = 0); static Modbus * getInstance() { return _inst; @@ -117,7 +94,7 @@ Thread *_daemon; TCPServer _server; - NetworkStack *m_ns; + EthernetInterface *m_ns; #ifdef MODBUS_ENABLE_CLOSER struct STATE _state[MODBUS_MAX_CLIENTS + 1]; @@ -126,9 +103,9 @@ #endif struct HANDLER { - char *uri; - char *dir; - void (*funcCgi)(int id); + int startRegister; + void (*funcCgiRead)(int id, unsigned short start_address, unsigned short length); + void (*funcCgiWrite)(int id); } _handler[MODBUS_MAX_HANDLES]; int _handler_count; @@ -138,22 +115,15 @@ static void closer (void const *arg); // --- HTTPD_req.cpp --- - int httpdFile (int id, char *dir); - void recvData (int id, char c); - int parseRequest (int id); - int parseHeader (int id); - void reqContentLength (int id, const char *buf); - void reqConnection (int id, const char *buf); - void reqUpgrade (int id, const char *buf); - void reqWebSocketVersion (int id, const char *buf); - void reqWebSocketKey (int id, const char *buf); + + void recvData (int id, char buf[], int size); + int mb_exception_handler(int id, char data_in[], unsigned short length, char error); // --- HTTPD_util.cpp --- - int getHandler (const char *uri); - char *mimetype (char *file); - int strnicmp (const char *p1, const char *p2, int n); + int getHandler (int startRegister); + int sendToClient(int id, char buffer[], unsigned short len); int from_hex (int ch); int to_hex (int code); };