/
random-test-client.c
143 lines (121 loc) · 3.95 KB
/
random-test-client.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
/*
* Copyright © 2001-2010 Stéphane Raimbault <stephane.raimbault@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <modbus.h>
#include <mysql.h>
#include <my_global.h>
/* The goal of this program is to check all major functions of
libmodbus:
- write_coil
- read_bits
- write_coils
- write_register
- read_registers
- write_registers
- read_registers
All these functions are called with random values on a address
range defined by the following defines.
*/
#define LOOP 1
#define SERVER_ID 1
#define ADDRESS_START 0
#define ADDRESS_END 2
/* At each loop, the program works in the range ADDRESS_START to
* ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on.
*/
int main(void)
{
modbus_t *ctx;
int rc;
int nb_fail;
int nb_loop;
int addr;
int nb;
uint16_t *tab_rp_registers;
char query[100];
int reg_address;
/* RTU */
ctx = modbus_new_rtu("/dev/ttyUSB0", 9600, 'N', 8, 1);
modbus_set_slave(ctx, SERVER_ID);
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n",
modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "localhost", "root", "root",
NULL, 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
if(mysql_select_db(con, "embedded")==0)/*success*/
printf( "Database Selected\n");
else
printf( "Failed to connect to Database: Error: \n");
if (mysql_query(con, "select address from configuration")) {
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
MYSQL_RES * res = mysql_use_result(con);
nb = mysql_num_rows(res);
tab_rp_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
memset(tab_rp_registers, 0, nb * sizeof(uint16_t));
MYSQL_ROW row;
/* output table name */
printf("Addresses from database:\n");
while ((row = mysql_fetch_row(res)) != NULL) {
reg_address = atoi(row[0]);
addr = reg_address - 30000;
printf("%i \n", addr);
rc = modbus_read_input_registers(ctx, addr, 1, tab_rp_registers);
if (rc == -1) {
printf("ERROR modbus_read_input_registers (%d)\n", rc);
nb_fail++;
} else{
printf("Address = %d, value %d \n",addr, tab_rp_registers[0]);
}
sprintf(query, "INSERT INTO solar_panel_data (address, value) VALUES (%i, %d)", reg_address, tab_rp_registers[0]);
printf("%s\n", query);
/* if (mysql_query(con, "INSERT INTO solar_panel_data (address, value) VALUES (30001, 65273)")) {
printf("ERROR writing to database");
}
*/ }
mysql_free_result(res);
//do the queries in to matrix
if (mysql_query(con, "INSERT INTO solar_panel_data (address, value) VALUES (30001, 65273)")) {
printf("ERROR writing to database");
}
/* Free the memory */
free(tab_rp_registers);
/* Close the connection */
modbus_close(ctx);
modbus_free(ctx);
mysql_close(con);
return 0;
}