Giter VIP home page Giter VIP logo

mbserver's Introduction

Build Status Coverage Status GoDoc Software License

Golang Modbus Server (Slave)

The Golang Modbus Server (Slave) responds to the following Modbus function requests:

Bit access:

  • Read Discrete Inputs
  • Read Coils
  • Write Single Coil
  • Write Multiple Coils

16-bit acess:

  • Read Input Registers
  • Read Multiple Holding Registers
  • Write Single Holding Register
  • Write Multiple Holding Registers

TCP and serial RTU access is supported.

The server internally allocates memory for 65536 coils, 65536 discrete inputs, 653356 holding registers and 65536 input registers. On start, all values are initialzied to zero. Modbus requests are processed in the order they are received and will not overlap/interfere with each other.

The golang mbserver documentation.

Example Modbus TCP Server

Create a Modbus TCP Server (Slave):

package main

import (
	"log"
	"time"

	"github.com/tbrandon/mbserver"
)

func main() {
	serv := mbserver.NewServer()
	err := serv.ListenTCP("127.0.0.1:1502")
	if err != nil {
		log.Printf("%v\n", err)
	}
	defer serv.Close()

	// Wait forever
	for {
		time.Sleep(1 * time.Second)
	}
}

The server will continue to listen until killed (<ctrl>-c). Modbus typically uses port 502 (standard users require special permissions to listen on port 502). Change the port number as required. Change the address to 0.0.0.0 to listen on all network interfaces.

An example of a client writing and reading holding regsiters:

package main

import (
	"fmt"

	"github.com/goburrow/modbus"
)

func main() {
	handler := modbus.NewTCPClientHandler("localhost:1502")
	// Connect manually so that multiple requests are handled in one session
	err := handler.Connect()
	defer handler.Close()
	client := modbus.NewClient(handler)

	_, err = client.WriteMultipleRegisters(0, 3, []byte{0, 3, 0, 4, 0, 5})
	if err != nil {
		fmt.Printf("%v\n", err)
	}

	results, err := client.ReadHoldingRegisters(0, 3)
	if err != nil {
		fmt.Printf("%v\n", err)
	}
	fmt.Printf("results %v\n", results)
}

Outputs:
results [0 3 0 4 0 5]

Example Listening on Multiple TCP Ports and Serial Devices

