Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8fbead4274 | |||
| 8c33fd2a89 | |||
| ebdbc92693 | |||
| 7b1533b3e6 | |||
| ce537e1373 | |||
| 9a92e47837 | |||
| a5f1936632 | |||
| 07ad6f4b24 | |||
| e450b2f3c8 | |||
| 4c3ff497f4 |
91
client.go
91
client.go
@@ -5,29 +5,65 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mbclient struct {
|
type Mbclient struct {
|
||||||
transactionCounter uint16
|
transactionCounter uint16
|
||||||
conn net.Conn
|
address string
|
||||||
header [7]byte
|
header [7]byte
|
||||||
unit uint8
|
unit uint8
|
||||||
|
conn net.Conn
|
||||||
|
t *time.Timer
|
||||||
|
keepAliveDuration time.Duration
|
||||||
|
timeOut time.Duration
|
||||||
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(address string, unit uint8) (*Mbclient, error) {
|
func New(Address string, Unit uint8, KeepAlive, TimeOut time.Duration) (*Mbclient, error) {
|
||||||
var err error
|
|
||||||
|
|
||||||
c := new(Mbclient)
|
c := new(Mbclient)
|
||||||
c.conn, err = net.Dial("tcp", address)
|
c.address = Address
|
||||||
if err != nil {
|
c.unit = Unit
|
||||||
return nil, err
|
c.t = time.NewTimer(0)
|
||||||
}
|
<-c.t.C
|
||||||
c.unit = unit
|
c.keepAliveDuration = KeepAlive
|
||||||
|
c.timeOut = TimeOut
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Mbclient) closer() {
|
||||||
|
|
||||||
|
<-m.t.C
|
||||||
|
m.conn.Close()
|
||||||
|
//m.transactionCounter = 0
|
||||||
|
m.wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
// closeConn can be called when a tcp communication failure require the current
|
||||||
|
// connection to be closed, it should only be used when closer routine is running
|
||||||
|
// and the timer is stopped.
|
||||||
|
func (m *Mbclient) closeConn() {
|
||||||
|
m.t.Reset(0)
|
||||||
|
m.wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error) {
|
func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error) {
|
||||||
|
var err error
|
||||||
|
// If The timer is expired, conn is closed and needs to be reopened
|
||||||
|
if !m.t.Stop() {
|
||||||
|
// Wait for closer to exit to mitigate race condiion
|
||||||
|
// between closer routine and this code path
|
||||||
|
m.wg.Wait()
|
||||||
|
m.conn, err = net.DialTimeout("tcp", m.address, m.timeOut)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
m.wg.Add(1)
|
||||||
|
go m.closer()
|
||||||
|
}
|
||||||
|
|
||||||
const requestLength = 12
|
const requestLength = 12
|
||||||
m.transactionCounter++
|
m.transactionCounter++
|
||||||
req := make([]byte, requestLength)
|
req := make([]byte, requestLength)
|
||||||
@@ -41,30 +77,53 @@ func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error)
|
|||||||
req[7] = 3 //FunctionCode
|
req[7] = 3 //FunctionCode
|
||||||
binary.BigEndian.PutUint16(req[8:10], first-1)
|
binary.BigEndian.PutUint16(req[8:10], first-1)
|
||||||
binary.BigEndian.PutUint16(req[10:12], numRegs)
|
binary.BigEndian.PutUint16(req[10:12], numRegs)
|
||||||
m.conn.SetDeadline(time.Now().Add(10 * time.Second))
|
m.conn.SetDeadline(time.Now().Add(5 * time.Second))
|
||||||
|
|
||||||
byteswritten, err := m.conn.Write(req)
|
byteswritten, err := m.conn.Write(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
m.closeConn()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if byteswritten != requestLength {
|
if byteswritten != requestLength {
|
||||||
return nil, fmt.Errorf("Failed to send request")
|
m.closeConn()
|
||||||
|
return nil, fmt.Errorf("failed to send request")
|
||||||
}
|
}
|
||||||
m.conn.SetDeadline(time.Now().Add(10 * time.Second))
|
m.conn.SetDeadline(time.Now().Add(m.timeOut))
|
||||||
_, err = io.ReadFull(m.conn, m.header[:])
|
_, err = io.ReadFull(m.conn, m.header[:])
|
||||||
|
if err != nil {
|
||||||
|
m.closeConn()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
responseHeader.unMarshal(m.header)
|
responseHeader.unMarshal(m.header)
|
||||||
expectedDataLength := responseHeader.length - 1
|
expectedDataLength := responseHeader.length - 1
|
||||||
|
|
||||||
|
if m.transactionCounter != responseHeader.transactionID {
|
||||||
|
m.closeConn()
|
||||||
|
return nil, fmt.Errorf("modbus transaction mismatch %v != %v", m.transactionCounter, responseHeader.transactionID)
|
||||||
|
}
|
||||||
response := make([]byte, expectedDataLength)
|
response := make([]byte, expectedDataLength)
|
||||||
_, err = m.conn.Read(response)
|
m.conn.SetDeadline(time.Now().Add(m.timeOut))
|
||||||
|
bytesRead, err := io.ReadFull(m.conn, response)
|
||||||
|
if err != nil {
|
||||||
|
m.closeConn()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if uint16(bytesRead) != expectedDataLength {
|
||||||
|
m.t.Reset(m.keepAliveDuration)
|
||||||
|
return nil, fmt.Errorf("failed to read complete package")
|
||||||
|
}
|
||||||
|
|
||||||
err = mbpayload.unMarshal(response)
|
err = mbpayload.unMarshal(response)
|
||||||
if mbpayload.functionCode != 3 {
|
if mbpayload.functionCode != 3 {
|
||||||
return nil, fmt.Errorf("modbus exception %v", mbpayload.functionCode&0x7F)
|
m.t.Reset(m.keepAliveDuration)
|
||||||
|
return nil, fmt.Errorf("modbus exception %v req: %x", mbpayload.functionCode&0x7F, req)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
m.t.Reset(m.keepAliveDuration)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m.t.Reset(m.keepAliveDuration)
|
||||||
return mbpayload.registers, nil
|
return mbpayload.registers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,10 +134,6 @@ type mbapHeader struct {
|
|||||||
unitIdentifier byte
|
unitIdentifier byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (m *mbapHeader) marshalBinary() ([]byte, error) {
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
func (m *mbapHeader) unMarshal(data [7]byte) error {
|
func (m *mbapHeader) unMarshal(data [7]byte) error {
|
||||||
m.transactionID = binary.BigEndian.Uint16(data[0:2])
|
m.transactionID = binary.BigEndian.Uint16(data[0:2])
|
||||||
m.protocolIdentifier = binary.BigEndian.Uint16(data[2:4])
|
m.protocolIdentifier = binary.BigEndian.Uint16(data[2:4])
|
||||||
@@ -98,7 +153,7 @@ func (d *mbPDU) unMarshal(data []byte) error {
|
|||||||
d.functionCode = data[0]
|
d.functionCode = data[0]
|
||||||
d.length = data[1]
|
d.length = data[1]
|
||||||
if d.length+2 != uint8(len(data)) {
|
if d.length+2 != uint8(len(data)) {
|
||||||
return fmt.Errorf("Lenght mismatch in modbus payload")
|
return fmt.Errorf("length mismatch in modbus payload")
|
||||||
}
|
}
|
||||||
d.registers = make([]uint16, d.length/2)
|
d.registers = make([]uint16, d.length/2)
|
||||||
var n uint8
|
var n uint8
|
||||||
|
|||||||
@@ -2,12 +2,14 @@ package modbustcpclient
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestReadOneRegister(t *testing.T) {
|
func TestReadOneRegisterKeepAlive(t *testing.T) {
|
||||||
c, err := New("192.168.0.154:502", 1)
|
c, err := New("IAM_248000012514.solver.nu:502", 1, 100*time.Millisecond, 5*time.Second)
|
||||||
|
t.Log("Connect")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
for n := 0; n < 5; n++ {
|
for n := 0; n < 5; n++ {
|
||||||
res, err := c.ReadRegisters(12401, 2)
|
res, err := c.ReadRegisters(12401, 2)
|
||||||
@@ -30,5 +32,64 @@ func TestReadOneRegister(t *testing.T) {
|
|||||||
assert.Len(t, res, 1)
|
assert.Len(t, res, 1)
|
||||||
t.Log(res)
|
t.Log(res)
|
||||||
}
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadOneRegisterShortKeepAlive(t *testing.T) {
|
||||||
|
c, err := New("IAM_248000012514.solver.nu:502", 1, 10*time.Nanosecond, 5*time.Second)
|
||||||
|
t.Log("Connect")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
for n := 0; n < 5; n++ {
|
||||||
|
res, err := c.ReadRegisters(12401, 2)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, res, 2)
|
||||||
|
t.Log(res)
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
|
res, err = c.ReadRegisters(12102, 2)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, res, 2)
|
||||||
|
t.Log(res)
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
|
res, err = c.ReadRegisters(12544, 1)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, res, 1)
|
||||||
|
t.Log(float32(res[0]) / 10)
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
|
res, err = c.ReadRegisters(12136, 1)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, res, 1)
|
||||||
|
t.Log(res)
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadALot(t *testing.T) {
|
||||||
|
c, err := New("IAM_248000012514.solver.nu:502", 1, 100*time.Millisecond, 5*time.Second)
|
||||||
|
t.Log("Connect")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
for n := 0; n < 500; n++ {
|
||||||
|
t.Log(n)
|
||||||
|
_, err := c.ReadRegisters(12401, 2)
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
}
|
||||||
|
_, err = c.ReadRegisters(12102, 2)
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = c.ReadRegisters(12544, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
}
|
||||||
|
_, err = c.ReadRegisters(12136, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user