Fix checksum calculation for pipsolar (#5299)

This commit is contained in:
Mat931 2023-09-02 09:54:03 +00:00 committed by GitHub
parent 2165960ba1
commit 5fdafc00e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View File

@ -769,7 +769,7 @@ uint8_t Pipsolar::check_incoming_length_(uint8_t length) {
uint8_t Pipsolar::check_incoming_crc_() {
uint16_t crc16;
crc16 = crc16be(read_buffer_, read_pos_ - 3);
crc16 = this->pipsolar_crc_(read_buffer_, read_pos_ - 3);
ESP_LOGD(TAG, "checking crc on incoming message");
if (((uint8_t) ((crc16) >> 8)) == read_buffer_[read_pos_ - 3] &&
((uint8_t) ((crc16) &0xff)) == read_buffer_[read_pos_ - 2]) {
@ -798,7 +798,7 @@ uint8_t Pipsolar::send_next_command_() {
this->command_start_millis_ = millis();
this->empty_uart_buffer_();
this->read_pos_ = 0;
crc16 = crc16be(byte_command, length);
crc16 = this->pipsolar_crc_(byte_command, length);
this->write_str(command);
// checksum
this->write(((uint8_t) ((crc16) >> 8))); // highbyte
@ -825,8 +825,8 @@ void Pipsolar::send_next_poll_() {
this->command_start_millis_ = millis();
this->empty_uart_buffer_();
this->read_pos_ = 0;
crc16 = crc16be(this->used_polling_commands_[this->last_polling_command_].command,
this->used_polling_commands_[this->last_polling_command_].length);
crc16 = this->pipsolar_crc_(this->used_polling_commands_[this->last_polling_command_].command,
this->used_polling_commands_[this->last_polling_command_].length);
this->write_array(this->used_polling_commands_[this->last_polling_command_].command,
this->used_polling_commands_[this->last_polling_command_].length);
// checksum
@ -893,5 +893,17 @@ void Pipsolar::add_polling_command_(const char *command, ENUMPollingCommand poll
}
}
uint16_t Pipsolar::pipsolar_crc_(uint8_t *msg, uint8_t len) {
uint16_t crc = crc16be(msg, len);
uint8_t crc_low = crc & 0xff;
uint8_t crc_high = crc >> 8;
if (crc_low == 0x28 || crc_low == 0x0d || crc_low == 0x0a)
crc_low++;
if (crc_high == 0x28 || crc_high == 0x0d || crc_high == 0x0a)
crc_high++;
crc = (crc_high << 8) | crc_low;
return crc;
}
} // namespace pipsolar
} // namespace esphome

View File

@ -193,7 +193,7 @@ class Pipsolar : public uart::UARTDevice, public PollingComponent {
void empty_uart_buffer_();
uint8_t check_incoming_crc_();
uint8_t check_incoming_length_(uint8_t length);
uint16_t cal_crc_half_(uint8_t *msg, uint8_t len);
uint16_t pipsolar_crc_(uint8_t *msg, uint8_t len);
uint8_t send_next_command_();
void send_next_poll_();
void queue_command_(const char *command, uint8_t length);