for (int i = 0; i < axiss.Length; i++)
{
int axis = axiss[i];
//Task.Run(() =>
//{
LogHelper.Write($"1--执行的轴号是{axis}");
var dyminW = new DummyWork2();
dyminW.ZMotionDammyReady += new DummyWork2.ZAxisDyminWorkingEndEvent(OnDyminWorkingEnd);
dyminW.zAxisDyminDropSenderEnd += new DummyWork2.ZAxisDyminDropSenderEndEvent(DropSender);
dyminW.Start(axis,0);
LogHelper.Write($"2--执行的轴号是{axis}");
//});
}
如代码所示,我用循环去开启6个对象,每个对象去操作设备上不同的轴进行动作,每个对象的操作都是异步的线程去管理,为什么我6个线程同时运动,会有时候某个线程会卡住?麻烦各位大佬帮忙指导下
public class DummyWork2
{
public delegate void ZAxisDyminWorkingEndEvent(int axis, bool success);
/// <summary>
/// 轴工作已完成触发
/// </summary>
public ZAxisDyminWorkingEndEvent ZMotionDammyReady;
public delegate void ZAxisDyminDropSenderEndEvent(int axis, int dropNum, int num);
public ZAxisDyminDropSenderEndEvent zAxisDyminDropSenderEnd;
public int Suckvalue;
public int SuckSpeed;
public int DropSpeed;
public int DropPulse;
public int DropNumber;
public int DyminNum;
public int DummyStartNum;
public int Axis = -1;
public int Num =1;
public int IntervalTime = Commond.Commond.ReadJson.typeNumber.IntervalTime;
public int SuckUnm()
{
try
{
LogHelper.Write($"{Axis}轴SuckUnm start!");
int position = 0;
var ans = Commond.Commond.Mc210Contorl.SscGetPosition(1, 1, Axis + 1, out position);
if (ans != "success")
{
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_OPERATION);
Thread.Sleep(50);
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_SYSTEM);
Thread.Sleep(50);
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_SERVO);
Thread.Sleep(50);
SuckUnm();
}
var number = Convert.ToInt32(Commond.Commond.ReadJson.auto[Axis].SuckValue) - Convert.ToInt32(position);
LogHelper.Write($"{Axis}轴SuckUnm end!");
return number;
}
catch (Exception ex) { return 0; }
}
System.Threading.Timer _SuckState;
public void Suck(int dummyStartNum)
{
var a = Task.Run(() =>
{
try
{
LogHelper.Write($"{Axis}轴回吸开始!");
DummyStartNum = dummyStartNum;
Suckvalue = SuckUnm();
LogHelper.Write($"{Axis}轴回吸量:{Suckvalue}!");
ZMotionTools.SetOp(Commond.Commond.IOConfig.ValveReverse[Axis], 0);
ZMotionTools.SetOp(Commond.Commond.IOConfig.ValveCorotation[Axis], 1);
var ret = Commond.Commond.Mc210Contorl.SscIncStart(1, 1, Axis + 1, Suckvalue, SuckSpeed, 1, 3);
if (ret != "success")
{
LogHelper.Write($"{Axis}轴回吸失败!");
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_OPERATION);
Thread.Sleep(50);
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_SYSTEM);
Thread.Sleep(50);
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_SERVO);
Thread.Sleep(50);
ret = Commond.Commond.Mc210Contorl.SscIncStart(1, 1, Axis + 1, Suckvalue, SuckSpeed, 1, 3);
if (ret != "success")
{
AxisState = 1;
WarnTools.Warn($"{Axis + 1}轴回吸失败", WarnTypeENum.P轴报警, true, 1);
return;
}
}
int Timeout = Convert.ToInt32(Suckvalue / (SuckSpeed));
int count = 0;
int state;
_SuckState = new Timer((s) =>
{
if (Commond.Commond.Mc210WorkingStop)
{
LogHelper.Write($"{Axis}轴停止任务!");
_SuckState.Dispose();
AxisState = 1;
return;
}
var status = Commond.Commond.Mc210Contorl.SscGetDriveFinStatus(1, 1, Axis + 1, SscApi.SSC_FIN_TYPE_SMZ, out state);
if (status == "success")
{
if (state == 1)
{
_SuckState.Dispose();
LogHelper.Write($"{Axis}轴回吸完成,开始第{dummyStartNum}任务!");
Dummy();
}
}
else
{
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_OPERATION);
Thread.Sleep(50);
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_SYSTEM);
Thread.Sleep(50);
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_SERVO);
Thread.Sleep(50);
}
if (count > 20)
{
_SuckState.Dispose();
AxisState = 1;
WarnTools.Warn($"{Axis + 1}轴吐料模式回吸超时!", WarnTypeENum.P轴报警, true, 1);
return;
}
count++;
}, null, Timeout, 500);
}
catch (Exception ex)
{
WarnTools.Warn($"{Axis + 1}轴吐料模式回吸错误:{ex.Message}!", WarnTypeENum.P轴报警, true, 1);
}
});
}
public void Dummy()
{
LogHelper.Write($"Dummy Start1->{Axis} DummyStartNum {DummyStartNum} DyminNum {DyminNum}");
Task.Run(() =>
{
try
{
LogHelper.Write($"Dummy Start2->{Axis}");
for (int i = DummyStartNum; i < DyminNum; i++)
{
ZMotionTools.SetOp(Commond.Commond.IOConfig.ValveReverse[Axis], 0);
ZMotionTools.SetOp(Commond.Commond.IOConfig.ValveCorotation[Axis], 0);
DropSenser.Zero(DropSenserNum(Axis));
for (int j = 0; j < DropNumber; j++)
{
if (Commond.Commond.Mc210WorkingStop)
{
AxisState = 1;
LogHelper.Write($"Dummy stop->{Axis}");
return;
}
var ret = Commond.Commond.Mc210Contorl.SscIncStart(1, 1, Axis + 1, -DropPulse, DropSpeed, 1, 5);
if (ret == "success")
{
Thread.Sleep(Convert.ToInt32(Commond.Commond.ReadJson.auto[Axis].DropInterval) + Convert.ToInt32(DropPulse / (DropSpeed)));
}
else
{
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_OPERATION);
Thread.Sleep(10);
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_SYSTEM);
Thread.Sleep(10);
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_SERVO);
Thread.Sleep(10);
Commond.Commond.Mc210Contorl.SscIncStart(1, 1, Axis + 1, -DropPulse, DropSpeed, 1, 5);
}
}
zAxisDyminDropSenderEnd?.Invoke(Axis, DropSenser.GetCount(DropSenserNum(Axis)), i + 1);
int position = 0;
var ans = Commond.Commond.Mc210Contorl.SscGetPosition(1, 1, Axis + 1, out position);
if (ans != "success")
{
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_OPERATION);
Thread.Sleep(50);
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_SYSTEM);
Thread.Sleep(50);
Commond.Commond.Mc210Contorl.SscResetAlarm(1, 1, Axis + 1, SscApi.SSC_ALARM_SERVO);
Thread.Sleep(50);
Commond.Commond.Mc210Contorl.SscGetPosition(1, 1, Axis + 1, out position);
}
if (position > DropPulse * DropNumber)
{
Thread.Sleep(IntervalTime);
}
else
{
Suck(i + 1);
return;
}
}
AxisState = 1;
return;
}
catch (Exception ex)
{
WarnTools.Warn($"{Axis}轴Dummy运动错误:{ex.Message}", WarnTypeENum.P轴报警, true, 1);
AxisState = 1;
}
});
}
public int DropSenserNum(int input)
{
try
{
int num = 0;
switch (input)
{
case 0:
num = Commond.Commond.IOConfig.InputAxis0DorpSenser;
break;
case 1:
num = Commond.Commond.IOConfig.InputAxis1DorpSenser;
break;
case 2:
num = Commond.Commond.IOConfig.InputAxis2DorpSenser;
break;
case 3:
num = Commond.Commond.IOConfig.InputAxis3DorpSenser;
break;
case 4:
num = Commond.Commond.IOConfig.InputAxis4DorpSenser;
break;
case 5:
num = Commond.Commond.IOConfig.InputAxis5DorpSenser;
break;
}
return num;
}
catch (Exception ex)
{
return -1;
}
}
public void Start(int axis,int type)
{
var a =Task.Run(() =>
{
try
{
//工作流程
//状态监听
Axis = axis;
SuckSpeed = Commond.Commond.UCMppConditions.MPPSuckSpeed[axis];
DropSpeed = Commond.Commond.UCMppConditions.MPPDropSpeed[axis];
DropPulse = Convert.ToInt32(Commond.Commond.ReadJson.auto[axis].StandardPulse);
DropNumber = Convert.ToInt16(Commond.Commond.ReadJson.auto[axis].DropCount);
if (type == 0)
Num = Commond.Commond.ReadJson.typeNumber.DyminNum;
else
Num = Commond.Commond.ReadJson.typeNumber.Dymin2Num;
Suckvalue = Convert.ToInt32(Commond.Commond.ReadJson.auto[Axis].SuckValue);
DyminNum = Convert.ToInt32(Suckvalue / (DropPulse * DropNumber)) * Num;
LogHelper.Write($"Start的轴号是{Axis}");
Suck(0);
}
catch (Exception ex)
{
WarnTools.Warn($"{axis + 1}轴运动报错:{ex.Message}", WarnTypeENum.P轴报警, true, 1);
AxisState = 1;
}
});
}
public int axisState;
public int AxisState
{
get => axisState;
set
{
axisState = value;
if (axisState == 1)
{
LogHelper.Write($"{Axis}轴完成");
ZMotionDammyReady?.Invoke(Axis, true);
}
}
}
}