I'm trying to test out performing a simple TCP MODBUS read of a single register from a device emulator. When running the code it is showing a response of 0 bytes and I get the message "connection reset by peer". Any ideas as to why it's not working?
UPDATE, my request was incorrect, the correct working MODBUS TCP poll code is:
package main
import (
"fmt"
"net"
)
// TCP MODBUS client
func main() {
conn, err := net.Dial("tcp", "192.168.98.114:502")
if err != nil {
fmt.Println(err)
}
numRegs := 1
# make a MODBUS TCP request (be careful, the format is different to MODBUS serial)
request := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x01, 0x00, 0x01}
n, err := conn.Write(request)
if err != nil {
fmt.Println(err)
}
expectedResponseLen := 5 + 2 * numRegs
response := make([]byte, expectedResponseLen)
n, err = conn.Read(response)
conn.Close()
if err != nil {
fmt.Println(err)
}
for i := 0; i < n; i++ {
fmt.Printf("%02x ", response[i])
}
fmt.Println("
")
}