Commit 8fb3faf1 几米阳光

serialBean改为Monitor加锁

1 个父辈 3de4897c
...@@ -226,53 +226,53 @@ namespace OnlineStore.Common ...@@ -226,53 +226,53 @@ namespace OnlineStore.Common
#endregion #endregion
#region 发送数据string类型 #region 发送数据string类型
public void SendData(string data) //public void SendData(string data)
{ //发送数据 //{ //发送数据
if (_serialPort.IsOpen) // if (_serialPort.IsOpen)
{ // {
lock (lockObj) // lock (lockObj)
{ // {
_serialPort.Write(data); // _serialPort.Write(data);
System.Threading.Thread.Sleep(10); // System.Threading.Thread.Sleep(10);
} // }
} // }
} //}
#endregion #endregion
#region 发送数据byte类型 #region 发送数据byte类型
/// <summary> ///// <summary>
/// 数据发送 ///// 数据发送
/// </summary> ///// </summary>
/// <param name="data">要发送的数据字节</param> ///// <param name="data">要发送的数据字节</param>
public void SendData(byte[] data, int offset, int count) //public void SendData(byte[] data, int offset, int count)
{ //{
string strSend = ""; // string strSend = "";
for (int i = 0; i < data.Length; i++) // for (int i = 0; i < data.Length; i++)
{ // {
strSend += string.Format("{0:X2} ", data[i]); // strSend += string.Format("{0:X2} ", data[i]);
} // }
LOGGER.Debug("【" + _serialPort.PortName + "】发送数据【" + strSend + "】"); // LOGGER.Debug("【" + _serialPort.PortName + "】发送数据【" + strSend + "】");
lock (lockObj) // lock (lockObj)
{ // {
try // try
{ // {
if (_serialPort.IsOpen) // if (_serialPort.IsOpen)
{ // {
_serialPort.DiscardInBuffer();//清空接收缓冲区 // _serialPort.DiscardInBuffer();//清空接收缓冲区
_serialPort.Write(data, offset, count); // _serialPort.Write(data, offset, count);
System.Threading.Thread.Sleep(10); // System.Threading.Thread.Sleep(10);
} // }
} // }
catch (Exception ex) // catch (Exception ex)
{ // {
_serialPort.DiscardOutBuffer(); // _serialPort.DiscardOutBuffer();
LogUtil.error(LOGGER, "SendData ERROR:" + ex.ToString(), 21); // LogUtil.error(LOGGER, "SendData ERROR:" + ex.ToString(), 21);
} // }
} // }
} //}
#endregion #endregion
#region 发送命令 #region 发送命令
/// <summary> /// <summary>
...@@ -287,8 +287,10 @@ namespace OnlineStore.Common ...@@ -287,8 +287,10 @@ namespace OnlineStore.Common
isOk = false; isOk = false;
if (_serialPort.IsOpen) if (_serialPort.IsOpen)
{ {
lock (lockObj) //lock (lockObj)
if(Monitor.TryEnter(lockObj,10))
{ {
//Monitor.Enter(lockObj);
try try
{ {
_serialPort.DiscardInBuffer(); //清空接收缓冲区 _serialPort.DiscardInBuffer(); //清空接收缓冲区
...@@ -323,8 +325,16 @@ namespace OnlineStore.Common ...@@ -323,8 +325,16 @@ namespace OnlineStore.Common
isOk = false; isOk = false;
LogUtil.error(LOGGER, "SendCommand ERROR:" + ex.ToString(), 20); LogUtil.error(LOGGER, "SendCommand ERROR:" + ex.ToString(), 20);
} }
finally
{
Monitor.Exit(lockObj);
}
} }
else
{
LogUtil.error(PortName + " 发送数据" + ByteToString(SendData) + "失败,未得到锁");
}
} }
return -1; return -1;
} }
...@@ -348,8 +358,9 @@ namespace OnlineStore.Common ...@@ -348,8 +358,9 @@ namespace OnlineStore.Common
if (_serialPort.IsOpen) if (_serialPort.IsOpen)
{ {
lock (lockObj) if (Monitor.TryEnter(lockObj, 10))
{ {
//Monitor.Enter(lockObj);
try try
{ {
_serialPort.DiscardInBuffer(); //清空接收缓冲区 _serialPort.DiscardInBuffer(); //清空接收缓冲区
...@@ -384,11 +395,18 @@ namespace OnlineStore.Common ...@@ -384,11 +395,18 @@ namespace OnlineStore.Common
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error(PortName + " 发送数据" + ByteToString(SendData) + " 出错:"+ex.ToString());
//throw ex;
} }
finally
{
Monitor.Exit(lockObj);
}
}
else
{
LogUtil.error(PortName + " 发送数据" + ByteToString(SendData) + "失败,未得到锁");
} }
} }
return -1; return -1;
} }
......
...@@ -29,8 +29,8 @@ namespace OnlineStore.DeviceLibrary ...@@ -29,8 +29,8 @@ namespace OnlineStore.DeviceLibrary
/// <summary> /// <summary>
/// 伺服运行时间列表,key=轴,key=开始时间,value=结束时间 /// 伺服运行时间列表,key=轴,key=开始时间,value=结束时间
/// </summary> /// </summary>
public Dictionary<int, Dictionary<DateTime, DateTime>> AxisRunTimeMap = new Dictionary<int, Dictionary<DateTime, DateTime>>(); //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, DateTime> AxisLastStartTime = new Dictionary<int, DateTime>();
/// <summary> /// <summary>
/// 最后一次气压检测变为0的时间 /// 最后一次气压检测变为0的时间
...@@ -93,42 +93,42 @@ namespace OnlineStore.DeviceLibrary ...@@ -93,42 +93,42 @@ namespace OnlineStore.DeviceLibrary
return new TimeSpan(0); return new TimeSpan(0);
} }
} }
/// <summary> ///// <summary>
/// 获取轴已运行时间 ///// 获取轴已运行时间
/// </summary> ///// </summary>
/// <returns></returns> ///// <returns></returns>
public TimeSpan GetAxisRunTime(int axisNo) //public TimeSpan GetAxisRunTime(int axisNo)
{ //{
TimeSpan span = new TimeSpan(0); // TimeSpan span = new TimeSpan(0);
try // try
{ // {
if (AxisRunTimeMap.ContainsKey(axisNo)) // if (AxisRunTimeMap.ContainsKey(axisNo))
{ // {
Dictionary<DateTime, DateTime> moveMap = AxisRunTimeMap[axisNo]; // Dictionary<DateTime, DateTime> moveMap = AxisRunTimeMap[axisNo];
if (moveMap != null && moveMap.Count > 0) // if (moveMap != null && moveMap.Count > 0)
{ // {
foreach (DateTime key in moveMap.Keys) // foreach (DateTime key in moveMap.Keys)
{ // {
span += moveMap[key] - key; // span += moveMap[key] - key;
} // }
} // }
} // }
} // }
catch (Exception ex) // catch (Exception ex)
{ // {
LogUtil.error(LOGGER, ex.ToString()); // LogUtil.error(LOGGER, ex.ToString());
} // }
return span; // return span;
} //}
protected void AddAxisMoveTime(ConfigMoveAxis axis) protected void AddAxisMoveTime(ConfigMoveAxis axis)
{ {
int axisNo = axis.GetAxisValue(); //int axisNo = axis.GetAxisValue();
if (AxisLastStartTime.ContainsKey(axisNo)) //if (AxisLastStartTime.ContainsKey(axisNo))
{ //{
AxisLastStartTime.Remove(axisNo); // AxisLastStartTime.Remove(axisNo);
} //}
AxisLastStartTime.Add(axisNo, DateTime.Now); //AxisLastStartTime.Add(axisNo, DateTime.Now);
} }
...@@ -233,7 +233,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -233,7 +233,7 @@ namespace OnlineStore.DeviceLibrary
/// <summary> /// <summary>
/// 停止所有运动 /// 停止所有运动
/// </summary> /// </summary>
public abstract void StopMove(); public abstract void StopMove( bool IsCloseAxis);
/// <summary> /// <summary>
/// 重置处理 /// 重置处理
...@@ -248,12 +248,12 @@ namespace OnlineStore.DeviceLibrary ...@@ -248,12 +248,12 @@ namespace OnlineStore.DeviceLibrary
/// 松下伺服轴原点返回运动,等待收到反馈后才会返回 /// 松下伺服轴原点返回运动,等待收到反馈后才会返回
/// </summary> /// </summary>
protected void ACAxisHomeMove(ConfigMoveAxis moveAxis) protected void ACAxisHomeMove(ConfigMoveAxis moveAxis)
{ {
moveAxis.TargetPosition = 0; moveAxis.TargetPosition = 0;
LogUtil.debug(LOGGER, moveAxis.DisplayStr + "speed[" + moveAxis.TargetSpeed + "]开始原点返回"); LogUtil.debug(LOGGER, moveAxis.DisplayStr + "speed[" + moveAxis.TargetSpeed + "]开始原点返回");
StoreMove.WaitList.Add(WaitResultInfo.WaitAxis(moveAxis, true));
AddAxisMoveTime(moveAxis); AddAxisMoveTime(moveAxis);
ACServerManager.HomeMove(moveAxis.DeviceName, (short)moveAxis.GetAxisValue(), moveAxis.HomeHighSpeed); ACServerManager.HomeMove(moveAxis.DeviceName, (short)moveAxis.GetAxisValue(), moveAxis.HomeHighSpeed);
StoreMove.WaitList.Add(WaitResultInfo.WaitAxis(moveAxis, true));
} }
/// <summary> /// <summary>
...@@ -261,10 +261,10 @@ namespace OnlineStore.DeviceLibrary ...@@ -261,10 +261,10 @@ namespace OnlineStore.DeviceLibrary
/// </summary> /// </summary>
protected void ACAxisMove(ConfigMoveAxis moveAxis, int targetPosition, int targetSpeed) protected void ACAxisMove(ConfigMoveAxis moveAxis, int targetPosition, int targetSpeed)
{ {
StoreMove.WaitList.Add(WaitResultInfo.WaitAxis(moveAxis, targetPosition, targetSpeed));
moveAxis.TargetPosition = targetPosition; moveAxis.TargetPosition = targetPosition;
AddAxisMoveTime(moveAxis); AddAxisMoveTime(moveAxis);
ACServerManager.AbsMove(moveAxis.DeviceName, moveAxis.GetAxisValue(), targetPosition,targetSpeed); ACServerManager.AbsMove(moveAxis.DeviceName, moveAxis.GetAxisValue(), targetPosition,targetSpeed);
StoreMove.WaitList.Add(WaitResultInfo.WaitAxis(moveAxis, targetPosition, targetSpeed));
} }
/// <summary> /// <summary>
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!