Skip to content

Commit 9c63200

Browse files
committed
Update examples and codelite project template
1 parent 4046687 commit 9c63200

File tree

11 files changed

+105
-64
lines changed

11 files changed

+105
-64
lines changed

dev/codelite/executable-modbuspp/main.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ using namespace Modbus;
88

99
// -----------------------------------------------------------------------------
1010
int main (int argc, char **argv) {
11+
string port ("/dev/ttyUSB0");
1112

12-
Master mb (Rtu, port, "19200E1"); // new master on RTU
13+
if (argc > 1) {
14+
15+
port = argv[1]; // the serial port can be provided as a parameter on the command line.
16+
}
17+
18+
Master mb (Rtu, port, "38400E1"); // new master on RTU
1319
// if you have to handle the DE signal of the line driver with RTS,
1420
// you should uncomment the lines below...
1521
// mb.rtu().setRts(RtsDown);
@@ -19,7 +25,7 @@ int main (int argc, char **argv) {
1925
// success, do what you want here
2026
uint16_t value;
2127

22-
mb.setSlave (8); // to the slave at address 8
28+
mb.setSlave (33); // to the slave at address 33
2329
mb.readInputRegisters (1, &value);
2430
// ....
2531
mb.close();

examples/master/read-coils/main.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ int main (int argc, char **argv) {
1414

1515
if (argc > 1) {
1616

17-
port = argv[1];
17+
port = argv[1]; // the serial port can be provided as a parameter on the command line.
1818
}
1919

2020
Master mb (Rtu, port, "19200E1"); // new master on RTU
@@ -38,8 +38,16 @@ int main (int argc, char **argv) {
3838
}
3939
cout << endl;
4040
}
41+
else {
42+
cerr << "Unable to read coils ! " << mb.lastError() << endl;
43+
exit (EXIT_FAILURE);
44+
}
4145
mb.close();
4246
}
47+
else {
48+
cerr << "Unable to open MODBUS connection to " << port << " : " << mb.lastError() << endl;
49+
exit (EXIT_FAILURE);
50+
}
4351

4452
return 0;
4553
}

examples/master/read-holding-data/main.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Reads floating point holding registers from SolarPi humidity meter
22

3-
// The humidity sensor calibration is composed of 4 decimal values.
4-
// The first 2 are the minimum and maximum humidity calibration values in %RH.
5-
// The following 2 are the output values of the analog-to-digital converter
3+
// The humidity sensor calibration is composed of 4 decimal values.
4+
// The first 2 are the minimum and maximum humidity calibration values in %RH.
5+
// The following 2 are the output values of the analog-to-digital converter
66
// (in LSB) corresponding to the calibration values.
77

88
// This example code is in the public domain.
@@ -14,7 +14,12 @@ using namespace std;
1414
using namespace Modbus;
1515

