Функция опроса контроллера void tracer_data_request dataTrade false Оп

  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
215
216
217
218
219
220
221
222
223
224
225
226
// Функция опроса контроллера
void tracer_data_request() {
dataTrade = false; // Опускаем флаг наличия обмена данными с контроллером
// *--Запрос текущих параметров --*//
delay(400);
node.clearResponseBuffer();
result = node.readInputRegisters(0x3100, 2);
if (result == node.ku8MBSuccess)
{
dataTrade = true;
pv_V = node.getResponseBuffer(0x00) / 100.0f;
pv_I = node.getResponseBuffer(0x01) / 100.0f;
//pv_P = (node.getResponseBuffer(0x02) + (long)(node.getResponseBuffer(0x03)) << 16) / 100.0f;
Serial.println();
Serial.print("1. Voltage PV: ");
Serial.println(pv_V);
Serial.print("2. Current PV: ");
Serial.println(pv_I);
}
yield();
node.clearResponseBuffer();
result = node.readInputRegisters(0x3104, 2);
if (result == node.ku8MBSuccess)
{
dataTrade = true;
batt_V = node.getResponseBuffer(0x00) / 100.0f;
batt_I = node.getResponseBuffer(0x01) / 100.0f;
Serial.print("3. Batt U: ");
Serial.println(batt_V);
Serial.print("4. Batt I: ");
Serial.println(batt_I);
}
// *--Запрос состояния батареи --*//
delay(100);
node.clearResponseBuffer();
result = node.readInputRegisters(0x310C, 2);
if (result == node.ku8MBSuccess)
{
dataTrade = true;
load_U = node.getResponseBuffer(0x00) / 100.0f;
load_I = node.getResponseBuffer(0x01) / 100.0f;
Serial.println();
Serial.print("5. U Load: ");
Serial.println(load_U);
Serial.print("6. I Load: ");
Serial.println(load_I);
}
yield();
node.clearResponseBuffer();
result = node.readInputRegisters(0x3110, 1);
if (result == node.ku8MBSuccess)
{
dataTrade = true;
batt_T = node.getResponseBuffer(0x00) / 100.0f;
Serial.print("7. T bat: ");
Serial.println(batt_T);
}
yield();
node.clearResponseBuffer();
result = node.readInputRegisters(0x311A, 1);
if (result == node.ku8MBSuccess)
{
dataTrade = true;
batt_SOC = node.getResponseBuffer(0x00);
Serial.print("8. SOC: ");
Serial.println(batt_SOC);
}
// *--Запрос накопленной энергии --*//
delay(100);
node.clearResponseBuffer();
result = node.readInputRegisters(0x330C, 2);
if (result == node.ku8MBSuccess)
{
dataTrade = true;
flag_TotEn = true;
EnergyToday = (node.getResponseBuffer(0x00) + (long)(node.getResponseBuffer(0x01)) << 16) / 10000.0f;
Serial.println();
Serial.print("9. Today energy kWH: ");
Serial.println(EnergyToday);
}
yield();
node.clearResponseBuffer();
result = node.readInputRegisters(0x3312, 2);
if (result == node.ku8MBSuccess)
{
dataTrade = true;
EnergyTotal = (node.getResponseBuffer(0x00) + (long)(node.getResponseBuffer(0x01)) << 16) / 10000.0f;
Serial.print("10. Total energy kWH: ");
Serial.println(EnergyTotal);
}
// *--Запрос статуса АКБ --*//
delay(100);
unsigned short batt_status;
unsigned char batt_st[16];
unsigned char batt_st_voltage;
unsigned char batt_st_temper;
node.clearResponseBuffer();
result = node.readInputRegisters(0x3200, 1);
if (result == node.ku8MBSuccess)
{
dataTrade = true;
batt_status = node.getResponseBuffer(0x00);
for (int i = 0; i < 16; i++)
{
batt_st[i] = bitRead(batt_status, i);
yield();
}
batt_st_voltage = batt_st[0] + ((unsigned char)(batt_st[1]) << 1) + ((unsigned char)(batt_st[2]) << 2) + ((unsigned char)(batt_st[3]) << 3);
batt_st_temper = batt_st[4] + ((unsigned char)(batt_st[5]) << 1) + ((unsigned char)(batt_st[6]) << 2) + ((unsigned char)(batt_st[7]) << 3);
switch (batt_st_voltage)
{
case 0: batt_Status = ""; break;
case 1: batt_Status = "Ovdervolt"; break;
case 2: batt_Status = "Under_Volt"; break;
case 3: batt_Status = "Low_Volt_Disconnect"; break;
case 4: batt_Status = "Fault"; break;
default: batt_Status = ""; break;
}
switch (batt_st_temper)
{
case 0: batt_Status += ""; break;
case 1: batt_Status += "Ovder_Temp"; break;
case 2: batt_Status += "Low_Temp"; break;
default: batt_Status += ""; break;
}
if (batt_st[8] == 1) {
batt_Status += " Fault_Rbatt";
}
if (batt_st[15] == 1) {
batt_Status += " Fault_Unom";
}
if (batt_Status == "") {
batt_Status = "Normal"; // Если ошибок нет - выводим "Normal"
}
Serial.println();
Serial.print("16. Ststus battery: ");
Serial.println(batt_Status);
}
// *--Запрос статуса заряда --*//
delay(100);
unsigned short charg_status;
unsigned char charg_st[16];
unsigned char charg_st_scharg;
unsigned char input;
node.clearResponseBuffer();
result = node.readInputRegisters(0x3201, 1);
if (result == node.ku8MBSuccess)
{
dataTrade = true;
charg_status = node.getResponseBuffer(0x00);
for (int j = 0; j < 16; j++)
{
charg_st[j] = bitRead(charg_status, j);
yield();
}
charg_st_scharg = charg_st[2] + ((unsigned char)(charg_st[3]) << 1);
input = charg_st[14] + ((unsigned char)(charg_st[15]) << 1);
switch (charg_st_scharg)
{
case 0: ch_Status = "No_charging"; break;
case 1: ch_Status = "Float"; break;
case 2: ch_Status = "Boost"; break;
case 3: ch_Status = "Equlization"; break;
default: ch_Status = ""; break;
}
switch (input)
{
case 0: ch_Status += ""; break;
case 1: ch_Status += " NO_power_connect"; break;
case 2: ch_Status += " Higher_volt"; break;
case 3: ch_Status += " Input_volt_error"; break;
default: ch_Status += ""; break;
}
if (charg_st[10] == 1) {
ch_Status += " Input_over_current";
}
if (charg_st[9] == 1) {
ch_Status += " Load_over_current";
}
if (charg_st[4] == 1) {
ch_Status += " PV_input_short";
}
Serial.print("17. Charing status: ");
Serial.println(ch_Status);
}
// *--Запрос состояния выходного реле --*//
/* //Не учитывает отключение выхода, при перенапряжении и низком заряде АКБ
yield();
node.clearResponseBuffer();
result = node.readCoils(0x0002, 1);
if (result == node.ku8MBSuccess)
{
Serial.print("18. Выходное реле: ");
Serial.println(node.getResponseBuffer(0x00));
}
*/
if ( dataTrade == true ) {
Serial.println("Данные из контроллера получены!");
}
else {
Serial.println("Нет связи с контроллером");
}
Serial.println();
}
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}