Skip to content

Commit

Permalink
Change to use bcd2int
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Jul 15, 2024
1 parent 9faf18e commit 522b039
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/devices/m_bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ for further processing by an Application layer (outside this program).
// Convert two BCD encoded nibbles to an integer
static unsigned bcd2int(uint8_t bcd)
{
return 10*(bcd>>4) + (bcd & 0xF);
return 10 * (bcd >> 4) + (bcd & 0xf);
}

// Mapping from 6 bits to 4 bits. "3of6" coding used for Mode T
Expand Down Expand Up @@ -156,9 +156,9 @@ typedef struct {
uint8_t tpci;
uint8_t apci;
/* Q-walk_by */
uint32_t q_total;
uint32_t q_lastyear;
uint32_t q_lastmonth;
float q_total;
float q_lastyear;
float q_lastmonth;
} m_bus_block2_t;

// Data structure for block 1
Expand Down Expand Up @@ -785,15 +785,19 @@ static int parse_block2(const m_bus_data_t *in, m_bus_block1_t *block1)
b2->CW = (b[9] << 8) | (b[8]);
b2->pl_offset = BLOCK1A_SIZE - 2 + 8;
// Note the offsets to skip block CRCs
b2->q_total = ((uint32_t)b[20+2] << 24) | (b[19+2] << 16) | (b[18+2] << 8) | (b[17+2]);
b2->q_lastyear = ((uint32_t)b[26+2] << 24) | (b[25+2] << 16) | (b[24+2] << 8) | (b[23+2]);
b2->q_lastmonth = ((uint32_t)b[32+4] << 24) | (b[31+2] << 16) | (b[30+2] << 8) | (b[29+2]);
b2->q_total = bcd2int(b[20 + 2]) * 1000000 + bcd2int(b[19 + 2]) * 10000 + bcd2int(b[18 + 2]) * 100 + bcd2int(b[17 + 2]);
b2->q_lastyear = bcd2int(b[26 + 2]) * 1000000 + bcd2int(b[25 + 2]) * 10000 + bcd2int(b[24 + 2]) * 100 + bcd2int(b[23 + 2]);
b2->q_lastmonth = bcd2int(b[32 + 4]) * 1000000 + bcd2int(b[31 + 2]) * 10000 + bcd2int(b[30 + 2]) * 100 + bcd2int(b[29 + 2]);
if (block1->A_DevType == 6) {
/* WarmWater */
// Value factor is 0.001, e.g. 123.456 m3
b2->q_total /= 1000.0f;
b2->q_lastyear /= 1000.0f;
b2->q_lastmonth /= 1000.0f;
}
if (block1->A_DevType == 8) {
/* Heat Cost Allocator */
// snprintf(extra, sizeof(extra), "%02x%02x%02x%02x%02x", b[6], b[7], b[8], b[9], b[10]);
// Value factor is 1, e.g. 123456 m3
}
}
// fprintf(stderr, "Instantaneous Value: %02x%02x : %f\n",b[9],b[10],((b[10]<<8)|b[9])*0.01);
Expand Down Expand Up @@ -936,9 +940,9 @@ static int m_bus_output_data(r_device *decoder, bitbuffer_t *bitbuffer, const m_
// "L", "Length", DATA_INT, block1->L,
"data_length", "Data Length", DATA_INT, out->length,
"data", "Data", DATA_STRING, str_buf,
"Q_total", "Q_total", DATA_COND, block1->block2.CI == 0x78, DATA_FORMAT, "%x m3", DATA_INT, block1->block2.q_total,
"Q_lastyear", "Q_lastyear", DATA_COND, block1->block2.CI == 0x78, DATA_FORMAT, "%x m3", DATA_INT, block1->block2.q_lastyear,
"Q_lastmonth", "Q_lastmonth", DATA_COND, block1->block2.CI == 0x78, DATA_FORMAT, "%x m3", DATA_INT, block1->block2.q_lastmonth,
"Q_total", "Q_total", DATA_COND, block1->block2.CI == 0x78, DATA_FORMAT, "%.3f m3", DATA_DOUBLE, block1->block2.q_total,
"Q_lastyear", "Q_lastyear", DATA_COND, block1->block2.CI == 0x78, DATA_FORMAT, "%.3f m3", DATA_DOUBLE, block1->block2.q_lastyear,
"Q_lastmonth", "Q_lastmonth", DATA_COND, block1->block2.CI == 0x78, DATA_FORMAT, "%.3f m3", DATA_DOUBLE, block1->block2.q_lastmonth,
"mic", "Integrity", DATA_STRING, "CRC",
NULL);
/* clang-format on */
Expand Down

0 comments on commit 522b039

Please sign in to comment.