Skip to content

Commit 59be9c8

Browse files
committed
Adds read-input-registers example
1 parent 3d83064 commit 59be9c8

File tree

7 files changed

+290
-1
lines changed

7 files changed

+290
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# How to build this example ?
2+
3+
This example is intended to be compiled with [Codelite](https://codelite.org).
4+
5+
[Codelite](https://codelite.org) is a free cross-platform software...
6+
7+
If you want to compile it without codelite, you can do it with [GNU make](https://www.gnu.org/software/make/) :
8+
9+
$ make -f read-input-registers.mk
10+
11+
Nevertheless, i am not sure that it works on Windows™ and macOS™.
12+
13+
You can then run the program that is in the Debug folder.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// libmodbuspp template
2+
// This example code is in the public domain.
3+
#include <iostream>
4+
#include <string>
5+
#include <modbuspp.h>
6+
7+
using namespace std;
8+
using namespace Modbus;
9+
10+
// -----------------------------------------------------------------------------
11+
int main (int argc, char **argv) {
12+
string port ("/dev/ttyUSB1");
13+
14+
Master mb (Rtu, port , "38400E1"); // new master on RTU
15+
// if you have to handle the DE signal of the line driver with RTS,
16+
// you should uncomment the lines below...
17+
// mb.rtu().setRts(RtsDown);
18+
// mb.rtu().setSerialMode(Rs485);
19+
20+
if (mb.open ()) { // open a connection
21+
// success, do what you want here
22+
uint16_t values[2];
23+
24+
mb.setSlave (33); // to the slave at address 33
25+
if (mb.readInputRegisters (1, values, 2) == 2) {
26+
27+
cout << "R0=" << values[0] << endl;
28+
cout << "R1=" << values[1] << endl;
29+
}
30+
else {
31+
cerr << "Unable to read input registers !" << endl;
32+
exit (EXIT_FAILURE);
33+
}
34+
mb.close();
35+
}
36+
else {
37+
cerr << "Unable to open MODBUS connection to " << port << endl;
38+
exit (EXIT_FAILURE);
39+
}
40+
41+
return 0;
42+
}
43+
/* ========================================================================== */
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
##
2+
## Auto Generated makefile by CodeLite IDE
3+
## any manual changes will be erased
4+
##
5+
## Debug
6+
ProjectName :=read-input-registers
7+
ConfigurationName :=Debug
8+
WorkspacePath :=/home/pascal/src/libmodbuspp
9+
ProjectPath :=/home/pascal/src/libmodbuspp/examples/master/read-input-registers
10+
IntermediateDirectory :=./Debug
11+
OutDir := $(IntermediateDirectory)
12+
CurrentFileName :=
13+
CurrentFilePath :=
14+
CurrentFileFullPath :=
15+
User :=epsilonrt
16+
Date :=19/01/19
17+
CodeLitePath :=/home/pascal/.codelite
18+
LinkerName :=/usr/bin/g++
19+
SharedObjectLinkerName :=/usr/bin/g++ -shared -fPIC
20+
ObjectSuffix :=.o
21+
DependSuffix :=.o.d
22+
PreprocessSuffix :=.i
23+
DebugSwitch :=-g
24+
IncludeSwitch :=-I
25+
LibrarySwitch :=-l
26+
OutputSwitch :=-o
27+
LibraryPathSwitch :=-L
28+
PreprocessorSwitch :=-D
29+
SourceSwitch :=-c
30+
OutputFile :=$(IntermediateDirectory)/$(ProjectName)
31+
Preprocessors :=
32+
ObjectSwitch :=-o
33+
ArchiveOutputSwitch :=
34+
PreprocessOnlySwitch :=-E
35+
ObjectsFileList :="read-input-registers.txt"
36+
PCHCompileFlags :=
37+
MakeDirCommand :=mkdir -p
38+
LinkOptions := $(shell pkg-config --libs libmodbuspp)
39+
IncludePath := $(IncludeSwitch). $(IncludeSwitch).
40+
IncludePCH :=
41+
RcIncludePath :=
42+
Libs :=
43+
ArLibs :=
44+
LibPath := $(LibraryPathSwitch).
45+
46+
##
47+
## Common variables
48+
## AR, CXX, CC, AS, CXXFLAGS and CFLAGS can be overriden using an environment variables
49+
##
50+
AR := /usr/bin/ar rcu
51+
CXX := /usr/bin/g++
52+
CC := /usr/bin/gcc
53+
CXXFLAGS := -std=c++14 $(shell pkg-config --cflags libmodbuspp) -g -O0 -Wall $(Preprocessors)
54+
CFLAGS := -std=c99 $(shell pkg-config --cflags libmodbuspp) -g -O0 -Wall $(Preprocessors)
55+
ASFLAGS :=
56+
AS := /usr/bin/as
57+
58+
59+
##
60+
## User defined environment variables
61+
##
62+
CodeLiteDir:=/usr/share/codelite
63+
Objects0=$(IntermediateDirectory)/main.cpp$(ObjectSuffix)
64+
65+
66+
67+
Objects=$(Objects0)
68+
69+
##
70+
## Main Build Targets
71+
##
72+
.PHONY: all clean PreBuild PrePreBuild PostBuild MakeIntermediateDirs
73+
all: $(OutputFile)
74+
75+
$(OutputFile): $(IntermediateDirectory)/.d $(Objects)
76+
@$(MakeDirCommand) $(@D)
77+
@echo "" > $(IntermediateDirectory)/.d
78+
@echo $(Objects0) > $(ObjectsFileList)
79+
$(LinkerName) $(OutputSwitch)$(OutputFile) @$(ObjectsFileList) $(LibPath) $(Libs) $(LinkOptions)
80+
81+
MakeIntermediateDirs:
82+
@test -d ./Debug || $(MakeDirCommand) ./Debug
83+
84+
85+
$(IntermediateDirectory)/.d:
86+
@test -d ./Debug || $(MakeDirCommand) ./Debug
87+
88+
PreBuild:
89+
90+
91+
##
92+
## Objects
93+
##
94+
$(IntermediateDirectory)/main.cpp$(ObjectSuffix): main.cpp $(IntermediateDirectory)/main.cpp$(DependSuffix)
95+
$(CXX) $(IncludePCH) $(SourceSwitch) "/home/pascal/src/libmodbuspp/examples/master/read-input-registers/main.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/main.cpp$(ObjectSuffix) $(IncludePath)
96+
$(IntermediateDirectory)/main.cpp$(DependSuffix): main.cpp
97+
@$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/main.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/main.cpp$(DependSuffix) -MM main.cpp
98+
99+
$(IntermediateDirectory)/main.cpp$(PreprocessSuffix): main.cpp
100+
$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/main.cpp$(PreprocessSuffix) main.cpp
101+
102+
103+
-include $(IntermediateDirectory)/*$(DependSuffix)
104+
##
105+
## Clean
106+
##
107+
clean:
108+
$(RM) -r ./Debug/
109+
110+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CodeLite_Project Name="read-input-registers" Version="10.0.0" InternalType="Console">
3+
<Description/>
4+
<Dependencies/>
5+
<Settings Type="Executable">
6+
<GlobalSettings>
7+
<Compiler Options="-std=c++14;$(shell pkg-config --cflags libmodbuspp)" C_Options="-std=c99;$(shell pkg-config --cflags libmodbuspp)" Assembler="">
8+
<IncludePath Value="."/>
9+
</Compiler>
10+
<Linker Options="$(shell pkg-config --libs libmodbuspp)">
11+
<LibraryPath Value="."/>
12+
</Linker>
13+
<ResourceCompiler Options=""/>
14+
</GlobalSettings>
15+
<Configuration Name="Debug" CompilerType="gnu g++" DebuggerType="GNU gdb debugger" Type="Executable" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append">
16+
<Compiler Options="-g;-O0;-Wall" C_Options="-g;-O0;-Wall" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="0">
17+
<IncludePath Value="."/>
18+
</Compiler>
19+
<Linker Options="" Required="yes"/>
20+
<ResourceCompiler Options="" Required="no"/>
21+
<General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Debug" Command="./$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
22+
<BuildSystem Name="Default"/>
23+
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
24+
<![CDATA[]]>
25+
</Environment>
26+
<Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath="" IsExtended="yes">
27+
<DebuggerSearchPaths/>
28+
<PostConnectCommands/>
29+
<StartupCommands/>
30+
</Debugger>
31+
<PreBuild/>
32+
<PostBuild/>
33+
<CustomBuild Enabled="no">
34+
<RebuildCommand/>
35+
<CleanCommand/>
36+
<BuildCommand/>
37+
<PreprocessFileCommand/>
38+
<SingleFileCommand/>
39+
<MakefileGenerationCommand/>
40+
<ThirdPartyToolName>None</ThirdPartyToolName>
41+
<WorkingDirectory/>
42+
</CustomBuild>
43+
<AdditionalRules>
44+
<CustomPostBuild/>
45+
<CustomPreBuild/>
46+
</AdditionalRules>
47+
<Completion EnableCpp11="no" EnableCpp14="no">
48+
<ClangCmpFlagsC/>
49+
<ClangCmpFlags/>
50+
<ClangPP/>
51+
<SearchPaths>/usr/include/modbuspp
52+
/usr/local/include/modbuspp</SearchPaths>
53+
</Completion>
54+
</Configuration>
55+
<Configuration Name="Release" CompilerType="gnu g++" DebuggerType="GNU gdb debugger" Type="Executable" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append">
56+
<Compiler Options="-O2;-Wall" C_Options="-O2;-Wall" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="0">
57+
<IncludePath Value="."/>
58+
<Preprocessor Value="NDEBUG"/>
59+
</Compiler>
60+
<Linker Options="" Required="yes"/>
61+
<ResourceCompiler Options="" Required="no"/>
62+
<General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Release" Command="./$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
63+
<BuildSystem Name="Default"/>
64+
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
65+
<![CDATA[]]>
66+
</Environment>
67+
<Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath="" IsExtended="no">
68+
<DebuggerSearchPaths/>
69+
<PostConnectCommands/>
70+
<StartupCommands/>
71+
</Debugger>
72+
<PreBuild/>
73+
<PostBuild/>
74+
<CustomBuild Enabled="no">
75+
<RebuildCommand/>
76+
<CleanCommand/>
77+
<BuildCommand/>
78+
<PreprocessFileCommand/>
79+
<SingleFileCommand/>
80+
<MakefileGenerationCommand/>
81+
<ThirdPartyToolName>None</ThirdPartyToolName>
82+
<WorkingDirectory/>
83+
</CustomBuild>
84+
<AdditionalRules>
85+
<CustomPostBuild/>
86+
<CustomPreBuild/>
87+
</AdditionalRules>
88+
<Completion EnableCpp11="no" EnableCpp14="no">
89+
<ClangCmpFlagsC/>
90+
<ClangCmpFlags/>
91+
<ClangPP/>
92+
<SearchPaths>/usr/include/modbuspp
93+
/usr/local/include/modbuspp</SearchPaths>
94+
</Completion>
95+
</Configuration>
96+
</Settings>
97+
<VirtualDirectory Name="src">
98+
<File Name="main.cpp"/>
99+
</VirtualDirectory>
100+
<VirtualDirectory Name="doc">
101+
<File Name="README.md"/>
102+
</VirtualDirectory>
103+
</CodeLite_Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./Debug/main.cpp.o

libmodbuspp.project

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<CodeLite_Project Name="libmodbuspp" Version="10.0.0" InternalType="Console">
3+
<VirtualDirectory Name="examples">
4+
<VirtualDirectory Name="master">
5+
<VirtualDirectory Name="read-coils">
6+
<File Name="examples/master/read-coils/main.cpp"/>
7+
<File Name="examples/master/read-coils/read-coils.mk"/>
8+
<File Name="examples/master/read-coils/read-coils.txt"/>
9+
<File Name="examples/master/read-coils/README.md"/>
10+
</VirtualDirectory>
11+
<VirtualDirectory Name="read-input-registers">
12+
<File Name="examples/master/read-input-registers/main.cpp"/>
13+
<File Name="examples/master/read-input-registers/read-input-registers.txt"/>
14+
<File Name="examples/master/read-input-registers/read-input-registers.mk"/>
15+
<File Name="examples/master/read-input-registers/README.md"/>
16+
</VirtualDirectory>
17+
</VirtualDirectory>
18+
</VirtualDirectory>
319
<VirtualDirectory Name="dev">
420
<VirtualDirectory Name="codelite">
521
<VirtualDirectory Name="executable-modbuspp">

modbuspp.workspace

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
<CodeLite_Workspace Name="modbuspp" Database="" Version="10.0.0">
33
<Project Name="libmodbuspp" Path="libmodbuspp.project" Active="Yes"/>
44
<Project Name="read-coils" Path="examples/master/read-coils/read-coils.project" Active="No"/>
5+
<Project Name="read-input-registers" Path="examples/master/read-input-registers/read-input-registers.project" Active="No"/>
56
<BuildMatrix>
67
<WorkspaceConfiguration Name="Debug" Selected="yes">
78
<Environment/>
89
<Project Name="libmodbuspp" ConfigName="Debug"/>
910
<Project Name="read-coils" ConfigName="Debug"/>
11+
<Project Name="read-input-registers" ConfigName="Debug"/>
1012
</WorkspaceConfiguration>
11-
<WorkspaceConfiguration Name="Release" Selected="no">
13+
<WorkspaceConfiguration Name="Release" Selected="yes">
1214
<Environment/>
1315
<Project Name="libmodbuspp" ConfigName="Release"/>
1416
<Project Name="read-coils" ConfigName="Release"/>
17+
<Project Name="read-input-registers" ConfigName="Release"/>
1518
</WorkspaceConfiguration>
1619
</BuildMatrix>
1720
</CodeLite_Workspace>

0 commit comments

Comments
 (0)