Commit eaa1096b 几米阳光

伺服通信使用最新代码

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