Skip to content

Commit

Permalink
Create new breaking release
Browse files Browse the repository at this point in the history
  • Loading branch information
hirschenberger committed Dec 20, 2023
1 parent d259a8a commit 4fc4a9c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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).
36 changes: 26 additions & 10 deletions src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,20 @@ impl Transport {
}

fn write_read_multiple(&mut self, fun: &Function) -> Result<Vec<u8>> {
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())?;
Expand Down Expand Up @@ -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<Vec<u16>> {
&mut self,
write_address: u16,
write_quantity: u16,
write_values: &[u16],
read_address: u16,
read_quantity: u16,
) -> Result<Vec<u16>> {
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[..])
}

Expand Down
14 changes: 7 additions & 7 deletions test-server/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
20 changes: 12 additions & 8 deletions test-server/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -20,15 +22,17 @@ pub fn start_dummy_server(port: Option<u16>) -> (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)
}
16 changes: 13 additions & 3 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 4fc4a9c

Please sign in to comment.