tokio-mc

Crates.iotokio-mc
lib.rstokio-mc
version0.1.4
created_at2024-11-26 01:58:46.417259+00
updated_at2025-08-02 12:52:25.911161+00
descriptionA library for communication with PLCs using the MC protocol
homepagehttps://github.com/hanHHHyU/tokio-mc
repositoryhttps://github.com/hanHHHyU/tokio-mc
max_upload_size
id1461094
size194,986
Yuhan (hanHHHyU)

documentation

README

tokio-mc

Crates.io Docs.rs License

tokio-mc is a pure Rust library for Mitsubishi Communication (MC) protocol, built on top of tokio.


Features

  • Async & Sync communication with Mitsubishi and Keyence PLCs using the 3E frame protocol.

  • Easy integration with the tokio ecosystem for async programming.


Installation

Add the following to your Cargo.toml to use tokio-mc with the desired features:

  • Async Feature (3e-async): For asynchronous communication
  • Sync Feature (3e-sync): For synchronous communication

Example Dependency

# For async usage
tokio-mc = { version = "0.1.3", features = ["3e-async"] }

# For sync usage
tokio-mc = { version = "0.1.3", features = ["3e-sync"] }

Async Example

Here's how to use the async features of tokio-mc:

use std::net::SocketAddr;
use tokio::time::Duration;
use tokio_mc::{
    client::{tcp::*, Reader, Writer},
    frame::Model,
    Error,
};

#[tokio::main]
async fn main() -> Result<(), Error> {
    let addr = "192.168.1.30:5000"
        .parse::<SocketAddr>()
        .map_err(|e| Error::Transport(std::io::Error::new(std::io::ErrorKind::InvalidInput, e)))?;

    let mut context = connect(addr).await?;
    context.set_plc_model(Model::Keyence);
    let u16_values = context.read_u16s("D1002", 2).await?;
    println!("Read U16s: {:?}", u16_values);
    
    Ok(())
}

Sync Example

Here's how to use the sync features of tokio-mc:

use std::net::SocketAddr;
use tokio_mc::{
    client::sync::{tcp::*, Reader, Writer},
    frame::Model,
    Error,
};

fn main() -> Result<(), Error> {
    let addr = "192.168.110.252:5000".parse::<SocketAddr>().unwrap();
    let mut context = connect(addr)?;
    context.set_plc_model(Model::Keyence);

    // Read different data types
    let u8_values = context.read_u8s("D1000", 2)?;
    println!("Read U8s: {:?}", u8_values);


    context.write_bools("M200", &[true, false, true, true])?;
    println!("Written Bools successfully");

    Ok(())
}


Disclaimer

When using this library for PLC communication, please first make sure that there is no abnormality in your connection. I used the 3E frame protocol, which has been tested with Keyence and Mitsubishi and used in actual projects. If you have any feedback or suggestions, please contact me via QQ email.

Some codes are referenced from tokio-modbus

Commit count: 44

cargo fmt