ads-rs icon indicating copy to clipboard operation
ads-rs copied to clipboard

Reading / writing strings of length 255

Open Mebus opened this issue 2 years ago • 3 comments

Hey Georg,

I am trying to read / write strings, that are 255 characters long from the PLC. I tried it like this, but this limits me to 32 bytes:

let handle = ads::Handle::new(device, "MyValue").unwrap();
let mut somevalue: [u8; 32];
somevalue =  handle.read_value().unwrap();

What is the correct way to achieve my goal?

Thanks

Mebus

Mebus avatar Jun 21 '22 13:06 Mebus

You have two options:

  • since you read a byte array anyway, use Handle::read instead of read_value
  • create a type with the correct length with ads::make_string_type! and use that

birkenfeld avatar Jun 21 '22 13:06 birkenfeld

This seems to work:

    const LEN: usize = 255;
    let mut msg: [u8; LEN] = [0; LEN];
    handle.read(&mut msg).unwrap();
    println!("MY_SYMBOL value is {:?}", msg);
    let thestring = str::from_utf8(&msg).unwrap();
    println!("{}", thestring);

:-)

Mebus

Mebus avatar Jun 21 '22 13:06 Mebus

Yes, that's option 1. Option 2, to compare:

ads::make_string_type!(String255, 255);  // on toplevel

let plc_str: String255 = handle.read_value().unwrap();
let rust_str: String = plc_str.try_into().unwrap();

You might have to use std::convert::TryInto if you're using Rust edition 2015/18.

birkenfeld avatar Jun 21 '22 13:06 birkenfeld