hisence-yolo-detection/HisenceYoloDetection/VirtualPLCTCPDriver.cs

213 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HslCommunication;
using HslCommunication.Profinet.Melsec;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using static HisenceYoloDetection.MainForm;
using static OpenCvSharp.FileStorage;
public class MelsecPLCTCPDriver
{
// private MelsecMcNet melsecMc = new MelsecMcNet();
// private HslCommunication.ModBus.ModbusTcpNet melsecMc = new HslCommunication.ModBus.ModbusTcpNet();
HslCommunication.Profinet.Melsec.MelsecMcServer melsecMc = new HslCommunication.Profinet.Melsec.MelsecMcServer();
#region PLCBase
public int ReadInt(string address)
{
try
{
// 读取Int变量
var result = melsecMc.ReadInt16("D"+address);
if (result.IsSuccess)
{
return result.Content;
}
else
{
// LogAsync(DateTime.Now, LogLevel.Error, $"{Name}未读取到数据:" + "地址:" + address + ";提示:" + result.Message);
return -1;
}
}
catch (Exception ex)
{
// LogAsync(DateTime.Now, LogLevel.Exception, $"{Name}读取异常:" + ex.GetExceptionMessage());
return -1;
}
}
/// <summary>
/// 写单独地址
/// </summary>
/// <param name="address">地址</param>
/// <param name="writeValue"></param>
/// <param name="waitForReply"></param>
public void WriteInt(string address, int writeValue, bool waitForReply = true)
{
if (string.IsNullOrEmpty(address))
{
// LogAsync(DateTime.Now, LogLevel.Error, $"{Name} 数据写入参数不能为空!");
return;
}
int repeatTime = 3;
Stopwatch sw = new Stopwatch();
do
{
try
{
var result = melsecMc.Write("D"+address, writeValue);
if (result.IsSuccess)
{
repeatTime = 0;
}
}
catch (Exception ex)
{
if (repeatTime < 0)
{
// LogAsync(DateTime.Now, LogLevel.Exception, $"{Name}数据写入异常:{ex.GetExceptionMessage()}");
}
}
} while (repeatTime > 0);
sw.Stop();
// LogAsync(DateTime.Now, LogLevel.Assist, $"{Name} {address},写入 {writeValue} 完成,耗时:{sw.ElapsedMilliseconds} ms");
}
#endregion
#region DeviceBase
public void Start()
{
// IConfig.ip
// 如果网络不太理想,配置了两个端口,一个有问题,立即切换另一个的话,可以配置如下的代码
// melsecMc.GetPipeSocket().SetMultiPorts(new int[] { 6000, 6001 });
//melsecMc = new HslCommunication.ModBus.ModbusTcpNet();
//melsecMc.IpAddress = "192.168.3.39";
//melsecMc.Port = 502;
//melsecMc.ConnectTimeOut = 10000; // 连接超时,单位毫秒
//melsecMc.ReceiveTimeOut = 5000; // 接收超时,单位毫秒
//melsecMc.Station = 1;
//melsecMc.AddressStartWithZero = true;
//melsecMc.IsStringReverse = false;
//melsecMc.DataFormat = HslCommunication.Core.DataFormat.CDAB;
//se("01:00:00");
// melsecMc.EnableIPv6 = false;
melsecMc.ServerStart(6000);
// melsecMc.ServerStart(6000);
// 连接对象
//OperateResult connect = melsecMc.ConnectServer();
CurrentState = true;
Task.Run(() =>
{
HeartbeatMonitor();
});
//else
//{
// // LogAsync(DateTime.Now, LogLevel.Exception, $"{Name}已开启异常:{connect.Message}");
//}
}
protected void Stop()
{
try
{
// 断开连接
// melsecMc.RemoteStop();
// OmronUDPNet.Stop();
}
catch (Exception ex)
{
// LogAsync(DateTime.Now, LogLevel.Exception, $"{Name}关闭异常:{ex.GetExceptionMessage()}");
}
}
#endregion
#region IMonitor
readonly byte[] scanBuffer = new byte[2048];
public List<int> GetMonitorValues(int startAddress, int length)
{
List<int> res = new List<int>();
// var result = melsecMc.ReadUInt16(startAddress.ToString(), (ushort)length);
// var result = melsecMc.ReadInt32("D" + startAddress, (ushort)length);
var result = melsecMc.ReadInt32("D" + startAddress, (ushort)length);
if (result.IsSuccess)
{
res = new List<int>(result.Content);
}
else
{
//LogAsync(DateTime.Now, LogLevel.Error, $"{Name}未读取到数据:" + "地址W" + startAddress + ";长度:" + length + ";提示:" + result.Message);
}
return res;
}
#endregion
int HeartbeatCycle = 2;
string HeartbeatAddress = "D800";
bool CurrentState = false;
public event Action<int> Heartbeat;
public async void HeartbeatMonitor()
{
if (HeartbeatCycle <= 0)
{
return;
}
await Task.Run(async () =>
{
if (!string.IsNullOrEmpty(HeartbeatAddress)
&& HeartbeatCycle > 0)
{
while (CurrentState != false)
{
if (HeartbeatCycle <= 0)
{
return;
}
try
{
await Task.Delay(HeartbeatCycle * 1000);
var result = melsecMc.Write(HeartbeatAddress, 1);
if (!result.IsSuccess)
{
Heartbeat?.Invoke(1);
//LogAsync(DateTime.Now, LogLevel.Error, $"{this.Name} 心跳监听失败");
}
}
catch (Exception ex)
{
}
}
}
});
}
}