Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix ip parse #5

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
debian/files
debian/modbus-utils*
modbus_client/modbus_client
modbus_server/modbus_server
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions common/mbu-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
53 changes: 53 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

modbus-utils (1.2.5) stable; urgency=medium

* bugfix

-- Andrey Radionov <[email protected]> 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 <[email protected]> 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 <[email protected]> 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 <[email protected]> Wed, 21 Dec 2016 13:16:41 +0300

modbus-utils (1.2.1) stable; urgency=medium

* fix timeout handling

-- Evgeny Boger <[email protected]> Wed, 22 Jul 2015 22:53:15 +0300

modbus-utils (1.2) stable; urgency=medium

* fix baud rate handling

-- Evgeny Boger <[email protected]> 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 <[email protected]> Wed, 06 Aug 2014 10:15:01 +0400

-- Evgeny Boger <[email protected]> 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 <[email protected]> Tue, 29 Apr 2014 01:59:04 +0400


1 change: 1 addition & 0 deletions debian/compat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9
11 changes: 11 additions & 0 deletions debian/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Source: modbus-utils
Maintainer: Evgeny Boger <[email protected]>
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
Empty file added debian/copyright
Empty file.
2 changes: 2 additions & 0 deletions debian/modbus-utils.dirs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
usr/bin
usr/share/man/man1
3 changes: 3 additions & 0 deletions debian/rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/make -f
%:
dh $@
1 change: 1 addition & 0 deletions debian/source/format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0 (native)
25 changes: 25 additions & 0 deletions modbus_client/Makefile
Original file line number Diff line number Diff line change
@@ -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



49 changes: 33 additions & 16 deletions modbus_client/modbus_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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;
Expand All @@ -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]);
}
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -387,5 +404,5 @@ int main(int argc, char **argv)
break;
}

exit(EXIT_SUCCESS);
exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
}
25 changes: 25 additions & 0 deletions modbus_server/Makefile
Original file line number Diff line number Diff line change
@@ -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