diff --git a/client.cc b/client.cc index 6a3d0ff..57bd003 100644 --- a/client.cc +++ b/client.cc @@ -27,7 +27,7 @@ int num_requests = 0; // request counter NetworkThread netthread; -std::string to_hex8 (char v) { +std::string to_hex8 (uint8_t v) { char HEX[] = "0123456789ABCDEF"; int i = v; int n; @@ -41,7 +41,7 @@ std::string to_hex8 (char v) { return txt; } -std::string to_hex16 (int v) { +std::string to_hex16 (uint16_t v) { char HEX[] = "0123456789ABCDEF"; int i = v; int n; @@ -123,7 +123,13 @@ gboolean mbcli_thread_cb_net(gpointer data) { char tmp[255]; printf ("%s:%d %s got some data\n", __FILE__, __LINE__, __FUNCTION__); - gtk_text_buffer_get_start_iter(textbuffer, &start); + + if (data == NULL) { + fprintf (stderr, "%s:%d data not set\n", __FILE__, __LINE__); + return FALSE; + } + + gtk_text_buffer_get_start_iter(textbuffer, &start); gtk_text_buffer_get_end_iter(textbuffer, &end); gtk_text_buffer_delete(textbuffer, &start, &end); diff --git a/client.h b/client.h index c1a2255..d622b36 100644 --- a/client.h +++ b/client.h @@ -53,8 +53,8 @@ void mbcli_set_update (int active); int mbcli_get_FC1516 (); void mbcli_readdata(); gboolean mbcli_refreshtimeout(gpointer data); -std::string to_hex16 (int v); -std::string to_hex8 (char v); +std::string to_hex16 (uint16_t v); +std::string to_hex8 (uint8_t v); G_MODULE_EXPORT void mbcli_cb_show(GtkWidget *widget, gpointer data); G_MODULE_EXPORT void mbcli_cb_updateread (GtkToggleButton *togglebutton, gpointer user_data); diff --git a/log.cc b/log.cc index e4566bc..bb16e2c 100644 --- a/log.cc +++ b/log.cc @@ -21,6 +21,11 @@ void log_addtext (int logtype, std::string text, NWTReqResult *data) { struct tm * curtime = localtime (&_tm); static int _once = 0; + if (textview == NULL || textbuffer == NULL) { + fprintf (stderr, "%s:%d textview (%p) or textbuffer (%p) empty.\n", __FILE__, __LINE__, textview, textbuffer); + exit (1); + } + if (_once == 0) { _once = 1; tag_datetime = gtk_text_buffer_create_tag (textbuffer, "LogDateTime", @@ -40,12 +45,14 @@ void log_addtext (int logtype, std::string text, NWTReqResult *data) { // display data if there are any // if (data != NULL && data->rawdatalen > 0 && data->rawdata != NULL) { - str = ""; + printf ("%s rawdatalen:%d rawdata:%p\n", __FUNCTION__, data->rawdatalen, data->rawdata); + str = ""; for (int i = 0; i < data->rawdatalen; i++) { - if (i & 16) str += "\n "; - else if (i & 4 && i > 0) str += " "; + if ((i % 16) == 0) str += "\n "; + else if ((i % 4) == 0 && i > 0) str += " "; str += to_hex8(data->rawdata[i]); } + printf ("\n%s:%d \n'%s'\n", __FILE__, __LINE__, str.c_str()); gtk_text_buffer_insert_with_tags_by_name(textbuffer, &start, str.c_str(), -1, "LogData", NULL); } diff --git a/nwthread.cc b/nwthread.cc index 7e3c628..75e66db 100644 --- a/nwthread.cc +++ b/nwthread.cc @@ -193,7 +193,6 @@ void NetworkThread::BuildAndSendReadReq(char *buffer, int size) { char *pos; NWTReqResult *res = (NWTReqResult*) malloc(sizeof (NWTReqResult)); - // req_mbh.transid++; // with modbus tcp/ip it should always be set to 0 req_mbh.transid = 0; pos = modbustcp_pkt_headerrq(buffer, size, &req_mbh);