|
|
|
@ -19,7 +19,11 @@ func ReadCoils(s *Handler, packet protocol.Packet) ([]byte, *protocol.MError) {
|
|
|
|
|
}
|
|
|
|
|
data := make([]byte, 1+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 {
|
|
|
|
|
shift := uint(i) % 8
|
|
|
|
|
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[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 {
|
|
|
|
|
shift := uint(i) % 8
|
|
|
|
|
data[1+i/8] |= byte(1 << shift)
|
|
|
|
@ -55,7 +63,8 @@ func ReadHoldingRegisters(s *Handler, packet protocol.Packet) ([]byte, *protocol
|
|
|
|
|
if endRegister > 65536 {
|
|
|
|
|
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
|
|
|
|
@ -64,7 +73,8 @@ func ReadInputRegisters(s *Handler, packet protocol.Packet) ([]byte, *protocol.M
|
|
|
|
|
if endRegister > 65536 {
|
|
|
|
|
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
|
|
|
|
@ -75,7 +85,9 @@ func WriteSingleCoil(s *Handler, packet protocol.Packet) ([]byte, *protocol.MErr
|
|
|
|
|
if value != 0 {
|
|
|
|
|
value = 1
|
|
|
|
|
}
|
|
|
|
|
s.Coils[register] = byte(value)
|
|
|
|
|
dataMgr := s.DataManager(packet.GetAddress())
|
|
|
|
|
dataMgr.WriteSingleCoil(register, byte(value))
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
data := packet.GetBody()
|
|
|
|
|
register, value := serialize.RegisterAddressAndValue(data)
|
|
|
|
|
s.HoldingRegisters[register] = value
|
|
|
|
|
|
|
|
|
|
dataMgr := s.DataManager(packet.GetAddress())
|
|
|
|
|
dataMgr.WriteSingleHoldingRegister(register, value)
|
|
|
|
|
|
|
|
|
|
return data[0:4], &protocol.Success
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -102,10 +117,14 @@ func WriteMultipleCoils(s *Handler, packet protocol.Packet) ([]byte, *protocol.M
|
|
|
|
|
// return []byte{}, &IllegalDataAddress
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
idxList := make([]int, 0)
|
|
|
|
|
values := make([]byte, 0)
|
|
|
|
|
bitCount := 0
|
|
|
|
|
for i, value := range valueBytes {
|
|
|
|
|
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++
|
|
|
|
|
if bitCount >= numRegs {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -133,7 +155,9 @@ func WriteHoldingRegisters(s *Handler, packet protocol.Packet) ([]byte, *protoco
|
|
|
|
|
|
|
|
|
|
// Copy data to memory
|
|
|
|
|
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 {
|
|
|
|
|
err = &protocol.Success
|
|
|
|
|
data = pkgData[0:4]
|
|
|
|
|