GithubHelp home page GithubHelp logo

hostlime / mothbus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chrisbfx/mothbus

0.0 0.0 0.0 35 KB

c++11 implementation of the modbus protocol

License: MIT License

C++ 94.60% CMake 5.40%

mothbus's Introduction

mothbus, a c++11 library for the modbus protocol

Build Status

Introduction

The goal of the mothbus library is to provide a complete and simple implementation of the modbus protocol as defined on modbus.org. It provides role agnostic intefaces for modbus-tcp and modbus-rtu, which means you can use the same code for server (called slaves in modbus context) and clients (called master), as well a simple client interface. The design goals (which set it apart of other modbus implementations) are:

  • Scalability. Mothbus scales from tiny microcontrollers to the cloud.

  • Network layer agnostic. Mothbus does not implement the network layer, which allows it to be ported to almost everything. As it is heavily influenced by boost::asio it works best with it.

  • Async api. A simple, based on boost::asio, asynchronous api (if your network layer support it).

  • Low memory footprint Dynamic memory allocations are kept to a minimum.

Status

The library is currently in a proof-of-concept state and while it maybe already works, there are many unaddressed issues and internal inconsistencies.

Requirements

  • C++11, propably some C++14 features required
  • gsl (tested with https://github.com/viboes/GSL)
  • boost (asio, system (program_options for examples))
  • google-test for unit tests

To Build

Mothbus is a header only library, so simple add the include path to your project. For tests and examples you can use cmake.

Example

Reading a register from a modbus tcp device:

#include <iostream>
#include <boost/asio.hpp>
#include <mothbus/adu/tcp.h>

int main(int argc, char** argv)
{	
	// network code
	using boost::asio::ip::tcp;
	std::string host = "localhost";	
	boost::asio::io_service io_service;
	tcp::resolver resolver(io_service);
	tcp::socket socket(io_service);
	boost::asio::connect(socket, resolver.resolve(tcp::resolver::query{host, "502"}));

	// mothbus reads from server
	mothbus::tcp_master<tcp::socket> client(socket);
	std::array<mothbus::byte, 2> singleRegister;
	client.read_registers(slave, register_address, singleRegister);
	
	// output value
	uint16_t value = (gsl::to_integer<uin16_t>(singleRegister[0]) << 8) + gsl::to_integer<uin16_t>(singleRegister[0]);
	std::cout << value;	
	return 0;
}

mothbus's People

Contributors

chrisbfx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.