Open tcp connection for every read. #1
25
client.go
25
client.go
@@ -10,24 +10,27 @@ import (
|
|||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(address string, unit uint8) (*Mbclient, error) {
|
func New(address string, unit uint8) (*Mbclient, error) {
|
||||||
var err error
|
|
||||||
|
|
||||||
c := new(Mbclient)
|
c := new(Mbclient)
|
||||||
c.conn, err = net.Dial("tcp", address)
|
c.address = address
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
c.unit = unit
|
c.unit = unit
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error) {
|
func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error) {
|
||||||
|
|
||||||
|
conn, err := net.Dial("tcp", m.address)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
const requestLength = 12
|
const requestLength = 12
|
||||||
m.transactionCounter++
|
m.transactionCounter++
|
||||||
req := make([]byte, requestLength)
|
req := make([]byte, requestLength)
|
||||||
@@ -41,21 +44,21 @@ 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))
|
conn.SetDeadline(time.Now().Add(10 * time.Second))
|
||||||
byteswritten, err := m.conn.Write(req)
|
byteswritten, err := conn.Write(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if byteswritten != requestLength {
|
if byteswritten != requestLength {
|
||||||
return nil, fmt.Errorf("Failed to send request")
|
return nil, fmt.Errorf("Failed to send request")
|
||||||
}
|
}
|
||||||
m.conn.SetDeadline(time.Now().Add(10 * time.Second))
|
conn.SetDeadline(time.Now().Add(10 * time.Second))
|
||||||
_, err = io.ReadFull(m.conn, m.header[:])
|
_, err = io.ReadFull(conn, m.header[:])
|
||||||
responseHeader.unMarshal(m.header)
|
responseHeader.unMarshal(m.header)
|
||||||
expectedDataLength := responseHeader.length - 1
|
expectedDataLength := responseHeader.length - 1
|
||||||
|
|
||||||
response := make([]byte, expectedDataLength)
|
response := make([]byte, expectedDataLength)
|
||||||
_, err = m.conn.Read(response)
|
_, err = conn.Read(response)
|
||||||
|
|
||||||
err = mbpayload.unMarshal(response)
|
err = mbpayload.unMarshal(response)
|
||||||
if mbpayload.functionCode != 3 {
|
if mbpayload.functionCode != 3 {
|
||||||
|
|||||||
Reference in New Issue
Block a user