Commit 80c67da1 LN

IO模块增加重连

1 个父辈 7bdb0072
......@@ -7,21 +7,20 @@
<!--是否开机自动启动料仓-->
<add key="App_AutoRun" value="1"/>
<add key="App_Title" value="AC_SA_料仓_1"/>
<!-- 开始吹气的判断值(配置值=服务器发送的湿度值-开始吹气值)-->
<add key="StartBlowValue" value="4"/>
<!-- 停止吹气的判断值(配置值=服务器发送的湿度值-停止吹气值)-->
<add key="StopBlowValue" value="4"/>
<!--Server address-->
<add key="http.server" value="http://192.168.201.68/myproject/" />
<add key="http.server" value="http://192.168.101.11/myproject/" />
<!--storeType-->
<add key="store_count" value="1"/>
<!--start one store config-->
<add key="Store_Position_Config" value="\StoreConfig\AC\linePositions.csv"/>
<add key="Store_ConfigPath" value="\StoreConfig\AC\StoreConfig.csv"/>
<add key="Store_Type" value="RC_AC_SA"/>
<add key="Store_CID" value="rc1250ac-4"/>
<add key ="Store_ID" value ="4"/>
<add key="Store_CID" value="line-ac1"/>
<add key ="Store_ID" value ="1"/>
<!--end one store config-->
<add key="ACBaudRate" value="115200" />
<add key="InOutDefaultPosition" value="8000"/>
......@@ -32,7 +31,7 @@
<add key="HumitureControllerType" value="0"/>
<add key="UseAIOBOX" value="1"/>
<!--流水线地址和端口配置-->
<add key ="LineServerIp" value ="192.168.1.114"/>
<add key ="LineServerIp" value ="192.168.101.11"/>
<add key ="LineServerPort" value ="5246"/>
<!--是否调试状态-->
<add key ="IsInDebug" value ="1"/>
......@@ -40,7 +39,7 @@
</appSettings>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs/ACStore-RC1250-1.log"/>
<file value="logs/ACStore-line-ac1.log"/>
<param name="Encoding" value="UTF-8" />
<appendToFile value="true"/>
<rollingStyle value="Date"/>
......
using Asa.IOModule;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace OnlineStore.DeviceLibrary
{
public abstract class AIManager
{
public bool NeedShow = false;
public static AIManager Instance = null;
public static void Init()
{
bool isAIOBox = ConfigAppSettings.GetIntValue(Setting_Init.UseAIOBOX).Equals(1);
if (isAIOBox)
{
Instance = new AIOAIManager();
}
else
{
Instance = new KNDAIManager();
}
}
public static double ConvertAI(double aiValue, double defaultValue)
{
double xishu = (double)StoreManager.Config.AI_ConvertPosition;
double result = Math.Round((aiValue - defaultValue) / xishu, 2);
return result;
}
public abstract void StartConnect(params string[] ioIp);
public abstract void CloseConnect();
public abstract double GetAIValue(string ioiP,int index);
}
}
using Asa.IOModule;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class AIOAIManager:AIManager
{
private object AILock = "";
private List<int> AIValList = null;
private int AILength = 4;
private Asa.IOModule.AIOBOX AIBox = null;
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
{
AIValList = new List<int>();
AIBox = new Asa.IOModule.AIOBOX();
AIBox.IP = ioIp;
// bool rtn = AIBox.AutoIP(ioIp);
AIBox.SetInput(Asa.IOModule.Box_Type.AI, AILength);
AIBox.SetOutput(Asa.IOModule.Box_Type.DO, 0);
AIBox.AutoReadInput(true, autoMS);
AIBox.AI_Changed_Event += Box_AI_Changed_Event;
LogUtil.debug("开始连接AI模块[" + ioIp + "][" + autoMS + "],尝试重连三次");
for (int i = 1; i <= 3; i++)
{
bool result = AIBox.Connect();
if (result)
{
if (needConIp.Contains(ioIp))
{
needConIp.Remove(ioIp);
}
LogUtil.info("第【" + i + "】次连接IO模块[" + ioIp + "][" + autoMS + "]成功:" + AIBox.ErrInfo);
return true;
}
else
{
LogUtil.error("第【" + i + "】次连接IO模块[" + ioIp + "][" + autoMS + "]失败:" + AIBox.ErrInfo + "");
}
Thread.Sleep(10);
}
}
catch (Exception error)
{
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
{
if (AIBox != null)
{
AIBox.Close();
}
}
catch (Exception ex)
{
LogUtil.error("断开AI连接出错:" + ex.ToString());
}
}
private void Box_AI_Changed_Event(AIOBOX box, int[] val)
{
try
{
if (val != null && val.Length >= AILength)
{
lock (AILock)
{
AIValList = new List<int>();
AIValList.AddRange(val);
}
}
}
catch (Exception ex)
{
LogUtil.error("Box_AI_Changed_Event出错:" + ex.ToString());
}
}
public override double GetAIValue(string ioiP,int index)
{
if (AIValList != null && AIValList.Count > index)
{
return AIValList[index];
}
return -1;
}
//public override double ConvertAI(double aiValue, double defaultValue)
//{
// double xishu = (double)ConfigAppSettings.GetNumValue(Setting_Init.AI_ConvertPosition);
// double result = Math.Round((aiValue - defaultValue) / xishu, 2);
// return result;
//}
}
}
......@@ -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());
}
}
......@@ -123,7 +177,7 @@ namespace OnlineStore.DeviceLibrary
{
foreach (string str in s)
{
LogUtil.AIOLog.Debug("[" + box.IP + "]" + str);
// LogUtil.AIOLog.Debug("[" + box.IP + "]" + str);
}
}
......@@ -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)
......
......@@ -15,22 +15,21 @@ namespace OnlineStore.DeviceLibrary
/// <summary>
/// 康奈德IO控制模块
/// </summary>
public class KNDAIManager
public class KNDAIManager : AIManager
{
public static ushort DefaultAILength = 8;
public static byte DefualtSlaveID = 255;
private static string AIStartAddress = "0258";
public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static Dictionary<string, AITcpClient> mastMap = new Dictionary<string, AITcpClient>();
public static Dictionary<string, List<KNDAI>> AIValueMap = new Dictionary<string, List<KNDAI>>();
private static object AIMapLock = "";
public ushort DefaultAILength = 8;
public byte DefualtSlaveID = 255;
private string AIStartAddress = "0258";
public Dictionary<string, AITcpClient> mastMap = new Dictionary<string, AITcpClient>();
public Dictionary<string, List<KNDAI>> AIValueMap = new Dictionary<string, List<KNDAI>>();
private object AIMapLock = "";
public static System.Timers.Timer timer = null;
private static ushort port = 502;
public System.Timers.Timer timer = null;
private ushort port = 502;
//public static bool IsNeedReadAI = true;
public static void ConnectionIP(string ioIp)
public override void StartConnect(params string[] ioIps)
{
if (timer == null)
{
......@@ -41,6 +40,8 @@ namespace OnlineStore.DeviceLibrary
timer.Enabled = true;
}
AITcpClient MBmaster = null;
foreach (string ioIp in ioIps)
{
if (mastMap.ContainsKey(ioIp))
{
MBmaster = mastMap[ioIp];
......@@ -68,19 +69,21 @@ namespace OnlineStore.DeviceLibrary
MBmaster.OnException += new AITcpClient.ExceptionData(MBmaster_OnException);
MBmaster.autoConnectOfBreak = false;
mastMap.Add(ioIp, MBmaster);
LogUtil.info(LOGGER, "连接AI模块[" + ioIp + "]成功");
LogUtil.info("连接AI模块[" + ioIp + "]成功");
Thread.Sleep(10);
WriteAIScope(ioIp, 1);
Thread.Sleep(10);
ReadAll(ioIp);
}
catch (Exception error)
{
LogUtil.error(LOGGER, "连接AI模块[" + ioIp + "]出错:" + error.ToString());
LogUtil.error("连接AI模块[" + ioIp + "]出错:" + error.ToString());
}
}
}
private static void ReadAll(string ioIp)
private void ReadAll(string ioIp)
{
//读取所有的DO
ReadMultipleAI(ioIp, DefualtSlaveID, AIStartAddress, DefaultAILength);
......@@ -89,7 +92,7 @@ namespace OnlineStore.DeviceLibrary
/// <summary>
/// 判断Io模块是否连接
/// </summary>
public static bool IsConnection(string ip)
public bool IsConnection(string ip)
{
try
{
......@@ -110,14 +113,14 @@ namespace OnlineStore.DeviceLibrary
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "出错啦:" + ex.ToString());
LogUtil.error("出错啦:" + ex.ToString());
}
return false;
}
private static DateTime PreCheckTime = DateTime.Now;
private static bool isProcess = false;
public static bool NeedShow = false;
private static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
private DateTime PreCheckTime = DateTime.Now;
private bool isProcess = false;
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (isProcess)
{
......@@ -145,26 +148,26 @@ namespace OnlineStore.DeviceLibrary
}
else if (span.TotalSeconds > 3)
{
LogUtil.error(LOGGER, "AI模块" + IP + "当前没有连上,重连" + IP);
ConnectionIP(IP);
LogUtil.error("AI模块" + IP + "当前没有连上,重连" + IP);
StartConnect(IP);
PreCheckTime = DateTime.Now;
}
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "出错啦:" + ex.ToString());
LogUtil.error("出错啦:" + ex.ToString());
}
Thread.Sleep(2);
isProcess = false;
}
private static void WriteAIScope(string ioIp, int value)
private void WriteAIScope(string ioIp, int value)
{
// 0 表示 0 - 10V, 1 表示 0 - 5V
WriteAIScope(ioIp, DefualtSlaveID, "03EA", value);
}
private static void WriteAIScope(string ioIp, byte slaveId, string Adress, int value)
private void WriteAIScope(string ioIp, byte slaveId, string Adress, int value)
{
// 写 AI 模块采样范围,可以往寄存器里面写入 0 或 1。 0 表示 0 - 10V, 1 表示 0 - 5V。默
//认 0,写 AI 模块采样范围为 0 - 5V
......@@ -177,10 +180,10 @@ namespace OnlineStore.DeviceLibrary
}
else
{
LogUtil.error(LOGGER, "WriteAIScope出错没有连接AI模块:" + ioIp);
LogUtil.error("WriteAIScope出错没有连接AI模块:" + ioIp);
}
}
private static void ReadMultipleAI(string ioIp, byte slaveId, string StartAddress, int length)
private void ReadMultipleAI(string ioIp, byte slaveId, string StartAddress, int length)
{
ushort ID = 1;
AITcpClient MBmaster = null;
......@@ -191,10 +194,10 @@ namespace OnlineStore.DeviceLibrary
}
else
{
LogUtil.error(LOGGER, "ReadMultipleAI出错没有连接AI模块:" + ioIp);
LogUtil.error("ReadMultipleAI出错没有连接AI模块:" + ioIp);
}
}
public static void CloseAllConnection()
public override void CloseConnect()
{
List<AITcpClient> list = new List<AITcpClient>();
foreach (AITcpClient tcp in list)
......@@ -203,20 +206,14 @@ namespace OnlineStore.DeviceLibrary
}
mastMap.Clear();
}
public static double ConvertAI(double aiValue, double defaultValue)
{
double xishu = (double)ConfigAppSettings.GetNumValue(Setting_Init.AI_ConvertPosition);
double result = Math.Round((aiValue - defaultValue) / xishu, 2);
return result;
}
public static double GetAIValue(string ioiP, int index)
public override double GetAIValue(string ioiP, int index)
{
double aiValue = GetAIValue(ioiP, DefualtSlaveID, index - 1);
return Math.Round(aiValue, 2);
}
public static double GetAIValue(string ioIP, byte slaveId, int index)
public double GetAIValue(string ioIP, byte slaveId, int index)
{
double value = 0;
try
......@@ -240,7 +237,7 @@ namespace OnlineStore.DeviceLibrary
}
return value;
}
private static void ClearAIData(string ioIp)
private void ClearAIData(string ioIp)
{
byte[] datas = new byte[32];
for (int i = 0; i < datas.Length; i++)
......@@ -249,7 +246,7 @@ namespace OnlineStore.DeviceLibrary
}
SaveAIData(ioIp, 3, datas);
}
private static void SaveAIData(string ioIp, ushort ID, byte[] values)
private void SaveAIData(string ioIp, ushort ID, byte[] values)
{
try
{
......@@ -322,18 +319,18 @@ namespace OnlineStore.DeviceLibrary
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "SaveAIData出错:" + ex.ToString());
LogUtil.error("SaveAIData出错:" + ex.ToString());
}
}
private static float BitToFloat(string hexString)
private float BitToFloat(string hexString)
{
uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);
byte[] floatVals = BitConverter.GetBytes(num);
float f = BitConverter.ToSingle(floatVals, 0);
return f;
}
private static void MBmaster_OnResponseData(string ioIp, ushort ID, byte function, byte[] values, byte[] reviceData)
private void MBmaster_OnResponseData(string ioIp, ushort ID, byte function, byte[] values, byte[] reviceData)
{
try
{
......@@ -394,10 +391,10 @@ namespace OnlineStore.DeviceLibrary
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "处理接受数据出错:" + ex.ToString());
LogUtil.error("处理接受数据出错:" + ex.ToString());
}
}
private static void MBmaster_OnException(string ioIp, ushort id, byte function, byte exception, byte[] reviceData)
private void MBmaster_OnException(string ioIp, ushort id, byte function, byte exception, byte[] reviceData)
{
string exc = "Modbus says error: ";
switch (exception)
......@@ -415,7 +412,7 @@ namespace OnlineStore.DeviceLibrary
default:
break;
}
LOGGER.Error("接收数据出错:" + exc);
LogUtil.error("接收数据出错:" + exc);
}
}
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!