fixed: ignore faulty requests

master
Steffen Pohle 2 years ago
parent 1d3f575df5
commit 49a1b404ee

@ -1,3 +1,6 @@
2024-02-22:
- fixed: ignore incorrect requests
2023-09-19: 2023-09-19:
- performance issues fixed. - performance issues fixed.
- gui issues if more than 200 values where added fixed. - gui issues if more than 200 values where added fixed.

@ -343,6 +343,7 @@ gboolean cb_thread_network_data_add (gpointer data) {
GtkTextTag *tag_data; GtkTextTag *tag_data;
GtkTextTag *tag_info; GtkTextTag *tag_info;
GtkTextTag *tag_modbus; GtkTextTag *tag_modbus;
GtkTextTag *tag_error;
time_t _tm =time(NULL); time_t _tm =time(NULL);
struct tm * curtime = localtime (&_tm); struct tm * curtime = localtime (&_tm);
static int _once = 0; static int _once = 0;
@ -355,6 +356,8 @@ gboolean cb_thread_network_data_add (gpointer data) {
"foreground", "black", "style", PANGO_WEIGHT_NORMAL, "family", "Monospace", NULL); "foreground", "black", "style", PANGO_WEIGHT_NORMAL, "family", "Monospace", NULL);
tag_info = gtk_text_buffer_create_tag (textbuffer, "Info", tag_info = gtk_text_buffer_create_tag (textbuffer, "Info",
"foreground", "green", "style", PANGO_WEIGHT_THIN, "family", "Sans", NULL); "foreground", "green", "style", PANGO_WEIGHT_THIN, "family", "Sans", NULL);
tag_error = gtk_text_buffer_create_tag (textbuffer, "Error",
"foreground", "red", "style", PANGO_WEIGHT_BOLD, "family", "Sans", NULL);
tag_modbus = gtk_text_buffer_create_tag (textbuffer, "Modbus", tag_modbus = gtk_text_buffer_create_tag (textbuffer, "Modbus",
"foreground", "black", "style", PANGO_WEIGHT_NORMAL, "family", "Sans", NULL); "foreground", "black", "style", PANGO_WEIGHT_NORMAL, "family", "Sans", NULL);
} }
@ -372,11 +375,12 @@ gboolean cb_thread_network_data_add (gpointer data) {
else if (i % 32 == 0) text += "\n "; else if (i % 32 == 0) text += "\n ";
else if (i % 4 == 0 && i > 0) text += " : "; else if (i % 4 == 0 && i > 0) text += " : ";
else if (i % 2 == 0 && i > 0) text += ":"; else if (i % 2 == 0 && i > 0) text += ":";
// else text += " ";
text += hexnum[c/16]; text += hexnum[c/16];
text += hexnum[c%16]; text += hexnum[c%16];
} }
text += "\n";
gtk_text_buffer_get_start_iter(textbuffer, &start); gtk_text_buffer_get_start_iter(textbuffer, &start);
gtk_text_buffer_insert_with_tags_by_name(textbuffer, &start, text.c_str(), -1, "Data", NULL); gtk_text_buffer_insert_with_tags_by_name(textbuffer, &start, text.c_str(), -1, "Data", NULL);
@ -407,20 +411,30 @@ gboolean cb_thread_network_data_add (gpointer data) {
text = text + " Cnt: " + std::to_string (mbdata->regcnt); text = text + " Cnt: " + std::to_string (mbdata->regcnt);
} }
} }
text = text + "\n"; text = text + " Bytecnt: "+ std::to_string (mbdata->bytecnt) +"\n";
gtk_text_buffer_get_start_iter(textbuffer, &start); gtk_text_buffer_get_start_iter(textbuffer, &start);
gtk_text_buffer_insert_with_tags_by_name(textbuffer, &start, text.c_str(), -1, "Modbus", NULL); gtk_text_buffer_insert_with_tags_by_name(textbuffer, &start, text.c_str(), -1, "Modbus", NULL);
// //
// number of bytes
text = std::to_string(mbdata->bufferlen) + " Bytes";
gtk_text_buffer_get_start_iter(textbuffer, &start);
gtk_text_buffer_insert_with_tags_by_name(textbuffer, &start, text.c_str(), -1, "Info", NULL);
// //
text =""; // Hostname
text += mbdata->hostname; text = mbdata->hostname;
if (mbdata->direction == 0) text = text + " RECV ";
else text = text + " SEND ";
text = text + std::to_string(mbdata->bufferlen) + " Bytes";
gtk_text_buffer_get_start_iter(textbuffer, &start); gtk_text_buffer_get_start_iter(textbuffer, &start);
gtk_text_buffer_insert_with_tags_by_name(textbuffer, &start, text.c_str(), -1, "Info", NULL); gtk_text_buffer_insert_with_tags_by_name(textbuffer, &start, text.c_str(), -1, "Info", NULL);
//
// direction or error
if (mbdata->direction == 0)
gtk_text_buffer_insert_with_tags_by_name(textbuffer, &start, (const char*)" RECV ", -1, "Info", NULL);
else if (mbdata->direction == 1)
gtk_text_buffer_insert_with_tags_by_name(textbuffer, &start, (const char*)" SEND ", -1, "Info", NULL);
else
gtk_text_buffer_insert_with_tags_by_name(textbuffer, &start, (const char*)" ** ERROR **", -1, "Error", NULL);
// //
// add date // add date

