Commit 48e51a7a LN

io模块增加重连功能

1 个父辈 2929183d
...@@ -30,7 +30,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -30,7 +30,7 @@ namespace OnlineStore.DeviceLibrary
double result = Math.Round((aiValue - defaultValue) / xishu, 2); double result = Math.Round((aiValue - defaultValue) / xishu, 2);
return result; return result;
} }
public abstract void ConnectionIP(string ioIp); public abstract void StartConnect(params string[] ioIp);
public abstract void CloseConnect(); public abstract void CloseConnect();
......
...@@ -15,7 +15,30 @@ namespace OnlineStore.DeviceLibrary ...@@ -15,7 +15,30 @@ namespace OnlineStore.DeviceLibrary
private List<int> AIValList = null; private List<int> AIValList = null;
private int AILength = 4; private int AILength = 4;
private Asa.IOModule.AIOBOX AIBox = null; private Asa.IOModule.AIOBOX AIBox = null;
public override void ConnectionIP(string ioIp) private System.Timers.Timer conTimer = null;
private List<string> needConIp = new List<string>();
public override void StartConnect(params string[] ipList)
{
if (conTimer == null)
{
conTimer = new System.Timers.Timer();
conTimer.AutoReset = true;
conTimer.Interval = 60000;
conTimer.Elapsed += ConTimer_Elapsed;
}
conTimer.Enabled = false;
needConIp = new List<string>(ipList);
foreach (string ip in ipList)
{
bool result = ConnectionIP(ip);
}
if (needConIp.Count > 0)
{
//启动定时器,1一分钟重连一次
conTimer.Start();
}
}
private bool ConnectionIP(string ioIp)
{ {
int autoMS = 150; int autoMS = 150;
try try
...@@ -35,9 +58,12 @@ namespace OnlineStore.DeviceLibrary ...@@ -35,9 +58,12 @@ namespace OnlineStore.DeviceLibrary
bool result = AIBox.Connect(); bool result = AIBox.Connect();
if (result) if (result)
{ {
if (needConIp.Contains(ioIp))
{
needConIp.Remove(ioIp);
}
LogUtil.info("第【" + i + "】次连接IO模块[" + ioIp + "][" + autoMS + "]成功:" + AIBox.ErrInfo); LogUtil.info("第【" + i + "】次连接IO模块[" + ioIp + "][" + autoMS + "]成功:" + AIBox.ErrInfo);
Thread.Sleep(10); return true;
break;
} }
else else
{ {
...@@ -50,8 +76,27 @@ namespace OnlineStore.DeviceLibrary ...@@ -50,8 +76,27 @@ namespace OnlineStore.DeviceLibrary
{ {
LogUtil.error("连接IO模块[" + ioIp + "][" + autoMS + "]出错:" + error.ToString()); LogUtil.error("连接IO模块[" + ioIp + "][" + autoMS + "]出错:" + error.ToString());
} }
return false;
}
private void ConTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
List<string> list = new List<string>(needConIp);
if (list.Count > 0)
{
foreach (string ip in list)
{
LogUtil.info("重连AOI AI 模块 :" + ip);
ConnectionIP(ip);
}
}
}
catch (Exception ex)
{
LogUtil.error("AOI AI ConTimer_Elapsed 出错: " + ex.ToString());
}
} }
public override void CloseConnect() public override void CloseConnect()
{ {
try try
......
...@@ -27,6 +27,52 @@ namespace OnlineStore.DeviceLibrary ...@@ -27,6 +27,52 @@ namespace OnlineStore.DeviceLibrary
private object DILock = ""; private object DILock = "";
private object DOLock = ""; private object DOLock = "";
private List<string> IoIPLIst = new List<string>();
private System.Timers.Timer conTimer = null;
public override void ConnectionIOList(List<string> DIONameList)
{
if (conTimer == null)
{
conTimer = new System.Timers.Timer();
conTimer.AutoReset = true;
conTimer.Interval = 60000;
conTimer.Elapsed += ConTimer_Elapsed;
}
conTimer.Enabled = false;
IoIPLIst = new List<string>(DIONameList);
foreach (string ip in DIONameList)
{
ConnectionIP(ip);
}
if (IoIPLIst.Count > 0)
{
//启动定时器,1一分钟重连一次
conTimer.Start();
}
}
private void ConTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
List<string> list = new List<string>(IoIPLIst);
if (list.Count > 0)
{
foreach (string ip in list)
{
LogUtil.info("重连AOI :" + ip);
ConnectionIP(ip);
}
}
}
catch (Exception ex)
{
LogUtil.error("AOI ConTimer_Elapsed 出错: " + ex.ToString());
}
}
public void ConnectionIP(string ioIp) public void ConnectionIP(string ioIp)
{ {
AIOBOX aioBox = null; AIOBOX aioBox = null;
...@@ -49,24 +95,27 @@ namespace OnlineStore.DeviceLibrary ...@@ -49,24 +95,27 @@ namespace OnlineStore.DeviceLibrary
{ {
DOValueMap.Remove(ioIp); DOValueMap.Remove(ioIp);
} }
int DIMS = ConfigAppSettings.GetIntValue("DIMS");
if (DIMS < 20)
{
DIMS = 20;
}
int DOMS = ConfigAppSettings.GetIntValue("DOMS");
if (DOMS < 200)
{
DOMS = 200;
}
int DILength = StoreManager.Config.GetDILength(ioIp);
int DOLength = StoreManager.Config.GetDOLength(ioIp);
string logName = "IO模块[" + ioIp + "] DI[" + DILength + "] DO[" + DOLength + "],[" + DIMS + "] [" + DOMS + "]";
try try
{ {
// Create new modbus master and add event functions // Create new modbus master and add event functions
aioBox = new AIOBOX(); aioBox = new AIOBOX();
aioBox.IP = ioIp; aioBox.IP = ioIp;
int DIMS = ConfigAppSettings.GetIntValue("DIMS");
if (DIMS < 20)
{
DIMS = 20;
}
int DOMS = ConfigAppSettings.GetIntValue("DOMS");
if (DOMS < 200)
{
DOMS = 200;
}
// bool rtn = aioBox.AutoIP(ioIp); // bool rtn = aioBox.AutoIP(ioIp);
int DILength = StoreManager.Config.GetDILength(ioIp);
int DOLength = StoreManager.Config.GetDOLength(ioIp);
aioBox.SetInput(Asa.IOModule.Box_Type.DI, DILength); aioBox.SetInput(Asa.IOModule.Box_Type.DI, DILength);
aioBox.SetOutput(Asa.IOModule.Box_Type.DO, DOLength); aioBox.SetOutput(Asa.IOModule.Box_Type.DO, DOLength);
aioBox.AutoReadInput(true, DIMS); aioBox.AutoReadInput(true, DIMS);
...@@ -75,32 +124,37 @@ namespace OnlineStore.DeviceLibrary ...@@ -75,32 +124,37 @@ namespace OnlineStore.DeviceLibrary
aioBox.DI_Changed_Event += AioBox_DI_Changed_Event; ; aioBox.DI_Changed_Event += AioBox_DI_Changed_Event; ;
aioBox.DO_Changed_Event += AioBox_DO_Changed_Event; aioBox.DO_Changed_Event += AioBox_DO_Changed_Event;
// aioBox.Log_Out_Event += AioBox_Log_Out_Event; // aioBox.Log_Out_Event += AioBox_Log_Out_Event;
//aioBox.Log_RxTx_Event += AioBox_Log_RxTx_Event; //aioBox.Log_RxTx_Event += AioBox_Log_RxTx_Event;
AIOMap.Add(ioIp, aioBox); AIOMap.Add(ioIp, aioBox);
LogUtil.info("开始连接IO模块[" + ioIp + "][" + DIMS + "][" + DOMS + "],尝试重连三次");
LogUtil.debug("开始连接" + logName + ",尝试重连5次");
for (int i = 1; i <= 3; i++) for (int i = 1; i <= 3; i++)
{ {
bool result = aioBox.Connect(); bool result = aioBox.Connect();
if (result) if (result)
{ {
LogUtil.info("第【" + i + "】次连接IO模块【" + ioIp + "】成功:" + aioBox.ErrInfo); LogUtil.info("第【" + i + "】次连接 " + logName + " 成功:" + aioBox.ErrInfo);
Thread.Sleep(10); Thread.Sleep(10);
//读取所有的DO //读取所有的DO
ReadAllDI(ioIp, 0); ReadAllDI(ioIp, 0);
if (IoIPLIst.Contains(ioIp))
{
IoIPLIst.Remove(ioIp);
}
break; break;
} }
else else
{ {
LogUtil.error("第【" + i + "】次连接IO模块【" + ioIp + "】失败:" + aioBox.ErrInfo + ""); LogUtil.error("第【" + i + "】次连接 " + logName + " 失败:" + aioBox.ErrInfo + "");
} }
Thread.Sleep(10); Thread.Sleep(2);
} }
} }
catch (Exception error) catch (Exception error)
{ {
LogUtil.error(LOGGER, "连接IO模块[" + ioIp + "]出错:" + error.ToString()); LogUtil.error(LOGGER, "连接IO模块 " + logName + " 出错:" + error.ToString());
} }
} }
...@@ -123,7 +177,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -123,7 +177,7 @@ namespace OnlineStore.DeviceLibrary
{ {
foreach (string str in s) foreach (string str in s)
{ {
// LogUtil.AIOLog.Debug("[" + box.IP + "]" + str); // LogUtil.AIOLog.Debug("[" + box.IP + "]" + str);
} }
} }
...@@ -155,6 +209,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -155,6 +209,7 @@ namespace OnlineStore.DeviceLibrary
{ {
if (sta != null && sta.Length >= StoreManager.Config.GetDILength(ip)) if (sta != null && sta.Length >= StoreManager.Config.GetDILength(ip))
{ {
string updateDi = "[" + ip + "]:";
bool needUpdate = false; bool needUpdate = false;
List<Box_Sta> newList = new List<Box_Sta>(); List<Box_Sta> newList = new List<Box_Sta>();
newList.AddRange(sta); newList.AddRange(sta);
...@@ -166,6 +221,14 @@ namespace OnlineStore.DeviceLibrary ...@@ -166,6 +221,14 @@ namespace OnlineStore.DeviceLibrary
} }
else else
{ {
//foreach(Box_Sta s in sta)
//{
// updateDi += s.ToString() + ",";
//}
//if (ip.Equals("192.168.201.61"))
//{
// LogUtil.info(updateDi);
//}
for (int i = 0; i < newList.Count; i++) for (int i = 0; i < newList.Count; i++)
{ {
if (!(oldList[i].Equals(newList[i]))) if (!(oldList[i].Equals(newList[i])))
...@@ -245,13 +308,6 @@ namespace OnlineStore.DeviceLibrary ...@@ -245,13 +308,6 @@ namespace OnlineStore.DeviceLibrary
} }
public override void ConnectionIOList(List<string> DIONameList)
{
foreach (string ip in DIONameList)
{
ConnectionIP(ip);
}
}
//关闭所有的DO //关闭所有的DO
public override void CloseAllDO() public override void CloseAllDO()
...@@ -372,7 +428,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -372,7 +428,7 @@ namespace OnlineStore.DeviceLibrary
AIOBOX aioBox = getAIO(ioIp); AIOBOX aioBox = getAIO(ioIp);
if (aioBox != null) if (aioBox != null)
{ {
// int startIndex = StoreManager.Config.GetDILength(ioIp); // int startIndex = StoreManager.Config.GetDILength(ioIp);
Box_Sta[] allDO = aioBox.ReadDO(0, StoreManager.Config.GetDOLength(ioIp)); Box_Sta[] allDO = aioBox.ReadDO(0, StoreManager.Config.GetDOLength(ioIp));
UpdateAllDO(ioIp, allDO); UpdateAllDO(ioIp, allDO);
} }
...@@ -392,8 +448,8 @@ namespace OnlineStore.DeviceLibrary ...@@ -392,8 +448,8 @@ namespace OnlineStore.DeviceLibrary
{ {
Box_Sta sta = Box_Sta.Off; Box_Sta sta = Box_Sta.Off;
// Box_Addr addr = GetAddr(StartAddress); // Box_Addr addr = GetAddr(StartAddress);
// int index = (int)StartAddress - (int)StoreManager.Config.GetDILength(ioIP); // int index = (int)StartAddress - (int)StoreManager.Config.GetDILength(ioIP);
if (DOValueMap.ContainsKey(ioIP) && DOValueMap[ioIP].Count > StartAddress) if (DOValueMap.ContainsKey(ioIP) && DOValueMap[ioIP].Count > StartAddress)
{ {
sta = DOValueMap[ioIP][StartAddress]; sta = DOValueMap[ioIP][StartAddress];
...@@ -451,11 +507,11 @@ namespace OnlineStore.DeviceLibrary ...@@ -451,11 +507,11 @@ namespace OnlineStore.DeviceLibrary
{ {
if (configIO.ProType.Equals(ConfigItemType.DI)) if (configIO.ProType.Equals(ConfigItemType.DI))
{ {
return GetDIValue(configIO.IO_IP, configIO.SlaveID, configIO.GetIOAddr()); return GetDIValue(configIO.DeviceName, configIO.SlaveID, configIO.GetIOAddr());
} }
else if (configIO.ProType.Equals(ConfigItemType.DO)) else if (configIO.ProType.Equals(ConfigItemType.DO))
{ {
return GetDOValue(configIO.IO_IP, configIO.SlaveID, configIO.GetIOAddr()); return GetDOValue(configIO.DeviceName, configIO.SlaveID, configIO.GetIOAddr());
} }
} }
catch (Exception ex) catch (Exception ex)
......
...@@ -29,7 +29,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -29,7 +29,7 @@ namespace OnlineStore.DeviceLibrary
private ushort port = 502; private ushort port = 502;
//public static bool IsNeedReadAI = true; //public static bool IsNeedReadAI = true;
public override void ConnectionIP(string ioIp) public override void StartConnect(params string[] ioIps)
{ {
if (timer == null) if (timer == null)
{ {
...@@ -40,42 +40,46 @@ namespace OnlineStore.DeviceLibrary ...@@ -40,42 +40,46 @@ namespace OnlineStore.DeviceLibrary
timer.Enabled = true; timer.Enabled = true;
} }
AITcpClient MBmaster = null; AITcpClient MBmaster = null;
if (mastMap.ContainsKey(ioIp)) foreach (string ioIp in ioIps)
{ {
MBmaster = mastMap[ioIp]; if (mastMap.ContainsKey(ioIp))
if (null != MBmaster)
{ {
MBmaster.disconnect(); MBmaster = mastMap[ioIp];
MBmaster.Dispose();
MBmaster = null; if (null != MBmaster)
lock (AIMapLock)
{ {
if (AIValueMap.ContainsKey(ioIp)) MBmaster.disconnect();
MBmaster.Dispose();
MBmaster = null;
lock (AIMapLock)
{ {
AIValueMap.Remove(ioIp); if (AIValueMap.ContainsKey(ioIp))
{
AIValueMap.Remove(ioIp);
}
} }
} }
mastMap.Remove(ioIp);
}
try
{
// Create new modbus master and add event functions
MBmaster = new AITcpClient(ioIp, port);
MBmaster.OnResponseData += new AITcpClient.ResponseData(MBmaster_OnResponseData);
MBmaster.OnException += new AITcpClient.ExceptionData(MBmaster_OnException);
MBmaster.autoConnectOfBreak = false;
mastMap.Add(ioIp, MBmaster);
LogUtil.info("连接AI模块[" + ioIp + "]成功");
Thread.Sleep(10);
WriteAIScope(ioIp, 1);
Thread.Sleep(10);
ReadAll(ioIp);
}
catch (Exception error)
{
LogUtil.error("连接AI模块[" + ioIp + "]出错:" + error.ToString());
} }
mastMap.Remove(ioIp);
}
try
{
// Create new modbus master and add event functions
MBmaster = new AITcpClient(ioIp, port);
MBmaster.OnResponseData += new AITcpClient.ResponseData(MBmaster_OnResponseData);
MBmaster.OnException += new AITcpClient.ExceptionData(MBmaster_OnException);
MBmaster.autoConnectOfBreak = false;
mastMap.Add(ioIp, MBmaster);
LogUtil.info("连接AI模块[" + ioIp + "]成功");
Thread.Sleep(10);
WriteAIScope(ioIp, 1);
Thread.Sleep(10);
ReadAll(ioIp);
}
catch (Exception error)
{
LogUtil.error("连接AI模块[" + ioIp + "]出错:" + error.ToString());
} }
} }
...@@ -145,7 +149,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -145,7 +149,7 @@ namespace OnlineStore.DeviceLibrary
else if (span.TotalSeconds > 3) else if (span.TotalSeconds > 3)
{ {
LogUtil.error("AI模块" + IP + "当前没有连上,重连" + IP); LogUtil.error("AI模块" + IP + "当前没有连上,重连" + IP);
ConnectionIP(IP); StartConnect(IP);
PreCheckTime = DateTime.Now; PreCheckTime = DateTime.Now;
} }
} }
......
...@@ -93,7 +93,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -93,7 +93,7 @@ namespace OnlineStore.DeviceLibrary
//初始化 //连接设备 //初始化 //连接设备
IOManager.instance.ConnectionIOList(Config.DIODeviceNameList); IOManager.instance.ConnectionIOList(Config.DIODeviceNameList);
AIManager.Init(); AIManager.Init();
AIManager.Instance.ConnectionIP(Config.AIDevice_IP); AIManager.Instance.StartConnect(Config.AIDevice_IP);
//scanSocket.OnScanRevice += onCodeReceived; //scanSocket.OnScanRevice += onCodeReceived;
mainTimer.Enabled = false; mainTimer.Enabled = false;
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!