Commit eaa1096b 几米阳光

伺服通信使用最新代码

1 个父辈 0522a86d
...@@ -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>
...@@ -282,13 +282,15 @@ namespace OnlineStore.Common ...@@ -282,13 +282,15 @@ namespace OnlineStore.Common
/// <param name="ReceiveData">接收数据</param> /// <param name="ReceiveData">接收数据</param>
/// <param name="Overtime">超时时间</param> /// <param name="Overtime">超时时间</param>
/// <returns></returns> /// <returns></returns>
public int SendCommand(byte[] SendData, ref byte[] ReceiveData, int Overtime,out bool isOk) public int SendCommand(byte[] SendData, ref byte[] ReceiveData, int Overtime, out bool isOk)
{ {
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,6 +325,14 @@ namespace OnlineStore.Common ...@@ -323,6 +325,14 @@ 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) + "失败,未得到锁");
} }
} }
...@@ -342,14 +352,15 @@ namespace OnlineStore.Common ...@@ -342,14 +352,15 @@ namespace OnlineStore.Common
{ {
if (_serialPort == null) if (_serialPort == null)
{ {
LogUtil.error(PortName+" 发送数据"+ByteToString(SendData)+ "失败,_serialPort=null"); LogUtil.error(PortName + " 发送数据" + ByteToString(SendData) + "失败,_serialPort=null");
return -1; return -1;
} }
if (_serialPort.IsOpen) if (_serialPort.IsOpen)
{ {
lock (lockObj) if (Monitor.TryEnter(lockObj, 10))
{ {
//Monitor.Enter(lockObj);
try try
{ {
_serialPort.DiscardInBuffer(); //清空接收缓冲区 _serialPort.DiscardInBuffer(); //清空接收缓冲区
...@@ -384,9 +395,16 @@ namespace OnlineStore.Common ...@@ -384,9 +395,16 @@ 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) + "失败,未得到锁");
} }
} }
......
...@@ -81,7 +81,7 @@ namespace OnlineStore.Common ...@@ -81,7 +81,7 @@ namespace OnlineStore.Common
if (lastErrorLogTime.ContainsKey(type)) if (lastErrorLogTime.ContainsKey(type))
{ {
TimeSpan span = DateTime.Now - lastErrorLogTime[type]; TimeSpan span = DateTime.Now - lastErrorLogTime[type];
if (span.TotalSeconds < 5) if (span.TotalSeconds < 10)
{ {
return; return;
} }
......
...@@ -200,7 +200,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -200,7 +200,7 @@ namespace OnlineStore.DeviceLibrary
{ {
WarnMsg = StoreName + "【" + StoreMove.MoveType + "】【" + StoreMove.MoveStep + "】等待超时 [" + NotOkMsg WarnMsg = StoreName + "【" + StoreMove.MoveType + "】【" + StoreMove.MoveStep + "】等待超时 [" + NotOkMsg
+ "]已等待[" + Math.Round(span.TotalSeconds, 1) + "]秒"; + "]已等待[" + Math.Round(span.TotalSeconds, 1) + "]秒";
LogUtil.error(LOGGER, WarnMsg); LogUtil.error(LOGGER, WarnMsg,100);
Alarm(StoreAlarmType.IoSingleTimeOut, "", WarnMsg, StoreMove.MoveType); Alarm(StoreAlarmType.IoSingleTimeOut, "", WarnMsg, StoreMove.MoveType);
} }
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!