-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_reg.c
203 lines (164 loc) · 5 KB
/
write_reg.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
200
201
202
203
/*This connection-progam tries to start a connection with the goodwe-inverter
importent: the SLAVE_ID for the inverter is 247; baudate: 9600; Bits 8N1; one register has 16bit or 4byte
execution: ./connection <register> <size in bytes>*/
#include <stdio.h>
#include <modbus.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int fd_272;
int fd_273;
void set_rts_custom(modbus_t*, int);
void config_gpio_pins();
void close_gpio_pins();
void print_bits(uint16_t val, uint16_t offset)
{
for (int i = 15; 0 <= i; i--) {
// if (i % 4 == 3) printf (" ");
// printf("%c", (val & (1 << i)) ? '1' : '0');
if (val & (1 << i)) {
printf("%d ", i+offset);
}
}
}
int main(int argc, char* argv[]){
if (argc != 3){
printf("usage: %s <register> <val>\n", argv[0]);
return -1;
}
const int REMOTE_ID = 247;
int reg = atoi(argv[1]);
int val = atoi(argv[2]);
modbus_t *ctx;
uint16_t tab_reg[64]={0};
config_gpio_pins();
ctx = modbus_new_rtu("/dev/ttyS5", 9600, 'N', 8, 1);
modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS232);
modbus_rtu_set_rts(ctx, MODBUS_RTU_RTS_UP);
modbus_set_slave(ctx, REMOTE_ID);
//Set custom function
if (modbus_rtu_set_custom_rts(ctx, &set_rts_custom) == -1){
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
close_gpio_pins();
return -1;
}
if (ctx == NULL) {
fprintf(stderr, "Unable to create the libmodbus context\n");
close_gpio_pins();
return -1;
}
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
close_gpio_pins();
return -1;
}
//Write register
if (modbus_write_register(ctx, reg, val) == -1){
fprintf(stderr, "Connection failed: %s\n; reg: %d; val: %d", modbus_strerror(errno),reg, val);
modbus_free(ctx);
close_gpio_pins();
return -1;
}
modbus_free(ctx);
close_gpio_pins();
return 0;
}
void set_rts_custom(modbus_t *ctx, int on){
if (on){
if (write(fd_272, "1", 1) != 1) {
perror("Error writing to /sys/class/gpio/gpio272/value");
exit(1);
}
if (write(fd_273, "1", 1) != 1) {
perror("Error writing to /sys/class/gpio/gpio273/value");
exit(1);
}
}else{
if (write(fd_272, "0", 1) != 1) {
perror("Error writing to /sys/class/gpio/gpio272/value");
exit(1);
}
if (write(fd_273, "0", 1) != 1) {
perror("Error writing to /sys/class/gpio/gpio273/value");
exit(1);
}
}
}
void config_gpio_pins(){
// Export the desired pin by writing to /sys/class/gpio/export
int fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/gpio/export");
exit(1);
}
if (write(fd, "272", 3) != 3) {
perror("Error writing to /sys/class/gpio/export");
exit(1);
}
close(fd);
// Set the pin to be an output by writing "out" to /sys/class/gpio/gpio24/direction
fd = open("/sys/class/gpio/gpio272/direction", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/gpio/gpio272/direction");
exit(1);
}
if (write(fd, "out", 3) != 3) {
perror("Error writing to /sys/class/gpio/gpio272/direction");
exit(1);
}
close(fd);
fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/gpio/export");
exit(1);
}
if (write(fd, "273", 3) != 3) {
perror("Error writing to /sys/class/gpio/export");
exit(1);
}
close(fd);
// Set the pin to be an output by writing "out" to /sys/class/gpio/gpio24/direction
fd = open("/sys/class/gpio/gpio273/direction", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/gpio/gpio273/direction");
exit(1);
}
if (write(fd, "out", 3) != 3) {
perror("Error writing to /sys/class/gpio/gpio273/direction");
exit(1);
}
close(fd);
fd_273 = open("/sys/class/gpio/gpio273/value", O_WRONLY);
if (fd_273 == -1) {
perror("Unable to open /sys/class/gpio/gpio273/value");
exit(1);
}
fd_272 = open("/sys/class/gpio/gpio272/value", O_WRONLY);
if (fd_272 == -1) {
perror("Unable to open /sys/class/gpio/gpio272/value");
exit(1);
}
}
void close_gpio_pins(){
close(fd_272);
close(fd_273);
int fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/gpio/unexport");
exit(1);
}
if (write(fd, "273", 3) != 3) {
perror("Error writing to /sys/class/unexport");
exit(1);
}
if (write(fd, "272", 3) != 3) {
perror("Error writing to /sys/class/unexport");
exit(1);
}
close(fd);
}