禁用Modbus调试日志

This commit is contained in:
iioter 2022-09-14 20:47:12 +08:00
parent 24c757087c
commit 8c39ef0260
11 changed files with 44 additions and 40 deletions

View File

@ -79,31 +79,31 @@
{
while (true)
{
Debug.WriteLine($"Begin reading header from Master at IP: {EndPoint}");
//Debug.WriteLine($"Begin reading header from Master at IP: {EndPoint}");
int readBytes = await Stream.ReadAsync(_mbapHeader, 0, 6).ConfigureAwait(false);
if (readBytes == 0)
{
Debug.WriteLine($"0 bytes read, Master at {EndPoint} has closed Socket connection.");
//Debug.WriteLine($"0 bytes read, Master at {EndPoint} has closed Socket connection.");
ModbusMasterTcpConnectionClosed?.Invoke(this, new TcpConnectionEventArgs(EndPoint));
return;
}
ushort frameLength = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt16(_mbapHeader, 4));
Debug.WriteLine($"Master at {EndPoint} sent header: \"{string.Join(", ", _mbapHeader)}\" with {frameLength} bytes in PDU");
//Debug.WriteLine($"Master at {EndPoint} sent header: \"{string.Join(", ", _mbapHeader)}\" with {frameLength} bytes in PDU");
_messageFrame = new byte[frameLength];
readBytes = await Stream.ReadAsync(_messageFrame, 0, frameLength).ConfigureAwait(false);
if (readBytes == 0)
{
Debug.WriteLine($"0 bytes read, Master at {EndPoint} has closed Socket connection.");
//Debug.WriteLine($"0 bytes read, Master at {EndPoint} has closed Socket connection.");
ModbusMasterTcpConnectionClosed?.Invoke(this, new TcpConnectionEventArgs(EndPoint));
return;
}
Debug.WriteLine($"Read frame from Master at {EndPoint} completed {readBytes} bytes");
//Debug.WriteLine($"Read frame from Master at {EndPoint} completed {readBytes} bytes");
byte[] frame = _mbapHeader.Concat(_messageFrame).ToArray();
Debug.WriteLine($"RX from Master at {EndPoint}: {string.Join(", ", frame)}");
//Debug.WriteLine($"RX from Master at {EndPoint}: {string.Join(", ", frame)}");
var request = ModbusMessageFactory.CreateModbusRequest(_messageFrame);
request.TransactionId = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(frame, 0));
@ -114,7 +114,7 @@
// write response
byte[] responseFrame = Transport.BuildMessageFrame(response);
Debug.WriteLine($"TX to Master at {EndPoint}: {string.Join(", ", responseFrame)}");
//Debug.WriteLine($"TX to Master at {EndPoint}: {string.Join(", ", responseFrame)}");
await Stream.WriteAsync(responseFrame, 0, responseFrame.Length).ConfigureAwait(false);
}
}

View File

@ -112,14 +112,14 @@
if (SerialTransport.CheckFrame && !SerialTransport.ChecksumsMatch(request, frame))
{
string msg = $"Checksums failed to match {string.Join(", ", request.MessageFrame)} != {string.Join(", ", frame)}.";
Debug.WriteLine(msg);
//Debug.WriteLine(msg);
throw new IOException(msg);
}
// only service requests addressed to this particular slave
if (request.SlaveAddress != UnitId)
{
Debug.WriteLine($"NModbus Slave {UnitId} ignoring request intended for NModbus Slave {request.SlaveAddress}");
//Debug.WriteLine($"NModbus Slave {UnitId} ignoring request intended for NModbus Slave {request.SlaveAddress}");
continue;
}
@ -131,12 +131,12 @@
}
catch (IOException ioe)
{
Debug.WriteLine($"IO Exception encountered while listening for requests - {ioe.Message}");
//Debug.WriteLine($"IO Exception encountered while listening for requests - {ioe.Message}");
SerialTransport.DiscardInBuffer();
}
catch (TimeoutException te)
{
Debug.WriteLine($"Timeout Exception encountered while listening for requests - {te.Message}");
//Debug.WriteLine($"Timeout Exception encountered while listening for requests - {te.Message}");
SerialTransport.DiscardInBuffer();
}

View File

