private MBMessage SendRequest(MBMessage req) { try { SocketError err; byte[] buff = req.encode(); //log.DebugFormat("> {0:##}", ValueHelper.BytesToHexStr(buff)); byte[] ack = _client.SSend(req.encode(), out err); //log.DebugFormat("< {0:##}, err={1}", ValueHelper.BytesToHexStr(ack), err); if (SocketError.Success != err) { log.ErrorFormat("异常: {0}", err); } MBMessage resp = new MBMessage(ack); if (resp.FC > 0x80) { byte ec = resp.GetByte(0); log.ErrorFormat("Modbus 异常: {0}-{1} ", ec, MBException.NameOf(ec)); } return resp; } catch (Exception) { return null; } }
// 异步发送应答. public void ASend(MBMessage resp) { if (resp != null) { byte[] buff = resp.encode(); if (this._socket != null) { Ack++; Sent += buff.Length; this._socket.Send(buff); } } }