From 4fc4a9c8bd6873e413300af55f6bee7b409fdae8 Mon Sep 17 00:00:00 2001 From: hirschenberger Date: Wed, 20 Dec 2023 12:34:36 +0100 Subject: [PATCH] Create new breaking release --- Cargo.toml | 2 +- README.md | 4 ++-- src/tcp.rs | 36 ++++++++++++++++++++++++++---------- test-server/build.rs | 14 +++++++------- test-server/lib.rs | 20 ++++++++++++-------- tests/lib.rs | 16 +++++++++++++--- 6 files changed, 61 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1b3527e5b..59dc90e5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ license = "MIT" name = "modbus" readme = "README.md" repository = "https://github.com/hirschenberger/modbus-rs.git" -version = "1.0.5" +version = "1.1.0" [dependencies] byteorder = "1" diff --git a/README.md b/README.md index 29f177424..b2059fd24 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Add `modbus` to your `Cargo.toml` dependencies: ```toml [dependencies] -modbus = "1.0" +modbus = "1.1" ``` Import the `modbus` crate and use it's functions: @@ -36,6 +36,6 @@ the [examples](https://github.com/hirschenberger/modbus-rs/tree/master/examples) ## License -Copyright © 2015-2023 Falco Hirschenberger +Copyright © 2015-2024 Falco Hirschenberger Distributed under the [MIT License](LICENSE). diff --git a/src/tcp.rs b/src/tcp.rs index 6efffad93..6d2d804c9 100644 --- a/src/tcp.rs +++ b/src/tcp.rs @@ -217,10 +217,20 @@ impl Transport { } fn write_read_multiple(&mut self, fun: &Function) -> Result> { - if let Function::WriteReadMultipleRegisters(write_addr, write_quantity, write_values, read_addr, read_quantity) = *fun { + if let Function::WriteReadMultipleRegisters( + write_addr, + write_quantity, + write_values, + read_addr, + read_quantity, + ) = *fun + { let expected_bytes = 2 * read_quantity as usize; - let header = Header::new(self, MODBUS_HEADER_SIZE as u16 + 10u16 + write_quantity * 2 + 1u16); + let header = Header::new( + self, + MODBUS_HEADER_SIZE as u16 + 10u16 + write_quantity * 2 + 1u16, + ); let mut buff = header.pack()?; buff.write_u8(fun.code())?; @@ -436,15 +446,21 @@ impl Client for Transport { /// Write a multiple 16bit registers starting at address `write_addr` and read starting at address `read_addr`. fn write_read_multiple_registers( - &mut self, - write_address: u16, - write_quantity: u16, - write_values: &[u16], - read_address: u16, - read_quantity: u16, - ) -> Result> { + &mut self, + write_address: u16, + write_quantity: u16, + write_values: &[u16], + read_address: u16, + read_quantity: u16, + ) -> Result> { let write_bytes = binary::unpack_bytes(write_values); - let read_bytes = self.write_read_multiple(&Function::WriteReadMultipleRegisters(write_address, write_quantity, &write_bytes, read_address, read_quantity))?; + let read_bytes = self.write_read_multiple(&Function::WriteReadMultipleRegisters( + write_address, + write_quantity, + &write_bytes, + read_address, + read_quantity, + ))?; binary::pack_bytes(&read_bytes[..]) } diff --git a/test-server/build.rs b/test-server/build.rs index 213903ea4..9c1e86014 100644 --- a/test-server/build.rs +++ b/test-server/build.rs @@ -3,16 +3,16 @@ use std::process::Command; fn main() { if cfg!(feature = "modbus-server-tests") { let pkg_cfg = Command::new("pkg-config") - .args(&["--libs", "--cflags", "libmodbus"]) - .output() - .unwrap_or_else(|e| panic!("Error running pkg-config: {}", e)); + .args(&["--libs", "--cflags", "libmodbus"]) + .output() + .unwrap_or_else(|e| panic!("Error running pkg-config: {}", e)); let output = String::from_utf8_lossy(&pkg_cfg.stdout); let flags: Vec<&str> = output.split_whitespace().collect(); let out = Command::new("gcc") - .args(&["test-server.c", "-o", "test-server"]) - .args(&flags[..]) - .output() - .unwrap_or_else(|e| panic!("Error running gcc: {}", e)); + .args(&["test-server.c", "-o", "test-server"]) + .args(&flags[..]) + .output() + .unwrap_or_else(|e| panic!("Error running gcc: {}", e)); if !out.status.success() { panic!("Error building testserver"); } diff --git a/test-server/lib.rs b/test-server/lib.rs index 5c9033c84..2f5b15774 100644 --- a/test-server/lib.rs +++ b/test-server/lib.rs @@ -4,7 +4,9 @@ use std::process::Child; use std::sync::atomic::{AtomicUsize, Ordering}; // global unique portnumber between all test threads -lazy_static!{ static ref PORT: AtomicUsize = AtomicUsize::new(22222); } +lazy_static! { + static ref PORT: AtomicUsize = AtomicUsize::new(22222); +} pub struct ChildKiller(Child); @@ -20,15 +22,17 @@ pub fn start_dummy_server(port: Option) -> (ChildKiller, u16) { use std::time::Duration; // get and increment global port number for current test - let p = match port { + let p = match port { Some(p) => p, - None => PORT.fetch_add(1, Ordering::SeqCst) as u16 + None => PORT.fetch_add(1, Ordering::SeqCst) as u16, }; - let ck = ChildKiller(Command::new("./test-server/test-server") - .arg(p.to_string()) - .stdout(Stdio::null()) - .spawn() - .unwrap_or_else(|e| panic!("failed to execute process: {}", e))); + let ck = ChildKiller( + Command::new("./test-server/test-server") + .arg(p.to_string()) + .stdout(Stdio::null()) + .spawn() + .unwrap_or_else(|e| panic!("failed to execute process: {}", e)), + ); sleep(Duration::from_millis(500)); (ck, p) } diff --git a/tests/lib.rs b/tests/lib.rs index 2124e5d15..2e8995325 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -179,10 +179,20 @@ mod modbus_server_tests { fn test_write_read_multiple_registers() { let (_s, cfg) = start_dummy_server_with_cfg(); let mut trans = Transport::new_with_cfg("127.0.0.1", cfg).unwrap(); - assert!(trans.write_read_multiple_registers(0, 3, &[1, 2, 3], 0, 3).is_ok()); + assert!(trans + .write_read_multiple_registers(0, 3, &[1, 2, 3], 0, 3) + .is_ok()); assert_eq!(trans.read_holding_registers(0, 3).unwrap(), &[1, 2, 3]); - assert_eq!(trans.write_read_multiple_registers(3, 2, &[4, 5], 1, 4).unwrap(), &[2, 3, 4, 5]); - assert_eq!(trans.read_holding_registers(0, 5).unwrap(), &[1, 2, 3, 4, 5]); + assert_eq!( + trans + .write_read_multiple_registers(3, 2, &[4, 5], 1, 4) + .unwrap(), + &[2, 3, 4, 5] + ); + assert_eq!( + trans.read_holding_registers(0, 5).unwrap(), + &[1, 2, 3, 4, 5] + ); } #[test]