@ -177,7 +177,7 @@
try
{
Debug.WriteLine(request.ToString());
//Debug.WriteLine(request.ToString());
var eventArgs = new ModbusSlaveRequestEventArgs(request);
ModbusSlaveRequestReceived?.Invoke(this, eventArgs);
@ -252,7 +252,7 @@
break;
default:
string msg = $"Unsupported function code {request.FunctionCode}.";
Debug.WriteLine(msg);
//Debug.WriteLine(msg);
throw new InvalidModbusRequestException(Modbus.IllegalFunction);
}
}

View File

@ -109,7 +109,7 @@
/// </summary>
public override async Task ListenAsync()
{
Debug.WriteLine("Start Modbus Tcp Server.");
//Debug.WriteLine("Start Modbus Tcp Server.");
// TODO: add state {stoped, listening} and check it before starting
Server.Start();
@ -197,7 +197,7 @@
throw new ArgumentException(msg);
}
Debug.WriteLine($"Removed Master {e.EndPoint}");
//Debug.WriteLine($"Removed Master {e.EndPoint}");
}
}
}

View File

@ -47,7 +47,7 @@
/// </summary>
public override async Task ListenAsync()
{
Debug.WriteLine("Start Modbus Udp Server.");
//Debug.WriteLine("Start Modbus Udp Server.");
try
{
@ -57,8 +57,8 @@
IPEndPoint masterEndPoint = receiveResult.RemoteEndPoint;
byte[] frame = receiveResult.Buffer;
Debug.WriteLine($"Read Frame completed {frame.Length} bytes");
Debug.WriteLine($"RX: {string.Join(", ", frame)}");
//Debug.WriteLine($"Read Frame completed {frame.Length} bytes");
//Debug.WriteLine($"RX: {string.Join(", ", frame)}");
IModbusMessage request =
ModbusMessageFactory.CreateModbusRequest(frame.Slice(6, frame.Length - 6).ToArray());
@ -70,7 +70,7 @@
// write response
byte[] responseFrame = Transport.BuildMessageFrame(response);
Debug.WriteLine($"TX: {string.Join(", ", responseFrame)}");
//Debug.WriteLine($"TX: {string.Join(", ", responseFrame)}");
await _udpClient.SendAsync(responseFrame, responseFrame.Length, masterEndPoint).ConfigureAwait(false);
}
}

View File

@ -57,7 +57,7 @@
// convert hex to bytes
byte[] frame = ModbusUtility.HexToBytes(frameHex);
Debug.WriteLine($"RX: {string.Join(", ", frame)}");
//Debug.WriteLine($"RX: {string.Join(", ", frame)}");
if (frame.Length < 3)
{

View File

@ -43,9 +43,9 @@
numBytesRead += bRead;
}
Debug.WriteLine($"MBAP header: {string.Join(", ", mbapHeader)}");
//Debug.WriteLine($"MBAP header: {string.Join(", ", mbapHeader)}");
var frameLength = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt16(mbapHeader, 4));
Debug.WriteLine($"{frameLength} bytes in PDU.");
//Debug.WriteLine($"{frameLength} bytes in PDU.");
// read message
var messageFrame = new byte[frameLength];
@ -63,9 +63,9 @@
numBytesRead += bRead;
}
Debug.WriteLine($"PDU: {frameLength}");
//Debug.WriteLine($"PDU: {frameLength}");
var frame = mbapHeader.Concat(messageFrame).ToArray();
Debug.WriteLine($"RX: {string.Join(", ", frame)}");
//Debug.WriteLine($"RX: {string.Join(", ", frame)}");
return frame;
}
@ -126,7 +126,7 @@
{
message.TransactionId = GetNewTransactionId();
byte[] frame = BuildMessageFrame(message);
Debug.WriteLine($"TX: {string.Join(", ", frame)}");
//Debug.WriteLine($"TX: {string.Join(", ", frame)}");
StreamResource.Write(frame, 0, frame.Length);
}

View File

