提交
This commit is contained in:
@ -11,6 +11,7 @@ using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
using DH.Commons.Base;
|
||||
using DH.Commons.Enums;
|
||||
using DH.Commons.Helper;
|
||||
using DH.Commons.Models;
|
||||
using HslCommunication;
|
||||
using HslCommunication.Enthernet;
|
||||
@ -37,6 +38,8 @@ namespace DH.Devices.PLC
|
||||
public event Action<LogMsg> OnLog;
|
||||
private XinJETcpNet TcpNet = new XinJETcpNet();
|
||||
|
||||
private TaskFactory _taskFac = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.LongRunning);
|
||||
|
||||
public override bool PLCConnect()
|
||||
{
|
||||
try
|
||||
@ -82,7 +85,7 @@ namespace DH.Devices.PLC
|
||||
|
||||
|
||||
}
|
||||
catch(Exception ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
Connected = false;
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"{IP}:{Port}PLC连接失败!失败原因:{ex.ToString()}");
|
||||
@ -182,7 +185,7 @@ namespace DH.Devices.PLC
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC未连接,地址{address}");
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -258,7 +261,8 @@ namespace DH.Devices.PLC
|
||||
public override bool ReadBool(string address)
|
||||
{
|
||||
try
|
||||
{ if (Connected)
|
||||
{
|
||||
if (Connected)
|
||||
{
|
||||
// 读取Bool变量
|
||||
var result = TcpNet.ReadBool(address);
|
||||
@ -343,7 +347,7 @@ namespace DH.Devices.PLC
|
||||
|
||||
public override bool WriteUInt16(string address, UInt16 writeValue, bool waitForReply = true)
|
||||
{
|
||||
if (Connected)
|
||||
if (Connected)
|
||||
{
|
||||
if (string.IsNullOrEmpty(address))
|
||||
{
|
||||
@ -472,13 +476,13 @@ namespace DH.Devices.PLC
|
||||
{
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 写单独地址 float 值
|
||||
/// </summary>
|
||||
@ -534,7 +538,7 @@ namespace DH.Devices.PLC
|
||||
/// <param name="waitForReply">是否等待回复</param>
|
||||
public override bool WriteBool(string address, bool writeValue, bool waitForReply = true)
|
||||
{
|
||||
if(Connected)
|
||||
if (Connected)
|
||||
{
|
||||
if (string.IsNullOrEmpty(address))
|
||||
{
|
||||
@ -570,17 +574,17 @@ namespace DH.Devices.PLC
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC未连接,地址{address}");
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public override bool PLCDisConnect()
|
||||
{
|
||||
|
||||
|
||||
if (Connected)
|
||||
{
|
||||
|
||||
|
||||
var res = TcpNet.ConnectClose();
|
||||
if (res.IsSuccess)
|
||||
{
|
||||
@ -598,7 +602,7 @@ namespace DH.Devices.PLC
|
||||
|
||||
private void MonitorPieces()
|
||||
{
|
||||
|
||||
|
||||
ThreadStart ts = new ThreadStart(MonitorPiecesImpl);
|
||||
Thread th = new Thread(ts);
|
||||
th.Priority = ThreadPriority.AboveNormal;
|
||||
@ -617,7 +621,9 @@ namespace DH.Devices.PLC
|
||||
/// int,int 轴号 捕获位置
|
||||
/// </summary>
|
||||
public event Action<int, uint> OnNewPieces;
|
||||
|
||||
private System.Threading.Timer timer;
|
||||
private System.Threading.TimerCallback timerCallback;
|
||||
|
||||
|
||||
public void NewPieces(int axisIndex, uint pieceNumber)
|
||||
{
|
||||
@ -631,14 +637,47 @@ namespace DH.Devices.PLC
|
||||
public async Task HeartbeatAsync1()
|
||||
{
|
||||
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "心跳地址");
|
||||
#if false
|
||||
|
||||
Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
|
||||
if (pLCItem == null)
|
||||
return;
|
||||
while (Connected)
|
||||
{
|
||||
|
||||
if (pLCItem == null)
|
||||
return;
|
||||
WriteBool(pLCItem.Address, true);
|
||||
await Task.Delay(900); // 非阻塞,等待1秒
|
||||
try
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"心跳\t");
|
||||
|
||||
WriteBool(pLCItem.Address, true);
|
||||
await Task.Delay(2000); // 非阻塞,等待1秒
|
||||
}
|
||||
catch (Exception ex) { }
|
||||
}
|
||||
#else
|
||||
|
||||
timerCallback = (object? state) =>
|
||||
{
|
||||
timer.Change(2000, 2000);
|
||||
try
|
||||
{
|
||||
|
||||
//WriteBool(pLCItem.Address, true);
|
||||
WriteBool("M31", true);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"心跳:{ex.Message}");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
timer = new System.Threading.Timer(
|
||||
timerCallback, null, 0, 2000);
|
||||
|
||||
timer.Change(2000, 2000);
|
||||
#endif
|
||||
}
|
||||
/// <summary>
|
||||
/// 入料监听
|
||||
@ -647,7 +686,7 @@ namespace DH.Devices.PLC
|
||||
private void MonitorPiecesImpl()
|
||||
{
|
||||
|
||||
PLCItem pLCItem= PLCItemList.FirstOrDefault(u => u.Name == "产品计数");
|
||||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "产品计数");
|
||||
if (pLCItem == null)
|
||||
return;
|
||||
string Count = pLCItem.Address;
|
||||
@ -661,8 +700,8 @@ namespace DH.Devices.PLC
|
||||
Stopwatch sw = new Stopwatch();
|
||||
uint tmpPieceNumber = 0;
|
||||
sw.Start();
|
||||
|
||||
// var ret = TcpNet.ReadUInt16("D1016");
|
||||
|
||||
// var ret = TcpNet.ReadUInt16("D1016");
|
||||
var ret = TcpNet.ReadUInt32(Count);
|
||||
|
||||
sw.Stop();
|
||||
@ -678,13 +717,13 @@ namespace DH.Devices.PLC
|
||||
//LogAsync(DateTime.Now, LogLevel.Information, $"转盘{0}产品入列 {piecesCountDic[0]} size:{sum}");
|
||||
if (tmpPieceNumber != piecesCount + 1)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"入列触发丢失\t{tmpPieceNumber}");
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"入列触发丢失\t{tmpPieceNumber}");
|
||||
// Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")}\t板卡{station}产品入列触发丢失,{piecesCountDic[station]}\t{tmpPieceNumber}");
|
||||
|
||||
}
|
||||
piecesCount = tmpPieceNumber;
|
||||
//NewPieces(ai, piecesCountDic[station]);
|
||||
NewPieces(1, piecesCount);
|
||||
NewPieces(1, piecesCount);
|
||||
sw.Stop();
|
||||
startTime = DateTime.Now;
|
||||
//if (idalarm)
|
||||
@ -695,7 +734,7 @@ namespace DH.Devices.PLC
|
||||
|
||||
}
|
||||
|
||||
Thread.Sleep(1);
|
||||
Thread.Sleep(2);
|
||||
}
|
||||
}
|
||||
|
||||
@ -703,19 +742,19 @@ namespace DH.Devices.PLC
|
||||
{
|
||||
//启用心跳
|
||||
OpenHeartbeat(true);
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"启用心跳");
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"启用心跳");
|
||||
//状态复位
|
||||
StatusReset();
|
||||
//LogAsync(DateTime.Now, LogLevel.Information, $"状态复位");
|
||||
//关闭定位
|
||||
VisionPos(false);
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"关闭定位");
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"关闭定位");
|
||||
//写入流程加载点位配置
|
||||
InitProcessAction();
|
||||
//LogAsync(DateTime.Now, LogLevel.Information, $"写入流程加载点位配置");
|
||||
//计数清零
|
||||
CountToZero();
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"计数清零");
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"计数清零");
|
||||
//停止转盘
|
||||
TurnStart(false);
|
||||
//LogAsync(DateTime.Now, LogLevel.Information, $"停止转盘");
|
||||
@ -725,16 +764,18 @@ namespace DH.Devices.PLC
|
||||
//开启入料监听
|
||||
MonitorPieces();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void StartProcess()
|
||||
{
|
||||
{
|
||||
//状态复位
|
||||
StatusReset();
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"状态复位");
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"状态复位");
|
||||
//关闭定位
|
||||
VisionPos(false);
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"关闭定位");
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"关闭定位");
|
||||
//写入流程启动点位配置
|
||||
StartProcessAction();
|
||||
PLCItem? pLCItem = ConfigModel.GlobalList?
|
||||
@ -743,7 +784,7 @@ namespace DH.Devices.PLC
|
||||
.Where(it => it.Name == "挡料电机回原点速度").FirstOrDefault();
|
||||
if (pLCItem == null)
|
||||
{
|
||||
throw new Exception( $"未找到挡料电机回原点速度地址,请检查该地址是否存在于点位表!");
|
||||
throw new Exception($"未找到挡料电机回原点速度地址,请检查该地址是否存在于点位表!");
|
||||
}
|
||||
PLCItem? pLCItem1 = ConfigModel.GlobalList?
|
||||
.FirstOrDefault()?
|
||||
@ -792,17 +833,26 @@ namespace DH.Devices.PLC
|
||||
//转盘启动
|
||||
TurnStart(true);
|
||||
}
|
||||
|
||||
public void StopProcess()
|
||||
{
|
||||
StatusReset();
|
||||
VisionPos(false);
|
||||
CountToZero();
|
||||
StopProcessAction();
|
||||
TurnStart(false);
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"PLC断开连接");
|
||||
}
|
||||
public void CloseProcess()
|
||||
{
|
||||
StatusReset();
|
||||
VisionPos(false);
|
||||
CountToZero();
|
||||
TurnStart(false);
|
||||
TurnStart(false);
|
||||
TurnEnable(false);
|
||||
StopProcessAction();
|
||||
OpenHeartbeat(false);
|
||||
PLCDisConnect();
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"PLC断开连接");
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"PLC断开连接");
|
||||
}
|
||||
public void InitProcessAction() =>
|
||||
ProcessAction(ConfigModel.GlobalList?.FirstOrDefault()?.InitProcessList?.ToList() ?? new List<PLCItem>());
|
||||
@ -875,7 +925,7 @@ namespace DH.Devices.PLC
|
||||
if (pLCItem == null)
|
||||
return;
|
||||
WriteBool(pLCItem.Address, b);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -888,7 +938,7 @@ namespace DH.Devices.PLC
|
||||
if (pLCItem == null)
|
||||
return;
|
||||
WriteUInt16(pLCItem.Address, (ushort)speed);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -968,12 +1018,16 @@ namespace DH.Devices.PLC
|
||||
{
|
||||
|
||||
|
||||
int timeout = 5000;
|
||||
int timeout = 60000;
|
||||
int elapsedTime = 0;
|
||||
int checkInterval = 100;
|
||||
MotorToZero(false);
|
||||
// 检查是否不在原点,如果不在,则回原点
|
||||
Thread.Sleep(300);
|
||||
|
||||
MotorClockwise(false);
|
||||
Thread.Sleep(300);
|
||||
MotorCounterclockwise(false);
|
||||
Thread.Sleep(300);
|
||||
|
||||
MotorSpeed(speed); // 速度
|
||||
Thread.Sleep(300);
|
||||
@ -984,22 +1038,34 @@ namespace DH.Devices.PLC
|
||||
|
||||
|
||||
// 等待回到原点
|
||||
while (!ReadMotorToZero())
|
||||
while (true)
|
||||
{
|
||||
if (elapsedTime >= timeout)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"超时");
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (ReadMotorRealPos() == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
Thread.Sleep(checkInterval);
|
||||
elapsedTime += checkInterval;
|
||||
}
|
||||
// }
|
||||
|
||||
MotorToZero(false);
|
||||
Thread.Sleep(200);
|
||||
MotorClockwise(false);
|
||||
Thread.Sleep(200);
|
||||
MotorCounterclockwise(false);
|
||||
// 无论是刚回到原点还是已经在原点,执行目标位置、速度和方向设置
|
||||
MotorSpeed(speed);
|
||||
|
||||
// MotorSpeed(speed);
|
||||
|
||||
Thread.Sleep(300);
|
||||
MotorPos(pos); // 目标位置
|
||||
Thread.Sleep(300);
|
||||
if (direction)
|
||||
{
|
||||
MotorClockwise(true); // 顺时针转动
|
||||
@ -1010,11 +1076,27 @@ namespace DH.Devices.PLC
|
||||
MotorCounterclockwise(true); // 逆时针转动
|
||||
|
||||
}
|
||||
Thread.Sleep(30);
|
||||
MotorPos(pos); // 目标位置
|
||||
Thread.Sleep(2000);
|
||||
|
||||
|
||||
int timeout1 = 60000;
|
||||
int elapsedTime1 = 0;
|
||||
int checkInterval1 = 100;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (elapsedTime1 >= timeout1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (ReadMotorRealPos() == pos)
|
||||
{
|
||||
break;
|
||||
}
|
||||
Thread.Sleep(checkInterval1);
|
||||
elapsedTime1 += checkInterval1;
|
||||
}
|
||||
Thread.Sleep(1500);
|
||||
|
||||
}
|
||||
|
||||
@ -1024,61 +1106,84 @@ namespace DH.Devices.PLC
|
||||
/// False: 逆时针
|
||||
/// </summary>
|
||||
/// <param name="u"></param>
|
||||
public void FeedingMotor( bool direction,int speed,int pos)
|
||||
public void FeedingMotor(bool direction, int speed, int pos)
|
||||
{
|
||||
|
||||
|
||||
int timeout = 5000;
|
||||
int timeout = 10000;
|
||||
int elapsedTime = 0;
|
||||
int checkInterval = 100;
|
||||
|
||||
BarrierToZero(false);
|
||||
Thread.Sleep(300);
|
||||
// 检查是否不在原点,如果不在,则回原点
|
||||
|
||||
|
||||
BarrierClockwise(false);
|
||||
Thread.Sleep(300);
|
||||
BarrierCounterclockwise(false);
|
||||
Thread.Sleep(300);
|
||||
BarrierToZeroSpeed(speed); // 速度
|
||||
Thread.Sleep(300);
|
||||
// 发送回原点指令
|
||||
BarrierToZero(true);
|
||||
Thread.Sleep(1000); // 给设备一些时间响应
|
||||
Thread.Sleep(300);
|
||||
// 发送回原点指令
|
||||
BarrierToZero(true);
|
||||
Thread.Sleep(300); // 给设备一些时间响应
|
||||
|
||||
// 等待回到原点
|
||||
while (!ReadBarrierToZero())
|
||||
// 等待回到原点
|
||||
while (true)
|
||||
{
|
||||
if (elapsedTime >= timeout)
|
||||
{
|
||||
if (elapsedTime >= timeout)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
Thread.Sleep(checkInterval);
|
||||
elapsedTime += checkInterval;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (ReadBarrierRealPos() == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
Thread.Sleep(checkInterval);
|
||||
elapsedTime += checkInterval;
|
||||
}
|
||||
|
||||
|
||||
// 无论是刚回到原点还是已经在原点,执行目标位置、速度和方向设置
|
||||
BarrierSpeed(speed);
|
||||
Thread.Sleep(300);
|
||||
|
||||
|
||||
if (direction)
|
||||
{
|
||||
BarrierClockwise(true); // 顺时针转动
|
||||
|
||||
BarrierClockwise(true); // 顺时针转动
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
BarrierCounterclockwise(true); // 逆时针转动
|
||||
|
||||
|
||||
}
|
||||
Thread.Sleep(30);
|
||||
Thread.Sleep(300);
|
||||
BarrierPos(pos); // 目标位置
|
||||
Thread.Sleep(2000);
|
||||
|
||||
Thread.Sleep(300);
|
||||
int timeout1 = 10000;
|
||||
int elapsedTime1 = 0;
|
||||
int checkInterval1 = 100;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (elapsedTime1 >= timeout1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (ReadBarrierRealPos() == pos)
|
||||
{
|
||||
break;
|
||||
}
|
||||
Thread.Sleep(checkInterval1);
|
||||
elapsedTime1 += checkInterval1;
|
||||
}
|
||||
Thread.Sleep(300);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 转盘清料
|
||||
/// </summary>
|
||||
@ -1106,7 +1211,8 @@ namespace DH.Devices.PLC
|
||||
if (b)
|
||||
{
|
||||
//开启心跳
|
||||
Task.Run(async () => await HeartbeatAsync1());
|
||||
//Task.Run(async () => await HeartbeatAsync1());
|
||||
_taskFac.StartNew(async () => await HeartbeatAsync1());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1116,7 +1222,7 @@ namespace DH.Devices.PLC
|
||||
int Register = (int)((productNumber - 1) % 30);
|
||||
currentRegister = 411 + Register;
|
||||
WriteUInt16($"D{currentRegister}", value);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -1155,6 +1261,20 @@ namespace DH.Devices.PLC
|
||||
WriteBool(pLCItem.Address, b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取挡料电机实时位置
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int ReadBarrierRealPos()
|
||||
{
|
||||
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机实时位置");
|
||||
if (pLCItem == null)
|
||||
{
|
||||
throw new Exception("未找到挡料电机实时位置点位");
|
||||
}
|
||||
|
||||
return ReadInt16(pLCItem.Address);
|
||||
}
|
||||
/// <summary>
|
||||
/// 读取 挡杆回原点状态
|
||||
/// </summary>
|
||||
@ -1169,7 +1289,7 @@ namespace DH.Devices.PLC
|
||||
// throw new Exception("未找到挡料电机回原点点位");
|
||||
//}
|
||||
//挡料电机传感器感应点
|
||||
return ReadBool("X11");
|
||||
return ReadBool("X11");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -1180,7 +1300,7 @@ namespace DH.Devices.PLC
|
||||
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机回原点速度");
|
||||
if (pLCItem == null)
|
||||
return;
|
||||
WriteUInt32(pLCItem.Address, (uint)speed);
|
||||
WriteInt32(pLCItem.Address, speed);
|
||||
}
|
||||
|
||||
|
||||
@ -1192,7 +1312,7 @@ namespace DH.Devices.PLC
|
||||
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机位置");
|
||||
if (pLCItem == null)
|
||||
return;
|
||||
WriteUInt16(pLCItem.Address, (ushort)value);
|
||||
WriteInt16(pLCItem.Address, (short)value);
|
||||
}
|
||||
|
||||
|
||||
@ -1253,12 +1373,13 @@ namespace DH.Devices.PLC
|
||||
/// <exception cref="Exception"></exception>
|
||||
public bool ReadMotorToZero()
|
||||
{
|
||||
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "相机步进原点");
|
||||
if (pLCItem == null)
|
||||
{
|
||||
throw new Exception("未找到挡料电机回原点点位");
|
||||
}
|
||||
return ReadBool(pLCItem.Address);
|
||||
//PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "相机步进原点");
|
||||
//if (pLCItem == null)
|
||||
//{
|
||||
// throw new Exception("未找到挡料电机回原点点位");
|
||||
//}
|
||||
//return ReadBool(pLCItem.Address);
|
||||
return ReadBool("X10");
|
||||
}
|
||||
|
||||
|
||||
@ -1271,7 +1392,7 @@ namespace DH.Devices.PLC
|
||||
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "相机步进位置");
|
||||
if (pLCItem == null)
|
||||
return;
|
||||
WriteUInt16(pLCItem.Address, (ushort)value);
|
||||
WriteInt32(pLCItem.Address, (ushort)value);
|
||||
}
|
||||
|
||||
|
||||
@ -1285,7 +1406,7 @@ namespace DH.Devices.PLC
|
||||
{
|
||||
throw new Exception("未找到相机步进实时位置");
|
||||
}
|
||||
return ReadInt16(pLCItem.Address);
|
||||
return ReadInt32(pLCItem.Address);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user