using HslCommunication; using HslCommunication.Core; using HslCommunication.ModBus; using HslCommunication.Profinet.Delta.Helper; using HslCommunication.Profinet.Siemens; using Seyounth.Auto.Plc.Helper; using Seyounth.Extensions.Plc; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using static System.Collections.Specialized.BitVector32; namespace Seyounth.Auto.Plc.Business { public class SycModbusTcpNet : IPlc { public string Host { get; } public int Port { get; } public byte station { get; } public bool IsConnected { get; } public int ConnectTimeOut = 1500; public DataFormat dataFormat = DataFormat.ABCD; private ModbusTcpNet Melse = new ModbusTcpNet(); /// /// 构造函数 /// public SycModbusTcpNet(string Ip, int Port, byte station = 1, DataFormat dataFormat = DataFormat.ABCD) { this.Host = Ip; this.Port = Port; this.station = station; this.dataFormat = dataFormat; } public async Task ConnectAsync() { if (string.IsNullOrWhiteSpace(Host)) return; //Melse.SetPersistentConnection(); Melse.ConnectTimeOut = ConnectTimeOut; Melse.AddressStartWithZero = true; Melse = new ModbusTcpNet(Host, Port, station); await Melse.ConnectServerAsync(); Melse.DataFormat = dataFormat; } public async Task DisconnectAsync() { await Melse.ConnectCloseAsync(); Melse.Dispose(); } public async Task ReadAsync(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(operateResult.Content, Melse, i * ReadByteLength); result[i] = item; } return result; } public async Task 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 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(string address, params T[] values) { if (values is short[]) await Melse.WriteAsync(address, values as short[]); else if (values is int[]) await Melse.WriteAsync(address, values as int[]); } 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); } } }