122 | | // 02 31 3c 4d 30 30 30 30 30 31 fd 03 |
---|
123 | | unsigned char req_month_yield[] = "\x02\x31\x3c\x4d\x30\x30\x30\x30\x30\x31\xfd\x03"; // Frame for querying the yield. Will be filled in by solQueryYield() |
---|
| 125 | // |
---|
| 126 | unsigned char req_month_yield[] = "\x02\x31\x3C\x6d\x30\x32\x30\x30\x30\x32\xff\x03"; // Frame for querying the monthly yield. Will be filled in by solQueryYield() |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | unsigned char req_minute_yield[] = "\x02\x31\x3C\x6D\x30\x32\x30\x30\x30\x32\x00\x03"; // Frame for querying the minutely yield. Will be filled in by solQueryYield() |
---|
| 130 | const unsigned char yield_minute_rsp[] = "\x02\x31\x6D\x30\x31\x30\x30\x30\x32\x30\x30\x30\x30\x30\x32\x41\x44\xC8\x03\x03"; |
---|
| 131 | const unsigned char yield_minute_r_m[] = "\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF"; |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | unsigned char req_day_yield[] = "\x02\x31\x3c\x44\x30\x32\x30\x30\x30\x32\x00\x03"; // Frame for querying the minutely yield. Will be filled in by solQueryYield() |
---|
| 135 | const unsigned char yield_day_rsp[] = "\x02\x31\x44\x30\x31\x30\x30\x30\x32\x30\x30\x30\x30\x30\x32\x41\x44\xC8\x03\x03"; |
---|
| 136 | const unsigned char yield_day_r_m[] = "\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF"; |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | unsigned char req_hour_yield[] = "\x02\x31\x3c\x68\x30\x32\x30\x30\x30\x32\x00\x03"; // Frame for querying the minutely yield. Will be filled in by solQueryYield() |
---|
| 140 | const unsigned char yield_hour_rsp[] = "\x02\x31\x68\x30\x31\x30\x30\x30\x32\x30\x30\x30\x30\x30\x30\x30\x30\x30\x03\x03"; |
---|
| 141 | const unsigned char yield_hour_r_m[] = "\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF"; |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | |
---|
131 | | // ad tt cc m1 m2 r1 r2 r3 r4 r5 r6 r7 ck |
---|
132 | | //specification: unsigned char yield_month_rsp[] = "\x02\x31\x4d\x30\x00\x00\x09\x0c\x04\x0a\x26\x86\xcd\x00\x03"; /* exp. query yield response. Will be filled in by solQueryYield() */ |
---|
133 | | //specification: unsigned char yield_month_r_m[] = "\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff"; /* exp. query yield seq response mask. Will be filled in by solQueryYield() */ |
---|
134 | | |
---|
135 | | // 02 31 4d 30 30 30 30 30 31 30 30 30 30 31 35 44 43 9a 03 03 |
---|
136 | | // 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 |
---|
137 | | unsigned char yield_month_rsp[] = "\x02\x31\x4d\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03"; /* exp. query yield response. Will be filled in by solQueryYield() */ |
---|
138 | | unsigned char yield_month_r_m[] = "\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff"; /* exp. query yield seq response mask. Will be filled in by solQueryYield() */ |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | // 02 31 4d 30 30 30 30 30 31 30 30 30 30 31 35 44 43 |
---|
| 157 | // 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 |
---|
| 158 | unsigned char yield_month_rsp[] = "\x02\x31\x4d\x33\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03"; /* exp. query yield response. Will be filled in by solQueryYield() */ |
---|
| 159 | unsigned char yield_month_r_m[] = "\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff"; /* exp. query yield seq response mask. Will be filled in by solQueryYield() */ |
---|
554 | | |
---|
555 | | |
---|
556 | | // req_month_yield[4] = 0x30 + 1; // channel |
---|
557 | | // req_month_yield[8] = 0x30 + 0; // month MSB (binary coded hex) |
---|
558 | | // req_month_yield[9] = 0x30 + 4; // month LSB (binary coded hex) |
---|
559 | | |
---|
560 | | /* then, calculate the CRC for tx */ |
---|
561 | | (void)checkRxOneByteCheckSum(&(req_month_yield[0]), 1, 7, 8); |
---|
562 | | /* checksum now in calculatedCrc1Byte */ |
---|
563 | | |
---|
564 | | |
---|
565 | | // crc |
---|
566 | | //req_month_yield[7] = calculatedCrc1Byte; /* fill in checksum */ |
---|
567 | | |
---|
568 | | resetUart3RxBuf(); |
---|
569 | | send_uart3((unsigned char *)req_month_yield, sizeof(req_month_yield)); |
---|
570 | | |
---|
571 | | solarState = SOL_QUERY_YIELD_WAIT_REPLY; /* change state: now wait for response */ |
---|
| 586 | |
---|
| 587 | |
---|
| 588 | if (currentChannel >= 6) |
---|
| 589 | { |
---|
| 590 | currentChannel = 0; |
---|
| 591 | solarState = SOL_NO_INIT; /* we are done with all the channels. go ahead */ |
---|
| 592 | } |
---|
| 593 | else |
---|
| 594 | { |
---|
| 595 | /* query the next channel */ |
---|
| 596 | |
---|
| 597 | checkDate = scDate[2]-1; /* we query the previous day */ |
---|
| 598 | |
---|
| 599 | if (checkDate == 0) |
---|
| 600 | { |
---|
| 601 | /* first day of month */ |
---|
| 602 | |
---|
| 603 | /* check new month */ |
---|
| 604 | if (scDate[1] == 3) |
---|
| 605 | { |
---|
| 606 | /* looking for end of february? handle leap years */ |
---|
| 607 | if ((scDate[0] % 4) == 0) |
---|
| 608 | { |
---|
| 609 | checkDate = 29; /* leap year */ |
---|
| 610 | } |
---|
| 611 | else |
---|
| 612 | { |
---|
| 613 | checkDate = 28; /* not a leap year */ |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | else if |
---|
| 617 | ((scDate[1] == 1) || |
---|
| 618 | (scDate[1] == 5) || |
---|
| 619 | (scDate[1] == 7) || |
---|
| 620 | (scDate[1] == 8) || |
---|
| 621 | (scDate[1] == 10) || |
---|
| 622 | (scDate[1] == 12)) |
---|
| 623 | { |
---|
| 624 | checkDate = 31; |
---|
| 625 | } |
---|
| 626 | else |
---|
| 627 | { |
---|
| 628 | checkDate = 30; |
---|
| 629 | } |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | send_yield_query_day(/* channel: */ currentChannel, /* day: (yesterday) */ checkDate ); |
---|
| 633 | xLastSolarStateChangeTime = xTaskGetTickCount(); |
---|
| 634 | solarState = SOL_QUERY_YIELD_WAIT_REPLY; /* query next channel. change state: now wait for response */ |
---|
| 635 | |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | //send_yield_query_minute(/* channel: */ 1, /* minute: */ 40 ); |
---|
| 639 | |
---|
| 640 | // hour is broken... ?! |
---|
| 641 | //send_yield_query_hour(/* channel: */ 1, /* hour: */ 11 ); |
---|
| 642 | |
---|
| 643 | |
---|
586 | | |
---|
587 | | |
---|
588 | | resultValueRaw = extractBinaryHexDigitString( &(rxUART3[0]), |
---|
589 | | 9, /* first byte of binary hex string */ |
---|
590 | | 16 /* last byte of binary hex string */ |
---|
591 | | ); |
---|
592 | | |
---|
593 | | |
---|
594 | | |
---|
595 | | solarState = SOL_NO_INIT; /* go ahead */ |
---|
596 | | |
---|
| 663 | |
---|
| 664 | if (currentChannel <= 5) |
---|
| 665 | { |
---|
| 666 | |
---|
| 667 | |
---|
| 668 | dayRxYield[currentChannel] = |
---|
| 669 | extractBinaryHexDigitString( &(rxUART3[0]), 13, 16 ); |
---|
| 670 | |
---|
| 671 | } /* if */ |
---|
| 672 | else |
---|
| 673 | { |
---|
| 674 | /* all channels done. nothing to do, we leave via SOL_QUERY_YIELD */ |
---|
| 675 | } |
---|
| 676 | |
---|
| 677 | |
---|
| 678 | currentChannel++; /* increment channel counter so we query the next channel in the next round */ |
---|
| 679 | xLastSolarStateChangeTime = xTaskGetTickCount(); |
---|
| 680 | solarState = SOL_QUERY_YIELD; /* go ahead */ |
---|
| 1204 | |
---|
| 1205 | |
---|
| 1206 | static unsigned char calculate_crc8(unsigned char * d_serdata_tuc, unsigned char nbytes) |
---|
| 1207 | { |
---|
| 1208 | unsigned char i; |
---|
| 1209 | unsigned char j; |
---|
| 1210 | unsigned char d_currbyte_tuc; |
---|
| 1211 | unsigned char d_crc_tuc; |
---|
| 1212 | |
---|
| 1213 | /* start with BISISTX and repeat the checksum calculation for all bytes until CHKS8 (excl.) */ |
---|
| 1214 | d_crc_tuc = 0; // init the checksum |
---|
| 1215 | |
---|
| 1216 | for( j = 1; j < (nbytes-2); j ++ ) |
---|
| 1217 | { |
---|
| 1218 | |
---|
| 1219 | d_currbyte_tuc = d_serdata_tuc[ j ]; |
---|
| 1220 | |
---|
| 1221 | for( i = 8; i > 0; i -- ) |
---|
| 1222 | { |
---|
| 1223 | /* if current bit != 0 */ |
---|
| 1224 | if (( (d_crc_tuc ^ d_currbyte_tuc) & BIT0 ) != 0) |
---|
| 1225 | { |
---|
| 1226 | d_crc_tuc ^= 0x18; |
---|
| 1227 | d_crc_tuc >>= 1; |
---|
| 1228 | d_crc_tuc |= BIT7; |
---|
| 1229 | } |
---|
| 1230 | else |
---|
| 1231 | { |
---|
| 1232 | d_crc_tuc >>= 1; |
---|
| 1233 | } |
---|
| 1234 | d_currbyte_tuc >>= 1; // prepare for next databit |
---|
| 1235 | } /* for */ |
---|
| 1236 | } // this byte is finished |
---|
| 1237 | |
---|
| 1238 | /* checksum is complete now - result always ORed with 0x80 and then compare */ |
---|
| 1239 | /* calculated and received checksum */ |
---|
| 1240 | d_crc_tuc |= BIT7; |
---|
| 1241 | return d_crc_tuc; |
---|
| 1242 | } |
---|
| 1243 | |
---|
| 1244 | |
---|
| 1245 | void send_yield_query_day(unsigned char channel /* 0 to 5 */, unsigned char day /* 1 to 31 */) |
---|
| 1246 | { |
---|
| 1247 | unsigned char newChkSum; |
---|
| 1248 | |
---|
| 1249 | req_day_yield[5] = 0x30 + channel; // channel |
---|
| 1250 | |
---|
| 1251 | |
---|
| 1252 | /* convert day to binary coded hex */ |
---|
| 1253 | if (day > 15) |
---|
| 1254 | { |
---|
| 1255 | req_day_yield[8] = 0x31; /* hex MSB day is 1 */ |
---|
| 1256 | day = day - 16; /* decrease day (is now remainder */ |
---|
| 1257 | } |
---|
| 1258 | else |
---|
| 1259 | { |
---|
| 1260 | req_day_yield[8] = 0x30; /* hex MSB day is 0 */ |
---|
| 1261 | } |
---|
| 1262 | req_day_yield[9] = 0x30 + day; /* hex LSB day */ |
---|
| 1263 | |
---|
| 1264 | newChkSum = calculate_crc8( (&(req_day_yield[0])), (sizeof(req_day_yield)-1)); |
---|
| 1265 | req_day_yield[10] = newChkSum; /* fill in checksum */ |
---|
| 1266 | |
---|
| 1267 | resetUart3RxBuf(); |
---|
| 1268 | send_uart3((unsigned char *)req_day_yield, sizeof(req_day_yield)); |
---|
| 1269 | |
---|
| 1270 | } /* prepare_yield_query_day() */ |
---|
| 1271 | |
---|
| 1272 | |
---|
| 1273 | void send_yield_query_hour(unsigned char channel /* 0 to 5 */, unsigned char hour /* 0 to 23 */) |
---|
| 1274 | { |
---|
| 1275 | |
---|
| 1276 | unsigned char newChkSum; |
---|
| 1277 | unsigned char hourMSB; |
---|
| 1278 | unsigned char hourLSB; |
---|
| 1279 | |
---|
| 1280 | req_hour_yield[5] = 0x30 + channel; // channel |
---|
| 1281 | |
---|
| 1282 | |
---|
| 1283 | hourMSB = hour % 16; |
---|
| 1284 | hourLSB = hour - hourMSB; |
---|
| 1285 | |
---|
| 1286 | req_hour_yield[8] = 0x30 + hourMSB; // minute MSB (binary coded hex) |
---|
| 1287 | req_hour_yield[9] = 0x30 + hourLSB; // minute LSB (binary coded hex) |
---|
| 1288 | |
---|
| 1289 | newChkSum = calculate_crc8( (&(req_hour_yield[0])), (sizeof(req_hour_yield)-1)); |
---|
| 1290 | |
---|
| 1291 | // crc |
---|
| 1292 | req_hour_yield[10] = newChkSum; /* fill in checksum */ |
---|
| 1293 | |
---|
| 1294 | resetUart3RxBuf(); |
---|
| 1295 | send_uart3((unsigned char *)req_hour_yield, sizeof(req_hour_yield)); |
---|
| 1296 | |
---|
| 1297 | |
---|
| 1298 | |
---|
| 1299 | } /* prepare_yield_query_hour() */ |
---|
| 1300 | |
---|
| 1301 | |
---|
| 1302 | void send_yield_query_minute(unsigned char channel /* 0 to 5 */, unsigned char minute /* 0 to 59 */) |
---|
| 1303 | { |
---|
| 1304 | unsigned char newChkSum; |
---|
| 1305 | unsigned char minuteMSB; |
---|
| 1306 | unsigned char minuteLSB; |
---|
| 1307 | |
---|
| 1308 | req_minute_yield[5] = 0x30 + channel; // channel |
---|
| 1309 | |
---|
| 1310 | |
---|
| 1311 | minuteMSB = minute % 16; |
---|
| 1312 | minuteLSB = minute - minuteMSB; |
---|
| 1313 | |
---|
| 1314 | req_minute_yield[6] = 0x30 + minuteMSB; // minute MSB (binary coded hex) |
---|
| 1315 | req_minute_yield[7] = 0x30 + minuteLSB; // minute LSB (binary coded hex) |
---|
| 1316 | |
---|
| 1317 | newChkSum = calculate_crc8( (&(req_minute_yield[0])), (sizeof(req_minute_yield)-1)); |
---|
| 1318 | |
---|
| 1319 | // crc |
---|
| 1320 | req_minute_yield[10] = newChkSum; /* fill in checksum */ |
---|
| 1321 | |
---|
| 1322 | resetUart3RxBuf(); |
---|
| 1323 | send_uart3((unsigned char *)req_minute_yield, sizeof(req_minute_yield)); |
---|
| 1324 | |
---|
| 1325 | |
---|
| 1326 | } /* prepare_yield_query_minute() */ |
---|