-
Notifications
You must be signed in to change notification settings - Fork 5
/
slave_server.c
109 lines (94 loc) · 2.43 KB
/
slave_server.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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <modbus.h>
#ifdef _WIN32
# include <winsock2.h>
#else
# include <sys/socket.h>
#endif
#define MODBUS_TCP_MAX_ADU_LENGTH 260
#define MODBUS_RTU_MAX_ADU_LENGTH 256
#define SERVER_ID 1
enum {
TCP,
RTU
};
int main(int argc, char*argv[])
{
modbus_t *ctx;
uint8_t *query;
int rc;
int use_backend;
int s = -1;
int header_length;
if (argc > 1) {
if (strcmp(argv[1], "tcp") == 0) {
use_backend = TCP;
} else if (strcmp(argv[1], "rtu") == 0) {
use_backend = RTU;
} else {
return -1;
}
} else {
/* Use TCP by default for testing */
use_backend = TCP;
}
/* Prepare a Modbus mapping with 2 holding registers
(plus no output coil, one input coil and two input registers)
This will also automatically set the value of each register to 0 */
modbus_mapping_t *mapping = modbus_mapping_new(1, 1, 1, 1);
if (!mapping) {
fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
exit(1);
}
/* Set some random values to registers */
mapping->tab_registers[0] = 20;
mapping->tab_bits[0] = 1;
if (use_backend == TCP) {
ctx = modbus_new_tcp("127.0.0.1", 1502);
query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
} else {
ctx = modbus_new_rtu("/dev/ttys020", 115200, 'N', 8, 1);
modbus_set_slave(ctx, SERVER_ID);
query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
}
header_length = modbus_get_header_length(ctx);
/* modbus_set_debug(ctx, TRUE); */
if (!ctx) {
fprintf(stderr, "Failed to create the context: %s\n", modbus_strerror(errno));
exit(1);
}
header_length = modbus_get_header_length(ctx);
modbus_set_debug(ctx, TRUE);
if (use_backend == TCP) {
s = modbus_tcp_listen(ctx, 1);
modbus_tcp_accept(ctx, &s);
} else {
rc = modbus_connect(ctx);
if (rc == -1) {
fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
}
/* length of the request/response */
int len;
for (;;) {
int rc;
rc = modbus_receive(ctx, query);
/* query size */
if (rc > 0) {
modbus_reply(ctx, query, rc, mapping);
} else if (rc == -1) {
/* Connection closed by the client or error */
break;
}
}
printf("Exit the loop: %s\n", modbus_strerror(errno));
modbus_mapping_free(mapping);
modbus_close(ctx);
modbus_free(ctx);
}