-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathuserfun.c
More file actions
95 lines (78 loc) · 2.22 KB
/
Copy pathuserfun.c
File metadata and controls
95 lines (78 loc) · 2.22 KB
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
#include <stdio.h>
#include <assert.h>
#define LIGHTMODBUS_SLAVE
#define LIGHTMODBUS_IMPL
#include <lightmodbus/lightmodbus.h>
/*
Responds with "ping\0" when called with even
value and with "pong\0" if called with odd value.
Reports an exception if the provided value is 0.
*/
LIGHTMODBUS_RET_ERROR parseMyRequest(
ModbusSlave *status,
uint8_t function,
const uint8_t *requestPDU,
uint8_t requestLength)
{
// Check request length
if (requestLength < 2)
return modbusBuildException(status, function, MODBUS_EXCEP_ILLEGAL_VALUE);
// Check for the 'invalid' case
if (requestPDU[1] == 0)
return modbusBuildException(status, function, MODBUS_EXCEP_ILLEGAL_VALUE);
// Allocate 6 bytes for the response. Handle memory allocation errors
if (modbusSlaveAllocateResponse(status, 6) != MODBUS_OK)
return MODBUS_GENERAL_ERROR(ALLOC);
// Write data to the response frame
// Be sure to write request.pdu and not request.data
status->response.pdu[0] = function;
status->response.pdu[1] = 'p';
status->response.pdu[2] = requestPDU[1] % 2 ? 'o' : 'i';
status->response.pdu[3] = 'n';
status->response.pdu[4] = 'g';
status->response.pdu[5] = 0;
return MODBUS_NO_ERROR();
}
/*
A dummy register callback
*/
ModbusError dummyRegCallback(
const ModbusSlave *slave,
const ModbusRegisterCallbackArgs *args,
ModbusRegisterCallbackResult *result)
{
return MODBUS_OK;
}
int main(void)
{
// Handler array only containing our ping-pong function
ModbusSlaveFunctionHandler functions[1] =
{
{65, parseMyRequest}
};
// Init slave
ModbusSlave slave;
ModbusErrorInfo err = modbusSlaveInit(
&slave,
dummyRegCallback,
NULL,
modbusDefaultAllocator,
functions,
1);
assert(modbusIsOk(err));
// Request frame with odd number
uint8_t request[] = {65, 33};
// Parse the request
err = modbusParseRequestPDU(&slave, request, sizeof request);
assert(modbusIsOk(err));
// Print the response
for (int i = 0; i < modbusSlaveGetResponseLength(&slave); i++)
printf("0x%02x ", modbusSlaveGetResponse(&slave)[i]);
printf("\n");
// Print the response as ASCII
for (int i = 0; i < modbusSlaveGetResponseLength(&slave); i++)
printf("%c ", modbusSlaveGetResponse(&slave)[i]);
printf("\n");
// Destroy slave
modbusSlaveDestroy(&slave);
}