1616
int main (int argc, char **argv) {
17-
string port ("/dev/ttyUSB2");
17+
string port ("/dev/ttyUSB0");
18+
19+
if (argc > 1) {
20+
21+
port = argv[1]; // the serial port can be provided as a parameter on the command line.
22+
}
1823

1924
Master mb (Rtu, port , "38400E1"); // new master on RTU
2025
// if you have to handle the DE signal of the line driver with RTS,
@@ -24,27 +29,30 @@ int main (int argc, char **argv) {
2429

2530
if (mb.open ()) { // open a connection
2631
// success, do what you want here
27-
32+
2833
// the bytes in the registers are arranged in big endian.
29-
// the solarpi calibration registers are arranged in little endian.
34+
// the solarpi calibration registers are arranged in little endian.
3035
Data<float, EndianBigLittle> registers[4];
3136

3237
mb.setSlave (33); // to the slave at address 33
38+
39+
// reads values ....
3340
if (mb.readRegisters (1, registers, 4) > 0) {
3441

42+
// then print them !
3543
cout << "R0=" << registers[0].value() << endl;
3644
cout << "R1=" << registers[1].value() << endl;
3745
cout << "R2=" << registers[2].value() << endl;
3846
cout << "R3=" << registers[3].value() << endl;
3947
}
4048
else {
41-
cerr << "Unable to read input registers !" << endl;
49+
cerr << "Unable to read input registers ! " << mb.lastError() << endl;
4250
exit (EXIT_FAILURE);
4351
}
4452
mb.close();
4553
}
4654
else {
47-
cerr << "Unable to open MODBUS connection to " << port << endl;
55+
cerr << "Unable to open MODBUS connection to " << port << " : " << mb.lastError() << endl;
4856
exit (EXIT_FAILURE);
4957
}
5058

examples/master/read-holding-data/read-holding-data.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CurrentFileName :=
1313
CurrentFilePath :=
1414
CurrentFileFullPath :=
1515
User :=epsilonrt
16-
Date :=25/01/19
16+
Date :=27/01/19
1717
CodeLitePath :=/home/pascal/.codelite
1818
LinkerName :=/usr/bin/g++
1919
SharedObjectLinkerName :=/usr/bin/g++ -shared -fPIC

examples/master/read-holding-data/read-holding-data.project

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<CodeLite_Project Name="read-holding-data" Version="10.0.0" InternalType="Console">
3+
<Plugins>
4+
<Plugin Name="qmake">
5+
<![CDATA[00010001N0005Debug000000000000]]>
6+
</Plugin>
7+
</Plugins>
38
<Description/>
49
<Dependencies/>
10+
<VirtualDirectory Name="src">
11+
<File Name="main.cpp"/>
12+
</VirtualDirectory>
13+
<VirtualDirectory Name="doc">
14+
<File Name="README.md"/>
15+
</VirtualDirectory>
516
<Settings Type="Executable">
617
<GlobalSettings>
718
<Compiler Options="-std=c++14;$(shell pkg-config --cflags libmodbuspp)" C_Options="-std=c99;$(shell pkg-config --cflags libmodbuspp)" Assembler="">
@@ -18,7 +29,7 @@
1829
</Compiler>
1930
<Linker Options="" Required="yes"/>
2031
<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"/>
32+
<General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Debug" Command="./$(ProjectName)" CommandArguments="/dev/ttyUSB2" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
2233
<BuildSystem Name="Default"/>
2334
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
2435
<![CDATA[]]>
@@ -94,12 +105,6 @@
94105
</Completion>
95106
</Configuration>
96107
</Settings>
97-
<VirtualDirectory Name="src">
98-
<File Name="main.cpp"/>
99-
</VirtualDirectory>
100-
<VirtualDirectory Name="doc">
101-
<File Name="README.md"/>
102-
</VirtualDirectory>
103108
<Dependencies Name="Debug"/>
104109
<Dependencies Name="Release"/>
105110
</CodeLite_Project>

examples/master/read-input-registers/main.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ using namespace Modbus;
1010

1111
// -----------------------------------------------------------------------------
1212
int main (int argc, char **argv) {
13-
string port ("/dev/ttyUSB1");
13+
string port ("/dev/ttyUSB0");
14+
15+
if (argc > 1) {
16+
17+
port = argv[1]; // the serial port can be provided as a parameter on the command line.
18+
}
1419

1520
Master mb (Rtu, port , "38400E1"); // new master on RTU
1621
// if you have to handle the DE signal of the line driver with RTS,
@@ -29,13 +34,13 @@ int main (int argc, char **argv) {
2934
cout << "R1=" << values[1] << endl;
3035
}
3136
else {
32-
cerr << "Unable to read input registers !" << endl;
37+
cerr << "Unable to read input registers ! " << mb.lastError() << endl;
3338
exit (EXIT_FAILURE);
3439
}
3540
mb.close();
3641
}
3742
else {
38-
cerr << "Unable to open MODBUS connection to " << port << endl;
43+
cerr << "Unable to open MODBUS connection to " << port << " : " << mb.lastError() << endl;
3944
exit (EXIT_FAILURE);
4045
}
4146

examples/master/read-input-registers/read-input-registers.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CurrentFileName :=
1313
CurrentFilePath :=
1414
CurrentFileFullPath :=
1515
User :=epsilonrt
16-
Date :=20/01/19
16+
Date :=27/01/19
1717
CodeLitePath :=/home/pascal/.codelite
1818
LinkerName :=/usr/bin/g++
1919
SharedObjectLinkerName :=/usr/bin/g++ -shared -fPIC

examples/master/read-input-registers/read-input-registers.project

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<CodeLite_Project Name="read-input-registers" Version="10.0.0" InternalType="Console">
3+
<Plugins>
4+
<Plugin Name="qmake">
5+
<![CDATA[00010001N0005Debug000000000000]]>
6+
</Plugin>
7+
</Plugins>
38
<Description/>
49
<Dependencies/>
10+
<VirtualDirectory Name="src">
11+
<File Name="main.cpp"/>
12+
</VirtualDirectory>
13+
<VirtualDirectory Name="doc">
14+
<File Name="README.md"/>
15+
</VirtualDirectory>
516
<Settings Type="Executable">
617
<GlobalSettings>
718
<Compiler Options="-std=c++14;$(shell pkg-config --cflags libmodbuspp)" C_Options="-std=c99;$(shell pkg-config --cflags libmodbuspp)" Assembler="">
@@ -18,7 +29,7 @@
1829
</Compiler>
1930
<Linker Options="" Required="yes"/>
2031
<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"/>
32+
<General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Debug" Command="./$(ProjectName)" CommandArguments="/dev/ttyUSB2" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
2233
<BuildSystem Name="Default"/>
2334
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
2435
<![CDATA[]]>
@@ -94,12 +105,6 @@
94105
</Completion>
95106
</Configuration>
96107
</Settings>
97-
<VirtualDirectory Name="src">
98-
<File Name="main.cpp"/>
99-
</VirtualDirectory>
100-
<VirtualDirectory Name="doc">
101-
<File Name="README.md"/>
102-
</VirtualDirectory>
103108
<Dependencies Name="Debug"/>
104109
<Dependencies Name="Release"/>
105110
</CodeLite_Project>

examples/master/write-holding-data/main.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Writes floating point holding registers to SolarPi humidity meter
22

3-
// The humidity sensor calibration is composed of 4 decimal values.
4-
// The first 2 are the minimum and maximum humidity calibration values in %RH.
5-
// The following 2 are the output values of the analog-to-digital converter
3+
// The humidity sensor calibration is composed of 4 decimal values.
4+
// The first 2 are the minimum and maximum humidity calibration values in %RH.
5+
// The following 2 are the output values of the analog-to-digital converter
66
// (in LSB) corresponding to the calibration values.
77

88
// This example code is in the public domain.
@@ -14,7 +14,12 @@ using namespace std;
1414
using namespace Modbus;
1515

1616
int main (int argc, char **argv) {
17-
string port ("/dev/ttyUSB2");
17+
string port ("/dev/ttyUSB0");
18+
19+
if (argc > 1) {
20+
21+
port = argv[1]; // the serial port can be provided as a parameter on the command line.
22+
}
1823

1924
Master mb (Rtu, port , "38400E1"); // new master on RTU
2025
// if you have to handle the DE signal of the line driver with RTS,
@@ -24,42 +29,36 @@ int main (int argc, char **argv) {
2429

2530
if (mb.open ()) { // open a connection
2631
// success, do what you want here
27-
32+
int ret;
33+
2834
// the bytes in the registers are arranged in big endian.
29-
// the solarpi calibration registers are arranged in little endian.
35+
// the solarpi calibration registers are arranged in little endian.
3036
Data<float, EndianBigLittle> registers[4];
3137

3238
mb.setSlave (33); // to the slave at address 33
33-
34-
// reads and print previous values
35-
if (mb.readRegisters (1, registers, 4) > 0) {
3639

37-
cout << "R0=" << registers[0].value() << endl;
38-
cout << "R1=" << registers[1].value() << endl;
39-
cout << "R2=" << registers[2].value() << endl;
40-
cout << "R3=" << registers[3].value() << endl;
41-
}
42-
else {
43-
cerr << "Unable to read input registers !" << endl;
44-
exit (EXIT_FAILURE);
45-
}
46-
47-
// modify to new values
40+
// set values ...
4841
registers[0] = 152.3;
4942
registers[1] = 1010.7;
5043
registers[2] = 45;
5144
registers[3] = 901;
45+
5246
// then writing to registers
53-
if (mb.writeRegisters (1, registers, 4) < 0) {
47+
ret = mb.writeRegisters (1, registers, 4);
48+
49+
if (ret < 0) {
5450

55-
cerr << "Unable to write input registers !" << endl;
51+
cerr << "Unable to write input registers ! " << mb.lastError() << endl;
5652
exit (EXIT_FAILURE);
5753
}
58-
54+
else {
55+
cout << ret << " registers written (16-bit)." << endl;
56+
}
57+
5958
mb.close();
6059
}
6160
else {
62-
cerr << "Unable to open MODBUS connection to " << port << endl;
61+
cerr << "Unable to open MODBUS connection to " << port << " : " << mb.lastError() << endl;
6362
exit (EXIT_FAILURE);
6463
}
6564

examples/master/write-holding-data/write-holding-data.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CurrentFileName :=
1313
CurrentFilePath :=
1414
CurrentFileFullPath :=
1515
User :=epsilonrt
16-
Date :=25/01/19
16+
Date :=27/01/19
1717
CodeLitePath :=/home/pascal/.codelite
1818
LinkerName :=/usr/bin/g++
1919
SharedObjectLinkerName :=/usr/bin/g++ -shared -fPIC

0 commit comments

Comments
 (0)