|
|
@ -19,7 +19,11 @@ func ReadCoils(s *Handler, packet protocol.Packet) ([]byte, *protocol.MError) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data := make([]byte, 1+dataSize)
|
|
|
|
data := make([]byte, 1+dataSize)
|
|
|
|
data[0] = byte(dataSize)
|
|
|
|
data[0] = byte(dataSize)
|
|
|
|
for i, value := range s.Coils[register:endRegister] {
|
|
|
|
|
|
|
|
|
|
|
|
dataMgr := s.DataManager(packet.GetAddress())
|
|
|
|
|
|
|
|
coils := dataMgr.ReadCoils(register, endRegister)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i, value := range coils {
|
|
|
|
if value != 0 {
|
|
|
|
if value != 0 {
|
|
|
|
shift := uint(i) % 8
|
|
|
|
shift := uint(i) % 8
|
|
|
|
data[1+i/8] |= byte(1 << shift)
|
|
|
|
data[1+i/8] |= byte(1 << shift)
|
|
|
@ -40,7 +44,11 @@ func ReadDiscreteInputs(s *Handler, packet protocol.Packet) ([]byte, *protocol.M
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data := make([]byte, 1+dataSize)
|
|
|
|
data := make([]byte, 1+dataSize)
|
|
|
|
data[0] = byte(dataSize)
|
|
|
|
data[0] = byte(dataSize)
|
|
|
|
for i, value := range s.DiscreteInputs[register:endRegister] {
|
|
|
|
|
|
|
|
|
|
|
|
dataMgr := s.DataManager(packet.GetAddress())
|
|
|
|
|
|
|
|
discreteInputs := dataMgr.ReadDiscreteInputs(register, endRegister)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i, value := range discreteInputs {
|
|
|
|
if value != 0 {
|
|
|
|
if value != 0 {
|
|
|
|
shift := uint(i) % 8
|
|
|
|
shift := uint(i) % 8
|
|
|
|
data[1+i/8] |= byte(1 << shift)
|
|
|
|
data[1+i/8] |= byte(1 << shift)
|
|
|
@ -55,7 +63,8 @@ func ReadHoldingRegisters(s *Handler, packet protocol.Packet) ([]byte, *protocol
|
|
|
|
if endRegister > 65536 {
|
|
|
|
if endRegister > 65536 {
|
|
|
|
return []byte{}, &protocol.IllegalDataAddress
|
|
|
|
return []byte{}, &protocol.IllegalDataAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append([]byte{byte(numRegs * 2)}, util.Uint16ToBytes(s.byteOrder, s.HoldingRegisters[register:endRegister])...), &protocol.Success
|
|
|
|
dataMgr := s.DataManager(packet.GetAddress())
|
|
|
|
|
|
|
|
return append([]byte{byte(numRegs * 2)}, util.Uint16ToBytes(s.byteOrder, dataMgr.ReadHoldingRegisters(register, endRegister))...), &protocol.Success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ReadInputRegisters 读输入寄存器(AO) 功能码0x04
|
|
|
|
// ReadInputRegisters 读输入寄存器(AO) 功能码0x04
|
|
|
@ -64,7 +73,8 @@ func ReadInputRegisters(s *Handler, packet protocol.Packet) ([]byte, *protocol.M
|
|
|
|
if endRegister > 65536 {
|
|
|
|
if endRegister > 65536 {
|
|
|
|
return []byte{}, &protocol.IllegalDataAddress
|
|
|
|
return []byte{}, &protocol.IllegalDataAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append([]byte{byte(numRegs * 2)}, util.Uint16ToBytes(s.byteOrder, s.InputRegisters[register:endRegister])...), &protocol.Success
|
|
|
|
dataMgr := s.DataManager(packet.GetAddress())
|
|
|
|
|
|
|
|
return append([]byte{byte(numRegs * 2)}, util.Uint16ToBytes(s.byteOrder, dataMgr.ReadInputRegisters(register, endRegister))...), &protocol.Success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WriteSingleCoil 写单个线圈寄存器 功能码0x05
|
|
|
|
// WriteSingleCoil 写单个线圈寄存器 功能码0x05
|
|
|
@ -75,7 +85,9 @@ func WriteSingleCoil(s *Handler, packet protocol.Packet) ([]byte, *protocol.MErr
|
|
|
|
if value != 0 {
|
|
|
|
if value != 0 {
|
|
|
|
value = 1
|
|
|
|
value = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Coils[register] = byte(value)
|
|
|
|
dataMgr := s.DataManager(packet.GetAddress())
|
|
|
|
|
|
|
|
dataMgr.WriteSingleCoil(register, byte(value))
|
|
|
|
|
|
|
|
|
|
|
|
return data[0:4], &protocol.Success
|
|
|
|
return data[0:4], &protocol.Success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -83,7 +95,10 @@ func WriteSingleCoil(s *Handler, packet protocol.Packet) ([]byte, *protocol.MErr
|
|
|
|
func WriteHoldingRegister(s *Handler, packet protocol.Packet) ([]byte, *protocol.MError) {
|
|
|
|
func WriteHoldingRegister(s *Handler, packet protocol.Packet) ([]byte, *protocol.MError) {
|
|
|
|
data := packet.GetBody()
|
|
|
|
data := packet.GetBody()
|
|
|
|
register, value := serialize.RegisterAddressAndValue(data)
|
|
|
|
register, value := serialize.RegisterAddressAndValue(data)
|
|
|
|
s.HoldingRegisters[register] = value
|
|
|
|
|
|
|
|
|
|
|
|
dataMgr := s.DataManager(packet.GetAddress())
|
|
|
|
|
|
|
|
dataMgr.WriteSingleHoldingRegister(register, value)
|
|
|
|
|
|
|
|
|
|
|
|
return data[0:4], &protocol.Success
|
|
|
|
return data[0:4], &protocol.Success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -102,10 +117,14 @@ func WriteMultipleCoils(s *Handler, packet protocol.Packet) ([]byte, *protocol.M
|
|
|
|
// return []byte{}, &IllegalDataAddress
|
|
|
|
// return []byte{}, &IllegalDataAddress
|
|
|
|
//}
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
idxList := make([]int, 0)
|
|
|
|
|
|
|
|
values := make([]byte, 0)
|
|
|
|
bitCount := 0
|
|
|
|
bitCount := 0
|
|
|
|
for i, value := range valueBytes {
|
|
|
|
for i, value := range valueBytes {
|
|
|
|
for bitPos := uint(0); bitPos < 8; bitPos++ {
|
|
|
|
for bitPos := uint(0); bitPos < 8; bitPos++ {
|
|
|
|
s.Coils[register+(i*8)+int(bitPos)] = util.BitAtPosition(value, bitPos)
|
|
|
|
idxList = append(idxList, register+(i*8)+int(bitPos))
|
|
|
|
|
|
|
|
values = append(values, util.BitAtPosition(value, bitPos))
|
|
|
|
|
|
|
|
|
|
|
|
bitCount++
|
|
|
|
bitCount++
|
|
|
|
if bitCount >= numRegs {
|
|
|
|
if bitCount >= numRegs {
|
|
|
|
break
|
|
|
|
break
|
|
|
@ -116,6 +135,9 @@ func WriteMultipleCoils(s *Handler, packet protocol.Packet) ([]byte, *protocol.M
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dataMgr := s.DataManager(packet.GetAddress())
|
|
|
|
|
|
|
|
dataMgr.WriteCoils(idxList, values)
|
|
|
|
|
|
|
|
|
|
|
|
return data[0:4], &protocol.Success
|
|
|
|
return data[0:4], &protocol.Success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -133,7 +155,9 @@ func WriteHoldingRegisters(s *Handler, packet protocol.Packet) ([]byte, *protoco
|
|
|
|
|
|
|
|
|
|
|
|
// Copy data to memory
|
|
|
|
// Copy data to memory
|
|
|
|
values := util.BytesToUint16(s.byteOrder, valueBytes)
|
|
|
|
values := util.BytesToUint16(s.byteOrder, valueBytes)
|
|
|
|
valuesUpdated := copy(s.HoldingRegisters[register:], values)
|
|
|
|
dataMgr := s.DataManager(packet.GetAddress())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
valuesUpdated := dataMgr.WriteHoldingRegisters(values, register)
|
|
|
|
if valuesUpdated == numRegs {
|
|
|
|
if valuesUpdated == numRegs {
|
|
|
|
err = &protocol.Success
|
|
|
|
err = &protocol.Success
|
|
|
|
data = pkgData[0:4]
|
|
|
|
data = pkgData[0:4]
|
|
|
|