The Golang Modbus Server can listen on multiple TCP ports and serial devices. In the following example, the Modbus server will be configured to listen on 127.0.0.1:1502, 0.0.0.0:3502, /dev/ttyUSB0 and /dev/ttyACM0

	serv := mbserver.NewServer()
	err := serv.ListenTCP("127.0.0.1:1502")
	if err != nil {
		log.Printf("%v\n", err)
	}

	err := serv.ListenTCP("0.0.0.0:3502")
	if err != nil {
		log.Printf("%v\n", err)
	}

	err := s.ListenRTU(&serial.Config{
		Address:  "/dev/ttyUSB0",
		BaudRate: 115200,
		DataBits: 8,
		StopBits: 1,
		Parity:   "N",
		Timeout:  10 * time.Second})
	if err != nil {
		t.Fatalf("failed to listen, got %v\n", err)
	}

	err := s.ListenRTU(&serial.Config{
		Address:  "/dev/ttyACM0",
		BaudRate: 9600,
		DataBits: 8,
		StopBits: 1,
		Parity:   "N",
		Timeout:  10 * time.Second,
		RS485: serial.RS485Config{
			Enabled: true,
			DelayRtsBeforeSend: 2 * time.Millisecond
			DelayRtsAfterSend: 3 * time.Millisecond
			RtsHighDuringSend: false,
			RtsHighAfterSend: false,
			RxDuringTx: false
			})
	if err != nil {
		t.Fatalf("failed to listen, got %v\n", err)
	}

	defer serv.Close()

Information on serial port settings.

Server Customization

RegisterFunctionHandler allows the default server functionality to be overridden for a Modbus function code.

func (s *Server) RegisterFunctionHandler(funcCode uint8, function func(*Server, Framer) ([]byte, *Exception))

Example of overriding the default ReadDiscreteInputs funtion:

serv := NewServer()

// Override ReadDiscreteInputs function.
serv.RegisterFunctionHandler(2,
    func(s *Server, frame Framer) ([]byte, *Exception) {
        register, numRegs, endRegister := frame.registerAddressAndNumber()
        // Check the request is within the allocated memory
        if endRegister > 65535 {
            return []byte{}, &IllegalDataAddress
        }
        dataSize := numRegs / 8
        if (numRegs % 8) != 0 {
            dataSize++
        }
        data := make([]byte, 1+dataSize)
        data[0] = byte(dataSize)
        for i := range s.DiscreteInputs[register:endRegister] {
            // Return all 1s, regardless of the value in the DiscreteInputs array.
            shift := uint(i) % 8
            data[1+i/8] |= byte(1 << shift)
        }
        return data, &Success
    })

// Start the server.
err := serv.ListenTCP("localhost:4321")
if err != nil {
    log.Printf("%v\n", err)
    return
}
defer serv.Close()

// Wait for the server to start
time.Sleep(1 * time.Millisecond)

// Example of a client reading from the server started above.
// Connect a client.
handler := modbus.NewTCPClientHandler("localhost:4321")
err = handler.Connect()
if err != nil {
    log.Printf("%v\n", err)
    return
}
defer handler.Close()
client := modbus.NewClient(handler)

// Read discrete inputs.
results, err := client.ReadDiscreteInputs(0, 16)
if err != nil {
    log.Printf("%v\n", err)
}

fmt.Printf("results %v\n", results)

Output:

results [255 255]

Benchmarks

Quanitify server read/write performance. Benchmarks are for Modbus TCP operations.

Run benchmarks:

$ go test -bench=.
BenchmarkModbusWrite1968MultipleCoils-8            50000             30912 ns/op
BenchmarkModbusRead2000Coils-8                     50000             27875 ns/op
BenchmarkModbusRead2000DiscreteInputs-8            50000             27335 ns/op
BenchmarkModbusWrite123MultipleRegisters-8        100000             22655 ns/op
BenchmarkModbusRead125HoldingRegisters-8          100000             21117 ns/op
PASS

Operations per second are higher when requests are not forced to be synchronously processed. In the case of simultaneous client access, synchronous Modbus request processing prevents data corruption.

To understand performanc limitations, create a CPU profile graph for the WriteMultipleCoils benchmark:

go test -bench=.MultipleCoils -cpuprofile=cpu.out
go tool pprof modbus-server.test cpu.out
(pprof) web

Race Conditions

There is a known race condition in the code relating to calling Serial Read() and Close() functions in different go routines.

To check for race conditions, run:

go test --race

mbserver's People

Contributors

broyojo avatar lilacpp avatar madivak avatar tbrandon 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  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  avatar  avatar  avatar  avatar

mbserver's Issues

Start server on localhost?

Does anyone try starting server on their local ip address?
I start on localhost:1502 and have error.
But 127.0.0.1:1502 no error, and only able to connect it locally. Would like to automate install on my IOT device by automatically start the server with their respective IP Address.

serial read only returns one byte

I have created a simple server and client apps:

https://github.com/simpleiot/simpleiot/blob/feature-modbus/cmd/modbus-client/main.go

https://github.com/simpleiot/simpleiot/blob/feature-modbus/cmd/modbus-server/main.go

When I send a packet to the server, it only ever receives one byte.

What causes the the Read() at this location:

https://github.com/tbrandon/mbserver/blob/master/servertu.go#L26

func (s *Server) acceptSerialRequests(port serial.Port) {
	for {
		buffer := make([]byte, 512)

		bytesRead, err := port.Read(buffer)
		if err != nil {
			if err != io.EOF {
				log.Printf("serial read error %v\n", err)
			}
			return
		}

		if bytesRead != 0 {

			// Set the length of the packet to the number of read bytes.
			packet := buffer[:bytesRead]

			frame, err := NewRTUFrame(packet)
			if err != nil {
				log.Printf("bad serial frame error %v\n", err)
				return
			}

			request := &Request{port, frame}

			s.requestChan <- request
		}
	}
}

To read an entire packet, instead of just several bytes?

Thanks,
Cliff

RTU server unstable

Most of the time serial did not read all the data frame and return error "bad serial frame error %v\n"

bytesRead, err := port.Read(buffer)

Because it don't know when to start/stop reading

Server customization issue

I am willing to put a handler on my Modbus connection and everything was going fine until the moment I tried to implement server customization as it's written in the documentation.


import (
	"errors"
	"fmt"
	"time"

	"github.com/goburrow/serial"

	. "github.com/tbrandon/mbserver"
)

var server *Server

func ModbusInit() {
	server = NewServer()
	server.RegisterFunctionHandler(2,
		func(s *Server, frame Framer) ([]byte, *Exception) {
			register, numRegs, endRegister := frame.registerAddressAndNumber()
			// Check the request is within the allocated memory
			if endRegister > 65535 {
				return []byte{}, &IllegalDataAddress
			}
			dataSize := numRegs / 8
			if (numRegs % 8) != 0 {
				dataSize++
			}
			data := make([]byte, 1+dataSize)
			data[0] = byte(dataSize)
			for i := range s.DiscreteInputs[register:endRegister] {
				// Return all 1s, regardless of the value in the DiscreteInputs array.
				shift := uint(i) % 8
				data[1+i/8] |= byte(1 << shift)
			}
			return data, &Success
		})
}

And when I'm trying to build it with my script:
#!/bin/bash

export GOPATH=pwd
go get github.com/tbrandon/mbserver
go get github.com/goburrow/serial
go build modbus.go

It appears with a message(the one and the only):

frame.registerAddressAndNumber undefined (type mbserver.Framer has no field or method registerAddressAndNumber)

This comes from the fact that registerAddressAndNumber(frame Framer) is a private function and we can't call it from outside of the package. And how do we deal with that if by documentation we are meant to call it from another package? Was it actually meant to be a private function? Do we need to override all structures and functions from that package to make it work as it was meant to work?

Hope there is a way of correctly implementing a customized server without changing function privacy status by ourselves something like this.

How to read Exceptions from goburrow modbus slave library

Hello, I have both modbus slave and modbus master (implemented in GoLang) but I am not able to figure out how to get below list of exceptions when there a invalid operation read/write.

In modbus slave how to call func GetException(frame Framer) (exception Exception) ?
I am calling below function to read holding registers and handling err if any

result, err := client.ReadHoldingRegisters(req.StartAddress, req.Count)


client := mb.NewClient(handler.client)
result, err := client.ReadHoldingRegisters(req.StartAddress, req.Count)
if err != nil {
// Added close in since the keep alive will prevent it from ever reconnecting
level.Error(logger).Log("err: Modbus TCP Client Unavailable ", err.Error())
handler.client.Close()

				go buildResponseRawData(*handler, req.Type, "", result, err, &req.ResultsChan)
			} else {
				go buildResponseRawData(*handler, req.Type, req.Endian, result, nil, &req.ResultsChan)
			}

// Success operation successful.
Success Exception
// IllegalFunction function code received in the query is not recognized or allowed by slave.
IllegalFunction Exception = 1
// IllegalDataAddress data address of some or all the required entities are not allowed or do not exist in slave.
IllegalDataAddress Exception = 2
// IllegalDataValue value is not accepted by slave.
IllegalDataValue Exception = 3
// SlaveDeviceFailure Unrecoverable error occurred while slave was attempting to perform requested action.
SlaveDeviceFailure Exception = 4
// AcknowledgeSlave has accepted request and is processing it, but a long duration of time is required. This response is returned to prevent a timeout error from occurring in the master. Master can next issue a Poll Program Complete message to determine whether processing is completed.
AcknowledgeSlave Exception = 5
// SlaveDeviceBusy is engaged in processing a long-duration command. Master should retry later.
SlaveDeviceBusy Exception = 6
// NegativeAcknowledge Slave cannot perform the programming functions. Master should request diagnostic or error information from slave.
NegativeAcknowledge Exception = 7
// MemoryParityError Slave detected a parity error in memory. Master can retry the request, but service may be required on the slave device.
MemoryParityError Exception = 8
// GatewayPathUnavailable Specialized for Modbus gateways. Indicates a misconfigured gateway.
GatewayPathUnavailable Exception = 10
// GatewayTargetDeviceFailedtoRespond Specialized for Modbus gateways. Sent when slave fails to respond.
GatewayTargetDeviceFailedtoRespond Exception = 11

Cannot create multiple TcpListener

hen running several TcpListener 127.0.0.1:1041 and 127.0.0.1:9898, there is a normal data exchange on one port, on the other I get only zeros in response

image

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.