-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathdemo.c
199 lines (174 loc) · 4.69 KB
/
demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#define LIGHTMODBUS_FULL
#define LIGHTMODBUS_DEBUG
#define LIGHTMODBUS_IMPL
#include <lightmodbus/lightmodbus.h>
#include <stdio.h>
#include <assert.h>
/*
A register callback that prints out everything what's happening
*/
ModbusError registerCallback(
const ModbusSlave *slave,
const ModbusRegisterCallbackArgs *args,
ModbusRegisterCallbackResult *result)
{
// Use functions from debug utilities to nicely
// convert enum values to strings
printf(
"Register query:\n"
"\tquery: %s\n"
"\t type: %s\n"
"\t id: %d\n"
"\tvalue: %d\n"
"\t fun: %d\n",
modbusRegisterQueryStr(args->query),
modbusDataTypeStr(args->type),
args->index,
args->value,
args->function
);
switch (args->query)
{
// Pretend we allow all access
// Tip: Change MODBUS_EXCEP_NONE to something else
// and see what happens
case MODBUS_REGQ_R_CHECK:
case MODBUS_REGQ_W_CHECK:
result->exceptionCode = MODBUS_EXCEP_NONE;
break;
// Return 7 when reading
case MODBUS_REGQ_R:
result->value = 7;
break;
// Ignore write requests
case MODBUS_REGQ_W:
break;
}
// Always return MODBUS_OK
return MODBUS_OK;
}
/*
Exception callback for printing out exceptions
*/
ModbusError slaveExceptionCallback(const ModbusSlave *slave, uint8_t function, ModbusExceptionCode code)
{
printf("Slave exception %s (function %d)\n", modbusExceptionCodeStr(code), function);
// Always return MODBUS_OK
return MODBUS_OK;
}
/*
Data callback for printing all incoming data
*/
ModbusError dataCallback(const ModbusMaster *master, const ModbusDataCallbackArgs *args)
{
printf(
"Received data:\n"
"\t from: %d\n"
"\t fun: %d\n"
"\t type: %s\n"
"\t id: %d\n"
"\tvalue: %d\n",
args->address,
args->function,
modbusDataTypeStr(args->type),
args->index,
args->value
);
// Always return MODBUS_OK
return MODBUS_OK;
}
/*
Exception callback for printing out exceptions on master side
*/
ModbusError masterExceptionCallback(const ModbusMaster *master, uint8_t address, uint8_t function, ModbusExceptionCode code)
{
printf("Received slave %d exception %s (function %d)\n", address, modbusExceptionCodeStr(code), function);
// Always return MODBUS_OK
return MODBUS_OK;
}
/*
Helper function for printing out frames
*/
void printBytes(const uint8_t *data, int length)
{
for (int i = 0; i < length; i++)
printf("0x%02x ", data[i]);
}
/*
Helper function for printing out ModbusErrorInfo
*/
void printErrorInfo(ModbusErrorInfo err)
{
if (modbusIsOk(err))
printf("OK");
else
printf("%s: %s",
modbusErrorSourceStr(modbusGetErrorSource(err)),
modbusErrorStr(modbusGetErrorCode(err)));
}
int main(int argc, char *argv[])
{
// Master and slave structs
ModbusErrorInfo err;
ModbusMaster master;
ModbusSlave slave;
// Init master
err = modbusMasterInit(
&master,
dataCallback,
masterExceptionCallback,
modbusDefaultAllocator,
modbusMasterDefaultFunctions,
modbusMasterDefaultFunctionCount);
printf("Slave init: "); printErrorInfo(err); printf("\n");
assert(modbusIsOk(err) && "modbusMasterInit() failed!");
// Init slave
err = modbusSlaveInit(
&slave,
registerCallback,
slaveExceptionCallback,
modbusDefaultAllocator,
modbusSlaveDefaultFunctions,
modbusSlaveDefaultFunctionCount);
printf("Master init: "); printErrorInfo(err); printf("\n");
assert(modbusIsOk(err) && "modbusSlaveInit() failed!");
// Read 2 registers starting at 78
err = modbusBuildRequest03RTU(&master, 1, 78, 2);
printf("Build request: "); printErrorInfo(err); printf("\n");
assert(modbusIsOk(err) && "failed to build the request!");
// Print out the request
printf("Request: ");
printBytes(modbusMasterGetRequest(&master), modbusMasterGetRequestLength(&master));
printf("\n");
// Let the slave parse this reqiest
err = modbusParseRequestRTU(
&slave,
1,
modbusMasterGetRequest(&master),
modbusMasterGetRequestLength(&master));
printf("Parse request: "); printErrorInfo(err); printf("\n");
// Check for any serious errors
assert(modbusGetGeneralError(err) == MODBUS_OK && "slave error!");
// Print out the response
printf("Response: ");
printBytes(modbusSlaveGetResponse(&slave), modbusSlaveGetResponseLength(&slave));
printf("\n");
// If the slave didn't respond, we're done here
if (!modbusIsOk(err))
goto cleanup;
// Let the master parse the response
err = modbusParseResponseRTU(
&master,
modbusMasterGetRequest(&master),
modbusMasterGetRequestLength(&master),
modbusSlaveGetResponse(&slave),
modbusSlaveGetResponseLength(&slave));
printf("Parse response: "); printErrorInfo(err); printf("\n");
// Check for any serious errors
assert(modbusGetGeneralError(err) == MODBUS_OK && "master error!");
// Cleanup
cleanup:
modbusSlaveDestroy(&slave);
modbusMasterDestroy(&master);
return 0;
}