feat: 为logic部分添加原始数据

main v1.0.3
NoahLan 1 year ago
parent 4283f1e5dd
commit 042f763a0c

@ -42,7 +42,7 @@ func (r *ReadCoils) Function(s *Handler, req protocol.Packet, resp protocol.Pack
}
}
s.HandleLogic(r.fnCode, register, numRegs, coils)
s.HandleLogic(r.fnCode, req, register, numRegs, coils)
resp.SetBody(data)
return &protocol.Success
@ -73,7 +73,7 @@ func (r *WriteSingleCoil) Function(s *Handler, req protocol.Packet, resp protoco
dataMgr := s.DataManager(req.GetAddress())
dataMgr.WriteSingleCoil(register, byte(value))
s.HandleLogic(r.fnCode, register, 1, value)
s.HandleLogic(r.fnCode, req, register, 1, value)
resp.SetBody(data[0:4])
@ -131,7 +131,7 @@ func (r *WriteMultipleCoils) Function(s *Handler, req protocol.Packet, resp prot
dataMgr := s.DataManager(req.GetAddress())
dataMgr.WriteCoils(idxList, values)
s.HandleLogic(r.fnCode, register, numRegs, values)
s.HandleLogic(r.fnCode, req, register, numRegs, values)
resp.SetBody(data[0:4])

@ -41,7 +41,7 @@ func (r *ReadDiscreteInputs) Function(s *Handler, req protocol.Packet, resp prot
}
}
s.HandleLogic(r.fnCode, register, numRegs, data)
s.HandleLogic(r.fnCode, req, register, numRegs, data)
resp.SetBody(data)
return &protocol.Success

@ -30,7 +30,7 @@ func (r *ReadHoldingRegisters) Function(s *Handler, req protocol.Packet, resp pr
resp.SetBody(append([]byte{byte(numRegs * 2)}, util.Uint16ToBytes(s.byteOrder, data)...))
s.HandleLogic(r.fnCode, register, numRegs, data)
s.HandleLogic(r.fnCode, req, register, numRegs, data)
return &protocol.Success
}
@ -59,7 +59,7 @@ func (r *WriteHoldingRegister) Function(s *Handler, req protocol.Packet, resp pr
resp.SetBody(data[0:4])
s.HandleLogic(r.fnCode, register, 1, value)
s.HandleLogic(r.fnCode, req, register, 1, value)
return &protocol.Success
}
@ -104,7 +104,7 @@ func (r *WriteHoldingRegisters) Function(s *Handler, req protocol.Packet, resp p
resp.SetBody(data)
s.HandleLogic(r.fnCode, register, numRegs, values)
s.HandleLogic(r.fnCode, req, register, numRegs, values)
return err
}

@ -29,7 +29,7 @@ func (r *ReadInputRegisters) Function(s *Handler, req protocol.Packet, resp prot
data := dataMgr.ReadInputRegisters(register, endRegister)
resp.SetBody(append([]byte{byte(numRegs * 2)}, util.Uint16ToBytes(s.byteOrder, data)...))
s.HandleLogic(r.fnCode, register, numRegs, data)
s.HandleLogic(r.fnCode, req, register, numRegs, data)
return &protocol.Success
}

@ -28,13 +28,12 @@ type (
FnCode() uint8
}
LogicFunc func(startAddr, number int, val any)
LogicFunc func(pkg protocol.Packet, startAddr, number int, val any)
)
func NewHandler(byteOrder binary.ByteOrder) *Handler {
p := &Handler{
byteOrder: byteOrder,
//functions: make(map[uint8]FunctionFunc),
byteOrder: byteOrder,
logics: make(map[uint8]LogicFunc),
handlerMap: make(map[uint8]FunctionHandler),
dataMap: make(map[uint8]*DataMgr),
@ -97,10 +96,10 @@ func (p *Handler) Handle(conn *connection.Connection, pkg packet.IPacket) {
_ = conn.Send(resp.GetHeader(), resp.GetBody())
}
func (p *Handler) HandleLogic(fnCode uint8, startAddr, number int, val any) {
func (p *Handler) HandleLogic(fnCode uint8, pkg protocol.Packet, startAddr, number int, val any) {
go func() {
if fn, ok := p.logics[fnCode]; ok {
fn(startAddr, number, val)
fn(pkg, startAddr, number, val)
}
}()
}

Loading…
Cancel
Save