From 018d96efb6aa4ddf6c3a91560fd4fcdbca968647 Mon Sep 17 00:00:00 2001 From: Steffen Pohle Date: Sat, 5 Aug 2023 13:20:42 +0200 Subject: [PATCH] preparation for testmodbus-client --- Makefile | 21 ++- Makefile.rules.crosswindows | 3 +- Makefile.rules.linux | 4 +- Makefile.rules.windows | 4 +- client.cc | 54 +++++++ copydlls.sh | 1 + gui.cc | 2 +- gui.h | 2 - guimodbusdata.cc | 2 +- guimodbusdata.h | 2 +- guivalues.cc | 2 +- guivalues.h | 2 +- mbsconfig.h | 2 +- modbus.cc => modbussrv.cc | 33 ++++- modbus.h => modbussrv.h | 10 ++ server.cc | 32 +--- testmodbus-client.ui | 288 ++++++++++++++++++++++++++++++++++++ 17 files changed, 414 insertions(+), 50 deletions(-) create mode 100644 client.cc rename modbus.cc => modbussrv.cc (98%) rename modbus.h => modbussrv.h (95%) create mode 100644 testmodbus-client.ui diff --git a/Makefile b/Makefile index 708ccc3..b14ce9e 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,18 @@ .SILENT: help VERSION = 1.0.1 -APP = testmodbus-server -include Makefile.rules -OBJECTS = gui.oo server.oo mbsconfig.oo modbus.oo guimodbusdata.oo guivalues.oo json.oo tcp.oo +OBJECTSSRV = gui.oo server.oo mbsconfig.oo modbussrv.oo guimodbusdata.oo guivalues.oo json.oo tcp.oo +OBJECTSCLI = client.oo tcp.oo + DISTNAME=testmodbus-server-$(VERSION) -ifeq ($(TARGET),) +ifeq ($(CONFIGSET),) noconfig: help endif -all: Makefile.rules $(TARGET) +all: Makefile.rules testmodbus-server$(TARGETEXT) testmodbus-client$(TARGETEXT) help: echo "set up configuration" @@ -41,8 +42,11 @@ config: Makefile.rules echo "#endif" >> config.h -$(TARGET): $(OBJECTS) - $(CPP) -o $(TARGET) $(OBJECTS) $(LDFLAGS) $(LIBS) +testmodbus-server$(TARGETEXT): $(OBJECTSSRV) + $(CPP) -o testmodbus-server$(TARGETEXT) $(OBJECTSSRV) $(LDFLAGS) $(LIBS) + +testmodbus-client$(TARGETEXT): $(OBJECTSCLI) + $(CPP) -o testmodbus-client$(TARGETEXT) $(OBJECTSCLI) $(LDFLAGS) $(LIBS) .SUFFIXES: .SUFFIXES: .c .cc .C .cpp .oo @@ -52,7 +56,7 @@ $(TARGET): $(OBJECTS) clean: rm -rf TestModbus-Server-$(VERSION)/ - rm -f *.o *.oo *.c~ *.h~ *.cc~ *.ui~ $(APP) Makefile~ + rm -f *.o *.oo *.c~ *.h~ *.cc~ *.ui~ testmodbus-server$(TARGETEXT) testmodbus-client$(TARGETEXT) Makefile~ rm -rf *.dll rm -rf *.exe rm -rf Makefile.rules @@ -95,6 +99,9 @@ buildwindows: clean cp Changelog TestModbus-Server-$(VERSION)/ cp LICENSE TestModbus-Server-$(VERSION)/ ./copydlls.sh + ./copydlls.sh + ./copydlls.sh + ./copydlls.sh ./copyshare.sh mv *.dll TestModbus-Server-$(VERSION)/ mv share TestModbus-Server-$(VERSION)/ diff --git a/Makefile.rules.crosswindows b/Makefile.rules.crosswindows index f35fbea..58a2bbe 100644 --- a/Makefile.rules.crosswindows +++ b/Makefile.rules.crosswindows @@ -1,5 +1,6 @@ -TARGET = $(APP).exe +TARGETEXT = .exe +CONFIGSET = YES CROSSENV = /opt/W64-cross-compile/ CPP = /usr/bin/x86_64-w64-mingw32-g++ CPPFLAGS = -ggdb -Wall -O0 `PKG_CONFIG_PATH=$(CROSSENV)/lib/pkgconfig pkg-config --cflags gtk+-3.0 gmodule-export-2.0` -Wl,--export-dynamic -DBUILD_WINDOWS=1 -Wdeprecated diff --git a/Makefile.rules.linux b/Makefile.rules.linux index 081b8a9..b5e0556 100644 --- a/Makefile.rules.linux +++ b/Makefile.rules.linux @@ -1,6 +1,6 @@ -TARGET = $(APP) - +TARGETEXT = +CONFIGSET = YES CPP = c++ CPPFLAGS = -ggdb -Wall -O0 `pkg-config --cflags gtk+-3.0 gmodule-export-2.0` -Wl,--export-dynamic -I/usr/include -DBUILD_LINUX=1 INCLUDES = diff --git a/Makefile.rules.windows b/Makefile.rules.windows index 883e91d..826eb30 100644 --- a/Makefile.rules.windows +++ b/Makefile.rules.windows @@ -1,6 +1,6 @@ -TARGET = $(APP).exe - +TARGETEXT = .exe +CONFIGSET = YES CPP = g++ CPPFLAGS = -ggdb -Wall -O0 `pkg-config --cflags gtk+-3.0 gmodule-export-2.0` -Wl,--export-dynamic -I/usr/include -DBUILD_WINDOWS=1 INCLUDES = diff --git a/client.cc b/client.cc new file mode 100644 index 0000000..401cb05 --- /dev/null +++ b/client.cc @@ -0,0 +1,54 @@ +#include + +#include "config.h" +#include "gui.h" + +#define BUILDER_FILE "testmodbus-client.ui" + +/************************************************************************************ + * Global Variables + */ +ModbusCli modbuscli; +GtkBuilder *_builder_ = NULL; // work around for the thread situation + +gboolean modbuscli_callback(gpointer data); + +int main (int argc, char **argv) { + GtkBuilder *builder; + GObject *window; + +#ifdef BUILD_WINDOWS + char buffer[16]; + setvbuf (stdout, buffer, _IONBF, 16); +#endif + + printf ("TestModbus-Client - %s\n", VERSION); + printf (" https://steffen.gulpe.de/modbus-tcpip\n"); + printf (" written by Steffen Pohle \n"); + + gtk_init (&argc, &argv); + _builder_ = builder = gtk_builder_new (); + gtk_builder_add_from_file (builder, BUILDER_FILE, NULL); + gtk_builder_connect_signals(builder, builder); + + // + // #if defined _WIN32 || defined _WIN64 || defined __CYGWIN__ + // #else + // #endif + // + +// modbuscli.SetCallback(&modbus_callback); + window = gtk_builder_get_object (builder, "testmodbus-client"); + gtk_widget_show_all (GTK_WIDGET(window)); + gtk_main (); + + return 0; +} + + +gboolean modbuscli_callback(gpointer data) { + struct modbus_callback_data *mdata = (struct modbus_callback_data *) data; + free (mdata); + + return FALSE; +}; diff --git a/copydlls.sh b/copydlls.sh index c11a144..7b7cbb4 100755 --- a/copydlls.sh +++ b/copydlls.sh @@ -35,4 +35,5 @@ copy_dependency() { } copy_dependency testmodbus-server.exe +copy_dependency testmodbus-client.exe diff --git a/gui.cc b/gui.cc index 98c1369..046655a 100644 --- a/gui.cc +++ b/gui.cc @@ -11,7 +11,7 @@ #include #include #include "gui.h" -#include "modbus.h" +#include "modbussrv.h" #include "guivalues.h" #include "json.h" #include "mbsconfig.h" diff --git a/gui.h b/gui.h index cc6188e..5b0fc3c 100644 --- a/gui.h +++ b/gui.h @@ -15,8 +15,6 @@ #include #include -#define BUILDER_FILE "testmodbus-server.ui" - #include "guivalues.h" #include "guimodbusdata.h" std::string to_hex16 (int v); diff --git a/guimodbusdata.cc b/guimodbusdata.cc index 7b818e3..b1019b1 100644 --- a/guimodbusdata.cc +++ b/guimodbusdata.cc @@ -12,7 +12,7 @@ #include "gui.h" #include "guimodbusdata.h" -#include "modbus.h" +#include "modbussrv.h" extern ModbusSrv modbussrv; diff --git a/guimodbusdata.h b/guimodbusdata.h index e2bc8f4..7b67155 100644 --- a/guimodbusdata.h +++ b/guimodbusdata.h @@ -8,7 +8,7 @@ #define _GUIMODBUSDATA_H_ #include "gui.h" -#include "modbus.h" +#include "modbussrv.h" #include "guivalues.h" enum { diff --git a/guivalues.cc b/guivalues.cc index 701f2a8..8e9415b 100644 --- a/guivalues.cc +++ b/guivalues.cc @@ -16,7 +16,7 @@ #include "gui.h" #include "config.h" #include "mbsconfig.h" -#include "modbus.h" +#include "modbussrv.h" #include "guivalues.h" void Value_ModStore(GtkTreeModel *model, GtkTreeIter *iter, GuiValue *g); diff --git a/guivalues.h b/guivalues.h index 386563f..1ff2d96 100644 --- a/guivalues.h +++ b/guivalues.h @@ -8,7 +8,7 @@ #define _GUIVALUE_H_ #include "gui.h" -#include "modbus.h" +#include "modbussrv.h" #include enum { diff --git a/mbsconfig.h b/mbsconfig.h index abcebde..a18886d 100644 --- a/mbsconfig.h +++ b/mbsconfig.h @@ -12,7 +12,7 @@ #define _MBDCONFIG_H_ #include "config.h" -#include "modbus.h" +#include "modbussrv.h" #include // maybe soon internationalisation diff --git a/modbus.cc b/modbussrv.cc similarity index 98% rename from modbus.cc rename to modbussrv.cc index 61219d5..d49687a 100644 --- a/modbus.cc +++ b/modbussrv.cc @@ -6,16 +6,47 @@ #include #include +#include + #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) #else #include /* close() */ #endif #include "gui.h" -#include "modbus.h" +#include "modbussrv.h" #include "mbsconfig.h" #include "config.h" + +std::string to_hex16 (int v) { + char HEX[] = "0123456789ABCDEF"; + int i = v; + int n; + std::string txt = ""; + + for (n = 0; n < 4; n++) { + txt = HEX[i%16]+ txt; + i = i / 16; + } + + return txt; +} + + +float get_cycletime(struct timeval *t) { + struct timeval t1; + float f = 0.0; + + t1 = *t; + gettimeofday(t, NULL); + f = (float)(t->tv_sec - t1.tv_sec) + ((t->tv_usec - t1.tv_usec) / 1000000.0); + + return f; +} + + + // // C / C++ Wrapper gpointer _ServerThread (gpointer data) { diff --git a/modbus.h b/modbussrv.h similarity index 95% rename from modbus.h rename to modbussrv.h index 9e27dcb..831c279 100644 --- a/modbus.h +++ b/modbussrv.h @@ -45,6 +45,7 @@ struct modbus_data { // return the cycletime in us +std::string to_hex16 (int v); float get_cycletime(struct timeval *t); struct { @@ -106,6 +107,15 @@ public: int isRunning(); }; + +class ModbusCli { +private: +public: + ModbusCli() {}; + ~ModbusCli() {}; +}; + + #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) #define usleep(_us_) Sleep(_us_/1000) #endif diff --git a/server.cc b/server.cc index 8e08332..0c2e90b 100644 --- a/server.cc +++ b/server.cc @@ -4,18 +4,18 @@ // ///////////////////////////////////////////////////////////////////////////////// -#include - #include "config.h" #include "mbsconfig.h" #include "gui.h" -#include "modbus.h" +#include "modbussrv.h" ////////////////////////////////////////////////////////////////////////////////////////////////// // // global variables // +#define BUILDER_FILE "testmodbus-server.ui" + Config config; ModbusSrv modbussrv; GtkBuilder *_builder_ = NULL; // work around for the thread situation @@ -67,20 +67,6 @@ int main (int argc, char **argv) { return 0; } -string to_hex16 (int v) { - char HEX[] = "0123456789ABCDEF"; - int i = v; - int n; - string txt = ""; - - for (n = 0; n < 4; n++) { - txt = HEX[i%16]+ txt; - i = i / 16; - } - - return txt; -} - gboolean modbus_callback(gpointer data) { struct modbus_callback_data *mdata = (struct modbus_callback_data *) data; @@ -93,15 +79,3 @@ gboolean modbus_callback(gpointer data) { return FALSE; }; - - -float get_cycletime(struct timeval *t) { - struct timeval t1; - float f = 0.0; - - t1 = *t; - gettimeofday(t, NULL); - f = (float)(t->tv_sec - t1.tv_sec) + ((t->tv_usec - t1.tv_usec) / 1000000.0); - - return f; -} diff --git a/testmodbus-client.ui b/testmodbus-client.ui new file mode 100644 index 0000000..a219041 --- /dev/null +++ b/testmodbus-client.ui @@ -0,0 +1,288 @@ + + + + + + False + + + True + False + vertical + + + True + False + 4 + 4 + 4 + 4 + 0 + + + True + False + 12 + + + True + False + vertical + 4 + + + True + False + 4 + + + True + False + vertical + 4 + + + True + False + 4 + + + True + False + Host: + + + False + True + 0 + + + + + True + True + localhost + + + False + True + 1 + + + + + True + False + Port: + + + False + True + 2 + + + + + True + True + 6 + 502 + + + False + True + 3 + + + + + False + True + 0 + + + + + True + False + 4 + + + True + False + Refresh [ms]: + + + False + True + 0 + + + + + True + True + 4 + 1000 + + + False + True + 1 + + + + + False + True + 1 + + + + + False + True + 0 + + + + + True + False + vertical + + + Auto Reconnect + True + True + False + True + True + + + False + True + 0 + + + + + Read Only + True + True + False + True + True + + + False + True + 1 + + + + + + + + False + True + 1 + + + + + True + False + vertical + 3 + + + gtk-connect + True + True + True + True + True + + + False + True + 0 + + + + + gtk-disconnect + True + False + True + True + True + True + + + False + True + 1 + + + + + False + True + end + 2 + + + + + False + True + 0 + + + + + True + False + last error | status + + + False + False + 1 + + + + + + + + + True + False + Destination Host + + + + + + + + + False + False + 0 + + + + + + + + + + + +