104 lines
3.3 KiB
C#
104 lines
3.3 KiB
C#
using HslCommunication;
|
||||
|
|
using HslCommunication.BasicFramework;
|
|||
|
|
using HslCommunication.Core;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using System.Windows.Forms;
|
|||
|
|
|
|||
|
|
namespace PLCCommunication
|
|||
|
|
{
|
|||
|
|
public class DemoUtils
|
|||
|
|
{
|
|||
|
|
public static readonly string IpAddressInputWrong = "IpAddress input wrong";
|
|||
|
|
|
|||
|
|
public static readonly string PortInputWrong = "Port input wrong";
|
|||
|
|
|
|||
|
|
public static readonly string SlotInputWrong = "Slot input wrong";
|
|||
|
|
|
|||
|
|
public static readonly string BaudRateInputWrong = "Baud rate input wrong";
|
|||
|
|
|
|||
|
|
public static readonly string DataBitsInputWrong = "Data bit input wrong";
|
|||
|
|
|
|||
|
|
public static readonly string StopBitInputWrong = "Stop bit input wrong";
|
|||
|
|
|
|||
|
|
public static void ReadResultRender<T>(OperateResult<T> result, string address, TextBox textBox)
|
|||
|
|
{
|
|||
|
|
if (result.IsSuccess)
|
|||
|
|
{
|
|||
|
|
textBox.AppendText(DateTime.Now.ToString("[HH:mm:ss] ") + $"[{address}] {result.Content}{Environment.NewLine}");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
MessageBox.Show(DateTime.Now.ToString("[HH:mm:ss] ") + "[" + address + "] Read Failed " + Environment.NewLine + "Reason:" + result.ToMessageShowString());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void ReadResultRender<T>(OperateResult<T[]> result, string address, TextBox textBox)
|
|||
|
|
{
|
|||
|
|
string resTemp = string.Empty;
|
|||
|
|
if (result.IsSuccess)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < result.Content.Length; i++)
|
|||
|
|
{
|
|||
|
|
resTemp = string.Concat(resTemp, result.Content[i], ",");
|
|||
|
|
}
|
|||
|
|
string Result = resTemp.Substring(0, resTemp.Length - 1);
|
|||
|
|
textBox.AppendText(DateTime.Now.ToString("[HH:mm:ss] ") + "[" + address + "] " + Result + Environment.NewLine);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
MessageBox.Show(DateTime.Now.ToString("[HH:mm:ss] ") + "[" + address + "] Read Failed " + Environment.NewLine + "Reason:" + result.ToMessageShowString());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void WriteResultRender(OperateResult result, string address)
|
|||
|
|
{
|
|||
|
|
if (result.IsSuccess)
|
|||
|
|
{
|
|||
|
|
MessageBox.Show(DateTime.Now.ToString("[HH:mm:ss] ") + "[" + address + "] Write Success");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
MessageBox.Show(DateTime.Now.ToString("[HH:mm:ss] ") + "[" + address + "] Write Failed " + Environment.NewLine + " Reason:" + result.ToMessageShowString());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void WriteResultRender(Func<OperateResult> write, string address)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
OperateResult result = write();
|
|||
|
|
if (result.IsSuccess)
|
|||
|
|
{
|
|||
|
|
MessageBox.Show(DateTime.Now.ToString("[HH:mm:ss] ") + "[" + address + "] Write Success");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
MessageBox.Show(DateTime.Now.ToString("[HH:mm:ss] ") + "[" + address + "] Write Failed " + Environment.NewLine + " Reason:" + result.ToMessageShowString());
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
MessageBox.Show("Data for writting is not corrent: " + ex.Message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void BulkReadRenderResult(IReadWriteNet readWrite, TextBox addTextBox, TextBox lengthTextBox, TextBox resultTextBox)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
OperateResult<byte[]> read = readWrite.Read(addTextBox.Text, ushort.Parse(lengthTextBox.Text));
|
|||
|
|
if (read.IsSuccess)
|
|||
|
|
{
|
|||
|
|
resultTextBox.Text = "Result:" + SoftBasic.ByteToHexString(read.Content);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
MessageBox.Show("Read Failed:" + read.ToMessageShowString());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
MessageBox.Show("Read Failed:" + ex.Message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|