@ -123,24 +123,6 @@ void ModbusSrv::SetCallback (gboolean (*callback_func)(gpointer data)) {
onchangecallback = callback_func; onchangecallback = callback_func;
} }
/***************************************************************************************************
* this should only be called from within the ServerThread function
*/
void ModbusSrv::ReadData(gpointer pdata) {
int slot;
long int len;
for (slot = 0; slot < MODBUS_MAXCLIENTS; slot++) if (&clients[slot] == pdata) break;
if (slot < 0 || slot >= MODBUS_MAXCLIENTS) return; // slot out of bound
if (clients[slot] == NULL) return; // slot not connected?
len = clients[slot]->Read(inbuffer, MODBUS_IOBUFFER);
if (len < 0) {
CloseConnection(slot);
}
};
void ModbusSrv::ServerThread() { void ModbusSrv::ServerThread() {
int keep_running = 1; int keep_running = 1;
@ -232,9 +214,12 @@ void ModbusSrv::ServerThread() {
mbindata->transactionid = 0; mbindata->transactionid = 0;
mbindata->protoolid = 0; mbindata->protoolid = 0;
Decode(mbindata); if (Decode(mbindata) == 0) {
mbindata->direction = 2; // mark as error
gdk_threads_add_idle(cb_thread_network_data_add, mbindata);
continue;
}
gdk_threads_add_idle(cb_thread_network_data_add, mbindata); gdk_threads_add_idle(cb_thread_network_data_add, mbindata);
// //
// fill in outdata // fill in outdata
// //
@ -677,12 +662,14 @@ int ModbusSrv::WorkerAndEncodeWriteMulti(struct modbus_data *mbin, struct modbus
} }
//
void ModbusSrv::Decode(struct modbus_data *mbdata) { // return 1 on success, 0 on error
int ModbusSrv::Decode(struct modbus_data *mbdata) {
// //
uint16_t i16; uint16_t i16;
uint8_t i8; uint8_t i8;
int pos = 0; int pos = 0;
int ret = 1;
mbdata->fc = 0; mbdata->fc = 0;
mbdata->regcnt = -1; mbdata->regcnt = -1;
@ -694,7 +681,7 @@ void ModbusSrv::Decode(struct modbus_data *mbdata) {
mbdata->protoolid = 0; mbdata->protoolid = 0;
mbdata->bytecnt = 0; mbdata->bytecnt = 0;
if (mbdata->bufferlen < 8) return; if (mbdata->bufferlen < 8) return 0;
// Transaction // Transaction
memcpy (&i16, mbdata->buffer+pos, sizeof(i16)); memcpy (&i16, mbdata->buffer+pos, sizeof(i16));
@ -712,29 +699,36 @@ void ModbusSrv::Decode(struct modbus_data *mbdata) {
mbdata->length = ntohs(i16); mbdata->length = ntohs(i16);
// unitid // unitid
if (mbdata->length < pos-6) ret = 0;
memcpy (&i8, mbdata->buffer+pos, sizeof(i8)); memcpy (&i8, mbdata->buffer+pos, sizeof(i8));
pos += sizeof(i8); pos += sizeof(i8);
mbdata->unitid = i8; mbdata->unitid = i8;
// function code // function code
if (mbdata->length < pos-6) ret = 0;
memcpy (&i8, mbdata->buffer+pos, sizeof(i8)); memcpy (&i8, mbdata->buffer+pos, sizeof(i8));
pos += sizeof(i8); pos += sizeof(i8);
mbdata->fc = i8; mbdata->fc = i8;
// register // register
if (mbdata->length < pos-6) ret = 0;
memcpy (&i16, mbdata->buffer+pos, sizeof(i16)); memcpy (&i16, mbdata->buffer+pos, sizeof(i16));
pos += sizeof(i16); pos += sizeof(i16);
mbdata->regstart = ntohs(i16); mbdata->regstart = ntohs(i16);
// number of registers // number of registers
if (mbdata->length < pos-6) ret = 0;
memcpy (&i16, mbdata->buffer+pos, sizeof(i16)); memcpy (&i16, mbdata->buffer+pos, sizeof(i16));
pos += sizeof(i16); pos += sizeof(i16);
mbdata->regcnt = ntohs(i16); mbdata->regcnt = ntohs(i16);
// unitid // bytecnt
// if (mbdata->length < pos-6) ret = 0;
memcpy (&i8, mbdata->buffer+pos, sizeof(i8)); memcpy (&i8, mbdata->buffer+pos, sizeof(i8));
pos += sizeof(i8); pos += sizeof(i8);
mbdata->bytecnt = i8; mbdata->bytecnt = i8;
return ret;
}; };
int ModbusSrv::GetRegister(int fc, int regnum, ModbusRegister *r) { int ModbusSrv::GetRegister(int fc, int regnum, ModbusRegister *r) {

@ -36,7 +36,7 @@ struct modbus_data {
int protoolid; int protoolid;
int length; int length;
int unitid; int unitid;
int direction; int direction; // 0 - RECV, 1 - SEND, 2 - ERROR
int fc; int fc;
int regstart; int regstart;
int regcnt; int regcnt;
@ -77,8 +77,8 @@ private:
char inbuffer[MODBUS_IOBUFFER]; char inbuffer[MODBUS_IOBUFFER];
char outbuffer[MODBUS_IOBUFFER]; char outbuffer[MODBUS_IOBUFFER];
void Decode(struct modbus_data *mbdata); int Decode(struct modbus_data *mbdata);
// return 0 on error, 1 on no error // return 0 on error, 1 on success
int WorkerAndEncodeRead(struct modbus_data *mbin, struct modbus_data *mbout); int WorkerAndEncodeRead(struct modbus_data *mbin, struct modbus_data *mbout);
int WorkerAndEncodeWriteSingle(struct modbus_data *mbin, struct modbus_data *mbout); int WorkerAndEncodeWriteSingle(struct modbus_data *mbin, struct modbus_data *mbout);
int WorkerAndEncodeWriteMulti(struct modbus_data *mbin, struct modbus_data *mbout); int WorkerAndEncodeWriteMulti(struct modbus_data *mbin, struct modbus_data *mbout);
@ -97,7 +97,7 @@ public:
int SetRegister(int fc, int regnum, ModbusRegister *r); int SetRegister(int fc, int regnum, ModbusRegister *r);
int SetRegisters(int fc, int regnum, int count, ModbusRegister *r); int SetRegisters(int fc, int regnum, int count, ModbusRegister *r);
void CloseConnection(int slot); void CloseConnection(int slot);
void ReadData(gpointer pdata); // void ReadData(gpointer pdata);
void Enable(int fc, int regstart, int count, int onoff); void Enable(int fc, int regstart, int count, int onoff);
void EnableAll(int onoff); void EnableAll(int onoff);

Loading…
Cancel
Save