Commit eaa1096b 几米阳光

伺服通信使用最新代码

1 个父辈 0522a86d
...@@ -24,7 +24,7 @@ namespace OnlineStore.Common ...@@ -24,7 +24,7 @@ namespace OnlineStore.Common
//public event SerialErrorReceivedEventHandler Error; //public event SerialErrorReceivedEventHandler Error;
//接收事件是否有效 false表示有效 //接收事件是否有效 false表示有效
//public bool ReceiveEventFlag = false; //public bool ReceiveEventFlag = false;
#endregion #endregion
#region 获取串口名 #region 获取串口名
...@@ -52,7 +52,7 @@ namespace OnlineStore.Common ...@@ -52,7 +52,7 @@ namespace OnlineStore.Common
} }
} }
#endregion #endregion
#region 默认构造函数 #region 默认构造函数
///// <summary> ///// <summary>
///// 默认构造函数,操作COM1,速度为9600,没有奇偶校验,8位字节,停止位为1 "COM1", 9600, Parity.None, 8, StopBits.One ///// 默认构造函数,操作COM1,速度为9600,没有奇偶校验,8位字节,停止位为1 "COM1", 9600, Parity.None, 8, StopBits.One
...@@ -62,7 +62,7 @@ namespace OnlineStore.Common ...@@ -62,7 +62,7 @@ namespace OnlineStore.Common
// _serialPort = new SerialPort(); // _serialPort = new SerialPort();
//} //}
#endregion #endregion
#region 构造函数 #region 构造函数
///// <summary> ///// <summary>
///// 构造函数, ///// 构造函数,
...@@ -81,7 +81,7 @@ namespace OnlineStore.Common ...@@ -81,7 +81,7 @@ namespace OnlineStore.Common
// setSerialPort(); // setSerialPort();
//} //}
#endregion #endregion
#region 构造函数,可以自定义串口的初始化参数 #region 构造函数,可以自定义串口的初始化参数
/// <summary> /// <summary>
/// 构造函数,可以自定义串口的初始化参数 /// 构造函数,可以自定义串口的初始化参数
...@@ -99,7 +99,7 @@ namespace OnlineStore.Common ...@@ -99,7 +99,7 @@ namespace OnlineStore.Common
setSerialPort(); setSerialPort();
} }
#endregion #endregion
#region 析构函数 #region 析构函数
/// <summary> /// <summary>
/// 析构函数,关闭串口 /// 析构函数,关闭串口
...@@ -110,7 +110,7 @@ namespace OnlineStore.Common ...@@ -110,7 +110,7 @@ namespace OnlineStore.Common
_serialPort.Close(); _serialPort.Close();
} }
#endregion #endregion
#region 设置串口参数 #region 设置串口参数
/// <summary> /// <summary>
/// 设置串口参数 /// 设置串口参数
...@@ -148,7 +148,7 @@ namespace OnlineStore.Common ...@@ -148,7 +148,7 @@ namespace OnlineStore.Common
//设置触发DataReceived事件的字节数为1 //设置触发DataReceived事件的字节数为1
_serialPort.ReceivedBytesThreshold = 1; _serialPort.ReceivedBytesThreshold = 1;
//接收到一个字节时,也会触发DataReceived事件 //接收到一个字节时,也会触发DataReceived事件
// _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived); // _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
//接收数据出错,触发事件 //接收数据出错,触发事件
_serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(_serialPort_ErrorReceived); _serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(_serialPort_ErrorReceived);
//打开串口 //打开串口
...@@ -183,7 +183,7 @@ namespace OnlineStore.Common ...@@ -183,7 +183,7 @@ namespace OnlineStore.Common
return ok; return ok;
} }
#endregion #endregion
#region 关闭串口 #region 关闭串口
/// <summary> /// <summary>
/// 关闭串口资源,操作完成后,一定要关闭串口 /// 关闭串口资源,操作完成后,一定要关闭串口
...@@ -211,8 +211,8 @@ namespace OnlineStore.Common ...@@ -211,8 +211,8 @@ namespace OnlineStore.Common
} }
#endregion #endregion
#region 接收数据出错事件 #region 接收数据出错事件
/// <summary> /// <summary>
/// 接收数据出错事件 /// 接收数据出错事件
...@@ -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,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;
} }
...@@ -338,18 +348,19 @@ namespace OnlineStore.Common ...@@ -338,18 +348,19 @@ namespace OnlineStore.Common
/// <param name="Overtime">超时时间</param> /// <param name="Overtime">超时时间</param>
/// <param name="ReceiveLength">接收数据长度</param> /// <param name="ReceiveLength">接收数据长度</param>
/// <returns></returns> /// <returns></returns>
public int SendCommand(byte[] SendData, ref byte[] ReceiveData, int Overtime, int ReceiveLength) public int SendCommand(byte[] SendData, ref byte[] ReceiveData, int Overtime, int ReceiveLength)
{ {
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(); //清空接收缓冲区
...@@ -359,15 +370,15 @@ namespace OnlineStore.Common ...@@ -359,15 +370,15 @@ namespace OnlineStore.Common
if (ReceiveData == null) if (ReceiveData == null)
{ {
ReceiveData = new byte[ReceiveLength]; ReceiveData = new byte[ReceiveLength];
} }
while (num++ < Overtime) while (num++ < Overtime)
{ {
if (_serialPort.BytesToRead >= ReceiveData.Length) if (_serialPort.BytesToRead >= ReceiveData.Length)
break; break;
System.Threading.Thread.Sleep(1); System.Threading.Thread.Sleep(1);
} }
if (num >= Overtime) if (num >= Overtime)
{ {
LogUtil.error(PortName + " 发送数据" + ByteToString(SendData) + "等待接受数据超时"); LogUtil.error(PortName + " 发送数据" + ByteToString(SendData) + "等待接受数据超时");
} }
if (_serialPort.BytesToRead >= ReceiveData.Length) if (_serialPort.BytesToRead >= ReceiveData.Length)
...@@ -379,21 +390,28 @@ namespace OnlineStore.Common ...@@ -379,21 +390,28 @@ namespace OnlineStore.Common
{ {
ret = _serialPort.Read(ReceiveData, 0, _serialPort.BytesToRead); ret = _serialPort.Read(ReceiveData, 0, _serialPort.BytesToRead);
} }
return ret; return ret;
} }
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;
} }
#endregion #endregion
#region 获取串口 #region 获取串口
...@@ -439,7 +457,7 @@ namespace OnlineStore.Common ...@@ -439,7 +457,7 @@ namespace OnlineStore.Common
#endregion #endregion
#region 十六进制字符串转字节型 #region 十六进制字符串转字节型
/// <summary> /// <summary>
/// 打包方法,可以将十六制字符串转成byte[] ,字符串没有空格 /// 打包方法,可以将十六制字符串转成byte[] ,字符串没有空格
/// </summary> /// </summary>
...@@ -475,7 +493,7 @@ namespace OnlineStore.Common ...@@ -475,7 +493,7 @@ namespace OnlineStore.Common
return putout; return putout;
} }
#endregion #endregion
#region 字节型转十六进制字符串 #region 字节型转十六进制字符串
/// <summary> /// <summary>
/// 字节数组转16进制字符串 /// 字节数组转16进制字符串
...@@ -539,4 +557,4 @@ namespace OnlineStore.Common ...@@ -539,4 +557,4 @@ namespace OnlineStore.Common
#endregion #endregion
} }
} }
...@@ -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!