From 042f763a0cd705c83698d15730e54165147bdfc9 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Mon, 19 Jun 2023 17:41:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=BAlogic=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8E=9F=E5=A7=8B=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nmodbus/handler/fn_coils.go | 6 +++--- nmodbus/handler/fn_discreteinputs.go | 2 +- nmodbus/handler/fn_holdingregisters.go | 6 +++--- nmodbus/handler/fn_inputregisters.go | 2 +- nmodbus/handler/handler.go | 9 ++++----- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/nmodbus/handler/fn_coils.go b/nmodbus/handler/fn_coils.go index a9896af..a686ed1 100644 --- a/nmodbus/handler/fn_coils.go +++ b/nmodbus/handler/fn_coils.go @@ -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]) diff --git a/nmodbus/handler/fn_discreteinputs.go b/nmodbus/handler/fn_discreteinputs.go index 66dbd27..42237dd 100644 --- a/nmodbus/handler/fn_discreteinputs.go +++ b/nmodbus/handler/fn_discreteinputs.go @@ -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 diff --git a/nmodbus/handler/fn_holdingregisters.go b/nmodbus/handler/fn_holdingregisters.go index 390e163..d6c2a5e 100644 --- a/nmodbus/handler/fn_holdingregisters.go +++ b/nmodbus/handler/fn_holdingregisters.go @@ -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 } diff --git a/nmodbus/handler/fn_inputregisters.go b/nmodbus/handler/fn_inputregisters.go index b1bcd9e..b229aaa 100644 --- a/nmodbus/handler/fn_inputregisters.go +++ b/nmodbus/handler/fn_inputregisters.go @@ -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 } diff --git a/nmodbus/handler/handler.go b/nmodbus/handler/handler.go index f6e7ca8..0faa78b 100644 --- a/nmodbus/handler/handler.go +++ b/nmodbus/handler/handler.go @@ -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) } }() }