Commit 7b083fb1 gujlg

修改IO

1 个父辈 d469e222
......@@ -34,7 +34,8 @@ namespace Asa.IOModule
private int _readDOSleep; //自动读取DO间隔
private List<string> _log; //日志
private List<string> _logRxTx; //日志
private List<byte[]> _send; //发送的命令
//private List<byte[]> _send; //发送的命令
private System.Collections.Concurrent.ConcurrentQueue<byte[]> _send;
private List<byte[]> _receive; //接收的数据
private Thread tSend; //发送命令处理
......@@ -117,7 +118,8 @@ namespace Asa.IOModule
_sta = new Box_Sta[32];
_log = new List<string>();
_logRxTx = new List<string>();
_send = new List<byte[]>();
//_send = new List<byte[]>();
_send = new System.Collections.Concurrent.ConcurrentQueue<byte[]>();
_receive = new List<byte[]>();
Type = Box_Type.DIO_32;
......@@ -288,7 +290,10 @@ namespace Asa.IOModule
_unrevd = 0;
_unrevdRemote = 0;
_flag = 0;
_send.Clear();
//_send.Clear();
while (_send.TryDequeue(out byte[] aa))
{ }
_receive.Clear();
_log.Clear();
_logRxTx.Clear();
......@@ -482,7 +487,9 @@ namespace Asa.IOModule
_log.Add(s);
}
_send.Add(buff);
//_send.Add(buff);
_send.Enqueue(buff);
if (_unrevdRemote == 0) ErrInfo = "OK";
return true;
}
......@@ -524,33 +531,59 @@ namespace Asa.IOModule
while (true)
{
if (_send.Count > 0)
//if (_send.Count > 0)
//{
// if (_send[0] != null)
// {
// try
// {
// _client.Send(_send[0]);
// if (LogOut)
// {
// byte[] bb = new byte[2];
// bb[0] = _send[0][1];
// bb[1] = _send[0][0];
// flag = BitConverter.ToUInt16(bb, 0);
// s = string.Format("{0:HH:mm:ss:fff} Send {1}", DateTime.Now, flag);
// _log.Add(s);
// _logRxTx.Add(flag + "," + _send[0][7]);
// }
// _send.RemoveAt(0);
// }
// catch (Exception ex)
// {
// ErrInfo = ex.Message;
// LogUtil.error(ex.ToString());
// _unrevdRemote = 1;
// break;
// }
// }
//}
bool rtn = _send.TryDequeue(out byte[] result);
if (rtn)
{
if (_send[0] != null)
try
{
try
{
_client.Send(_send[0]);
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = _send[0][1];
bb[1] = _send[0][0];
flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} Send {1}", DateTime.Now, flag);
_log.Add(s);
_logRxTx.Add(flag + "," + _send[0][7]);
}
_send.RemoveAt(0);
}
catch (Exception ex)
_client.Send(result);
if (LogOut)
{
ErrInfo = ex.Message;
LogUtil.error(ex.ToString());
_unrevdRemote = 1;
break;
byte[] bb = new byte[2];
bb[0] = result[1];
bb[1] = result[0];
flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} Send {1}", DateTime.Now, flag);
_log.Add(s);
_logRxTx.Add(flag + "," + result[7]);
}
}
catch (Exception ex)
{
ErrInfo = ex.Message;
LogUtil.error(ex.ToString());
_unrevdRemote = 1;
break;
}
}
Thread.Sleep(SEND_SLEEP);
......@@ -861,7 +894,8 @@ namespace Asa.IOModule
buff[11] = 8; //个数
else if (_type == Box_Type.DIO_32)
buff[11] = 16; //个数
_send.Add(buff);
//_send.Add(buff);
_send.Enqueue(buff);
}
Thread.Sleep(_readDISleep);
}
......@@ -887,7 +921,8 @@ namespace Asa.IOModule
buff[11] = 16; //个数
else if (_type == Box_Type.DO_16)
buff[11] = 16; //个数
_send.Add(buff);
//_send.Add(buff);
_send.Enqueue(buff);
}
Thread.Sleep(_readDOSleep);
}
......@@ -944,62 +979,68 @@ namespace Asa.IOModule
try
{
if (IsConn)
{
if (_unrevd > 10000 || _unrevdRemote > 0) //断开10s后重连
{
if (_unrevd > 10000 || _unrevdRemote > 0) //断开10s后重连
{
//临时
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (System.Reflection.PropertyInfo info in _client.GetType().GetProperties())
try {
dic.Add(info.Name, info.GetValue(_client) == null ? "null" : info.GetValue(_client).ToString());
} catch(Exception ex)
//临时
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("send.Count = ", _send.Count.ToString());
foreach (System.Reflection.PropertyInfo info in _client.GetType().GetProperties())
{
dic.Add("错误"+info.Name, ex.Message);
}
try
{
dic.Add(info.Name, info.GetValue(_client) == null ? "null" : info.GetValue(_client).ToString());
}
catch (Exception ex)
{
dic.Add("错误" + info.Name, ex.Message);
}
}
CloseConn();
Thread.Sleep(100);
if (Reconnect_Event != null)
{
loop = false;
conn = false;
do
CloseConn();
Thread.Sleep(100);
if (Reconnect_Event != null)
{
if (!loop)
loop = false;
conn = false;
do
{
if (_unrevdRemote == 0)
ErrInfo = "本地缓存连续10s未收到数据";
}
times++;
Reconnect_Event.Invoke(this, times, ref conn, dic);
if (conn)
{
rtn = Connect();
if (rtn)
if (!loop)
{
loop = false;
times = 0;
if (_unrevdRemote == 0)
ErrInfo = "本地缓存连续10s未收到数据";
}
times++;
Reconnect_Event.Invoke(this, times, ref conn, dic);
if (conn)
{
rtn = Connect();
if (rtn)
{
loop = false;
times = 0;
}
else
{
conn = false;
loop = true;
}
}
else
{
conn = false;
loop = true;
loop = false;
}
}
else
{
loop = false;
}
} while (loop);
} while (loop);
}
}
}
}
}
catch (Exception ex)
{
LogUtil.error("Reconn出错:"+ex.ToString());
LogUtil.error("Reconn出错:" + ex.ToString());
}
Thread.Sleep(1000);
}
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!