Skip to content

Commit 522b039

Browse files
committed
Change to use bcd2int
1 parent 9faf18e commit 522b039

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/devices/m_bus.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ for further processing by an Application layer (outside this program).
2525
// Convert two BCD encoded nibbles to an integer
2626
static unsigned bcd2int(uint8_t bcd)
2727
{
28-
return 10*(bcd>>4) + (bcd & 0xF);
28+
return 10 * (bcd >> 4) + (bcd & 0xf);
2929
}
3030

3131
// Mapping from 6 bits to 4 bits. "3of6" coding used for Mode T
@@ -156,9 +156,9 @@ typedef struct {
156156
uint8_t tpci;
157157
uint8_t apci;
158158
/* Q-walk_by */
159-
uint32_t q_total;
160-
uint32_t q_lastyear;
161-
uint32_t q_lastmonth;
159+
float q_total;
160+
float q_lastyear;
161+
float q_lastmonth;
162162
} m_bus_block2_t;
163163

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

0 commit comments

Comments
 (0)