Giter Site home page Giter Site logo

sharpmodbus's Introduction

SharpModbus

C# Modbus Tools

Quick Start

var settings = new SerialSettings()
{
    PortName = "COM3",
    BaudRate = 57600,
};
//Modbus RTU over serial
using (var master = ModbusMaster.RTU(settings))
{
    master.WriteCoil(1, 3000, false);
    master.WriteCoils(1, 3001, false, true);
}
//Modbus TCP over socket
using (var master = ModbusMaster.TCP("10.77.0.2", 502))
{
    master.WriteCoils(1, 4, false, true);
}

Documentation

No documentation yet. Resort to tests at SharpModbus.Test and SharpModbus.Test.Special subprojects for guidance. A couple of real device communication samples are available there.

SharpMaster uses SharpModbus too.

Development Setup

Setup 1

  • Windows 10 Pro 64x VM
  • VirtualBox 7.0.6 on Kubuntu 20.04
  • VS Code 1.74.3
  • .NET SDK 6.0.405
  • com0com-2.2.2.0-x64-fre-signed COM98/99
  • For Special Test subproject
    • FTDI USB-RS485-WE-1800-BT COM3
    • Comfile MD-H485+MD-DOSO8+5SlotBoard
    • CUI SWI25-24-N-P5
    • B&B BB-UH401

Setup 2

  • Mac mini - Ventura 13.1
  • VS Code 1.74.3
  • .NET SDK 6.0.405 + 7.0.2

Setup 3

  • Kubuntu 20.04
  • VS Code 1.74.2
  • asdf dotnet 6.0.405

Development CLI

#packing for nuget
dotnet clean SharpModbus -c Release
dotnet pack SharpModbus -c Release
#cross platform test cases
dotnet test SharpModbus.Test
#avoid complains on missing mono on MacOS/Linux
dotnet test SharpModbus.Test -f net6.0
#console output for test cases
dotnet test SharpModbus.Test -v n

#com0com based serial test for Windows
#com0com pair required as COM98 and COM99
#ignore yellow stderr and stdout
dotnet test SharpModbus.Test.Windows
#test in release to avoid yellow output
dotnet test SharpModbus.Test.Windows -c Release

#socat based serial test for MacOS/Linux
#brew install socat
#ignore yellow stderr and stdout
dotnet test SharpModbus.Test.Unix
#test in release to avoid yellow output
dotnet test SharpModbus.Test.Unix -c Release

#tests below require a hard to replicate environment
#do not bother unless really important to you

#comfile modport connected on COM10 see details inside
dotnet test SharpModbus.Test.Special --filter FullyQualifiedName~ComfileModportTest
#opto22 snappac connected on 10.77.0.2:502 see details inside
dotnet test SharpModbus.Test.Special --filter FullyQualifiedName~Opto22SnapPacTest
#compile check special tests
dotnet build SharpModbus.Test.Special

Roadmap

  • Support Linux/MacOS as well
  • Improve documentation and samples
  • Support Modbus ASCII

sharpmodbus's People

Contributors

pieterh avatar samuelventura avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sharpmodbus's Issues

Support for net6.0

Hi,
Would be great if SharpModbus and SharpSerial would be available for .NET 6.0. I have created a small PR for both of the packages. With these changes they will support 4 and 6.0 target. thanks in advance

TransactionId de-synchronisation caused by comms timeout/delay

Hey mate,

Great project, much appreciated, hope the below helps.

Problem
If the timeout period is exceeded when trying to read over a TCP connection, the socket receive buffer isn't flushed before retrying.

Steps to reproduce
Place breakpoint as per below inside ModbusSocketStream, it will pause during a read, keep it paused longer than the timeout period, then continue, you will get a partial read exception, subsequent reads on the stream will read the previous transaction and cause a Transaction Id mismatch exception (presuming the data eventually arrives).
reprosteps

Potential Solution:
After an exception, read all available data available from the socket before reading another transaction, or, flush buffer each time before writing our next request, this way the buffer is emptied more readily.

Add below to IModbusStream Interface

void FlushBuffer();

Implement FlushBuffer as below in ModbusSocketStream.cs (and all other IModbusStream implementors)

public void FlushBuffer() { if (socket?.Available == 0) return; var AvailableBytes = socket.Available; socket.GetStream().Read(new byte[AvailableBytes], 0, AvailableBytes); }

Flush buffer before writing to stream in ModbusMaster.cs

wrapper.FillRequest(request, 0); stream.FlushBuffer(); stream.Write(request);

fix

Note

I think a similar issue occurs in the scanner, I implemented a buffer flush as well on the RTU scanner on an older version however haven't verified if the issue is still present.

Thanks again.

Muzz.

How do I use this?

There are no examples at all..

And a question about the modbus slave: Can it respond for request for two different slave IDs?

Can we implement custom modbus user functions?

Hi. Some devices use custom implementation of Modbus protocol. For example, they have function 0x41 for reading archive data. Time is given in the request. And the device gives the archive data (in exact size) of this time. Can we implement this with your library?

.Net Standard 2.0

Hello! very nice little library, are there any plans or possibility to transfer to .Net Standard 2.0? I had a quick look but it looks like it relies on another one of your libraries that is also on .net 4.6.

Thanks again

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.