# libmodbus-rs
## [libmodbus](http://libmodbus.org/) bindings for Rust
[](https://crates.io/crates/libmodbus-rs)
[](https://travis-ci.org/zzeroo/libmodbus-rs)
[](https://coveralls.io/github/zzeroo/libmodbus-rs?branch=master)
[Homepage |][homepage]
[Documentation |][doc]
[Original libmodbus documentation |][libmodbus-doc]
[Github repo |][repo]
**This crate is in early beta state. Please don't use in production and expect odd behavior.**
This crate based on the latest libmodbus git:master branch. I plan to support the different libmodbus version via cargo's `feature` feature.
## Usage
Include the dependencies into your `Cargo.toml` file.
### for stable (crates.io)
```toml
[dependencies]
libmodbus-rs = "0.5"
```
### or development (github master)
```toml
[dependencies]
libmodbus-rs = { git = "https://github.com/zzeroo/libmodbus-rs.git" }
```
Some header files of the original libmodbus C library are recreated as traits (e.g. ModbusTCP, ModbusRTU, ModbusServer, ModbusClient, ...).
For example if you what to build an modbus server, in the modbus tcp context, include the following:
```rust
extern crate libmodbus_rs;
use libmodbus_rs::{Modbus, ModbusServer, ModbusTCP};
```
The examples in the examples directory show this.
## Documentation
[Documentation (crates.io)][doc]
[Documentation (master)][doc-master]
## Building libmodbus-rs
The libmodbus ffi bindings (libmodbus-sys) are build using [bindgen][bindgen]. [Bindgen need Clang 3.9 or greater on your system.][bindgen-reg]
### Dependencies Archlinux
```sh
pacman -S autoconf clang39 git libtool make
```
### Dependencies Debian based (e.g. Ubuntu)
```sh
apt-get install autoconf build-essential curl clang-3.9 git-core libtool
```
Look also at the local ci/ docker files under `./ci/docker-archlinux` and `.ci/docker-debian9` for a known working, minimal setup.
For more information about the bindgen requirements please visit [https://servo.github.io/rust-bindgen/requirements.html][bindgen-reg]
If all dependencies are solved, compile with `cargo build` and/ or run the tests with `cargo test`.
```sh
git clone https://github.com/zzeroo/libmodbus-rs
cd libmodbus-rs
cargo build
```
## Examples
All original libmodbus examples are reproduced in Rust. You can find them in the
`examples` directory of this crate.
**Please have look at the README.md in the examples directory for more information about the examples.**
To start, for example, the random test server/ client use the following commands
```sh
cargo run --example random-test-server
```
In another shell start the client after the server
```sh
cargo run --example random-test-client
```
# License
`libmodbus-rs` is distributed under the terms of the LGPL-2.1 license, which is the same license, [libmodbus](http://libmodbus.org/) is using.
# Links
* [http://libmodbus.org][libmodbus]
* [https://github.com/stephane/libmodbus.git][libmodbus-repo]
* [https://github.com/servo/rust-bindgen][bindgen]
* [https://doc.rust-lang.org/book/ffi.html](https://doc.rust-lang.org/book/ffi.html)
This project hosts the original libmodbus documentation, used here, as well. Please have a look at http://zzeroo.github.io/libmodbus-rs/libmodbus/libmodbus.html.
[homepage]: http://zzeroo.github.io/libmodbus-rs
[repo]: https://github.com/zzeroo/libmodbus-rs
[doc]: https://docs.rs/crate/libmodbus-rs
[doc-master]: http://zzeroo.github.io/libmodbus-rs/libmodbus_rs/index.html
[libmodbus]: http://libmodbus.org
[libmodbus-repo]: https://github.com/stephane/libmodbus.git
[libmodbus-doc]: http://zzeroo.github.io/libmodbus-rs/libmodbus/libmodbus.html
[bindgen]: https://github.com/servo/rust-bindgen
[bindgen-reg]: https://servo.github.io/rust-bindgen/requirements.html