Skip to content
Snippets Groups Projects
Commit 18095043 authored by Benjamin Koch's avatar Benjamin Koch
Browse files

use to_be_bytes() instead of byteorder crate

parent f4a2935d
No related branches found
No related tags found
No related merge requests found
......@@ -1027,7 +1027,6 @@ name = "heizung"
version = "0.1.0"
dependencies = [
"byte-slice-cast 1.2.2",
"byteorder",
"cortex-m",
"cortex-m-rt",
"crc",
......
......@@ -56,7 +56,6 @@ pio = "0.2.1"
heapless = "0.7.16"
crc = "3.0.1"
byteorder = "1.4.3"
[profile.release]
debug = true
......@@ -4,8 +4,6 @@ use futures::Future;
use heapless::Vec;
use crc::{Crc, CRC_16_MODBUS, Digest};
use embassy_rp::uart;
use core::fmt::Write;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use crate::rs485::{RS485Handler};
......@@ -102,18 +100,20 @@ impl<REGS: ModbusRegisters> ModbusServer<REGS> {
txbuf.clear();
let capacity = txbuf.capacity();
let map_err = |x: Result<(), ()> | x.or(Err(ServerDeviceFailure));
fn map_err<T>(x: Result<(), T>) -> Result<(), ModbusErrorCode> {
x.or(Err(ServerDeviceFailure))
}
fn push(txbuf: &mut Vec<u8, BUF_LENGTH>, x: u8) -> Result<(), ModbusErrorCode> {
map_err(txbuf.push(x));
map_err(txbuf.push(x))
}
fn push_many(txbuf: &mut Vec<u8, BUF_LENGTH>, xs: &[u8]) -> Result<(), ModbusErrorCode> {
for x in xs {
push(txbuf, x)?;
push(txbuf, *x)?;
}
Ok(())
}
fn push_u16be(txbuf: &mut Vec<u8, BUF_LENGTH>, x: u16) -> Result<(), ModbusErrorCode> {
map_err(txbuf.write_u16::<BigEndian>(x));
push_many(txbuf, &x.to_be_bytes())
}
// see https://modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf
......@@ -210,7 +210,7 @@ impl<REGS: ModbusRegisters> ModbusServer<REGS> {
let actual_new_value = self.regs.write_register(rxbuf[0], start, value)?;
if should_reply {
push_many(txbuf, &rxbuf[0..4])?;
push_u16be(txbuf, value)?;
push_u16be(txbuf, actual_new_value)?;
}
Ok(())
},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment