Commit eaa1096b 几米阳光

伺服通信使用最新代码

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