unit-test-master.c
5.16 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
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
/*
* Copyright (C) 2001-2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
*
* Licensed under the GNU General Public License Version 2
*
* 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 2 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 <modbus/modbus.h>
#include "unit-test.h"
/* Tests based on PI-MBUS-300 documentation */
#define SLAVE 0x11
int main(void)
{
uint8_t *tab_rp_status;
uint16_t *tab_rp_registers;
modbus_param_t mb_param;
int i;
uint8_t value;
uint16_t address;
uint16_t nb_points;
/* RTU parity : none, even, odd */
/* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
/* TCP */
modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
modbus_connect(&mb_param);
/* Allocate and initialize the memory to store the status */
nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ?
UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS;
tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
memset(tab_rp_status, 0, nb_points * sizeof(uint8_t));
/* Allocate and initialize the memory to store the registers */
nb_points = (UT_HOLDING_REGISTERS_NB_POINTS > UT_INPUT_REGISTERS_NB_POINTS) ?
UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
printf("Unit testing\n");
/** COIL STATUS **/
read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS,
UT_COIL_STATUS_NB_POINTS, tab_rp_status);
i = 0;
address = UT_COIL_STATUS_ADDRESS;
nb_points = UT_COIL_STATUS_NB_POINTS;
while (nb_points > 0) {
int nb_bits = (nb_points > 8) ? 8 : nb_points;
value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
if (value != UT_COIL_STATUS_TAB[i]) {
printf("Coil status: FAILED (%0X != %0X)\n",
value, UT_COIL_STATUS_TAB[i]);
goto close;
}
nb_points -= nb_bits;
i++;
}
printf("Coil status: OK\n");
/** INPUT STATUS **/
read_input_status(&mb_param, SLAVE, UT_INPUT_STATUS_ADDRESS,
UT_INPUT_STATUS_NB_POINTS, tab_rp_status);
i = 0;
address = UT_INPUT_STATUS_ADDRESS;
nb_points = UT_INPUT_STATUS_NB_POINTS;
while (nb_points > 0) {
int nb_bits = (nb_points > 8) ? 8 : nb_points;
value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
if (value != UT_INPUT_STATUS_TAB[i]) {
printf("Input status: FAILED (%0X != %0X)\n",
value, UT_INPUT_STATUS_TAB[i]);
goto close;
}
nb_points -= nb_bits;
i++;
}
printf("Input status: OK\n");
/** HOLDING REGISTERS **/
read_holding_registers(&mb_param, SLAVE, UT_HOLDING_REGISTERS_ADDRESS,
UT_HOLDING_REGISTERS_NB_POINTS, tab_rp_registers);
for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) {
if (tab_rp_registers[i] != UT_HOLDING_REGISTERS_TAB[i]) {
printf("Holding registers: FAILED (%0X != %0X)\n",
tab_rp_registers[i], UT_HOLDING_REGISTERS_TAB[i]);
goto close;
}
}
printf("Holding registers: OK\n");
/** INPUT REGISTERS **/
read_input_registers(&mb_param, SLAVE, UT_INPUT_REGISTERS_ADDRESS,
UT_INPUT_REGISTERS_NB_POINTS, tab_rp_registers);
for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
printf("Input registers: FAILED (%0X != %0X)\n",
tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
goto close;
}
}
printf("Input registers: OK\n");
close:
/* Free the memory */
free(tab_rp_status);
free(tab_rp_registers);
/* Close the connection */
modbus_close(&mb_param);
return 0;
/*
force_single_coil(&mb_param, SLAVE, 0xAC, ON);
*/
}