Commit 55efe6d1 几米阳光

AIO模块修改

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