Commit ddb06505 LN

修改IO模块Listen方法

1 个父辈 6b1a51e0
...@@ -450,6 +450,7 @@ namespace Asa.IOModule ...@@ -450,6 +450,7 @@ namespace Asa.IOModule
/// </summary> /// </summary>
private void Listen() private void Listen()
{ {
int index = 0;
while (_loop) while (_loop)
{ {
Thread.Sleep(10); Thread.Sleep(10);
...@@ -465,7 +466,7 @@ namespace Asa.IOModule ...@@ -465,7 +466,7 @@ namespace Asa.IOModule
byte[] temp = new byte[count]; byte[] temp = new byte[count];
Array.Copy(buff, 0, temp, 0, count); Array.Copy(buff, 0, temp, 0, count);
lock (_receive) // lock (_receive)
{ {
_receive.AddRange(temp); _receive.AddRange(temp);
while (_receive.Count > 6) //分解连包 while (_receive.Count > 6) //分解连包
...@@ -475,12 +476,14 @@ namespace Asa.IOModule ...@@ -475,12 +476,14 @@ namespace Asa.IOModule
byte[] cmd = new byte[len]; byte[] cmd = new byte[len];
_receive.CopyTo(0, cmd, 0, len); _receive.CopyTo(0, cmd, 0, len);
_receive.RemoveRange(0, len); _receive.RemoveRange(0, len);
index++;
log.Debug("Receive: " + HexBuff(cmd)); log.Debug("Receive: " + HexBuff(cmd));
if (IP.Equals("192.168.101.22") && cmd.Length>7&& (cmd[7] == 2)) if (IP.Equals("192.168.101.22") && cmd.Length>7&& (cmd[7] == 2))
{ {
LogUtil.LOGGER.Info(LogName + " Listen 收到数据 " + HexBuff(cmd)); LogUtil.LOGGER.Info(LogName + " Listen 收到数据 ["+index+"] " + HexBuff(cmd));
} }
System.Threading.Tasks.Task.Run(() => CommandProcess(cmd)); CommandProcess(cmd, index);
// System.Threading.Tasks.Task.Run(() => CommandProcess(cmd, index));
} }
} }
} }
...@@ -498,7 +501,7 @@ namespace Asa.IOModule ...@@ -498,7 +501,7 @@ namespace Asa.IOModule
/// 接收到的命令处理方法,(task多线程) /// 接收到的命令处理方法,(task多线程)
/// </summary> /// </summary>
/// <param name="cmd"></param> /// <param name="cmd"></param>
private void CommandProcess(byte[] cmd) private void CommandProcess(byte[] cmd,int index)
{ {
string str; string str;
int idx = 0; int idx = 0;
...@@ -509,7 +512,7 @@ namespace Asa.IOModule ...@@ -509,7 +512,7 @@ namespace Asa.IOModule
if (cmd[7] == 1) //ReadDO if (cmd[7] == 1) //ReadDO
{ {
Box_Sta[] staDO = new Box_Sta[_stateDO.Length]; Box_Sta[] staDO = new Box_Sta[_stateDO.Length];
lock (_stateDO) // lock (_stateDO)
{ {
for (int i = 1; i <= count; i++) for (int i = 1; i <= count; i++)
{ {
...@@ -518,11 +521,11 @@ namespace Asa.IOModule ...@@ -518,11 +521,11 @@ namespace Asa.IOModule
for (int j = 0; j < 8; j++) //字节的0-7位 for (int j = 0; j < 8; j++) //字节的0-7位
{ {
int n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move; int n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_stateDO[idx++] = n == 1 ? Box_Sta.On : Box_Sta.Off; staDO[idx++] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++; move++;
} }
} }
Array.Copy(_stateDO, 0, staDO, 0, staDO.Length); Array.Copy(staDO, 0, _stateDO, 0, staDO.Length);
if (MonitorDO != null) if (MonitorDO != null)
{ {
str = "MonitorDO "; str = "MonitorDO ";
...@@ -536,7 +539,7 @@ namespace Asa.IOModule ...@@ -536,7 +539,7 @@ namespace Asa.IOModule
else if (cmd[7] == 2) //ReadDI else if (cmd[7] == 2) //ReadDI
{ {
Box_Sta[] staDI = new Box_Sta[_stateDI.Length]; Box_Sta[] staDI = new Box_Sta[_stateDI.Length];
lock (_stateDI) // lock (_stateDI)
{ {
for (int i = 1; i <= count; i++) for (int i = 1; i <= count; i++)
{ {
...@@ -545,11 +548,11 @@ namespace Asa.IOModule ...@@ -545,11 +548,11 @@ namespace Asa.IOModule
for (int j = 0; j < 8; j++) //字节的0-7位 for (int j = 0; j < 8; j++) //字节的0-7位
{ {
int n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move; int n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_stateDI[idx++] = n == 1 ? Box_Sta.On : Box_Sta.Off; staDI[idx++] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++; move++;
} }
} }
Array.Copy(_stateDI, 0, staDI, 0, staDI.Length); Array.Copy(staDI, 0, _stateDI, 0, staDI.Length);
if (MonitorDI != null) if (MonitorDI != null)
{ {
str = "MonitorDI "; str = "MonitorDI ";
...@@ -567,7 +570,7 @@ namespace Asa.IOModule ...@@ -567,7 +570,7 @@ namespace Asa.IOModule
{ {
msg += " " + (int)s; msg += " " + (int)s;
} }
LogUtil.LOGGER.Info(LogName + " DI_Changed_Event " + msg); LogUtil.LOGGER.Info(LogName + " DI_Changed_Event ["+index+"] " + msg);
} }
DI_Changed_Event?.Invoke(this, staDI); DI_Changed_Event?.Invoke(this, staDI);
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!