Commit 8fb3faf1 几米阳光

serialBean改为Monitor加锁

1 个父辈 3de4897c
......@@ -226,53 +226,53 @@ namespace OnlineStore.Common
#endregion
#region 发送数据string类型
public void SendData(string data)
{ //发送数据
//public void SendData(string data)
//{ //发送数据
if (_serialPort.IsOpen)
{
lock (lockObj)
{
_serialPort.Write(data);
System.Threading.Thread.Sleep(10);
}
}
}
// if (_serialPort.IsOpen)
// {
// lock (lockObj)
// {
// _serialPort.Write(data);
// System.Threading.Thread.Sleep(10);
// }
// }
//}
#endregion
#region 发送数据byte类型
/// <summary>
/// 数据发送
/// </summary>
/// <param name="data">要发送的数据字节</param>
public void SendData(byte[] data, int offset, int count)
{
///// <summary>
///// 数据发送
///// </summary>
///// <param name="data">要发送的数据字节</param>
//public void SendData(byte[] data, int offset, int count)
//{
string strSend = "";
for (int i = 0; i < data.Length; i++)
{
strSend += string.Format("{0:X2} ", data[i]);
}
LOGGER.Debug("【" + _serialPort.PortName + "】发送数据【" + strSend + "】");
lock (lockObj)
{
try
{
if (_serialPort.IsOpen)
{
_serialPort.DiscardInBuffer();//清空接收缓冲区
_serialPort.Write(data, offset, count);
System.Threading.Thread.Sleep(10);
}
}
catch (Exception ex)
{
_serialPort.DiscardOutBuffer();
LogUtil.error(LOGGER, "SendData ERROR:" + ex.ToString(), 21);
}
}
// string strSend = "";
// for (int i = 0; i < data.Length; i++)
// {
// strSend += string.Format("{0:X2} ", data[i]);
// }
// LOGGER.Debug("【" + _serialPort.PortName + "】发送数据【" + strSend + "】");
// lock (lockObj)
// {
// try
// {
// if (_serialPort.IsOpen)
// {
// _serialPort.DiscardInBuffer();//清空接收缓冲区
// _serialPort.Write(data, offset, count);
// System.Threading.Thread.Sleep(10);
// }
// }
// catch (Exception ex)
// {
// _serialPort.DiscardOutBuffer();
// LogUtil.error(LOGGER, "SendData ERROR:" + ex.ToString(), 21);
// }
// }
}
//}
#endregion
#region 发送命令
/// <summary>
......@@ -287,8 +287,10 @@ namespace OnlineStore.Common
isOk = false;
if (_serialPort.IsOpen)
{
lock (lockObj)
//lock (lockObj)
if(Monitor.TryEnter(lockObj,10))
{
//Monitor.Enter(lockObj);
try
{
_serialPort.DiscardInBuffer(); //清空接收缓冲区
......@@ -323,8 +325,16 @@ namespace OnlineStore.Common
isOk = false;
LogUtil.error(LOGGER, "SendCommand ERROR:" + ex.ToString(), 20);
}
finally
{
Monitor.Exit(lockObj);
}
}
else
{
LogUtil.error(PortName + " 发送数据" + ByteToString(SendData) + "失败,未得到锁");
}
}
return -1;
}
......@@ -348,8 +358,9 @@ namespace OnlineStore.Common
if (_serialPort.IsOpen)
{
lock (lockObj)
if (Monitor.TryEnter(lockObj, 10))
{
//Monitor.Enter(lockObj);
try
{
_serialPort.DiscardInBuffer(); //清空接收缓冲区
......@@ -384,11 +395,18 @@ namespace OnlineStore.Common
}
catch (Exception ex)
{
//throw ex;
LogUtil.error(PortName + " 发送数据" + ByteToString(SendData) + " 出错:"+ex.ToString());
}
finally
{
Monitor.Exit(lockObj);
}
}
else
{
LogUtil.error(PortName + " 发送数据" + ByteToString(SendData) + "失败,未得到锁");
}
}
return -1;
}
......
......@@ -238,15 +238,15 @@ namespace OnlineStore.DeviceLibrary
public void SetShuokeSpeed()
{
ShuoKeControls.SetSpeed(Config.CompressAxis_Slv, ShuoKeCMD.SetAddSpeed, Config.CompressAxis_AddSpeed);
Thread.Sleep(100);
Thread.Sleep(60);
ShuoKeControls.SetSpeed(Config.CompressAxis_Slv, ShuoKeCMD.SetDelSpeed, Config.CompressAxis_DelSpeed);
Thread.Sleep(100);
Thread.Sleep(60);
ShuoKeControls.SetSpeed(Config.CompressAxis_Slv, ShuoKeCMD.SetEndSpeed, Config.CompressAxis_EndSpeed);
Thread.Sleep(100);
Thread.Sleep(60);
ShuoKeControls.SetSpeed(Config.CompressAxis_Slv, ShuoKeCMD.SetHomeSpeed, Config.CompressAxis_HomeSpeed);
Thread.Sleep(100);
Thread.Sleep(60);
ShuoKeControls.SetSpeed(Config.CompressAxis_Slv, ShuoKeCMD.SetMaxSpeed, Config.CompressAxis_MaxSpeed);
Thread.Sleep(100);
Thread.Sleep(60);
ShuoKeControls.SetSpeed(Config.CompressAxis_Slv, ShuoKeCMD.SetStartSpeed, Config.CompressAxis_StartSpeed);
}
public void MoveToP1()
......@@ -389,8 +389,8 @@ namespace OnlineStore.DeviceLibrary
StoreMove.NextMoveStep(StoreMoveStep.BOX_H_OtherAxisBack);
if (IsHasCompress_Axis)
{
ShuoKeControls.HomeMove(Config.CompressAxis_Slv, 1);
StoreMove.WaitList.Add(WaitResultInfo.WaitShuoKe(Config.CompressAxis_Slv, 0, true));
ShuoKeControls.HomeMove(Config.CompressAxis_Slv, 1);
}
ACAxisHomeMove(Config.Middle_Axis);
ACAxisHomeMove(Config.UpDown_Axis);
......@@ -419,8 +419,8 @@ namespace OnlineStore.DeviceLibrary
LogUtil.info(LOGGER, StoreName + "到待机状态,压紧轴回原点,关闭舱门");
if (IsHasCompress_Axis)
{
ShuoKeControls.HomeMove(Config.CompressAxis_Slv, 1);
StoreMove.WaitList.Add(WaitResultInfo.WaitShuoKe(Config.CompressAxis_Slv, 1, true));
ShuoKeControls.HomeMove(Config.CompressAxis_Slv, 1);
}
//关闭舱门
CloseDoorAndWait();
......@@ -452,8 +452,8 @@ namespace OnlineStore.DeviceLibrary
if (IsHasCompress_Axis)
{
LogUtil.debug("压紧轴绝对运动目标位置:"+targetPosition);
ShuoKeControls.AbsMove(Config.CompressAxis_Slv, targetPosition);
StoreMove.WaitList.Add(WaitResultInfo.WaitShuoKe(Config.CompressAxis_Slv, targetPosition, false));
ShuoKeControls.AbsMove(Config.CompressAxis_Slv, targetPosition);
Thread.Sleep(100);
}
......@@ -519,8 +519,8 @@ namespace OnlineStore.DeviceLibrary
StoreMove.NextMoveStep(StoreMoveStep.BOX_H_OtherAxisBack);
if (IsHasCompress_Axis)
{
ShuoKeControls.HomeMove(Config.CompressAxis_Slv, 1);
StoreMove.WaitList.Add(WaitResultInfo.WaitShuoKe(Config.CompressAxis_Slv, 0, true));
ShuoKeControls.HomeMove(Config.CompressAxis_Slv, 1);
}
ACAxisHomeMove(Config.Middle_Axis);
ACAxisHomeMove(Config.UpDown_Axis);
......@@ -665,11 +665,10 @@ namespace OnlineStore.DeviceLibrary
autoNext = false;
IoCheckTimer.Enabled = false;
serverConnectTimer.Enabled = false;
StopMove();
StopMove(true);
storeRunStatus = StoreRunStatus.Wait;
mainTimer.Enabled = false;
CloseAllAxis();
mainTimer.Enabled = false;
TimeSpan span = DateTime.Now - StartTime;
IsRun = false;
......@@ -700,17 +699,15 @@ namespace OnlineStore.DeviceLibrary
{
LogUtil.error(LOGGER, StoreName + "轴报警,关闭刹车,停止运动,关闭轴,打开报警灯");
KNDIOMove(IO_Type.Axis_Brake, IO_VALUE.LOW);
StopMove();
CloseAllAxis();
StopMove(true );
}
else if (alarmType == StoreAlarmType.SuddenStop)
{
isInSuddenDown = true;
LogUtil.error(LOGGER, StoreName + "收到急停信号,关闭刹车,停止运动,关闭轴,打开报警灯 ");
KNDIOMove(IO_Type.Axis_Brake, IO_VALUE.LOW);
StopMove();
CloseAllAxis();
StoreMove.EndMove();
StopMove(true);
storeStatus = StoreStatus.SuddenStop;
}
else if (alarmType.Equals(StoreAlarmType.NoAirCheck))
......@@ -718,9 +715,8 @@ namespace OnlineStore.DeviceLibrary
isNoAirCheck = true;
LogUtil.error(LOGGER, StoreName + " 未检测到气压信号 ,打开刹车,停止运动,关闭轴,打开报警灯 ");
KNDIOMove(IO_Type.Axis_Brake, IO_VALUE.LOW);
StopMove();
CloseAllAxis();
StoreMove.EndMove();
StoreMove.EndMove();
StopMove(true );
storeStatus = StoreStatus.SuddenStop;
}
//if (alarmType != StoreAlarmType.IoSingleTimeOut)
......@@ -730,18 +726,20 @@ namespace OnlineStore.DeviceLibrary
}
private bool InProcess = false;
private DateTime preProcessTime = DateTime.Now;
private bool IsChongfu = false;
protected override void timersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (InProcess)
{
TimeSpan span = DateTime.Now - preProcessTime;
if (span.TotalMinutes < 1)
{
{
return;
}
else
{
LogUtil.error("主定时器:InProcess已等待"+span.ToString()+"重新处理");
LogUtil.error("主定时器:InProcess已等待" + span.ToString() + "重新处理");
IsChongfu = true;
}
}
try
......@@ -749,15 +747,31 @@ namespace OnlineStore.DeviceLibrary
InProcess = true;
preProcessTime = DateTime.Now;
IoCheckProcess();
TimerProcess();
ShowTimeLog("IoCheckProcess");
TimerProcess();
ShowTimeLog("TimerProcess");
//检查运动轴报警
if (storeRunStatus > StoreRunStatus.Wait && (!isInSuddenDown)&&(!isNoAirCheck))
{
ShowTimeLog("开始检测轴报警");
CheckAxisAlarm();
ShowTimeLog("轴报警检测完成");
}
}
catch (Exception ex)
{
LOGGER.Error(StoreName + "定时处理出错:" + ex.ToString());
}
IsChongfu = false;
InProcess = false;
}
private void ShowTimeLog(string info)
{
if (IsChongfu)
{
LogUtil.info("【" + info + "】 处理完成,耗时:" + (DateTime.Now - preProcessTime).ToString());
}
}
private void LedProcess()
{
try
......@@ -898,12 +912,15 @@ namespace OnlineStore.DeviceLibrary
}
}
}
//检查运动轴报警
if (storeRunStatus > StoreRunStatus.Wait && (!isInSuddenDown) )
{
CheckAxisAlarm();
}
//ShowTimeLog("复位和启动按钮");
////检查运动轴报警
//if (storeRunStatus > StoreRunStatus.Wait && (!isInSuddenDown) )
//{
// ShowTimeLog("开始检测轴报警");
// CheckAxisAlarm();
// ShowTimeLog("轴报警检测完成");
//}
}
private int NeedCheckSafetyLight = 0;
......@@ -942,8 +959,8 @@ namespace OnlineStore.DeviceLibrary
DateTime time = DateTime.Now;
if (StoreMove.MoveType != StoreMoveType.None)
{
BusyMoveProcess();
BusyMoveProcess();
ShowTimeLog("BusyMoveProcess");
}
else if (storeRunStatus.Equals(StoreRunStatus.Runing))
{
......@@ -993,8 +1010,12 @@ namespace OnlineStore.DeviceLibrary
isWaitScan = false;
}
}
ShowTimeLog("判断是否需要出入库");
AutoResetProcess();
ShowTimeLog("AutoResetProcess");
IOTimeOutProcess();
ShowTimeLog("IOTimeOutProcess");
}
}
......@@ -1128,7 +1149,6 @@ namespace OnlineStore.DeviceLibrary
}
else
{
if (span.TotalSeconds < 3)
{
return false;
......@@ -1136,13 +1156,14 @@ namespace OnlineStore.DeviceLibrary
}
checkAlarmTime = DateTime.Now;
bool isInAlarm = false;
//Task.Factory.StartNew(delegate
// {
foreach (ConfigMoveAxis axisInfo in moveAxisList)
{
short axis = axisInfo.GetAxisValue();
string deviceName = axisInfo.DeviceName;
AxisAlarmInfo info = AxisAlarmCodeMap[deviceName];
int alarmIo = ACServerManager.GetAlarmStatus(deviceName, axis);
if (alarmIo == 1)
......@@ -1162,15 +1183,16 @@ namespace OnlineStore.DeviceLibrary
}
AxisAlarmCodeMap[deviceName] = info;
}
//});
//判断报警状态
return isInAlarm;
}
/// <summary>
/// 停止所有运行
/// </summary>
public override void StopMove()
public override void StopMove(bool IsCloseAxis)
{
HDevelopExport.CloseAllCamera();
//运动版停止
ACServerManager.SuddenStop(Config.Middle_Axis.DeviceName, Config.Middle_Axis.GetAxisValue());
ACServerManager.SuddenStop(Config.UpDown_Axis.DeviceName, Config.UpDown_Axis.GetAxisValue());
......@@ -1188,9 +1210,14 @@ namespace OnlineStore.DeviceLibrary
KNDIOMove(IO_Type.LocationCylinder_Up, IO_VALUE.LOW);
}
}
if (IsCloseAxis)
{
CloseAllAxis();
}
LogUtil.info(LOGGER, StoreName + "StopMove");
KNDIOMove(IO_Type.Door_Down, IO_VALUE.LOW);
KNDIOMove(IO_Type.Door_Up, IO_VALUE.LOW);
HDevelopExport.CloseAllCamera();
isInPro = false;
}
......@@ -1572,9 +1599,7 @@ namespace OnlineStore.DeviceLibrary
LOGGER.Error(StoreName + "HumidityProcess出错:" + ex.ToString());
}
}
#endregion
#endregion
#region 与服务器通信定时器,每1秒向服务器通知一次状态,同时执行出库操作
private bool isInProcess = false;
public void server_connect_timer_Tick(object sender, EventArgs e)
......@@ -1584,9 +1609,7 @@ namespace OnlineStore.DeviceLibrary
return;
}
//HumitureServer.RandomData(Config.GetTempAddrList());
HumitureController.QueryData();
HumidityProcess();
LedProcess();
isInProcess = true;
if (ACStoreManager.IsConnectServer)
{
......@@ -1599,6 +1622,9 @@ namespace OnlineStore.DeviceLibrary
LOGGER.Error("定时给服务器发送消息出错:", ex);
}
}
HumitureController.QueryData();
HumidityProcess();
LedProcess();
isInProcess = false;
}
......
......@@ -29,8 +29,8 @@ namespace OnlineStore.DeviceLibrary
/// <summary>
/// 伺服运行时间列表,key=轴,key=开始时间,value=结束时间
/// </summary>
public Dictionary<int, Dictionary<DateTime, DateTime>> AxisRunTimeMap = new Dictionary<int, Dictionary<DateTime, DateTime>>();
public Dictionary<int, DateTime> AxisLastStartTime = new Dictionary<int, DateTime>();
//public Dictionary<int, Dictionary<DateTime, DateTime>> AxisRunTimeMap = new Dictionary<int, Dictionary<DateTime, DateTime>>();
//public Dictionary<int, DateTime> AxisLastStartTime = new Dictionary<int, DateTime>();
/// <summary>
/// 最后一次气压检测变为0的时间
......@@ -93,42 +93,42 @@ namespace OnlineStore.DeviceLibrary
return new TimeSpan(0);
}
}
/// <summary>
/// 获取轴已运行时间
/// </summary>
/// <returns></returns>
public TimeSpan GetAxisRunTime(int axisNo)
{
TimeSpan span = new TimeSpan(0);
try
{
if (AxisRunTimeMap.ContainsKey(axisNo))
{
Dictionary<DateTime, DateTime> moveMap = AxisRunTimeMap[axisNo];
if (moveMap != null && moveMap.Count > 0)
{
foreach (DateTime key in moveMap.Keys)
{
span += moveMap[key] - key;
}
}
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, ex.ToString());
}
return span;
}
///// <summary>
///// 获取轴已运行时间
///// </summary>
///// <returns></returns>
//public TimeSpan GetAxisRunTime(int axisNo)
//{
// TimeSpan span = new TimeSpan(0);
// try
// {
// if (AxisRunTimeMap.ContainsKey(axisNo))
// {
// Dictionary<DateTime, DateTime> moveMap = AxisRunTimeMap[axisNo];
// if (moveMap != null && moveMap.Count > 0)
// {
// foreach (DateTime key in moveMap.Keys)
// {
// span += moveMap[key] - key;
// }
// }
// }
// }
// catch (Exception ex)
// {
// LogUtil.error(LOGGER, ex.ToString());
// }
// return span;
//}
protected void AddAxisMoveTime(ConfigMoveAxis axis)
{
int axisNo = axis.GetAxisValue();
if (AxisLastStartTime.ContainsKey(axisNo))
{
AxisLastStartTime.Remove(axisNo);
}
AxisLastStartTime.Add(axisNo, DateTime.Now);
//int axisNo = axis.GetAxisValue();
//if (AxisLastStartTime.ContainsKey(axisNo))
//{
// AxisLastStartTime.Remove(axisNo);
//}
//AxisLastStartTime.Add(axisNo, DateTime.Now);
}
......@@ -233,7 +233,7 @@ namespace OnlineStore.DeviceLibrary
/// <summary>
/// 停止所有运动
/// </summary>
public abstract void StopMove();
public abstract void StopMove( bool IsCloseAxis);
/// <summary>
/// 重置处理
......@@ -248,12 +248,12 @@ namespace OnlineStore.DeviceLibrary
/// 松下伺服轴原点返回运动,等待收到反馈后才会返回
/// </summary>
protected void ACAxisHomeMove(ConfigMoveAxis moveAxis)
{
{
moveAxis.TargetPosition = 0;
LogUtil.debug(LOGGER, moveAxis.DisplayStr + "speed[" + moveAxis.TargetSpeed + "]开始原点返回");
StoreMove.WaitList.Add(WaitResultInfo.WaitAxis(moveAxis, true));
AddAxisMoveTime(moveAxis);
ACServerManager.HomeMove(moveAxis.DeviceName, (short)moveAxis.GetAxisValue(), moveAxis.HomeHighSpeed);
StoreMove.WaitList.Add(WaitResultInfo.WaitAxis(moveAxis, true));
}
/// <summary>
......@@ -261,10 +261,10 @@ namespace OnlineStore.DeviceLibrary
/// </summary>
protected void ACAxisMove(ConfigMoveAxis moveAxis, int targetPosition, int targetSpeed)
{
StoreMove.WaitList.Add(WaitResultInfo.WaitAxis(moveAxis, targetPosition, targetSpeed));
moveAxis.TargetPosition = targetPosition;
AddAxisMoveTime(moveAxis);
ACServerManager.AbsMove(moveAxis.DeviceName, moveAxis.GetAxisValue(), targetPosition,targetSpeed);
StoreMove.WaitList.Add(WaitResultInfo.WaitAxis(moveAxis, targetPosition, targetSpeed));
}
/// <summary>
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!