tbrandon / mbserver

Golang Modbus Server (Slave)
MIT License
242 stars 133 forks source link

Server customization issue #10

Open tynrol opened 3 years ago

tynrol commented 3 years ago

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.

incase commented 2 years ago

frame.registerAddressAndNumber() is an unexported function of the mbserver package. As such, the customization section of the docs is wrong - you can't use that function outside the mbserver package.

It gets worse: The Framer interface (which you'd receive as frame in your func) doesn't contain that function.

tynrol commented 2 years ago

I think its pretty clear by now that the project is no longer supported. :(