Commit 1040b75b 顾剑亮

Handle WriteDO Not SendDO

1 个父辈 cc200872
此文件类型无法预览
...@@ -41,7 +41,7 @@ namespace Asa.IOModule ...@@ -41,7 +41,7 @@ namespace Asa.IOModule
private readonly Box_Type _typeOutput; //输出类型 private readonly Box_Type _typeOutput; //输出类型
private readonly byte[] _addressInput; //输入地址 private readonly byte[] _addressInput; //输入地址
private readonly byte[] _addressOutput; //输出地址 private readonly byte[] _addressOutput; //输出地址
private const int SEND_SLEEP = 50; //每条命令发送的间隔,不能小于15,会出现IO接收不到的情况,小于30时,会出现接收数据连包的情况 private const int SEND_SLEEP = 50; //每条命令发送的间隔
private const int PORT = 502; //端口 private const int PORT = 502; //端口
private const int UPLOAD_TIME = 8000; //8秒 private const int UPLOAD_TIME = 8000; //8秒
...@@ -339,58 +339,77 @@ namespace Asa.IOModule ...@@ -339,58 +339,77 @@ namespace Asa.IOModule
} }
else else
{ {
if (writeTimer >= 3) //连续发送WriteDO3次,强制发送DO if (_writeDO.TryDequeue(out buff))
{ {
buff = GetReadDO_Command();
writeTimer = 0;
} }
else else
{ {
if (_writeDO.TryDequeue(out buff)) continue;
{
writeTimer++;
}
else
{
buff = GetReadDO_Command();
writeTimer = 0;
}
} }
//2020年5月23日
// if (writeTimer >= 3) //连续发送WriteDO3次,强制发送DO
// {
// buff = GetReadDO_Command();
// writeTimer = 0;
// }
// else
// {
// if (_writeDO.TryDequeue(out buff))
// {
// writeTimer++;
// }
// else
// {
// buff = GetReadDO_Command();
// writeTimer = 0;
// }
// }
} }
time += SEND_SLEEP; time += SEND_SLEEP;
} }
else else
{ {
if (writeTimer >= 3) //连续发送WriteDO3次,强制发送DI和DO if (_writeDO.TryDequeue(out buff))
{ {
if (shift)
buff = GetReadDI_Command();
else
buff = GetReadDO_Command();
shift = !shift;
if (writeStop)
writeTimer = 0;
else
writeStop = true;
} }
else else
{ {
if (_writeDO.TryDequeue(out buff)) buff = GetReadDI_Command();
{
writeTimer++;
writeStop = false;
}
else
{
if (shift)
buff = GetReadDI_Command();
else
buff = GetReadDO_Command();
shift = !shift;
writeTimer = 0;
}
} }
//2020年5月23日
//if (writeTimer >= 3) //连续发送WriteDO3次,强制发送DI和DO
//{
// if (shift)
// buff = GetReadDI_Command();
// else
// buff = GetReadDO_Command();
// shift = !shift;
// if (writeStop)
// writeTimer = 0;
// else
// writeStop = true;
//}
//else
//{
// if (_writeDO.TryDequeue(out buff))
// {
// writeTimer++;
// writeStop = false;
// }
// else
// {
// if (shift)
// buff = GetReadDI_Command();
// else
// buff = GetReadDO_Command();
// shift = !shift;
// writeTimer = 0;
// }
//}
} }
...@@ -465,19 +484,16 @@ namespace Asa.IOModule ...@@ -465,19 +484,16 @@ 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) _receive.AddRange(temp);
while (_receive.Count > 6) //分解连包
{ {
_receive.AddRange(temp); int len = _receive[5] + 6;
while (_receive.Count > 6) //分解连包 if (len > _receive.Count) break;
{ byte[] cmd = new byte[len];
int len = _receive[5] + 6; _receive.CopyTo(0, cmd, 0, len);
if (len > _receive.Count) break; _receive.RemoveRange(0, len);
byte[] cmd = new byte[len]; log.Debug("Receive: " + HexBuff(cmd));
_receive.CopyTo(0, cmd, 0, len); System.Threading.Tasks.Task.Run(() => CommandProcess(cmd));
_receive.RemoveRange(0, len);
log.Debug("Receive: " + HexBuff(cmd));
System.Threading.Tasks.Task.Run(() => CommandProcess(cmd));
}
} }
} }
} }
...@@ -557,6 +573,17 @@ namespace Asa.IOModule ...@@ -557,6 +573,17 @@ namespace Asa.IOModule
} }
else if (cmd[7] == 5) else if (cmd[7] == 5)
{ {
idx = Array.FindIndex(_addressOutput, s => s == cmd[9]);
if (idx != -1)
{
Box_Sta[] staDO = new Box_Sta[_stateDO.Length];
lock (_stateDO)
{
_stateDO[idx] = cmd[10] == 0xFF ? Box_Sta.On : Box_Sta.Off;
Array.Copy(_stateDO, staDO, staDO.Length);
}
DO_Changed_Event?.Invoke(this, staDO);
}
} }
} }
catch (Exception ex) catch (Exception ex)
......
...@@ -32,5 +32,5 @@ using System.Runtime.InteropServices; ...@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
//通过使用 "*",如下所示: //通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.4.0.0")] [assembly: AssemblyVersion("2.4.0.2")]
[assembly: AssemblyFileVersion("2.4.0.0")] [assembly: AssemblyFileVersion("2.4.0.2")]
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!