birkenfeld / ads-rs

Rust crate to access PLCs via the Beckhoff ADS protocol
https://crates.io/crates/ads
Apache License 2.0
45 stars 8 forks source link

Reading / writing strings of length 255 #7

Closed Mebus closed 2 years ago

Mebus commented 2 years ago

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

birkenfeld commented 2 years ago

You have two options:

Mebus commented 2 years ago

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

birkenfeld commented 2 years ago

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.