From e6f7c36bd9d1acdde51cc9682aafcd1511de97be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20W=2E=20Crompton?= Date: Mon, 1 Oct 2018 13:50:12 +0200 Subject: [PATCH 1/4] Updated .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index dd2baa6..e58c0a2 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ modbustcp *.out modbus-demo-*.tar.bz2 modbus-demo-* +modbustcp From 8e736d6638cda7f14420b7aff2de64df3de4e63d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20W=2E=20Crompton?= Date: Mon, 1 Oct 2018 14:01:49 +0200 Subject: [PATCH 2/4] Updated README.md --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 01c46fe..4bd34eb 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,19 @@ ModBus TCP Server This is a basic server based of the code by [tbrandon](https://github.com/tbrandon/mbserver) +# Usage +To use this demo you need to get the dependencies and compile the tool. + +``` +$ go get +$ go build +$ ./modbustcp +``` + +Once the server is running you can connect with a *Modbus over TCP* client. + + +# Issues + +If you find an issue please [report it](https://github.com/webhat/modbustcp/issues). \ No newline at end of file From 902351786260296c50966dd9a3ee1ac9535a3984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20W=2E=20Crompton?= Date: Mon, 8 Oct 2018 12:28:55 +0200 Subject: [PATCH 3/4] Updated to support multiple holdin registers --- modbusserver.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/modbusserver.go b/modbusserver.go index 35314c6..0c167e4 100644 --- a/modbusserver.go +++ b/modbusserver.go @@ -21,9 +21,11 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + package main import ( + "fmt" "log" "time" @@ -36,6 +38,7 @@ func main() { serv.RegisterFunctionHandler(3, ReadRegisters) serv.RegisterFunctionHandler(4, ReadRegisters) serv.RegisterFunctionHandler(6, WriteRegisters) + serv.RegisterFunctionHandler(16, WriteHoldingRegisters) err := serv.ListenTCP(":502") if err != nil { @@ -66,6 +69,7 @@ func ReadRegisters(s *mbserver.Server, frame mbserver.Framer) ([]byte, *mbserver shift := uint(i) % 8 data[1+i/8] |= byte(1 << shift) } + fmt.Println("READ: ", data) return data, &mbserver.Success } @@ -82,12 +86,36 @@ func WriteRegisters(s *mbserver.Server, frame mbserver.Framer) ([]byte, *mbserve data[2] = byte(int(value / 256)) data[3] = byte(value % 256) + fmt.Println("WRITE: ", data) + + return data, &mbserver.Success +} + +func WriteHoldingRegisters(s *mbserver.Server, frame mbserver.Framer) ([]byte, *mbserver.Exception) { + register, numregs, _ := registerAddressAndNumber(frame) + value := frame.GetData()[5:] + // Check the request is within the allocated memory + if register > 65535 { + return []byte{}, &mbserver.IllegalDataAddress + } + + if len(value)/2 != numregs { + return []byte{}, &mbserver.IllegalDataAddress + } + + data := make([]byte, 4) + data = frame.GetData()[0:4] + + fmt.Println("WRITE: ", data) + return data, &mbserver.Success } func registerAddressAndNumber(frame mbserver.Framer) (register int, numRegs int, endRegister int) { data := frame.GetData() + fmt.Println("DATA: ", data) register = int(binary.BigEndian.Uint16(data[0:2])) + fmt.Println("REG: ", register) numRegs = int(binary.BigEndian.Uint16(data[2:4])) endRegister = register + numRegs return register, numRegs, endRegister @@ -95,7 +123,9 @@ func registerAddressAndNumber(frame mbserver.Framer) (register int, numRegs int, func registerAddressAndValue(frame mbserver.Framer) (int, uint16) { data := frame.GetData() + fmt.Println("DATA: ", data) register := int(binary.BigEndian.Uint16(data[0:2])) + fmt.Println("REG: ", register) value := binary.BigEndian.Uint16(data[2:4]) return register, value } From 3a05598225a9a3c8fa4c241b3edfc5baa15953fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20W=2E=20Crompton?= Date: Mon, 8 Oct 2018 16:49:06 +0200 Subject: [PATCH 4/4] print each register --- modbusserver.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modbusserver.go b/modbusserver.go index 0c167e4..68a7fa7 100644 --- a/modbusserver.go +++ b/modbusserver.go @@ -103,6 +103,11 @@ func WriteHoldingRegisters(s *mbserver.Server, frame mbserver.Framer) ([]byte, * return []byte{}, &mbserver.IllegalDataAddress } + for i := 0; i < len(value); i += 2 { + b := value[i : i+2] + fmt.Println("Data ", register+(i/2), b) + } + data := make([]byte, 4) data = frame.GetData()[0:4]