diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e455f32 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +debian/files +debian/modbus-utils* +modbus_client/modbus_client +modbus_server/modbus_server diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2be773b --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +DESTDIR=/ +prefix=usr + +ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE)) + CC=gcc +else + CC=$(DEB_HOST_GNU_TYPE)-gcc +endif + +CC_FLAGS=-I../common -I/usr/include/modbus -L../common -lmodbus + +all: client server + +client: + $(MAKE) -C modbus_client + +server: + $(MAKE) -C modbus_server + +install: $(BIN_NAME) + $(MAKE) -C modbus_client install + $(MAKE) -C modbus_server install + + +clean: + $(MAKE) -C modbus_client clean + $(MAKE) -C modbus_server clean + +.PHONY: install clean all \ No newline at end of file diff --git a/common/mbu-common.h b/common/mbu-common.h index 483c542..ee9f48a 100644 --- a/common/mbu-common.h +++ b/common/mbu-common.h @@ -14,9 +14,12 @@ typedef enum { int getInt(const char str[], int *ok) { int value; int ret = sscanf(str, "0x%x", &value); - if (0 >= ret) {//couldn't convert from hex, try dec - ret = sscanf(str, "%d", &value); - } + if (0 >= ret) { // couldn't convert from hex, try dec + if (strstr(str, ".")) // dotted ip address + ret = 0; + else + ret = sscanf(str, "%d", &value); + } if (0 != ok) { *ok = (0 < ret); @@ -55,7 +58,7 @@ int setRtuParam(void *backend, char c, char *value) { switch (c) { case 'b': { rtuParams->baud = getInt(value, &ok); - if (0 != ok) { + if (0 == ok) { printf("Baudrate is invalid %s", value); ok = 0; } diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..6b99667 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,53 @@ + +modbus-utils (1.2.5) stable; urgency=medium + + * bugfix + + -- Andrey Radionov Mon, 17 May 2021 19:14:12 +0300 + +modbus-utils (1.2.4) stable; urgency=medium + + * added check if any value is passed to write function + * improved parsing args for port + + -- Vladimir Romanov Sun, 27 Jan 2018 18:33:12 +0300 + +modbus-utils (1.2.3) stable; urgency=medium + + * disable annoying "timeout is set" information message + + -- Evgeny Boger Wed, 21 Dec 2016 14:24:32 +0300 + +modbus-utils (1.2.2) stable; urgency=medium + + * exit with EXIT_FAILURE in case Modbus request is failed + + -- Evgeny Boger Wed, 21 Dec 2016 13:16:41 +0300 + +modbus-utils (1.2.1) stable; urgency=medium + + * fix timeout handling + + -- Evgeny Boger Wed, 22 Jul 2015 22:53:15 +0300 + +modbus-utils (1.2) stable; urgency=medium + + * fix baud rate handling + + -- Evgeny Boger Thu, 09 Jul 2015 19:55:45 +0300 + +modbus-utils (1.1) wheezy; urgency=low + + * Add set-rd-address.sh script + * Fix ReadDiscreteInput function + (was) Ivan Shvedunov Wed, 06 Aug 2014 10:15:01 +0400 + + -- Evgeny Boger Wed, 06 Aug 2014 10:15:01 +0400 + +modbus-utils (1.0) wheezy; urgency=low + + * Initial debianization using https://github.com/Krzysztow/modbus-utils + + -- Evgeny Boger Tue, 29 Apr 2014 01:59:04 +0400 + + diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +9 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..11c6fb3 --- /dev/null +++ b/debian/control @@ -0,0 +1,11 @@ +Source: modbus-utils +Maintainer: Evgeny Boger +Section: misc +Priority: optional +Standards-Version: 3.9.2 +Build-Depends: debhelper (>= 9), pkg-config, libmodbus-dev + +Package: modbus-utils +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends}, libmodbus5 +Description: Modbus client and server command line tools based on libmodbus \ No newline at end of file diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..e69de29 diff --git a/debian/modbus-utils.dirs b/debian/modbus-utils.dirs new file mode 100644 index 0000000..98d1583 --- /dev/null +++ b/debian/modbus-utils.dirs @@ -0,0 +1,2 @@ +usr/bin +usr/share/man/man1 diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..cbe925d --- /dev/null +++ b/debian/rules @@ -0,0 +1,3 @@ +#!/usr/bin/make -f +%: + dh $@ diff --git a/debian/source/format b/debian/source/format new file mode 100644 index 0000000..9f67427 --- /dev/null +++ b/debian/source/format @@ -0,0 +1 @@ +3.0 (native) \ No newline at end of file diff --git a/modbus_client/Makefile b/modbus_client/Makefile new file mode 100644 index 0000000..08f24c7 --- /dev/null +++ b/modbus_client/Makefile @@ -0,0 +1,25 @@ +DESTDIR=/ +prefix=usr +BIN_NAME=modbus_client + +ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE)) + CC=gcc +else + CC=$(DEB_HOST_GNU_TYPE)-gcc +endif + +CC_FLAGS=-I../common `pkg-config --libs --cflags libmodbus` + +$(BIN_NAME): modbus_client.c + $(CC) modbus_client.c $(CC_FLAGS) -o $(BIN_NAME) + + +install: $(BIN_NAME) + install -m 0755 $(BIN_NAME) $(DESTDIR)/$(prefix)/bin/$(BIN_NAME) + +clean: + -@rm $(BIN_NAME) +.PHONY: install clean all + + + diff --git a/modbus_client/modbus_client.c b/modbus_client/modbus_client.c index f42280d..284b7fb 100644 --- a/modbus_client/modbus_client.c +++ b/modbus_client/modbus_client.c @@ -51,6 +51,8 @@ int main(int argc, char **argv) int c; int ok; + int gotData = 0; //Data_to_write existance flag + int raw_data = 0; //While parsing not keyword args int debug = 0; BackendParams *backend = 0; int slaveAddr = 1; @@ -153,7 +155,6 @@ int main(int argc, char **argv) printUsage(argv[0]); exit(EXIT_FAILURE); } - printf("Timeout set to %d\r\n", timeout_ms); } break; @@ -194,14 +195,11 @@ int main(int argc, char **argv) if (1 == startReferenceAt0) { startAddr--; } - //choose write data type switch (fType) { case(ReadCoils): - wDataType = Data8Array; - break; case(ReadDiscreteInput): - wDataType = DataInt; + wDataType = Data8Array; break; case(ReadHoldingRegisters): case(ReadInputRegisters): @@ -256,8 +254,10 @@ int main(int argc, char **argv) if (1 == debug && 1 == isWriteFunction) printf("Data to write: "); if (optind < argc) { + while (optind < argc) { - if (0 == hasDevice) { + raw_data = getInt(argv[optind], &ok); + if (0 == hasDevice && ok == 0) { //Portname couldn't consist of only numbers if (0 != backend) { if (Rtu == backend->type) { RtuBackend *rtuP = (RtuBackend*)backend; @@ -271,21 +271,24 @@ int main(int argc, char **argv) } } } - else {//setting write data buffer + else {//Got int as data => setting write data buffer switch (wDataType) { case (DataInt): - data.dataInt = getInt(argv[optind], 0); + data.dataInt = raw_data; + gotData = 1; if (debug) printf("0x%x", data.dataInt); break; case (Data8Array): { - data.data8[wDataIdx] = getInt(argv[optind], 0); + data.data8[wDataIdx] = raw_data; + gotData = 1; if (debug) printf("0x%02x ", data.data8[wDataIdx]); } break; case (Data16Array): { - data.data16[wDataIdx] = getInt(argv[optind], 0); + data.data16[wDataIdx] = raw_data; + gotData = 1; if (debug) printf("0x%04x ", data.data16[wDataIdx]); } @@ -296,14 +299,28 @@ int main(int argc, char **argv) optind++; } } - if (1 == debug && 1 == isWriteFunction) - printf("\n"); + + if (isWriteFunction == 1){ + if (gotData == 0) { + printf("\nSeems you are using a write function\nDon't forget to specify the value!\n"); + exit(EXIT_FAILURE);} + if (debug == 1) + printf("\n"); + } //create modbus context, and preapare it modbus_t *ctx = backend->createCtxt(backend); modbus_set_debug(ctx, debug); modbus_set_slave(ctx, slaveAddr); + struct timeval response_timeout; + response_timeout.tv_sec = 0; + response_timeout.tv_usec = timeout_ms * 1000; + #if LIBMODBUS_VERSION_CHECK(3, 1, 2) + modbus_set_response_timeout(ctx, response_timeout.tv_sec, response_timeout.tv_usec); + #else + modbus_set_response_timeout(ctx, &response_timeout); + #endif //issue the request int ret = -1; if (modbus_connect(ctx) == -1) { @@ -317,8 +334,7 @@ int main(int argc, char **argv) ret = modbus_read_bits(ctx, startAddr, readWriteNo, data.data8); break; case(ReadDiscreteInput): - printf("ReadDiscreteInput: not implemented yet!\n"); - wDataType = DataInt; + ret = modbus_read_input_bits(ctx, startAddr, readWriteNo, data.data8); break; case(ReadHoldingRegisters): ret = modbus_read_registers(ctx, startAddr, readWriteNo, data.data16); @@ -344,8 +360,9 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } } + uint8_t success = (ret == readWriteNo); - if (ret == readWriteNo) {//success + if (success) { if (isWriteFunction) printf("SUCCESS: written %d elements!\n", readWriteNo); else { @@ -387,5 +404,5 @@ int main(int argc, char **argv) break; } - exit(EXIT_SUCCESS); + exit(success ? EXIT_SUCCESS : EXIT_FAILURE); } diff --git a/modbus_server/Makefile b/modbus_server/Makefile new file mode 100644 index 0000000..99fbc57 --- /dev/null +++ b/modbus_server/Makefile @@ -0,0 +1,25 @@ +DESTDIR=/ +prefix=usr +BIN_NAME=modbus_server + +ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE)) + CC=gcc +else + CC=$(DEB_HOST_GNU_TYPE)-gcc +endif + +CC_FLAGS=-I../common `pkg-config --libs --cflags libmodbus` + +$(BIN_NAME): + $(CC) modbus_server.c $(CC_FLAGS) -o $(BIN_NAME) + + +install: $(BIN_NAME) + install -m 0755 $(BIN_NAME) $(DESTDIR)/$(prefix)/bin/$(BIN_NAME) + +clean: + -@rm $(BIN_NAME) +.PHONY: install clean all + + +