go fmt ./...
This commit is contained in:
parent
079cd7efc9
commit
c846ac1ead
2 changed files with 122 additions and 87 deletions
187
modbus/client.go
187
modbus/client.go
|
@ -31,13 +31,16 @@ func NewClient2(packager Packager, transporter Transporter) Client {
|
|||
}
|
||||
|
||||
// Request:
|
||||
// Function code : 1 byte (0x01)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of coils : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x01)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of coils : 2 bytes
|
||||
//
|
||||
// Response:
|
||||
// Function code : 1 byte (0x01)
|
||||
// Byte count : 1 byte
|
||||
// Coil status : N* bytes (=N or N+1)
|
||||
//
|
||||
// Function code : 1 byte (0x01)
|
||||
// Byte count : 1 byte
|
||||
// Coil status : N* bytes (=N or N+1)
|
||||
func (mb *client) ReadCoils(address, quantity uint16) (results []byte, err error) {
|
||||
if quantity < 1 || quantity > 2000 {
|
||||
err = fmt.Errorf("modbus: quantity '%v' must be between '%v' and '%v',", quantity, 1, 2000)
|
||||
|
@ -62,13 +65,16 @@ func (mb *client) ReadCoils(address, quantity uint16) (results []byte, err error
|
|||
}
|
||||
|
||||
// Request:
|
||||
// Function code : 1 byte (0x02)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of inputs : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x02)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of inputs : 2 bytes
|
||||
//
|
||||
// Response:
|
||||
// Function code : 1 byte (0x02)
|
||||
// Byte count : 1 byte
|
||||
// Input status : N* bytes (=N or N+1)
|
||||
//
|
||||
// Function code : 1 byte (0x02)
|
||||
// Byte count : 1 byte
|
||||
// Input status : N* bytes (=N or N+1)
|
||||
func (mb *client) ReadDiscreteInputs(address, quantity uint16) (results []byte, err error) {
|
||||
if quantity < 1 || quantity > 2000 {
|
||||
err = fmt.Errorf("modbus: quantity '%v' must be between '%v' and '%v',", quantity, 1, 2000)
|
||||
|
@ -93,13 +99,16 @@ func (mb *client) ReadDiscreteInputs(address, quantity uint16) (results []byte,
|
|||
}
|
||||
|
||||
// Request:
|
||||
// Function code : 1 byte (0x03)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of registers : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x03)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of registers : 2 bytes
|
||||
//
|
||||
// Response:
|
||||
// Function code : 1 byte (0x03)
|
||||
// Byte count : 1 byte
|
||||
// Register value : Nx2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x03)
|
||||
// Byte count : 1 byte
|
||||
// Register value : Nx2 bytes
|
||||
func (mb *client) ReadHoldingRegisters(address, quantity uint16) (results []byte, err error) {
|
||||
if quantity < 1 || quantity > 125 {
|
||||
err = fmt.Errorf("modbus: quantity '%v' must be between '%v' and '%v',", quantity, 1, 125)
|
||||
|
@ -124,13 +133,16 @@ func (mb *client) ReadHoldingRegisters(address, quantity uint16) (results []byte
|
|||
}
|
||||
|
||||
// Request:
|
||||
// Function code : 1 byte (0x04)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of registers : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x04)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of registers : 2 bytes
|
||||
//
|
||||
// Response:
|
||||
// Function code : 1 byte (0x04)
|
||||
// Byte count : 1 byte
|
||||
// Input registers : N bytes
|
||||
//
|
||||
// Function code : 1 byte (0x04)
|
||||
// Byte count : 1 byte
|
||||
// Input registers : N bytes
|
||||
func (mb *client) ReadInputRegisters(address, quantity uint16) (results []byte, err error) {
|
||||
if quantity < 1 || quantity > 125 {
|
||||
err = fmt.Errorf("modbus: quantity '%v' must be between '%v' and '%v',", quantity, 1, 125)
|
||||
|
@ -155,13 +167,16 @@ func (mb *client) ReadInputRegisters(address, quantity uint16) (results []byte,
|
|||
}
|
||||
|
||||
// Request:
|
||||
// Function code : 1 byte (0x05)
|
||||
// Output address : 2 bytes
|
||||
// Output value : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x05)
|
||||
// Output address : 2 bytes
|
||||
// Output value : 2 bytes
|
||||
//
|
||||
// Response:
|
||||
// Function code : 1 byte (0x05)
|
||||
// Output address : 2 bytes
|
||||
// Output value : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x05)
|
||||
// Output address : 2 bytes
|
||||
// Output value : 2 bytes
|
||||
func (mb *client) WriteSingleCoil(address, value uint16) (results []byte, err error) {
|
||||
// The requested ON/OFF state can only be 0xFF00 and 0x0000
|
||||
if value != 0xFF00 && value != 0x0000 {
|
||||
|
@ -196,13 +211,16 @@ func (mb *client) WriteSingleCoil(address, value uint16) (results []byte, err er
|
|||
}
|
||||
|
||||
// Request:
|
||||
// Function code : 1 byte (0x06)
|
||||
// Register address : 2 bytes
|
||||
// Register value : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x06)
|
||||
// Register address : 2 bytes
|
||||
// Register value : 2 bytes
|
||||
//
|
||||
// Response:
|
||||
// Function code : 1 byte (0x06)
|
||||
// Register address : 2 bytes
|
||||
// Register value : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x06)
|
||||
// Register address : 2 bytes
|
||||
// Register value : 2 bytes
|
||||
func (mb *client) WriteSingleRegister(address, value uint16) (results []byte, err error) {
|
||||
request := ProtocolDataUnit{
|
||||
FunctionCode: FuncCodeWriteSingleRegister,
|
||||
|
@ -232,15 +250,18 @@ func (mb *client) WriteSingleRegister(address, value uint16) (results []byte, er
|
|||
}
|
||||
|
||||
// Request:
|
||||
// Function code : 1 byte (0x0F)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of outputs : 2 bytes
|
||||
// Byte count : 1 byte
|
||||
// Outputs value : N* bytes
|
||||
//
|
||||
// Function code : 1 byte (0x0F)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of outputs : 2 bytes
|
||||
// Byte count : 1 byte
|
||||
// Outputs value : N* bytes
|
||||
//
|
||||
// Response:
|
||||
// Function code : 1 byte (0x0F)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of outputs : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x0F)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of outputs : 2 bytes
|
||||
func (mb *client) WriteMultipleCoils(address, quantity uint16, value []byte) (results []byte, err error) {
|
||||
if quantity < 1 || quantity > 1968 {
|
||||
err = fmt.Errorf("modbus: quantity '%v' must be between '%v' and '%v',", quantity, 1, 1968)
|
||||
|
@ -274,15 +295,18 @@ func (mb *client) WriteMultipleCoils(address, quantity uint16, value []byte) (re
|
|||
}
|
||||
|
||||
// Request:
|
||||
// Function code : 1 byte (0x10)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of outputs : 2 bytes
|
||||
// Byte count : 1 byte
|
||||
// Registers value : N* bytes
|
||||
//
|
||||
// Function code : 1 byte (0x10)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of outputs : 2 bytes
|
||||
// Byte count : 1 byte
|
||||
// Registers value : N* bytes
|
||||
//
|
||||
// Response:
|
||||
// Function code : 1 byte (0x10)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of registers : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x10)
|
||||
// Starting address : 2 bytes
|
||||
// Quantity of registers : 2 bytes
|
||||
func (mb *client) WriteMultipleRegisters(address, quantity uint16, value []byte) (results []byte, err error) {
|
||||
if quantity < 1 || quantity > 123 {
|
||||
err = fmt.Errorf("modbus: quantity '%v' must be between '%v' and '%v',", quantity, 1, 123)
|
||||
|
@ -316,15 +340,18 @@ func (mb *client) WriteMultipleRegisters(address, quantity uint16, value []byte)
|
|||
}
|
||||
|
||||
// Request:
|
||||
// Function code : 1 byte (0x16)
|
||||
// Reference address : 2 bytes
|
||||
// AND-mask : 2 bytes
|
||||
// OR-mask : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x16)
|
||||
// Reference address : 2 bytes
|
||||
// AND-mask : 2 bytes
|
||||
// OR-mask : 2 bytes
|
||||
//
|
||||
// Response:
|
||||
// Function code : 1 byte (0x16)
|
||||
// Reference address : 2 bytes
|
||||
// AND-mask : 2 bytes
|
||||
// OR-mask : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x16)
|
||||
// Reference address : 2 bytes
|
||||
// AND-mask : 2 bytes
|
||||
// OR-mask : 2 bytes
|
||||
func (mb *client) MaskWriteRegister(address, andMask, orMask uint16) (results []byte, err error) {
|
||||
request := ProtocolDataUnit{
|
||||
FunctionCode: FuncCodeMaskWriteRegister,
|
||||
|
@ -359,17 +386,20 @@ func (mb *client) MaskWriteRegister(address, andMask, orMask uint16) (results []
|
|||
}
|
||||
|
||||
// Request:
|
||||
// Function code : 1 byte (0x17)
|
||||
// Read starting address : 2 bytes
|
||||
// Quantity to read : 2 bytes
|
||||
// Write starting address: 2 bytes
|
||||
// Quantity to write : 2 bytes
|
||||
// Write byte count : 1 byte
|
||||
// Write registers value : N* bytes
|
||||
//
|
||||
// Function code : 1 byte (0x17)
|
||||
// Read starting address : 2 bytes
|
||||
// Quantity to read : 2 bytes
|
||||
// Write starting address: 2 bytes
|
||||
// Quantity to write : 2 bytes
|
||||
// Write byte count : 1 byte
|
||||
// Write registers value : N* bytes
|
||||
//
|
||||
// Response:
|
||||
// Function code : 1 byte (0x17)
|
||||
// Byte count : 1 byte
|
||||
// Read registers value : Nx2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x17)
|
||||
// Byte count : 1 byte
|
||||
// Read registers value : Nx2 bytes
|
||||
func (mb *client) ReadWriteMultipleRegisters(readAddress, readQuantity, writeAddress, writeQuantity uint16, value []byte) (results []byte, err error) {
|
||||
if readQuantity < 1 || readQuantity > 125 {
|
||||
err = fmt.Errorf("modbus: quantity to read '%v' must be between '%v' and '%v',", readQuantity, 1, 125)
|
||||
|
@ -397,14 +427,17 @@ func (mb *client) ReadWriteMultipleRegisters(readAddress, readQuantity, writeAdd
|
|||
}
|
||||
|
||||
// Request:
|
||||
// Function code : 1 byte (0x18)
|
||||
// FIFO pointer address : 2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x18)
|
||||
// FIFO pointer address : 2 bytes
|
||||
//
|
||||
// Response:
|
||||
// Function code : 1 byte (0x18)
|
||||
// Byte count : 2 bytes
|
||||
// FIFO count : 2 bytes
|
||||
// FIFO count : 2 bytes (<=31)
|
||||
// FIFO value register : Nx2 bytes
|
||||
//
|
||||
// Function code : 1 byte (0x18)
|
||||
// Byte count : 2 bytes
|
||||
// FIFO count : 2 bytes
|
||||
// FIFO count : 2 bytes (<=31)
|
||||
// FIFO value register : Nx2 bytes
|
||||
func (mb *client) ReadFIFOQueue(address uint16) (results []byte, err error) {
|
||||
request := ProtocolDataUnit{
|
||||
FunctionCode: FuncCodeReadFIFOQueue,
|
||||
|
|
|
@ -67,12 +67,13 @@ type tcpPackager struct {
|
|||
}
|
||||
|
||||
// Encode adds modbus application protocol header:
|
||||
// Transaction identifier: 2 bytes
|
||||
// Protocol identifier: 2 bytes
|
||||
// Length: 2 bytes
|
||||
// Unit identifier: 1 byte
|
||||
// Function code: 1 byte
|
||||
// Data: n bytes
|
||||
//
|
||||
// Transaction identifier: 2 bytes
|
||||
// Protocol identifier: 2 bytes
|
||||
// Length: 2 bytes
|
||||
// Unit identifier: 1 byte
|
||||
// Function code: 1 byte
|
||||
// Data: n bytes
|
||||
func (mb *tcpPackager) Encode(pdu *ProtocolDataUnit) (adu []byte, err error) {
|
||||
adu = make([]byte, tcpHeaderSize+1+len(pdu.Data))
|
||||
|
||||
|
@ -118,10 +119,11 @@ func (mb *tcpPackager) Verify(aduRequest []byte, aduResponse []byte) (err error)
|
|||
}
|
||||
|
||||
// Decode extracts PDU from TCP frame:
|
||||
// Transaction identifier: 2 bytes
|
||||
// Protocol identifier: 2 bytes
|
||||
// Length: 2 bytes
|
||||
// Unit identifier: 1 byte
|
||||
//
|
||||
// Transaction identifier: 2 bytes
|
||||
// Protocol identifier: 2 bytes
|
||||
// Length: 2 bytes
|
||||
// Unit identifier: 1 byte
|
||||
func (mb *tcpPackager) Decode(adu []byte) (pdu *ProtocolDataUnit, err error) {
|
||||
// Read length value in the header
|
||||
length := binary.BigEndian.Uint16(adu[4:])
|
||||
|
|
Loading…
Reference in a new issue