Skip to content

Commit cfda78b

Browse files
committed
Add a very experimental C++ interface
1 parent 1a7ed58 commit cfda78b

File tree

8 files changed

+510
-0
lines changed

8 files changed

+510
-0
lines changed

.github/workflows/v3.0-ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ jobs:
5252
- name: Build AVR slave example
5353
run: make -C examples/avrslave
5454

55+
cpp-build:
56+
name: "C++ build test"
57+
runs-on: ubuntu-latest
58+
steps:
59+
- name: Checkout
60+
uses: actions/checkout@v2
61+
- name: Compile
62+
run: make -C test/cpp
63+
5564
examples-build:
5665
name: "Build examples"
5766
runs-on: ubuntu-latest
@@ -66,6 +75,12 @@ jobs:
6675
run: make -C examples/integration
6776
- name: Build user-defined functions example
6877
run: make -C examples/userfun
78+
- name: Run user-defined functions example
79+
run: ./examples/userfun/userfun
80+
- name: Build C++ example
81+
run: make -C examples/cpp
82+
- name: Run C++ example
83+
run: ./examples/cpp/cppdemo
6984

7085
main-test:
7186
name: "Tests"

examples/cpp/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cppdemo

examples/cpp/cppdemo.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
#define LIGHTMODBUS_FULL
4+
#define LIGHTMODBUS_IMPL
5+
#include <lightmodbus/lightmodbus.hpp>
6+
7+
ModbusError regCallback(
8+
const ModbusSlave *slave,
9+
const ModbusRegisterCallbackArgs *args,
10+
ModbusRegisterCallbackResult *result)
11+
{
12+
switch (args->query)
13+
{
14+
// No write access
15+
case MODBUS_REGQ_W_CHECK:
16+
result->exceptionCode = MODBUS_EXCEP_SLAVE_FAILURE;
17+
break;
18+
19+
// Read access to everything
20+
case MODBUS_REGQ_R_CHECK:
21+
result->exceptionCode = MODBUS_EXCEP_NONE;
22+
break;
23+
24+
// Return 7 when reading
25+
case MODBUS_REGQ_R:
26+
result->value = 7;
27+
break;
28+
29+
// Ignore write requests (should not happen)
30+
case MODBUS_REGQ_W:
31+
throw std::runtime_error{"this should never happen"};
32+
break;
33+
}
34+
35+
// Always return MODBUS_OK
36+
return MODBUS_OK;
37+
}
38+
39+
ModbusError dataCallback(const ModbusMaster *master, const ModbusDataCallbackArgs *args)
40+
{
41+
// I'm sorry, C++ people... I'm too tired right now to use std::cout
42+
std::printf(
43+
"Received data:\n"
44+
"\t from: %d\n"
45+
"\t fun: %d\n"
46+
"\t type: %s\n"
47+
"\t id: %d\n"
48+
"\tvalue: %d\n",
49+
args->address,
50+
args->function,
51+
modbusDataTypeStr(args->type),
52+
args->index,
53+
args->value
54+
);
55+
56+
// Always return MODBUS_OK
57+
return MODBUS_OK;
58+
}
59+
60+
int main()
61+
{
62+
llm::Slave slave(regCallback);
63+
llm::Master master(dataCallback);
64+
65+
try
66+
{
67+
// Look how neat it looks
68+
master.buildRequest01RTU(4, 15, 3);
69+
slave.parseRequestRTU(4, master.getRequest(), master.getRequestLength());
70+
master.parseResponseRTU(
71+
master.getRequest(),
72+
master.getRequestLength(),
73+
slave.getResponse(),
74+
slave.getResponseLength());
75+
}
76+
catch (const llm::GeneralError &e)
77+
{
78+
std::cerr << "General error (serious): " << e.what() << std::endl;
79+
}
80+
catch (const llm::RequestError &e)
81+
{
82+
std::cerr << "Request error: " << e.what() << std::endl;
83+
}
84+
catch (const llm::ResponseError &e)
85+
{
86+
std::cerr << "Response error: " << e.what() << std::endl;
87+
}
88+
89+
return 0;
90+
}

examples/cpp/makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
g++ -o cppdemo cppdemo.cpp -Wall -I../../include

0 commit comments

Comments
 (0)