From 1a277e94b002652708f5f9a5d5ff4eda25f6cc8a Mon Sep 17 00:00:00 2001 From: Alexander Schwinn <al.schwinn@gsi.de> Date: Thu, 30 Jun 2022 12:02:14 +0200 Subject: [PATCH] Enable modbus support - squash modbus library into silecs library, so that no extra-linking will be required --- silecs-cli-client/Makefile.dep | 3 +-- silecs-communication-cpp/Makefile | 20 +++++++++++++------ silecs-communication-cpp/Makefile.dep | 11 +++------- silecs-communication-cpp/install.sh | 12 ++++++++++- .../squash_modbus_lib.mri | 5 +++++ .../interface/communication/MBConnection.cpp | 14 ++++++------- .../interface/communication/MBConnection.h | 2 +- .../interface/communication/MBHardware.h | 2 +- 8 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 silecs-communication-cpp/squash_modbus_lib.mri diff --git a/silecs-cli-client/Makefile.dep b/silecs-cli-client/Makefile.dep index ef15141..a51ce18 100644 --- a/silecs-cli-client/Makefile.dep +++ b/silecs-cli-client/Makefile.dep @@ -13,10 +13,9 @@ DEPENDENT_COMPILER_OPTIONS += -I$(SILECS_COMM_HOME)/include DEPENDENT_COMPILER_OPTIONS += -I$(LIBXML_PATH) DEPENDENT_COMPILER_OPTIONS += -I$(BOOST_HOME)/include -DEPENDENT_LINKER_OPTIONS += -L$(SILECS_COMM_HOME)/lib/$(CPU) DEPENDENT_LINKER_OPTIONS += -L$(SNAP7_BASE) -lsnap7 DEPENDENT_LINKER_OPTIONS += -L/usr/lib64 -lxml2 -DEPENDENT_LINKER_OPTIONS += -lsilecs-comm +DEPENDENT_LINKER_OPTIONS += -L$(SILECS_COMM_HOME)/lib/$(CPU) -lsilecs-comm DEPENDENT_LINKER_OPTIONS += -lstdc++ -lxml2 -lboost_system -lboost_filesystem -lcurl # Set default library search path diff --git a/silecs-communication-cpp/Makefile b/silecs-communication-cpp/Makefile index 1a542d4..df95e10 100644 --- a/silecs-communication-cpp/Makefile +++ b/silecs-communication-cpp/Makefile @@ -38,11 +38,19 @@ COMPILER_FLAGS = -DMAJOR=$(MAJOR) -DMINOR=$(MINOR) -DPATCH=$(PATCH) #Enable C++11 Support COMPILER_FLAGS += -std=c++11 -# Comment IN/Out to enable NI-Support -#COMPILER_FLAGS += -DNI_SUPPORT_ENABLED=TRUE - -# Comment IN/Out to enable Modbus-Support -#COMPILER_FLAGS += -DMODBUS_SUPPORT_ENABLED=TRUE +ifdef NI_SUPPORT_ENABLED +COMPILER_FLAGS += -DNI_SUPPORT_ENABLED=TRUE +$(info --- Build with NI support: enabled) +else +$(info --- Build with NI support: disabled) +endif + +ifdef MODBUS_SUPPORT_ENABLED +COMPILER_FLAGS += -DMODBUS_SUPPORT_ENABLED=TRUE +$(info --- Build with modbus support: enabled) +else +$(info --- Build with modbus support: disabled) +endif # Include the generic make file -include $(COMMON_MAKE_PATH)/Make.generic \ No newline at end of file +include $(COMMON_MAKE_PATH)/Make.generic diff --git a/silecs-communication-cpp/Makefile.dep b/silecs-communication-cpp/Makefile.dep index 580e172..1716312 100644 --- a/silecs-communication-cpp/Makefile.dep +++ b/silecs-communication-cpp/Makefile.dep @@ -5,16 +5,11 @@ SNAP7_BASE ?= ../snap7/snap7-full BOOST_HOME ?= /opt/gsi/3rdparty/boost/$(BOOST_VERSION) ifdef MODBUS_SUPPORT_ENABLED - MODBUS_VERSION = 3.0.6 - MODBUS_ROOT=$(RELEASE_LOCATION)/$(CPU)/3rdparty/libmodbus-$(MODBUS_VERSION) - MODBUS_SRC=$(MODBUS_ROOT)/src - MODBUS_LIB=$(MODBUS_SRC)/.libs - MODBUS_RPATH=$(RELEASE_LOCATION)/$(CPU)/3rdparty/libmodbus-$(MODBUS_VERSION)/src/.libs + MODBUS_VERSION = 3.1.7 + MODBUS_BASE=/common/usr/cscofe/silecs/modbus/$(MODBUS_VERSION) + DEPENDENT_COMPILER_OPTIONS += -I$(MODBUS_BASE)/include endif -ifdef MODBUS_SUPPORT_ENABLED - DEPENDENT_COMPILER_OPTIONS += -I$(MODBUS_SRC) -endif DEPENDENT_COMPILER_OPTIONS += -I$(LIBXML_PATH) DEPENDENT_COMPILER_OPTIONS += -I$(SNAP7_BASE)/release/Wrappers/c-cpp DEPENDENT_COMPILER_OPTIONS += -I$(BOOST_HOME)/include \ No newline at end of file diff --git a/silecs-communication-cpp/install.sh b/silecs-communication-cpp/install.sh index aa870ca..0e31cea 100755 --- a/silecs-communication-cpp/install.sh +++ b/silecs-communication-cpp/install.sh @@ -7,6 +7,8 @@ SILECS_VERSION=$2 SCRIPT=$(readlink -f "$0") SCRIPTPATH=$(dirname "$SCRIPT") # path where this script is located in +CPU=x86_64 + RELEASE_DIR=${RELEASE_DIR_BASE}/${SILECS_VERSION}/silecs-communication-cpp if [ -d ${RELEASE_DIR} ]; then echo "Error: ${RELEASE_DIR} already exists ...skipping" @@ -18,7 +20,15 @@ MINOR=`echo $SILECS_VERSION | cut -d. -f2` PATCH=`echo $SILECS_VERSION | cut -d. -f3` make -C ${SCRIPTPATH} clean -make -C ${SCRIPTPATH} CPU=x86_64 MAJOR=${MAJOR} MINOR=${MINOR} PATCH=${PATCH} -j4 +MODBUS_SUPPORT_ENABLED=true make -C ${SCRIPTPATH} CPU=${CPU} MAJOR=${MAJOR} MINOR=${MINOR} PATCH=${PATCH} -j4 + +# Squash the silecs library with the modbus library, so no extra-lining will be required +SILECS_LIB=${SCRIPTPATH}/build/lib/${CPU}/libsilecs-comm.a +SQUASHED_LIB=${SCRIPTPATH}/build/lib/${CPU}/libsquashed.a +cd ${SCRIPTPATH} +ar -M <squash_modbus_lib.mri +rm ${SILECS_LIB} +mv ${SQUASHED_LIB} ${SILECS_LIB} mkdir -p ${RELEASE_DIR} cp -r ${SCRIPTPATH}/build/include ${RELEASE_DIR} diff --git a/silecs-communication-cpp/squash_modbus_lib.mri b/silecs-communication-cpp/squash_modbus_lib.mri new file mode 100644 index 0000000..145b315 --- /dev/null +++ b/silecs-communication-cpp/squash_modbus_lib.mri @@ -0,0 +1,5 @@ +create build/lib/x86_64/libsquashed.a +addlib build/lib/x86_64/libsilecs-comm.a +addlib /common/usr/cscofe/silecs/modbus/3.1.7/lib/libmodbus.a +save +end diff --git a/silecs-communication-cpp/src/silecs-communication/interface/communication/MBConnection.cpp b/silecs-communication-cpp/src/silecs-communication/interface/communication/MBConnection.cpp index 9cc7500..f4d5eb9 100644 --- a/silecs-communication-cpp/src/silecs-communication/interface/communication/MBConnection.cpp +++ b/silecs-communication-cpp/src/silecs-communication/interface/communication/MBConnection.cpp @@ -257,7 +257,8 @@ int MBConnection::readRegisters(PLC *thePLC, long address, unsigned long offset, if (doOpen(thePLC)) { // connection is established then acquire data - readLock(); + Lock lock(readMux_); + // Schneider uses 16bit alignment memory. Block address is expressed in // bytes (==> /2) long addr = (address + offset) / 2; @@ -269,7 +270,6 @@ int MBConnection::readRegisters(PLC *thePLC, long address, unsigned long offset, err = readFrames(readCtx_, addr, (unsigned short)size, pBuffer, isIO); checkError(thePLC, err, false); // close the connection, will try again // at the next access - readUnlock(); } return err; } @@ -298,7 +298,8 @@ int MBConnection::writeRegisters(PLC *thePLC, long address, unsigned long offset if (doOpen(thePLC)) { // connection is established then send data - writeLock(); + Lock lock(writeMux_); + // Schneider uses 16bit alignment memory. Block address is expressed in // bytes (==> /2) long addr = (address + offset) / 2; @@ -310,7 +311,6 @@ int MBConnection::writeRegisters(PLC *thePLC, long address, unsigned long offset err = writeFrames(writeCtx_, (unsigned short)addr, (unsigned short)size, pBuffer); checkError(thePLC, err, false); // close the connection, will try again // at the next access - writeUnlock(); } return err; } @@ -334,7 +334,7 @@ int MBConnection::readBits(PLC *thePLC, long address, unsigned long offset, unsi if (doOpen(thePLC)) { // connection is established then acquire data - readLock(); + Lock lock(readMux_); // addr is a byte address, therefore we don't need to convert to a word address // However, we need to calculate the coil address from the register address @@ -351,7 +351,6 @@ int MBConnection::readBits(PLC *thePLC, long address, unsigned long offset, unsi err = readCoils(readCtx_, addr, (unsigned short)size, pBuffer); checkError(thePLC, err, false); // close the connection, will try again // at the next access - readUnlock(); } return err; } @@ -375,7 +374,7 @@ int MBConnection::writeBits(PLC *thePLC, long address, unsigned long offset, uns if (doOpen(thePLC)) { // connection is established then send data - writeLock(); + Lock lock(writeMux_); // addr is a byte address, therefore we don't need to convert to a word address // However, we need to calculate the coil address from the register address @@ -391,7 +390,6 @@ int MBConnection::writeBits(PLC *thePLC, long address, unsigned long offset, uns err = writeCoils(writeCtx_, (unsigned short)addr, (unsigned short)size, pBuffer); checkError(thePLC, err, false); // close the connection, will try again // at the next access - writeUnlock(); } return err; } diff --git a/silecs-communication-cpp/src/silecs-communication/interface/communication/MBConnection.h b/silecs-communication-cpp/src/silecs-communication/interface/communication/MBConnection.h index ec13ffa..3810a27 100644 --- a/silecs-communication-cpp/src/silecs-communication/interface/communication/MBConnection.h +++ b/silecs-communication-cpp/src/silecs-communication/interface/communication/MBConnection.h @@ -15,7 +15,7 @@ Contributors: #define _MB_CONNECTION_H_ #include <silecs-communication/interface/communication/SilecsConnection.h> -#include <modbus.h> +#include <modbus/modbus.h> namespace Silecs { diff --git a/silecs-communication-cpp/src/silecs-communication/interface/communication/MBHardware.h b/silecs-communication-cpp/src/silecs-communication/interface/communication/MBHardware.h index 8b0d1d0..48650f5 100644 --- a/silecs-communication-cpp/src/silecs-communication/interface/communication/MBHardware.h +++ b/silecs-communication-cpp/src/silecs-communication/interface/communication/MBHardware.h @@ -15,7 +15,7 @@ Contributors: #define _MBHARDWARE_H_ #include <silecs-communication/interface/communication/ietype.h> -#include <modbus.h> +#include <modbus/modbus.h> /*----------------------------------------------------------*/ /* Time funtion -- GitLab