You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
testmodbus-server/main.cc

108 lines
2.4 KiB

/////////////////////////////////////////////////////////////////////////////////
//
// main.cc is part of TestModbus-Server.
//
/////////////////////////////////////////////////////////////////////////////////
#include <sys/time.h>
#include "config.h"
#include "mbsconfig.h"
#include "gui.h"
#include "modbus.h"
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// global variables
//
Config config;
Modbus modbus;
GtkBuilder *_builder_ = NULL; // work around for the thread situation
uint16_t modbusdata[4][0x10000]; // needed for work with the gui will by synced by the modbus call back functions
gboolean modbus_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-Server - %s\n", VERSION);
printf (" https://steffen.gulpe.de/modbus-tcpip\n");
printf (" written by Steffen Pohle <steffen@gulpe.de>\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
//
modbus.SetCallback(&modbus_callback);
window = gtk_builder_get_object (builder, "testmodbus-server");
gtk_widget_show_all (GTK_WIDGET(window));
if (argc == 2) {
printf ("Load File:%s\n", argv[1]);
config.SetFilename(argv[1]);
load_file(config.GetFilename());
}
else {
printf ("Load File:default.modbus\n");
config.SetFilename("default.modbus");
load_file(config.GetFilename());
}
gtk_main ();
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;
MBData_ChangeRegs(mdata->fc, mdata->regstart, mdata->count, mdata->r);
Values_ChangeRegs(mdata->fc, mdata->regstart, mdata->count, mdata->r);
if (mdata->r) free (mdata->r);
free (mdata);
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;
}