Commit 55efe6d1 几米阳光

AIO模块修改

1 个父辈 7871d050
此文件类型无法预览
...@@ -9,15 +9,16 @@ ...@@ -9,15 +9,16 @@
AIOBOX-32 AIOBOX-32
</summary> </summary>
</member> </member>
<member name="T:Asa.AIOBOX.AIOBOX_32.DI_Changed"> <member name="T:Asa.AIOBOX.AIOBOX_32.DIO_Changed">
<summary> <summary>
自动读取DI委托 自动读取DIO委托
</summary> </summary>
<param name="sta">状态</param> <param name="staDI">所有DI状态</param>
<param name="staDO">所有DO状态</param>
</member> </member>
<member name="E:Asa.AIOBOX.AIOBOX_32.DI_Changed_Event"> <member name="E:Asa.AIOBOX.AIOBOX_32.DIO_Changed_Event">
<summary> <summary>
自动读取DI事件触发 自动读取DIO事件触发
</summary> </summary>
</member> </member>
<member name="M:Asa.AIOBOX.AIOBOX_32.#ctor"> <member name="M:Asa.AIOBOX.AIOBOX_32.#ctor">
...@@ -55,6 +56,11 @@ ...@@ -55,6 +56,11 @@
错误信息 错误信息
</summary> </summary>
</member> </member>
<member name="P:Asa.AIOBOX.AIOBOX_32.AutoRead">
<summary>
自动读取DIO状态,触发DIO_Changed_Event
</summary>
</member>
<member name="M:Asa.AIOBOX.AIOBOX_32.Connect"> <member name="M:Asa.AIOBOX.AIOBOX_32.Connect">
<summary> <summary>
连接 连接
...@@ -148,14 +154,14 @@ ...@@ -148,14 +154,14 @@
监听结果线程,把读取到的数据保存到buff 监听结果线程,把读取到的数据保存到buff
</summary> </summary>
</member> </member>
<member name="M:Asa.AIOBOX.AIOBOX_32.Auto_Read_DI"> <member name="M:Asa.AIOBOX.AIOBOX_32.Auto_Read_DIO">
<summary> <summary>
自动读取DI线程 自动读取DIO线程
</summary> </summary>
</member> </member>
<member name="M:Asa.AIOBOX.AIOBOX_32.Trigger_DI"> <member name="M:Asa.AIOBOX.AIOBOX_32.Trigger_DIO">
<summary> <summary>
触发DI改变事件 触发DIO改变事件
</summary> </summary>
</member> </member>
<member name="T:Asa.AIOBOX.Reg"> <member name="T:Asa.AIOBOX.Reg">
......
...@@ -13,24 +13,21 @@ namespace OnlineStore.DeviceLibrary ...@@ -13,24 +13,21 @@ namespace OnlineStore.DeviceLibrary
{ {
public class AIOBOXManager : IOManager public class AIOBOXManager : IOManager
{ {
public static int DefaultDICount = 16;
public static uint DefaultDOCount = 16;
public readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); public readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public Dictionary<string, AIOBOX_32> AIOMap = new Dictionary<string, AIOBOX_32>(); public Dictionary<string, AIOBOX_32> AIOMap = new Dictionary<string, AIOBOX_32>();
public string DefaultIP = "";
public Dictionary<string, Dictionary<Addr, Status>> DIValueMap = new Dictionary<string, Dictionary<Addr, Status>>();
public Dictionary<string, Dictionary<Addr, Status>> DOValueMap = new Dictionary<string, Dictionary<Addr, Status>>();
private object DIMapLock = ""; private object DIMapLock = "";
private object DOMapLock = ""; private object DOMapLock = "";
public System.Timers.Timer timer = null; public System.Timers.Timer timer = null;
private object DILock = "";
private object DOLock = "";
public void ConnectionIP(string ioIp ) public void ConnectionIP(string ioIp )
{ {
if (timer == null)
{
timer = new System.Timers.Timer();
timer.Interval = 300;
timer.AutoReset = true;
timer.Elapsed += timer_Elapsed;
timer.Enabled = true;
}
AIOBOX_32 aioBox = null; AIOBOX_32 aioBox = null;
if (AIOMap.ContainsKey(ioIp)) if (AIOMap.ContainsKey(ioIp))
{ {
...@@ -43,51 +40,104 @@ namespace OnlineStore.DeviceLibrary ...@@ -43,51 +40,104 @@ namespace OnlineStore.DeviceLibrary
} }
AIOMap.Remove(ioIp); AIOMap.Remove(ioIp);
} }
if (DIValueMap.ContainsKey(ioIp))
{
DIValueMap.Remove(ioIp);
}
if (DOValueMap.ContainsKey(ioIp))
{
DOValueMap.Remove(ioIp);
}
try try
{ {
// Create new modbus master and add event functions // Create new modbus master and add event functions
aioBox = new AIOBOX_32(); aioBox = new AIOBOX_32();
aioBox.IP = ioIp; aioBox.IP = ioIp;
bool result = aioBox.Connect(); DefaultIP = ioIp;
if (result) aioBox.AutoRead = true;
{ aioBox.DIO_Changed_Event += AioBox_DIO_Changed_Event;
AIOMap.Add(ioIp, aioBox); LogUtil.info("开始连接IO模块【" + ioIp + "】,尝试重连三次");
} for (int i = 1; i <= 3; i++)
else
{ {
LogUtil.error("连接IO模块【"+ioIp+"】失败:"+aioBox.ErrInfo); bool result = aioBox.Connect();
if (result)
{
AIOMap.Add(ioIp, aioBox);
LogUtil.info("第【"+i+"】次连接IO模块【" + ioIp + "】成功:" + aioBox.ErrInfo);
Thread.Sleep(10);
//读取所有的DO
ReadAllDI(ioIp, 0);
break;
}
else
{
LogUtil.error("第【" + i + "】次连接IO模块【" + ioIp + "】失败:" + aioBox.ErrInfo + "");
}
Thread.Sleep(10);
} }
Thread.Sleep(10);
//读取所有的DO
ReadAllDI(ioIp, 0);
} }
catch (Exception error) catch (Exception error)
{ {
LogUtil.error(LOGGER, "连接IO模块[" + ioIp + "]出错:" + error.ToString()); LogUtil.error(LOGGER, "连接IO模块[" + ioIp + "]出错:" + error.ToString());
} }
} }
/// <summary>
/// 判断Io模块是否连接 private void AioBox_DIO_Changed_Event(Status[] staDI, Status[] staDO)
/// </summary>
public bool IsConnection(string ip)
{ {
try try
{ {
if (AIOMap.ContainsKey(ip)) UpdateAllDI(DefaultIP, staDI);
UpdateAllDO(DefaultIP, staDO);
}catch(Exception ex)
{
LogUtil.error("AioBox_DIO_Changed_Event出错:"+ex.ToString());
}
}
private void UpdateAllDI(string ip, Status[] sta)
{
if (sta != null && sta.Length >= DefaultDICount)
{
lock (DIValueMap)
{ {
if (AIOMap[ip].IsConn) Dictionary<Addr, Status> keyValues = new Dictionary<Addr, Status>();
if (DIValueMap.ContainsKey(ip))
{
DIValueMap.Remove(ip);
}
ushort i = 0;
foreach (Status s in sta)
{ {
return true; Addr addr = GetAddr(i);
keyValues.Add(addr, s);
i++;
} }
DIValueMap.Add(ip, keyValues);
} }
} }
catch (Exception ex) }
private void UpdateAllDO(string ip, Status[] sta)
{
if (sta != null && sta.Length >= DefaultDOCount)
{ {
LogUtil.error(LOGGER, "出错啦:" + ex.ToString()); lock (DOLock)
{
Dictionary<Addr, Status> keyValues = new Dictionary<Addr, Status>();
if (DOValueMap.ContainsKey(ip))
{
DOValueMap.Remove(ip);
}
ushort i = (ushort)(DefaultDICount);
foreach (Status s in sta)
{
Addr addr = GetAddr(i);
keyValues.Add(addr, s);
i++;
}
DOValueMap.Add(ip, keyValues);
}
} }
return false;
} }
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{ {
...@@ -204,42 +254,106 @@ namespace OnlineStore.DeviceLibrary ...@@ -204,42 +254,106 @@ namespace OnlineStore.DeviceLibrary
} }
public override void ReadAllDI(string ioIp, byte slaveId) public override void ReadAllDI(string ioIp, byte slaveId)
{ {
try
{
AIOBOX_32 aioBox = getAIO(ioIp);
if (aioBox != null)
{
Status[] allDi;
if (aioBox.ReadDI(Addr.DI_1, DefaultDICount, out allDi))
{
UpdateAllDI(ioIp, allDi);
}
else
{
LogUtil.error("读取所有DI出错:" + aioBox.ErrInfo);
}
}
}
catch (Exception ex)
{
LogUtil.error("ReadAllDI出错:" + ioIp);
}
} }
public override void ReadAllDO(string ioIp, byte slaveId) public override void ReadAllDO(string ioIp, byte slaveId)
{ {
try
{
AIOBOX_32 aioBox = getAIO(ioIp);
if (aioBox != null)
{
Status[] allDO;
if (aioBox.ReadDO(Addr.DO_1, DefaultDOCount, out allDO))
{
UpdateAllDO(ioIp, allDO);
}
else
{
LogUtil.error("读取所有DO出错:" + aioBox.ErrInfo);
}
}
}
catch (Exception ex)
{
LogUtil.error("ReadAllDO出错:" + ioIp);
}
} }
public override IO_VALUE GetDOValue(string ioIP, byte slaveId, ushort StartAddress) public override IO_VALUE GetDOValue(string ioIP, byte slaveId, ushort StartAddress)
{ {
IO_VALUE value = IO_VALUE.LOW; IO_VALUE value = IO_VALUE.LOW;
AIOBOX_32 aioBox = getAIO(ioIP); try
if (aioBox != null)
{ {
Status status = Status.Off; AIOBOX_32 aioBox = getAIO(ioIP);
aioBox.ReadDO(GetAddr(StartAddress), out status); if (aioBox != null)
if (status.Equals(Status.On))
{ {
value = IO_VALUE.HIGH; Status status = Status.Off;
Addr addr = GetAddr(StartAddress);
if (DOValueMap.ContainsKey(ioIP) && DOValueMap[ioIP].TryGetValue(addr, out status))
{
}
else
{ aioBox.ReadDO(addr, out status); }
if (status.Equals(Status.On))
{
value = IO_VALUE.HIGH;
}
} }
} }
catch (Exception ex)
{
LogUtil.error("ReadDI 出错:" + ex.ToString());
}
return value; return value;
} }
public override IO_VALUE GetDIValue(string ioIP, byte slaveId, ushort StartAddress) public override IO_VALUE GetDIValue(string ioIP, byte slaveId, ushort StartAddress)
{ {
IO_VALUE value = IO_VALUE.LOW; IO_VALUE value = IO_VALUE.LOW;
AIOBOX_32 aioBox = getAIO(ioIP); try
if (aioBox != null)
{ {
Status status = Status.Off; AIOBOX_32 aioBox = getAIO(ioIP);
aioBox.ReadDI(GetAddr(StartAddress), out status); if (aioBox != null)
if (status.Equals(Status.On))
{ {
value = IO_VALUE.HIGH; Status status = Status.Off;
Addr addr = GetAddr(StartAddress);
if (DIValueMap.ContainsKey(ioIP) && DIValueMap[ioIP].TryGetValue(addr, out status))
{
}
else
{
aioBox.ReadDI(addr, out status);
}
if (status.Equals(Status.On))
{
value = IO_VALUE.HIGH;
}
} }
} }
catch (Exception ex)
{
LogUtil.error("ReadDI 出错:" + ex.ToString());
}
return value; return value;
} }
public override IO_VALUE GetIOValue(ConfigIO configIO) public override IO_VALUE GetIOValue(ConfigIO configIO)
...@@ -265,7 +379,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -265,7 +379,7 @@ namespace OnlineStore.DeviceLibrary
private Addr GetAddr(ushort StartAddress) private Addr GetAddr(ushort StartAddress)
{ {
return (Addr)(StartAddress - 1); return (Addr)(StartAddress );
} }
private Status GetStatus(IO_VALUE onOff) private Status GetStatus(IO_VALUE onOff)
{ {
......
...@@ -85,24 +85,25 @@ namespace OnlineStore.DeviceLibrary ...@@ -85,24 +85,25 @@ namespace OnlineStore.DeviceLibrary
//初始化摄像机配置 //初始化摄像机配置
string nameStr = ConfigAppSettings.GetValue(Setting_Init.CameraName); string nameStr = ConfigAppSettings.GetValue(Setting_Init.CameraName);
string codeStr = ConfigAppSettings.GetValue(Setting_Init.CodeType); string codeStr = ConfigAppSettings.GetValue(Setting_Init.CodeType);
CodeManager.LoadConfig(nameStr, codeStr);
//初始化 //连接设备 CodeManager.LoadConfig(nameStr, codeStr);
IOManager.instance.ConnectionKND(Config.DIODeviceNameList); Task.Factory.StartNew(delegate
{
timersTimer.Enabled = false; IOManager.instance.ConnectionKND(Config.DIODeviceNameList);
int isAuto = ConfigAppSettings.GetIntValue(Setting_Init.App_AutoRun); timersTimer.Enabled = false;
if (isAuto == 1) int isAuto = ConfigAppSettings.GetIntValue(Setting_Init.App_AutoRun);
{ if (isAuto == 1)
timersTimer.Enabled = true; {
} timersTimer.Enabled = true;
}
Thread.Sleep(300); Thread.Sleep(300);
//默认三个灯都亮 //默认三个灯都亮
KNDIOMove(IO_Type.Alarm_HddLed, IO_VALUE.HIGH); KNDIOMove(IO_Type.Alarm_HddLed, IO_VALUE.HIGH);
KNDIOMove(IO_Type.AutoRun_HddLed, IO_VALUE.HIGH); KNDIOMove(IO_Type.AutoRun_HddLed, IO_VALUE.HIGH);
KNDIOMove(IO_Type.RunSign_HddLed, IO_VALUE.HIGH); KNDIOMove(IO_Type.RunSign_HddLed, IO_VALUE.HIGH);
LogUtil.info("软件启动:默认三个灯都亮");
});
} }
private void IoCheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) private void IoCheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
......
...@@ -49,7 +49,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -49,7 +49,7 @@ namespace OnlineStore.DeviceLibrary
LoadCamera(); LoadCamera();
CodeLibrary.HDCodeLearnHelper.LoadConfig(nameStr, codeStr); CodeLibrary.HDCodeLearnHelper.LoadConfig(nameStr, codeStr);
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error("解析摄像机配置出错:" + ex.ToString()); LogUtil.error("解析摄像机配置出错:" + ex.ToString());
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!