117 lines
3.7 KiB
C#
117 lines
3.7 KiB
C#
using HslCommunication.Profinet.Melsec;
|
|
using HslCommunication;
|
|
using System.Text;
|
|
using System.Net;
|
|
using Seyounth.Extensions.Plc;
|
|
using Seyounth.Auto.Plc.Helper;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading.Tasks.Dataflow;
|
|
|
|
namespace Seyounth.Auto.Plc.Business
|
|
{
|
|
public class SycMelsecMcNet : IPlc
|
|
{
|
|
/// <summary>
|
|
/// PLC读取对象
|
|
/// </summary>
|
|
public MelsecMcNet Melse;
|
|
|
|
public string Host { get; }
|
|
|
|
public int Port { get; }
|
|
|
|
public bool IsConnected { get; }
|
|
public int ConnectTimeOut = 1500;
|
|
|
|
// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
public SycMelsecMcNet(string Ip, int Port)
|
|
{
|
|
this.Host = Ip;
|
|
this.Port = Port;
|
|
}
|
|
|
|
public async Task ConnectAsync()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(Host)) return;
|
|
if (Host == default || Port == default) return;
|
|
|
|
Melse = new MelsecMcNet(Host, Port);
|
|
//Melse.SetPersistentConnection();
|
|
Melse.ConnectTimeOut = ConnectTimeOut;
|
|
await Melse.ConnectServerAsync();
|
|
return;
|
|
}
|
|
|
|
public async Task DisconnectAsync()
|
|
{
|
|
await Melse.ConnectCloseAsync();
|
|
Melse.Dispose();
|
|
}
|
|
|
|
public async Task<T[]> ReadAsync<T>(string address, int count)
|
|
{
|
|
T[] result = new T[count];
|
|
var structName = typeof(T).ToString();
|
|
var ReadByteLength = AnalysisHelper.TructToByteCount(structName: structName);
|
|
var ReadRegLength = AnalysisHelper.TructToRegCount(structName: structName);
|
|
var operateResult = await Melse.ReadAsync(address, (ushort)(count * ReadRegLength));
|
|
|
|
if (!operateResult.IsSuccess)
|
|
{
|
|
throw new Exception(operateResult.Message);
|
|
}
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
T item = AnalysisHelper.ConvertBytes<T>(operateResult.Content, Melse, i * ReadByteLength);
|
|
result[i] = item;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public async Task<string> ReadStringAsync(string address, int length)
|
|
{
|
|
var operateResult = await Melse.ReadAsync(address, (ushort)length);
|
|
if (operateResult != null && operateResult.IsSuccess)
|
|
{
|
|
return Encoding.ASCII.GetString(operateResult.Content).Replace("\0", "").Trim();
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
public async Task<string> ReadStringAsync(string address, int length, Encoding encoding)
|
|
{
|
|
var operateResult = await Melse.ReadAsync(address, (ushort)length);
|
|
if (operateResult != null && operateResult.IsSuccess)
|
|
{
|
|
return encoding.GetString(operateResult.Content).Replace("\0", "").Trim();
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
public async Task WriteAsync<T>(string address, params T[] values)
|
|
{
|
|
var type = typeof(T);
|
|
var buffer = new byte[values.Length * Marshal.SizeOf(type)];
|
|
if (type.IsPrimitive)
|
|
{
|
|
Buffer.BlockCopy(values, 0, buffer, 0, buffer.Length);
|
|
}
|
|
//Encoding.ASCII.GetBytes();
|
|
await Melse.WriteAsync(address, buffer);
|
|
}
|
|
|
|
public async Task WriteStringAsync(string address, string value)
|
|
{
|
|
byte[] bytes = Encoding.ASCII.GetBytes(value);
|
|
await Melse.WriteAsync(address, bytes);
|
|
}
|
|
|
|
public async Task WriteStringAsync(string address, string value, Encoding encoding)
|
|
{
|
|
byte[] bytes = encoding.GetBytes(value);
|
|
await Melse.WriteAsync(address, bytes);
|
|
}
|
|
}
|
|
} |