mirror of
https://github.com/zhaopeiym/IoTClient
synced 2025-10-26 22:15:44 +08:00
添加请求报文 和 响应报文的监控
This commit is contained in:
parent
5a44588ba4
commit
fc5dc3545d
19
IoTClient.Demo/ModBusTcpForm.Designer.cs
generated
19
IoTClient.Demo/ModBusTcpForm.Designer.cs
generated
|
|
@ -57,6 +57,7 @@
|
|||
this.groupBox1 = new System.Windows.Forms.GroupBox();
|
||||
this.groupBox2 = new System.Windows.Forms.GroupBox();
|
||||
this.groupBox3 = new System.Windows.Forms.GroupBox();
|
||||
this.checkBox1 = new System.Windows.Forms.CheckBox();
|
||||
this.groupBox1.SuspendLayout();
|
||||
this.groupBox2.SuspendLayout();
|
||||
this.groupBox3.SuspendLayout();
|
||||
|
|
@ -156,7 +157,7 @@
|
|||
//
|
||||
// txt_value
|
||||
//
|
||||
this.txt_value.Location = new System.Drawing.Point(447, 16);
|
||||
this.txt_value.Location = new System.Drawing.Point(359, 15);
|
||||
this.txt_value.Name = "txt_value";
|
||||
this.txt_value.Size = new System.Drawing.Size(100, 21);
|
||||
this.txt_value.TabIndex = 12;
|
||||
|
|
@ -164,7 +165,7 @@
|
|||
// label4
|
||||
//
|
||||
this.label4.AutoSize = true;
|
||||
this.label4.Location = new System.Drawing.Point(400, 22);
|
||||
this.label4.Location = new System.Drawing.Point(312, 21);
|
||||
this.label4.Name = "label4";
|
||||
this.label4.Size = new System.Drawing.Size(29, 12);
|
||||
this.label4.TabIndex = 11;
|
||||
|
|
@ -172,7 +173,7 @@
|
|||
//
|
||||
// button4
|
||||
//
|
||||
this.button4.Location = new System.Drawing.Point(567, 15);
|
||||
this.button4.Location = new System.Drawing.Point(479, 14);
|
||||
this.button4.Name = "button4";
|
||||
this.button4.Size = new System.Drawing.Size(75, 23);
|
||||
this.button4.TabIndex = 10;
|
||||
|
|
@ -321,6 +322,7 @@
|
|||
//
|
||||
// groupBox3
|
||||
//
|
||||
this.groupBox3.Controls.Add(this.checkBox1);
|
||||
this.groupBox3.Controls.Add(this.button3);
|
||||
this.groupBox3.Controls.Add(this.label3);
|
||||
this.groupBox3.Controls.Add(this.txt_address);
|
||||
|
|
@ -333,6 +335,16 @@
|
|||
this.groupBox3.TabIndex = 26;
|
||||
this.groupBox3.TabStop = false;
|
||||
//
|
||||
// checkBox1
|
||||
//
|
||||
this.checkBox1.AutoSize = true;
|
||||
this.checkBox1.Location = new System.Drawing.Point(575, 17);
|
||||
this.checkBox1.Name = "checkBox1";
|
||||
this.checkBox1.Size = new System.Drawing.Size(72, 16);
|
||||
this.checkBox1.TabIndex = 13;
|
||||
this.checkBox1.Text = "显示报文";
|
||||
this.checkBox1.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// ModBusTcpForm
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
|
||||
|
|
@ -386,6 +398,7 @@
|
|||
private System.Windows.Forms.GroupBox groupBox1;
|
||||
private System.Windows.Forms.GroupBox groupBox2;
|
||||
private System.Windows.Forms.GroupBox groupBox3;
|
||||
private System.Windows.Forms.CheckBox checkBox1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -113,6 +113,11 @@ namespace IoTClient.Demo
|
|||
txt_content.AppendText($"[{DateTime.Now.ToLongTimeString()}][读取 {txt_address.Text?.Trim()} 成功]:{result.Value}\r\n");
|
||||
else
|
||||
txt_content.AppendText($"[{DateTime.Now.ToLongTimeString()}][读取 {txt_address.Text?.Trim()} 失败]:{result.Err}\r\n");
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
txt_content.AppendText($"[请求报文]{result.Requst}\r\n");
|
||||
txt_content.AppendText($"[响应报文]{result.Response}\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
private void button4_Click(object sender, EventArgs e)
|
||||
|
|
@ -143,7 +148,7 @@ namespace IoTClient.Demo
|
|||
MessageBox.Show("请输入 True 或 False");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
result = client.Write(txt_address.Text, coil, stationNumber);
|
||||
}
|
||||
else if (rd_short.Checked)
|
||||
|
|
@ -181,9 +186,14 @@ namespace IoTClient.Demo
|
|||
|
||||
|
||||
if (result.IsSucceed)
|
||||
txt_content.AppendText($"[{DateTime.Now.ToLongTimeString()}][写入 {txt_address.Text?.Trim()} 成功]:OK\r\n");
|
||||
txt_content.AppendText($"[{DateTime.Now.ToLongTimeString()}][写入 {txt_address.Text?.Trim()} 成功]:{txt_value.Text?.Trim()} OK\r\n");
|
||||
else
|
||||
txt_content.AppendText($"[{DateTime.Now.ToLongTimeString()}][写入 {txt_address.Text?.Trim()} 失败]:{result.Err}\r\n");
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
txt_content.AppendText($"[请求报文]{result.Requst}\r\n");
|
||||
txt_content.AppendText($"[响应报文]{result.Response}\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
private void ModBusTcp_Load(object sender, EventArgs e)
|
||||
|
|
|
|||
|
|
@ -69,42 +69,45 @@ namespace IoTClient.Clients.ModBus
|
|||
public Result<byte[]> Read(string address, byte stationNumber = 1, byte functionCode = 3, ushort readLength = 1)
|
||||
{
|
||||
ConnectManager();
|
||||
var readResult = new Result<byte[]>();
|
||||
var result = new Result<byte[]>();
|
||||
try
|
||||
{
|
||||
//1 获取命令(组装报文)
|
||||
byte[] command = GetReadCommand(address, stationNumber, functionCode, readLength);
|
||||
result.Requst = string.Join(" ", command.Select(t => t.ToString("X2")));
|
||||
//2 发送命令
|
||||
socket.Send(command);
|
||||
//3 获取响应报文
|
||||
var headBytes = SocketRead(socket, 8);
|
||||
int length = headBytes[4] * 256 + headBytes[5] - 2;
|
||||
var result = SocketRead(socket, length);
|
||||
var dataBytes = SocketRead(socket, length);
|
||||
|
||||
byte[] resultBuffer = new byte[result.Length - 1];
|
||||
Array.Copy(result, 1, resultBuffer, 0, resultBuffer.Length);
|
||||
byte[] resultBuffer = new byte[dataBytes.Length - 1];
|
||||
Array.Copy(dataBytes, 1, resultBuffer, 0, resultBuffer.Length);
|
||||
|
||||
result.Response = string.Join(" ", headBytes.Concat(dataBytes).Select(t => t.ToString("X2")));
|
||||
//4 获取响应报文数据(字节数组形式)
|
||||
readResult.Value = resultBuffer.Reverse().ToArray();
|
||||
result.Value = resultBuffer.Reverse().ToArray();
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
readResult.IsSucceed = false;
|
||||
result.IsSucceed = false;
|
||||
if (ex.SocketErrorCode == SocketError.TimedOut)
|
||||
{
|
||||
readResult.Err = "连接超时";
|
||||
readResult.ErrList.Add("连接超时");
|
||||
result.Err = "连接超时";
|
||||
result.ErrList.Add("连接超时");
|
||||
}
|
||||
else
|
||||
{
|
||||
readResult.Err = ex.Message;
|
||||
readResult.ErrList.Add(ex.Message);
|
||||
result.Err = ex.Message;
|
||||
result.ErrList.Add(ex.Message);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (isAutoOpen.Value) Dispose();
|
||||
}
|
||||
return readResult;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -122,6 +125,8 @@ namespace IoTClient.Clients.ModBus
|
|||
IsSucceed = readResut.IsSucceed,
|
||||
Err = readResut.Err,
|
||||
ErrList = readResut.ErrList,
|
||||
Requst = readResut.Requst,
|
||||
Response = readResut.Response,
|
||||
};
|
||||
if (result.IsSucceed)
|
||||
result.Value = BitConverter.ToInt16(readResut.Value, 0);
|
||||
|
|
@ -143,6 +148,8 @@ namespace IoTClient.Clients.ModBus
|
|||
IsSucceed = readResut.IsSucceed,
|
||||
Err = readResut.Err,
|
||||
ErrList = readResut.ErrList,
|
||||
Requst = readResut.Requst,
|
||||
Response = readResut.Response,
|
||||
};
|
||||
if (result.IsSucceed)
|
||||
result.Value = BitConverter.ToUInt16(readResut.Value, 0);
|
||||
|
|
@ -164,6 +171,8 @@ namespace IoTClient.Clients.ModBus
|
|||
IsSucceed = readResut.IsSucceed,
|
||||
Err = readResut.Err,
|
||||
ErrList = readResut.ErrList,
|
||||
Requst = readResut.Requst,
|
||||
Response = readResut.Response,
|
||||
};
|
||||
if (result.IsSucceed)
|
||||
result.Value = BitConverter.ToInt32(readResut.Value, 0);
|
||||
|
|
@ -185,6 +194,8 @@ namespace IoTClient.Clients.ModBus
|
|||
IsSucceed = readResut.IsSucceed,
|
||||
Err = readResut.Err,
|
||||
ErrList = readResut.ErrList,
|
||||
Requst = readResut.Requst,
|
||||
Response = readResut.Response,
|
||||
};
|
||||
if (result.IsSucceed)
|
||||
result.Value = BitConverter.ToUInt32(readResut.Value, 0);
|
||||
|
|
@ -206,6 +217,8 @@ namespace IoTClient.Clients.ModBus
|
|||
IsSucceed = readResut.IsSucceed,
|
||||
Err = readResut.Err,
|
||||
ErrList = readResut.ErrList,
|
||||
Requst = readResut.Requst,
|
||||
Response = readResut.Response,
|
||||
};
|
||||
if (result.IsSucceed)
|
||||
result.Value = BitConverter.ToInt64(readResut.Value, 0);
|
||||
|
|
@ -227,6 +240,8 @@ namespace IoTClient.Clients.ModBus
|
|||
IsSucceed = readResut.IsSucceed,
|
||||
Err = readResut.Err,
|
||||
ErrList = readResut.ErrList,
|
||||
Requst = readResut.Requst,
|
||||
Response = readResut.Response,
|
||||
};
|
||||
if (result.IsSucceed)
|
||||
result.Value = BitConverter.ToUInt64(readResut.Value, 0);
|
||||
|
|
@ -248,6 +263,8 @@ namespace IoTClient.Clients.ModBus
|
|||
IsSucceed = readResut.IsSucceed,
|
||||
Err = readResut.Err,
|
||||
ErrList = readResut.ErrList,
|
||||
Requst = readResut.Requst,
|
||||
Response = readResut.Response,
|
||||
};
|
||||
if (result.IsSucceed)
|
||||
result.Value = BitConverter.ToSingle(readResut.Value, 0);
|
||||
|
|
@ -269,6 +286,8 @@ namespace IoTClient.Clients.ModBus
|
|||
IsSucceed = readResut.IsSucceed,
|
||||
Err = readResut.Err,
|
||||
ErrList = readResut.ErrList,
|
||||
Requst = readResut.Requst,
|
||||
Response = readResut.Response,
|
||||
};
|
||||
if (result.IsSucceed)
|
||||
result.Value = BitConverter.ToDouble(readResut.Value, 0);
|
||||
|
|
@ -290,6 +309,8 @@ namespace IoTClient.Clients.ModBus
|
|||
IsSucceed = readResut.IsSucceed,
|
||||
Err = readResut.Err,
|
||||
ErrList = readResut.ErrList,
|
||||
Requst = readResut.Requst,
|
||||
Response = readResut.Response,
|
||||
};
|
||||
if (result.IsSucceed)
|
||||
result.Value = BitConverter.ToBoolean(readResut.Value, 0);
|
||||
|
|
@ -357,11 +378,12 @@ namespace IoTClient.Clients.ModBus
|
|||
{
|
||||
var command = GetWriteCommand(address, values, stationNumber, functionCode);
|
||||
socket.Send(command);
|
||||
|
||||
result.Requst = string.Join(" ", command.Select(t => t.ToString("X2")));
|
||||
//获取响应报文
|
||||
var headBytes = SocketRead(socket, 8);
|
||||
int length = headBytes[4] * 256 + headBytes[5] - 2;
|
||||
SocketRead(socket, length);
|
||||
var dataBytes = SocketRead(socket, length);
|
||||
result.Response = string.Join(" ", headBytes.Concat(dataBytes).Select(t => t.ToString("X2")));
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,37 +5,6 @@ namespace IoTClient.Core
|
|||
/// <summary>
|
||||
/// 请求结果
|
||||
/// </summary>
|
||||
public class Result<T>
|
||||
{
|
||||
public Result()
|
||||
{
|
||||
}
|
||||
|
||||
public Result(T data)
|
||||
{
|
||||
Value = data;
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否成功
|
||||
/// </summary>
|
||||
public bool IsSucceed { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 异常消息
|
||||
/// </summary>
|
||||
public string Err { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 异常集合
|
||||
/// </summary>
|
||||
public List<string> ErrList { get; set; } = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// 数据结果
|
||||
/// </summary>
|
||||
public T Value { get; set; }
|
||||
}
|
||||
|
||||
public class Result
|
||||
{
|
||||
public Result()
|
||||
|
|
@ -56,5 +25,37 @@ namespace IoTClient.Core
|
|||
/// 异常集合
|
||||
/// </summary>
|
||||
public List<string> ErrList { get; set; } = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// 请求报文
|
||||
/// </summary>
|
||||
public string Requst { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 响应报文
|
||||
/// </summary>
|
||||
public string Response { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 请求结果
|
||||
/// </summary>
|
||||
public class Result<T> : Result
|
||||
{
|
||||
public Result()
|
||||
{
|
||||
}
|
||||
|
||||
public Result(T data)
|
||||
{
|
||||
Value = data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据结果
|
||||
/// </summary>
|
||||
public T Value { get; set; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -143,7 +143,8 @@ namespace IoTServer.Servers.ModBus
|
|||
catch (SocketException ex)
|
||||
{
|
||||
//todo
|
||||
if (ex.SocketErrorCode != SocketError.ConnectionRefused)
|
||||
if (ex.SocketErrorCode != SocketError.ConnectionRefused ||
|
||||
ex.SocketErrorCode != SocketError.ConnectionReset)
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user