Skip to content

Commit

Permalink
example seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
maebli committed Mar 21, 2024
1 parent 16c3389 commit ed6876c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
9 changes: 8 additions & 1 deletion examples/example_parsing_long_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
if let Frame::LongFrame {
function,
address,
data: _,
data,
} = frame
{
assert_eq!(
Expand All @@ -27,5 +27,12 @@ fn main() {
}
);
assert_eq!(address, Address::Primary(1));

if let Ok(parsed) = m_bus_parser::user_data::UserDataBlock::try_from(data) {
println!("user data: {:?}", parsed);
if let Ok(data) = m_bus_parser::user_data::DataRecords::try_from(data) {
println!("user data: {:?}", data);
}
}
}
}
10 changes: 10 additions & 0 deletions examples/example_parsing_user_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use m_bus_parser::user_data::DataRecords;

fn main() {
/* Data block 1: unit 0, storage No 0, no tariff, instantaneous volume, 12565 l (24 bit integer) */
/* Data block 2: unit 0, storage No 0, no tariff, instantaneous volume, 12565 l (24 bit integer) */
let data = vec![0x03, 0x13, 0x15, 0x31, 0x00, 0x03, 0x13, 0x15, 0x31, 0x00];
let result = DataRecords::try_from(data.as_slice());
assert!(result.is_ok());
assert!(result.unwrap().len() == 2);
}
2 changes: 1 addition & 1 deletion src/user_data/value_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl TryFrom<&[u8]> for ValueInformation {
fn try_from(data: &[u8]) -> Result<Self, ValueInformationError> {
Ok(match data[0] {
0x00..=0x7B | 0x80..=0xFA => ValueInformation::Primary(data[0]),
0x7C => ValueInformation::PlainText,
0x7C | 0xFC => ValueInformation::PlainText,
0xFD => ValueInformation::Extended(match data[1] {
0x00..=0x03 => VIFExtension::CreditOfCurrencyUnits(0b11 & data[1]),
0x04..=0x07 => VIFExtension::DebitOfCurrencyUnits(0b11 & data[1]),
Expand Down
94 changes: 93 additions & 1 deletion src/user_data/variable_user_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ impl TryFrom<&[u8]> for DataRecord {
match value_information {
ValueInformation::Primary(_) => {
let value = match data_information.data_field_coding {
data_information::DataFieldCoding::Real32Bit => {
total_size += 4;
f32::from_le_bytes([
data[current_index],
data[current_index + 1],
data[current_index + 2],
data[current_index + 3],
]) as f64
}
data_information::DataFieldCoding::Integer8Bit => {
total_size += 1;
data[current_index] as f64
Expand All @@ -95,7 +104,90 @@ impl TryFrom<&[u8]> for DataRecord {
| (data[current_index + 2] as u32) << 8
| data[current_index + 1] as u32) as f64
}
_ => 0.0,
data_information::DataFieldCoding::Integer32Bit => {
total_size += 4;
((data[current_index + 3] as u32) << 24
| (data[current_index + 2] as u32) << 16
| (data[current_index + 1] as u32) << 8
| data[current_index] as u32) as f64
}
data_information::DataFieldCoding::Integer48Bit => {
total_size += 6;
((data[current_index + 5] as u64) << 40
| (data[current_index + 4] as u64) << 32
| (data[current_index + 3] as u64) << 24
| (data[current_index + 2] as u64) << 16
| (data[current_index + 1] as u64) << 8
| data[current_index] as u64) as f64
}
data_information::DataFieldCoding::Integer64Bit => {
total_size += 8;
((data[current_index + 7] as u64) << 56
| (data[current_index + 6] as u64) << 48
| (data[current_index + 5] as u64) << 40
| (data[current_index + 4] as u64) << 32
| (data[current_index + 3] as u64) << 24
| (data[current_index + 2] as u64) << 16
| (data[current_index + 1] as u64) << 8
| data[current_index] as u64) as f64
}
data_information::DataFieldCoding::BCD2Digit => {
total_size += 1;
((data[current_index] >> 4) as f64 * 10.0)
+ (data[current_index] & 0x0F) as f64
}
data_information::DataFieldCoding::BCD4Digit => {
total_size += 2;
((data[current_index + 1] >> 4) as f64 * 1000.0)
+ ((data[current_index + 1] & 0x0F) as f64 * 100.0)
+ ((data[current_index] >> 4) as f64 * 10.0)
+ (data[current_index] & 0x0F) as f64
}
data_information::DataFieldCoding::BCD6Digit => {
total_size += 3;
((data[current_index + 2] >> 4) as f64 * 100000.0)
+ ((data[current_index + 2] & 0x0F) as f64 * 10000.0)
+ ((data[current_index + 1] >> 4) as f64 * 1000.0)
+ ((data[current_index + 1] & 0x0F) as f64 * 100.0)
+ ((data[current_index] >> 4) as f64 * 10.0)
+ (data[current_index] & 0x0F) as f64
}
data_information::DataFieldCoding::BCD8Digit => {
total_size += 4;
((data[current_index + 3] >> 4) as f64 * 10000000.0)
+ ((data[current_index + 3] & 0x0F) as f64 * 1000000.0)
+ ((data[current_index + 2] >> 4) as f64 * 100000.0)
+ ((data[current_index + 2] & 0x0F) as f64 * 10000.0)
+ ((data[current_index + 1] >> 4) as f64 * 1000.0)
+ ((data[current_index + 1] & 0x0F) as f64 * 100.0)
+ ((data[current_index] >> 4) as f64 * 10.0)
+ (data[current_index] & 0x0F) as f64
}
data_information::DataFieldCoding::BCDDigit12 => {
total_size += 6;
((data[current_index + 5] >> 4) as f64 * 100000000000.0)
+ ((data[current_index + 5] & 0x0F) as f64 * 10000000000.0)
+ ((data[current_index + 4] >> 4) as f64 * 1000000000.0)
+ ((data[current_index + 4] & 0x0F) as f64 * 100000000.0)
+ ((data[current_index + 3] >> 4) as f64 * 10000000.0)
+ ((data[current_index + 3] & 0x0F) as f64 * 1000000.0)
+ ((data[current_index + 2] >> 4) as f64 * 100000.0)
+ ((data[current_index + 2] & 0x0F) as f64 * 10000.0)
+ ((data[current_index + 1] >> 4) as f64 * 1000.0)
+ ((data[current_index + 1] & 0x0F) as f64 * 100.0)
+ ((data[current_index] >> 4) as f64 * 10.0)
+ (data[current_index] & 0x0F) as f64
}
data_information::DataFieldCoding::NoData => 0.0,
data_information::DataFieldCoding::SelectionForReadout => {
total_size += 1;
0.0
}
data_information::DataFieldCoding::SpecialFunctions(_) => {
total_size += 1;
0.0
}
data_information::DataFieldCoding::VariableLength => 0.0,
};
Ok(DataRecord {
function: data_information.function_field,
Expand Down

0 comments on commit ed6876c

Please sign in to comment.