@ -46,7 +46,7 @@
break;
default:
string msg = $"Function code {functionCode} not supported.";
Debug.WriteLine(msg);
//Debug.WriteLine(msg);
throw new NotImplementedException(msg);
}
@ -81,7 +81,7 @@
break;
default:
string msg = $"Function code {functionCode} not supported.";
Debug.WriteLine(msg);
//Debug.WriteLine(msg);
throw new NotImplementedException(msg);
}
@ -124,7 +124,7 @@
byte[] frameStart = Read(ResponseFrameStartLength);
byte[] frameEnd = Read(ResponseBytesToRead(frameStart));
byte[] frame = Enumerable.Concat(frameStart, frameEnd).ToArray();
Debug.WriteLine($"RX: {string.Join(", ", frame)}");
//Debug.WriteLine($"RX: {string.Join(", ", frame)}");
return CreateResponse<T>(frame);
}
@ -134,7 +134,7 @@
byte[] frameStart = Read(RequestFrameStartLength);
byte[] frameEnd = Read(RequestBytesToRead(frameStart));
byte[] frame = Enumerable.Concat(frameStart, frameEnd).ToArray();
Debug.WriteLine($"RX: {string.Join(", ", frame)}");
//Debug.WriteLine($"RX: {string.Join(", ", frame)}");
return frame;
}

View File

@ -38,7 +38,7 @@
DiscardInBuffer();
byte[] frame = BuildMessageFrame(message);
Debug.WriteLine($"TX: {string.Join(", ", frame)}");
//Debug.WriteLine($"TX: {string.Join(", ", frame)}");
StreamResource.Write(frame, 0, frame.Length);
}
@ -50,7 +50,7 @@
if (CheckFrame && !ChecksumsMatch(response, frame))
{
string msg = $"Checksums failed to match {string.Join(", ", response.MessageFrame)} != {string.Join(", ", frame)}";
Debug.WriteLine(msg);
//Debug.WriteLine(msg);
throw new IOException(msg);
}

View File

@ -142,7 +142,7 @@
if (readAgain)
{
Debug.WriteLine($"Received ACKNOWLEDGE slave exception response, waiting {_waitToRetryMilliseconds} milliseconds and retrying to read response.");
//Debug.WriteLine($"Received ACKNOWLEDGE slave exception response, waiting {_waitToRetryMilliseconds} milliseconds and retrying to read response.");
Sleep(WaitToRetryMilliseconds);
}
else
@ -173,7 +173,7 @@
throw;
}
Debug.WriteLine($"Received SLAVE_DEVICE_BUSY exception response, waiting {_waitToRetryMilliseconds} milliseconds and resubmitting request.");
//Debug.WriteLine($"Received SLAVE_DEVICE_BUSY exception response, waiting {_waitToRetryMilliseconds} milliseconds and resubmitting request.");
Sleep(WaitToRetryMilliseconds);
}
catch (Exception e)
@ -183,7 +183,7 @@
e is TimeoutException ||
e is IOException)
{
Debug.WriteLine($"{e.GetType().Name}, {(_retries - attempt + 1)} retries remaining - {e}");
//Debug.WriteLine($"{e.GetType().Name}, {(_retries - attempt + 1)} retries remaining - {e}");
if (attempt++ > _retries)
{

View File

@ -77,6 +77,7 @@ namespace Plugin
//Message: {"device": "Device A", "data": {"attribute1": "value1", "attribute2": 42}}
subTopics.Add(new MqttTopicFilterBuilder().WithTopic("v1/gateway/attributese").WithExactlyOnceQoS().Build());
break;
case IoTPlatformType.IotDB:
case IoTPlatformType.IoTSharp:
subTopics.Add(new MqttTopicFilterBuilder().WithTopic("devices/+/rpc/request/+/+").WithExactlyOnceQoS().Build());
subTopics.Add(new MqttTopicFilterBuilder().WithTopic("devices/+/attributes/update").WithExactlyOnceQoS().Build());
@ -230,13 +231,16 @@ namespace Plugin
if (!string.IsNullOrEmpty(rpcMethodName) && !string.IsNullOrEmpty(rpcDeviceName) &&
!string.IsNullOrEmpty(rpcRequestId))
{
OnExcRpc(Client, new RpcRequest()
Task.Run(() =>
{
Method = rpcMethodName,
DeviceName = rpcDeviceName,
RequestId = rpcRequestId,
Params = JsonConvert.DeserializeObject<Dictionary<string, object>>(e.ApplicationMessage
.ConvertPayloadToString())
OnExcRpc(Client, new RpcRequest()
{
Method = rpcMethodName,
DeviceName = rpcDeviceName,
RequestId = rpcRequestId,
Params = JsonConvert.DeserializeObject<Dictionary<string, object>>(e.ApplicationMessage
.ConvertPayloadToString())
});
});
}
}