-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodprobe.c
214 lines (154 loc) · 4.91 KB
/
modprobe.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
204
205
206
207
208
209
210
211
212
213
214
/*************************************************************************
ModProbe -- a simple Modbus/TCP testing utility based on libmodbus 3.0.6
By Tony R. Kuphaldt
Last update 10 May 2019
This software is released under the CC0 1.0 Universal license,
which is equivalent to Public Domain.
This program reads arbitrary Modbus registers specified by the user.
*************************************************************************/
#include <stdio.h>
#include "modbus_open_close.h" // Contains Modbus network parameters
// Prototyping all functions
int convert_int_float (int);
// Declaring all global variables
uint16_t inreg_word[10000];
float value_ABCD;
float value_DCBA;
float value_CDAB;
float value_BADC;
int
main (void)
{
int n;
int address;
int num;
int read_count;
// Opens up Modbus network connection
open_modbus_connection ();
printf ("Welcome to ModProbe!\n\n");
while (1)
{
printf
("\n\nEnter the absolute address of the Modbus register you wish to read, and the number of contiguous registers to be read.\n\n");
// printf ("Addresses 00001 through 09999 = Discrete output `coil' registers \n");
// printf ("Addresses 10001 through 19999 = Discrete input `contact' registers \n");
printf ("Addresses 30001 through 39999 = Analog input registers \n");
printf ("Addresses 40001 through 49999 = `Holding' registers \n\n");
printf ("Absolute address = ");
scanf ("%i", &address);
printf ("Number of addresses to read = ");
scanf ("%i", &num);
// Prevents reading less than one register or more than 98
if (num < 1)
num = 1;
if (num > 9998)
num = 9998;
// `coil' register address range
if (address > 0 && address < 10000)
{
printf
("This function is not yet supported by modprobe -- sorry!\n\n");
close_modbus_connection ();
return -2;
// read_count = modbus_read_registers (Device, address, num, inreg_word);
}
// `contact' register address range
else if (address > 10000 && address < 20000)
{
printf
("This function is not yet supported by modprobe -- sorry!\n\n");
close_modbus_connection ();
return -2;
// read_count = modbus_read_registers (Device, address, num, inreg_word);
}
// `analog input' register address range
else if (address > 30000 && address < 40000)
{
read_count =
modbus_read_input_registers (Device, address - 30001, num + 1,
inreg_word);
}
// `holding' register address range
else if (address > 40000 && address < 50000)
{
read_count =
modbus_read_registers (Device, address - 40001, num + 1,
inreg_word);
}
else
{
printf ("That is an invalid address value!\n\n");
close_modbus_connection ();
return -2;
}
// If read command is unsuccessful, get out of ncurses display mode,
// print an error message, and then return to ncurses display mode.
if (read_count < 1)
{
fprintf (stderr, "Unable to read data from the Modbus device! \n");
close_modbus_connection ();
return -2;
}
printf ("\n");
for (n = 0; n < num; ++n)
{
convert_int_float (n);
printf ("Register %i = %i\n", address + n, inreg_word[n]);
printf ("\t%i/%i FP (ABCD order) = %.2f \n", address + n,
address + n + 1, value_ABCD);
printf ("\t%i/%i FP (DCBA order) = %.2f \n", address + n,
address + n + 1, value_DCBA);
printf ("\t%i/%i FP (CDAB order) = %.2f \n", address + n,
address + n + 1, value_CDAB);
printf ("\t%i/%i FP (BADC order) = %.2f \n", address + n,
address + n + 1, value_BADC);
}
}
// Closes out Modbus network connection
close_modbus_connection ();
// Force of habit here -- I like all functions to return *something*
return 1;
}
/********************************************/
int
convert_int_float (int n)
{
union
{
uint16_t word[2];
uint8_t byte[4];
} in;
union
{
uint8_t byte[4];
float real;
} out;
in.word[0] = inreg_word[n];
in.word[1] = inreg_word[n + 1];
// Re-ordering the bytes according to the pattern ABCD
out.byte[0] = in.byte[0]; // A
out.byte[1] = in.byte[1]; // B
out.byte[2] = in.byte[2]; // C
out.byte[3] = in.byte[3]; // D
value_ABCD = out.real;
// Re-ordering the bytes according to the pattern DCBA
out.byte[0] = in.byte[3]; // D
out.byte[1] = in.byte[2]; // C
out.byte[2] = in.byte[1]; // B
out.byte[3] = in.byte[0]; // A
value_DCBA = out.real;
// Re-ordering the bytes according to the pattern CDAB
out.byte[0] = in.byte[2]; // C
out.byte[1] = in.byte[3]; // D
out.byte[2] = in.byte[0]; // A
out.byte[3] = in.byte[1]; // B
value_CDAB = out.real;
// Re-ordering the bytes according to the pattern BADC
out.byte[0] = in.byte[1]; // B
out.byte[1] = in.byte[0]; // A
out.byte[2] = in.byte[3]; // D
out.byte[3] = in.byte[2]; // C
value_BADC = out.real;
// Force of habit here -- I like all functions to return *something*
return 1;
}