Commit 0bc3a2d7 LN

增加异常处理

1 个父辈 d91c34b0
......@@ -115,7 +115,13 @@ namespace DeviceLibrary
{
if (RobotManage.InoutDebugMode)
{
return;
}
if (RobotManage.mainMachine.runStatus!=RunStatus.Running)
{
return;
}
lock (serverclock)
{
var code = ProcessCode(codelist, reel.PlateW, reel.PlateH);
......@@ -133,7 +139,7 @@ namespace DeviceLibrary
operation.data.Add("singleIn", "false");
}
Operation resultOperation = HttpHelper.Post(GetPostApi(), operation, 5000, printlog);
if (operation.seq != resultOperation?.seq)
if (resultOperation != null && resultOperation.seq > 0 && operation.seq != resultOperation?.seq)
{
Thread.Sleep(1000);
SendInStoreRequest(codelist, reel, printlog);
......
......@@ -220,53 +220,62 @@ namespace DeviceLibrary
}
byte[] SendAndReceive(byte[] buff, string cmdname = "")
{
lock (port)
try
{
byte[] restr = new byte[0];
if (port == null || !port.IsOpen)
lock (port)
{
if (!Open(PortName,out string msg))
byte[] restr = new byte[0];
if (port == null || !port.IsOpen)
{
log(PortName+"未初始化,或已断开连接");
return restr;
if (!Open(PortName, out string msg))
{
log(PortName + "未初始化,或已断开连接");
return restr;
}
}
}
var t = Task.Run(delegate
{
byte[] recv = new byte[256];
try
var t = Task.Run(delegate
{
port.Write(buff, 0, buff.Length);
//port.DiscardInBuffer();
Thread.Sleep(60);
int r = port.Read(recv, 0, recv.Length);
var recvdata = recv.Take(r).ToArray();
port.DiscardOutBuffer();
port.DiscardInBuffer();
CalculateCRC(recvdata, recvdata.Length - 2, out ushort checksun);
var crcbyte = BitConverter.GetBytes(checksun);
debuglog(cmdname + "\trecvdata: " + BitConverter.ToString(recvdata).Replace("-", " ") + " crc:" + BitConverter.ToString(crcbyte).Replace("-", " "));
if (recvdata.Length > 5
&& recvdata[1] == buff[1]
&& recvdata[recvdata.Length - 2] == crcbyte[0]
&& recvdata[recvdata.Length - 1] == crcbyte[1])
byte[] recv = new byte[256];
try
{
port.Write(buff, 0, buff.Length);
//port.DiscardInBuffer();
Thread.Sleep(60);
int r = port.Read(recv, 0, recv.Length);
var recvdata = recv.Take(r).ToArray();
port.DiscardOutBuffer();
port.DiscardInBuffer();
CalculateCRC(recvdata, recvdata.Length - 2, out ushort checksun);
var crcbyte = BitConverter.GetBytes(checksun);
debuglog(cmdname + "\trecvdata: " + BitConverter.ToString(recvdata).Replace("-", " ") + " crc:" + BitConverter.ToString(crcbyte).Replace("-", " "));
if (recvdata.Length > 5
&& recvdata[1] == buff[1]
&& recvdata[recvdata.Length - 2] == crcbyte[0]
&& recvdata[recvdata.Length - 1] == crcbyte[1])
{
return recvdata;
}
return restr;
}
catch (SocketException e)
{
return recvdata;
log(string.Format("{0} Error code: {1}.", e.Message, e.ErrorCode));
}
return restr;
}
catch (SocketException e)
{
log(string.Format("{0} Error code: {1}.", e.Message, e.ErrorCode));
}
return restr;
});
t.Wait(2000);
if (t.IsCompleted)
return t.Result;
else
return new byte[0];
});
t.Wait(2000);
if (t.IsCompleted)
return t.Result;
else
return new byte[0];
}
}
catch (Exception ex)
{
error("SendAndReceive , " + cmdname + ", error :" + ex.ToString());
}
return new byte[0];
}
/// <summary>
/// 初始化设置
......
......@@ -276,83 +276,90 @@ namespace DeviceLibrary
public void Run()
{
mstart = true;
while (mstart)
try
{
try
while (mstart)
{
canRunning = DeviceCheck();
if (canRunning)
try
{
BtnProcess();
canRunning = SafeCheck();
//if (canRunning && !lastSafeCheckStatus){}
//lastSafeCheckStatus = canRunning;
}
Thread.Sleep(200);
if (!canRunning || !mstart)
continue;
if (runStatus == RunStatus.Running)
{
ioMonitor();
AGVProcess();
StringProcess();
ClampProcess();
SelfAuditProcess();
boxTransport.Process();
if (!InSelfAudit())
canRunning = DeviceCheck();
if (canRunning)
{
if (AutoInOutTest)
AutoInOutTestProcess();
else
StoreProcess();
BtnProcess();
canRunning = SafeCheck();
//if (canRunning && !lastSafeCheckStatus){}
//lastSafeCheckStatus = canRunning;
}
}
else if (runStatus == RunStatus.HomeReset)
{
HomeReset();
}
}
catch (Exception ex)
{
Msg.add(ex.ToString(), MsgLevel.warning);
Msg.setlogones();
}
finally
{
var m = Msg.get();
ProcessMsgEvent?.Invoke(m);
ServerCM.ProcessMsg(m);
StoreStatus currnetstoreStatus = StoreStatus.None;
if (m.Find((aa) => aa.msgLevel == MsgLevel.alarm) == null)
{
hasAlarm = false;
AlarmBuzzer.OFF();
if (ServerCM.storeStatus != StoreStatus.InStoreExecute
&& ServerCM.storeStatus != StoreStatus.OutStoreExecute
&& ServerCM.storeStatus != StoreStatus.ResetMove && ServerCM.storeStatus != StoreStatus.StoreOnline)
Thread.Sleep(200);
if (!canRunning || !mstart)
continue;
if (runStatus == RunStatus.Running)
{
LogUtil.info($"test storeStatus is {ServerCM.storeStatus} change to StoreOnline");
currnetstoreStatus = StoreStatus.StoreOnline;
ioMonitor();
AGVProcess();
StringProcess();
ClampProcess();
SelfAuditProcess();
boxTransport.Process();
if (!InSelfAudit())
{
if (AutoInOutTest)
AutoInOutTestProcess();
else
StoreProcess();
}
}
else if (runStatus == RunStatus.HomeReset)
{
HomeReset();
}
}
else
catch (Exception ex)
{
hasAlarm = true;
AlarmBuzzer.ON();
currnetstoreStatus = isInSuddenDown ? StoreStatus.SuddenStop : StoreStatus.Warning;
Msg.add(ex.ToString(), MsgLevel.warning);
Msg.setlogones();
}
//ProcessMoveinfoEvent?.Invoke(MoveInfo.List);
if (!UserPause && mstart)
finally
{
Msg.clear();
}
//else
// currnetstoreStatus = StoreStatus.Debugging;
var m = Msg.get();
ProcessMsgEvent?.Invoke(m);
ServerCM.ProcessMsg(m);
StoreStatus currnetstoreStatus = StoreStatus.None;
if (m.Find((aa) => aa.msgLevel == MsgLevel.alarm) == null)
{
hasAlarm = false;
AlarmBuzzer.OFF();
if (ServerCM.storeStatus != StoreStatus.InStoreExecute
&& ServerCM.storeStatus != StoreStatus.OutStoreExecute
&& ServerCM.storeStatus != StoreStatus.ResetMove && ServerCM.storeStatus != StoreStatus.StoreOnline)
{
LogUtil.info($"test storeStatus is {ServerCM.storeStatus} change to StoreOnline");
currnetstoreStatus = StoreStatus.StoreOnline;
}
}
else
{
hasAlarm = true;
AlarmBuzzer.ON();
currnetstoreStatus = isInSuddenDown ? StoreStatus.SuddenStop : StoreStatus.Warning;
}
//ProcessMoveinfoEvent?.Invoke(MoveInfo.List);
if (!UserPause && mstart)
{
Msg.clear();
}
//else
// currnetstoreStatus = StoreStatus.Debugging;
if (currnetstoreStatus != StoreStatus.None)
ServerCM.storeStatus = currnetstoreStatus;
if (currnetstoreStatus != StoreStatus.None)
ServerCM.storeStatus = currnetstoreStatus;
}
}
}
catch (Exception ex)
{
LogUtil.error("Run error :" + ex.ToString());
}
RobotManage.isRunning = false;
RobotManage.UserPause($"isRunning = false", false);
LogUtil.info("主线程已退出.");
......@@ -364,20 +371,30 @@ namespace DeviceLibrary
}
public void Stop()
{
LogUtil.info("开始停止系统2.");
mstart = false;
AutoInOutTest = false;
ClearOutJob();
ServerCM.StopConnectServer();
Thread.Sleep(300);
Alarm(AlarmType.None);
StopMove(true);
IOMove(IO_Type.LineRun, IO_VALUE.LOW);
IOMove(IO_Type.LineRev, IO_VALUE.LOW);
LedProcess(null);
CloseRgbLed();
SoundsController.StopPlay();
LogUtil.info("开始停止系统3.");
try
{
LogUtil.info("开始停止系统2.");
mstart = false;
AutoInOutTest = false;
ClearOutJob();
ServerCM.StopConnectServer();
Thread.Sleep(300);
Alarm(AlarmType.None);
StopMove(true);
IOMove(IO_Type.LineRun, IO_VALUE.LOW);
IOMove(IO_Type.LineRev, IO_VALUE.LOW);
LedProcess(null);
CloseRgbLed();
SoundsController.StopPlay();
runStatus = RunStatus.Stop;
mstart = false;
LogUtil.info("开始停止系统3.");
}
catch (Exception ex)
{
LogUtil.error("停止系统 Stop error :" + ex.ToString());
}
}
public void BeginHomeReset(bool firstRun = false)
{
......
......@@ -605,6 +605,18 @@ namespace TheMachine
if (RobotManage.isRunning)
{
btn_stop.Enabled = true;
if (e)
{
(btn_run as Button).Text = crc.GetString(L.device_resume, "恢复运行");
(btn_run as Button).BackColor = Color.LightGreen;
cb_IgnoreSafecheck.Enabled = true;
}
else
{
(btn_run as Button).Text = crc.GetString(L.device_pause, "暂停运行");
(btn_run as Button).BackColor = Color.Yellow;
cb_IgnoreSafecheck.Enabled = false;
}
}
else
{
......@@ -614,18 +626,6 @@ namespace TheMachine
cb_IgnoreSafecheck.Enabled = true;
return;
}
if (e)
{
(btn_run as Button).Text = crc.GetString(L.device_resume, "恢复运行");
(btn_run as Button).BackColor = Color.LightGreen;
cb_IgnoreSafecheck.Enabled = true;
}
else
{
(btn_run as Button).Text = crc.GetString(L.device_pause, "暂停运行");
(btn_run as Button).BackColor = Color.Yellow;
cb_IgnoreSafecheck.Enabled = false;
}
}
private void btn_stop_Click(object sender, EventArgs e)
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!