Skip to content

Commit 2fb368d

Browse files
author
Evgeny Prikhodko
committed
Debugging RTU
1 parent d3e6e5e commit 2fb368d

22 files changed

+241
-62
lines changed

libmodbus_cpp.pro

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#-------------------------------------------------
66

77
QT -= gui
8-
QT += network
8+
QT += network serialport
99
QT += testlib
1010

1111
TARGET = libmodbus_cpp
@@ -48,7 +48,8 @@ SOURCES += \
4848
libmodbus_cpp/master_rtu_backend.cpp \
4949
libmodbus_cpp/master_rtu.cpp \
5050
tests/abstract_read_write_test.cpp \
51-
tests/tcp_read_write_test.cpp
51+
tests/tcp_read_write_test.cpp \
52+
tests/rtu_read_write_test.cpp
5253

5354
HEADERS += \
5455
libmodbus_cpp/backend.h \
@@ -74,7 +75,8 @@ HEADERS += \
7475
libmodbus_cpp/master_rtu_backend.h \
7576
libmodbus_cpp/master_rtu.h \
7677
tests/abstract_read_write_test.h \
77-
tests/tcp_read_write_test.h
78+
tests/tcp_read_write_test.h \
79+
tests/rtu_read_write_test.h
7880

7981
unix {
8082
target.path = /usr/local/lib/libmodbus_cpp

libmodbus_cpp/abstract_slave.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ void libmodbus_cpp::AbstractSlave::addHook(libmodbus_cpp::FunctionCode funcCode,
2727
getBackend()->addHook(funcCode, address, func);
2828
}
2929

30+
bool libmodbus_cpp::AbstractSlave::startListen()
31+
{
32+
return getBackend()->startListen();
33+
}
34+
35+
void libmodbus_cpp::AbstractSlave::stopListen()
36+
{
37+
getBackend()->stopListen();
38+
}
39+
3040
void libmodbus_cpp::AbstractSlave::setValueToCoil(uint16_t address, bool value) {
3141
if (!getBackend()->getMap())
3242
throw LocalWriteError("map was not inited");

libmodbus_cpp/abstract_slave.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class AbstractSlave
2626
bool setDefaultAddress();
2727
void addHook(FunctionCode funcCode, Address address, HookFunction func);
2828

29+
bool startListen();
30+
void stopListen();
31+
2932
void setValueToCoil(uint16_t address, bool value);
3033
bool getValueFromCoil(uint16_t address);
3134

libmodbus_cpp/backend.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ AbstractBackend::~AbstractBackend()
2323

2424
bool AbstractBackend::openConnection()
2525
{
26-
return (modbus_connect(getCtx()) == 0);
26+
int errorCode = modbus_connect(getCtx());
27+
if (errorCode == 0)
28+
return true;
29+
else
30+
throw ConnectionError(modbus_strerror(errno));
2731
}
2832

2933
void AbstractBackend::closeConnection()

libmodbus_cpp/backend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ class AbstractSlaveBackend : public AbstractBackend
8484
bool initMap(int holdingBitsCount, int inputBitsCount, int holdingRegistersCount, int inputRegistersCount);
8585
bool initRegisterMap(int holdingRegistersCount, int inputRegistersCount);
8686

87+
virtual bool startListen() = 0;
88+
virtual void stopListen() = 0;
89+
8790
void addHook(FunctionCode funcCode, Address address, HookFunction func);
8891
};
8992

libmodbus_cpp/defs.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ enum class Parity : char {
2929
Odd = 'O'
3030
};
3131

32+
enum class DataBits {
33+
b5 = 5,
34+
b6 = 6,
35+
b7 = 7,
36+
b8 = 8
37+
};
38+
39+
enum class StopBits {
40+
b1 = 1,
41+
b2 = 2
42+
};
43+
3244
enum class DataType {
3345
Coil,
3446
DiscreteInput,
@@ -64,6 +76,11 @@ class RemoteWriteError : public RemoteRWError {
6476
RemoteWriteError(const std::string &msg) : RemoteRWError(msg) {}
6577
};
6678

79+
class ConnectionError : public RemoteRWError {
80+
public:
81+
ConnectionError(const std::string &msg) : RemoteRWError(msg) {}
82+
};
83+
6784
using LocalRWError = std::logic_error;
6885

6986
class LocalReadError : public LocalRWError {

libmodbus_cpp/factory.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ libmodbus_cpp::SlaveTcp *libmodbus_cpp::Factory::createTcpSlave(const char *addr
1818
return s;
1919
}
2020

21-
libmodbus_cpp::MasterRtu *libmodbus_cpp::Factory::createRtuMaster(const char *device, int baud, libmodbus_cpp::Parity parity, int dataBit, int stopBit)
21+
libmodbus_cpp::MasterRtu *libmodbus_cpp::Factory::createRtuMaster(const char *device, int baud, libmodbus_cpp::Parity parity, DataBits dataBits, StopBits stopBits)
2222
{
23-
MasterRtuBackend *b = new MasterRtuBackend(device, baud, parity, dataBit, stopBit);
23+
MasterRtuBackend *b = new MasterRtuBackend(device, baud, parity, dataBits, stopBits);
2424
MasterRtu *m = new MasterRtu(b);
2525
return m;
2626
}
2727

28-
libmodbus_cpp::SlaveRtu *libmodbus_cpp::Factory::createRtuSlave(const char *device, int baud, libmodbus_cpp::Parity parity, int dataBit, int stopBit)
28+
libmodbus_cpp::SlaveRtu *libmodbus_cpp::Factory::createRtuSlave(const char *device, int baud, libmodbus_cpp::Parity parity, DataBits dataBits, StopBits stopBits)
2929
{
30-
SlaveRtuBackend *b = new SlaveRtuBackend(device, baud, parity, dataBit, stopBit);
30+
SlaveRtuBackend *b = new SlaveRtuBackend(device, baud, parity, dataBits, stopBits);
3131
SlaveRtu *s = new SlaveRtu(b);
3232
return s;
3333
}

libmodbus_cpp/factory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class Factory
1515
public:
1616
static MasterTcp *createTcpMaster(const char *address, int port);
1717
static SlaveTcp *createTcpSlave(const char *address, int port);
18-
static MasterRtu *createRtuMaster(const char *device, int baud, Parity parity = Parity::None, int dataBit = 8, int stopBit = 1);
19-
static SlaveRtu *createRtuSlave(const char *device, int baud, Parity parity = Parity::None, int dataBit = 8, int stopBit = 1);
18+
static MasterRtu *createRtuMaster(const char *device, int baud, Parity parity = Parity::None, DataBits dataBits = DataBits::b8, StopBits stopBits = StopBits::b1);
19+
static SlaveRtu *createRtuSlave(const char *device, int baud, Parity parity = Parity::None, DataBits dataBits = DataBits::b8, StopBits stopBits = StopBits::b1);
2020
};
2121

2222
}

libmodbus_cpp/main.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
#include <QCoreApplication>
44
#include "tests/reg_map_read_write_test.h"
55
#include "tests/tcp_read_write_test.h"
6+
#include "tests/rtu_read_write_test.h"
67

78
#include "slave_tcp.h"
9+
#include "slave_rtu.h"
810
#include "factory.h"
911

1012
using namespace libmodbus_cpp;
@@ -19,38 +21,42 @@ using namespace libmodbus_cpp;
1921
// }
2022
//}
2123

22-
const bool RUN_TESTS = true;
24+
const bool RUN_TESTS = false;
2325

2426
int main(int argc, char *argv[])
2527
{
2628
if (RUN_TESTS) {
2729
QCoreApplication app(argc, argv);
28-
libmodbus_cpp::RegMapReadWriteTest t1;
29-
QTest::qExec(&t1);
30+
// libmodbus_cpp::RegMapReadWriteTest t1;
31+
// QTest::qExec(&t1);
3032

31-
libmodbus_cpp::TcpReadWriteTest t2;
32-
QTest::qExec(&t2);
33+
// libmodbus_cpp::TcpReadWriteTest t2;
34+
// QTest::qExec(&t2);
35+
36+
libmodbus_cpp::RtuReadWriteTest t3;
37+
QTest::qExec(&t3);
3338
return 0;
3439
}
3540

3641
QCoreApplication app(argc, argv);
37-
SlaveTcp *s = Factory::createTcpSlave("127.0.0.1", 1502);
42+
// SlaveTcp *s = Factory::createTcpSlave("127.0.0.1", 1502);
43+
SlaveRtu *s = Factory::createRtuSlave("/home/prikhodko_ev/ttySimSlave", 9600);
3844
s->initMap(32, 32, 32, 32);
39-
// s->setValueToInputRegister(8, 0x0102030405060708L);
45+
s->setValueToInputRegister(8, 0x0102030405060708L);
4046
// printInputRegisters(s);
41-
s->setValueToInputRegister(8, 0x12345678);
47+
// s->setValueToInputRegister(8, 0x12345678);
4248
// printInputRegisters(s);
43-
s->addHook(MODBUS_FC_READ_INPUT_REGISTERS, 8, [s, counter = 0ull]() mutable -> void {
44-
s->setValueToInputRegister(8, ++counter);
45-
// printInputRegisters(s);
46-
});
49+
// s->addHook(MODBUS_FC_READ_INPUT_REGISTERS, 8, [s, counter = 0ull]() mutable -> void {
50+
// s->setValueToInputRegister(8, ++counter);
51+
//// printInputRegisters(s);
52+
// });
4753

4854
// for (int i = 0; i < 32; ++i) {
4955
// s.setValueToHoldingRegister(i, (short)(i + 1));
5056
// s.setValueToInputRegister(i, (short)(i + 1));
5157
// }
5258

53-
s->startListen(10);
59+
s->startListen();
5460

5561
return app.exec();
5662
}

libmodbus_cpp/master_rtu_backend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "libmodbus_cpp/master_rtu_backend.h"
22

33

4-
libmodbus_cpp::MasterRtuBackend::MasterRtuBackend(const char *device, int baud, libmodbus_cpp::Parity parity, int dataBit, int stopBit) :
5-
AbstractBackend(modbus_new_rtu(device, baud, (char)parity, dataBit, stopBit))
4+
libmodbus_cpp::MasterRtuBackend::MasterRtuBackend(const char *device, int baud, libmodbus_cpp::Parity parity, DataBits dataBits, StopBits stopBits) :
5+
AbstractBackend(modbus_new_rtu(device, baud, (char)parity, (int)dataBits, (int)stopBits))
66
{
77
}
88

0 commit comments

Comments
 (0)