Commit 48e51a7a LN

io模块增加重连功能

1 个父辈 2929183d
......@@ -30,7 +30,7 @@ namespace OnlineStore.DeviceLibrary
double result = Math.Round((aiValue - defaultValue) / xishu, 2);
return result;
}
public abstract void ConnectionIP(string ioIp);
public abstract void StartConnect(params string[] ioIp);
public abstract void CloseConnect();
......
......@@ -15,7 +15,30 @@ namespace OnlineStore.DeviceLibrary
private List<int> AIValList = null;
private int AILength = 4;
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;
try
......@@ -35,9 +58,12 @@ namespace OnlineStore.DeviceLibrary
bool result = AIBox.Connect();
if (result)
{
if (needConIp.Contains(ioIp))
{
needConIp.Remove(ioIp);
}
LogUtil.info("第【" + i + "】次连接IO模块[" + ioIp + "][" + autoMS + "]成功:" + AIBox.ErrInfo);
Thread.Sleep(10);
break;
return true;
}
else
{
......@@ -50,8 +76,27 @@ namespace OnlineStore.DeviceLibrary
{
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()
{
try
......
......@@ -27,6 +27,52 @@ namespace OnlineStore.DeviceLibrary
private object DILock = "";
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)
{
AIOBOX aioBox = null;
......@@ -49,11 +95,6 @@ namespace OnlineStore.DeviceLibrary
{
DOValueMap.Remove(ioIp);
}
try
{
// Create new modbus master and add event functions
aioBox = new AIOBOX();
aioBox.IP = ioIp;
int DIMS = ConfigAppSettings.GetIntValue("DIMS");
if (DIMS < 20)
{
......@@ -64,9 +105,17 @@ namespace OnlineStore.DeviceLibrary
{
DOMS = 200;
}
// bool rtn = aioBox.AutoIP(ioIp);
int DILength = StoreManager.Config.GetDILength(ioIp);
int DOLength = StoreManager.Config.GetDOLength(ioIp);
string logName = "IO模块[" + ioIp + "] DI[" + DILength + "] DO[" + DOLength + "],[" + DIMS + "] [" + DOMS + "]";
try
{
// Create new modbus master and add event functions
aioBox = new AIOBOX();
aioBox.IP = ioIp;
// bool rtn = aioBox.AutoIP(ioIp);
aioBox.SetInput(Asa.IOModule.Box_Type.DI, DILength);
aioBox.SetOutput(Asa.IOModule.Box_Type.DO, DOLength);
aioBox.AutoReadInput(true, DIMS);
......@@ -78,29 +127,34 @@ namespace OnlineStore.DeviceLibrary
// aioBox.Log_Out_Event += AioBox_Log_Out_Event;
//aioBox.Log_RxTx_Event += AioBox_Log_RxTx_Event;
AIOMap.Add(ioIp, aioBox);
LogUtil.info("开始连接IO模块[" + ioIp + "][" + DIMS + "][" + DOMS + "],尝试重连三次");
LogUtil.debug("开始连接" + logName + ",尝试重连5次");
for (int i = 1; i <= 3; i++)
{
bool result = aioBox.Connect();
if (result)
{
LogUtil.info("第【" + i + "】次连接IO模块【" + ioIp + "】成功:" + aioBox.ErrInfo);
LogUtil.info("第【" + i + "】次连接 " + logName + " 成功:" + aioBox.ErrInfo);
Thread.Sleep(10);
//读取所有的DO
ReadAllDI(ioIp, 0);
if (IoIPLIst.Contains(ioIp))
{
IoIPLIst.Remove(ioIp);
}
break;
}
else
{
LogUtil.error("第【" + i + "】次连接IO模块【" + ioIp + "】失败:" + aioBox.ErrInfo + "");
LogUtil.error("第【" + i + "】次连接 " + logName + " 失败:" + aioBox.ErrInfo + "");
}
Thread.Sleep(10);
Thread.Sleep(2);
}
}
catch (Exception error)
{
LogUtil.error(LOGGER, "连接IO模块[" + ioIp + "]出错:" + error.ToString());
LogUtil.error(LOGGER, "连接IO模块 " + logName + " 出错:" + error.ToString());
}
}
......@@ -155,6 +209,7 @@ namespace OnlineStore.DeviceLibrary
{
if (sta != null && sta.Length >= StoreManager.Config.GetDILength(ip))
{
string updateDi = "[" + ip + "]:";
bool needUpdate = false;
List<Box_Sta> newList = new List<Box_Sta>();
newList.AddRange(sta);
......@@ -166,6 +221,14 @@ namespace OnlineStore.DeviceLibrary
}
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++)
{
if (!(oldList[i].Equals(newList[i])))
......@@ -245,13 +308,6 @@ namespace OnlineStore.DeviceLibrary
}
public override void ConnectionIOList(List<string> DIONameList)
{
foreach (string ip in DIONameList)
{
ConnectionIP(ip);
}
}
//关闭所有的DO
public override void CloseAllDO()
......@@ -451,11 +507,11 @@ namespace OnlineStore.DeviceLibrary
{
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))
{
return GetDOValue(configIO.IO_IP, configIO.SlaveID, configIO.GetIOAddr());
return GetDOValue(configIO.DeviceName, configIO.SlaveID, configIO.GetIOAddr());
}
}
catch (Exception ex)
......
......@@ -29,7 +29,7 @@ namespace OnlineStore.DeviceLibrary
private ushort port = 502;
//public static bool IsNeedReadAI = true;
public override void ConnectionIP(string ioIp)
public override void StartConnect(params string[] ioIps)
{
if (timer == null)
{
......@@ -40,6 +40,8 @@ namespace OnlineStore.DeviceLibrary
timer.Enabled = true;
}
AITcpClient MBmaster = null;
foreach (string ioIp in ioIps)
{
if (mastMap.ContainsKey(ioIp))
{
MBmaster = mastMap[ioIp];
......@@ -73,11 +75,13 @@ namespace OnlineStore.DeviceLibrary
Thread.Sleep(10);
ReadAll(ioIp);
}
catch (Exception error)
{
LogUtil.error("连接AI模块[" + ioIp + "]出错:" + error.ToString());
}
}
}
private void ReadAll(string ioIp)
{
......@@ -145,7 +149,7 @@ namespace OnlineStore.DeviceLibrary
else if (span.TotalSeconds > 3)
{
LogUtil.error("AI模块" + IP + "当前没有连上,重连" + IP);
ConnectionIP(IP);
StartConnect(IP);
PreCheckTime = DateTime.Now;
}
}
......
......@@ -93,7 +93,7 @@ namespace OnlineStore.DeviceLibrary
//初始化 //连接设备
IOManager.instance.ConnectionIOList(Config.DIODeviceNameList);
AIManager.Init();
AIManager.Instance.ConnectionIP(Config.AIDevice_IP);
AIManager.Instance.StartConnect(Config.AIDevice_IP);
//scanSocket.OnScanRevice += onCodeReceived;
mainTimer.Enabled = false;
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!