using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.NetworkInformation; using System.Data; using System.Drawing; using System.Threading; using System.Windows.Forms; using Crossing; using System.Data.SqlClient; using Siemens_S200_Serial; //2 using Siemens_net_Protocol;//3 using SCM_Protocol; //4 using Omron_Serial_Fins; //1 using Omron_Serial_Hostlink;//0 using Protocol.Socket_Uart.S7_200_PPI; using BllSql; using AGVSystem.Cls; using AGVSystem.A17168; using HxBusiness; namespace AGVSystem { public class Cls_A17168 { static Thread[] th_getData; public static DataSet ds_mileage = new DataSet(); public DataSet ds_Mileage { get { return ds_mileage; } set { ds_mileage = value; } } static string connectStr = ""; public string ConnectStr { get { return connectStr; } set { connectStr = value; } } static bool startFlag = false; /// /// AGV调度启动标志 /// public bool StartFlag { get { return startFlag; } set { startFlag = value; } } static int closedFlag = -1; /// /// AGV调度关闭标志 /// public int ClosedFlag { get { return closedFlag; } set { closedFlag = value; } } public Thread[] th_GetData { get { return th_getData; } set { th_getData = value; } } public static BllBase bBllBase = new BllBase(); public void AGV_start(object agvinfo) { agv newagv = new agv(); Cls.AgvInfo temp = (Cls.AgvInfo)agvinfo; if (temp.Type == 6) { th_GetData[temp.Agv_ID - 1] = new Thread(newagv.AGV_Data); } else { th_GetData[temp.Agv_ID - 1] = new Thread(newagv.AGV_Data_PLC); } th_GetData[temp.Agv_ID - 1].IsBackground = true; th_GetData[temp.Agv_ID - 1].Start(agvinfo); } public class agv { /// /// 单片机 /// /// public void AGV_Data(object agvinfo) { Cls.AgvInfo temp = (Cls.AgvInfo)agvinfo; int location_temp = 0; int location_value = 0; int[] param_info = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] Write_re = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int lockCode = 0; int errorcount = 0; int t = 0, t_temp = 0; Ping pingAgv = new Ping(); Soc_Client Link_To_PC = new Soc_Client(temp.IP, temp.Port); temp.Soc = Link_To_PC.clientSocket; string[] warn_info = { "", "", "", "", "", "", "", "" }; closedFlag = -1; while (true) { if (!temp.Enable) { Thread.Sleep(3000); if (temp.Location_Display != 0) { Crossing.Crossing.BlockCross(temp.AssemblyLine, 0, temp.Agv_ID, mainfrm.ds_CrossInfo); } temp.Location_Display = 0; temp.Cross_Location_Go = 0; continue; } if (!startFlag) { Thread.Sleep(500); continue; } try { if (temp.bPlcLinked || pingAgv.Send(temp.IP, 150).Status == IPStatus.Success) { //来到这里,表示PING成功。 if (!temp.bPlcLinked) { if (Link_To_PC.OpenLinkPLC()) { //来到这里,表示链路成功打开 temp.bPlcLinked = true; } else { //来到这里,表示要重新建立链路 temp.bPlcLinked = false; Link_To_PC.CloseLinkPLC(); Link_To_PC = new Soc_Client(temp.IP, temp.Port); temp.Soc = Link_To_PC.clientSocket; errorcount = 0; } } else { param_info[0] = 0; //来到这里,表示PLC的链路已经打开,可以直接读写数据 if ((temp.Type == 6 && 0 != Link_To_PC.ReadInfo(out param_info))) { //来到这里,表示读数据失败 errorcount++; if (errorcount >= 5) { //来到这里,表示一再失败,要重新建立链路 temp.bPlcLinked = false; errorcount = 0; Link_To_PC.CloseLinkPLC(); Link_To_PC = new Soc_Client(temp.IP, temp.Port); temp.Soc = Link_To_PC.clientSocket; temp.Warn_ID = "0000000000000000000000000000000010000000000000000000000000000000"; temp.Warn_Level = 4; temp.Warn_info = "网络异常"; //Func.db_conn.Command("INSERT INTO [Net_Log] ([dt] ,[AGV_ID] ,[Type]) VALUES ('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "','" + temp.Agv_ID + "','Read Fail')"); string strsql = "INSERT INTO[Net_Log] ([dt],[AGV_ID],[Type]) VALUES('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "', '" + temp.Agv_ID + "', 'Read Fail')"; UPDateDBList.sqlList.Add(strsql); } } else { errorcount = 0; location_value = temp.Type == 6 ? param_info[3] * 256 + param_info[2] : param_info[0]; location_temp = Func.GetLandMarkId(temp.AssemblyLine, location_value); if (temp.Location_Display != location_temp && location_temp != 0) { WriteTxt.SaveLog1("AGV经过地标:" + location_temp, temp.Agv_ID + "#Agv", "地标日志"); //地标信息 temp.Location_Display = location_temp; temp.Read_dt = DateTime.Now; UPDateDBList.upDbList.Add(new Location_Log(DateTime.Now, temp.Agv_ID, temp.Location_Display, location_value, 0, 0, 0)); } WriteTxt.SaveLog1("=========》(11):" + location_temp, temp.Agv_ID + "#Agv", "调试日志"); #region 路口放行判断 if (temp.Cross_Location_Go != location_temp && location_value > 0 && location_value < 2000) { #region 发动机限流 152 if (temp.AssemblyLine == 1 && temp.Location_Display == 152) { if (Agv_Land_Count(152) == 1) { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (Link_To_PC.WriteInfo(1, 0, 0) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 仪表线限流 136 else if (temp.AssemblyLine == 3 && temp.Location_Display == 136) { if (Agv_Land_Count(136) == 1) { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (Link_To_PC.WriteInfo(1, 0, 0) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 内饰一线 106 182 ,196 else if (temp.AssemblyLine == 5 && new int[] { 106, 182, 196 }.Contains(location_temp)) { if ((temp.Location_Display == 106 && Agv_Land_Count(106) == 1 && Agv_Land_Count(167) == 0) || (temp.Location_Display == 182 && Agv_Land_Count(182) == 1 && Agv_Land_Count(196) == 0) || (temp.Location_Display == 196 && Agv_Land_Count(196) == 1 && Agv_Land_Count(195) == 0)) { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (Link_To_PC.WriteInfo(1, 0, 0) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 内饰二线 180 else if (temp.AssemblyLine == 6 && new int[] { 180 }.Contains(location_temp)) { if ((temp.Location_Display == 180 && Agv_Land_Count(180) == 1)) { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (Link_To_PC.WriteInfo(1, 0, 0) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 最终线限流 500,174,156,154,216 else if (temp.AssemblyLine == 7 && new int[] { 500, 174, 156, 154, 216, 504 }.Contains(location_temp)) { if ((temp.Location_Display == 500 && Agv_Land_Count(500) == 1) || (temp.Location_Display == 174 && Agv_Land_Count(174) == 1) || (temp.Location_Display == 154 && Agv_Land_Count(154) == 1 && Agv_Land_Count(156) == 0) || (temp.Location_Display == 156 && Agv_Land_Count(156) == 1) || (temp.Location_Display == 216 && Agv_Land_Count(216) == 1) || (temp.Location_Display == 504 && Agv_Land_Count(504) == 1)) { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (Link_To_PC.WriteInfo(1, 0, 0) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 新增限流 else if (new int[] { 316, 328, 332, 300, 314, 310, 342 }.Contains(location_temp)) { if ((temp.Location_Display == 316 && GetAreaAGVNum(new int[] { 316, 320, 321 }) == 1) || (temp.Location_Display == 328 && GetAreaAGVNum(new int[] { 328, 332 }) == 1) || (temp.Location_Display == 332 && GetAreaAGVNum(new int[] { 332 }) == 1) || (temp.Location_Display == 300 && GetAreaAGVNum(new int[] { 300 }) == 1) || (temp.Location_Display == 310 && GetAreaAGVNum(new int[] { 310,312,313 }) == 1) || (temp.Location_Display == 342 && GetAreaAGVNum(new int[] { 342,304,305 }) == 1) || (temp.Location_Display == 314 && GetAreaAGVNum(new int[] { 314 }) == 1)) { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (Link_To_PC.WriteInfo(1, 0, 0) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 海康交管 else if (Frm_A17168.businessCtrl.apiModel.waitRfidList.Split(',').Contains(location_temp.ToString())) { WriteTxt.SaveLog1("=========》(12):" + location_temp, temp.Agv_ID + "#Agv", "调试日志"); if (Frm_A17168.businessCtrl == null) Frm_A17168.businessCtrl = new BusinessManage(); if (Frm_A17168.leavedDic == null) Frm_A17168.leavedDic = new Dictionary(); if (!Frm_A17168.leavedDic.ContainsKey(temp.Agv_ID)) Frm_A17168.leavedDic.Add(temp.Agv_ID, 0); bool flg = false; WriteTxt.SaveLog1(location_temp.ToString() + "开始请求放行", "放行日志" + (temp.Agv_ID), "插件日志"); //2秒请求一次 if (Frm_A17168.leavedDic[temp.Agv_ID] % Frm_A17168.businessCtrl.apiModel.requestSecond == 0) { flg = Frm_A17168.businessCtrl.RequestLeaved(temp.Agv_ID.ToString(), location_temp.ToString()); WriteTxt.SaveLog1(location_temp.ToString() + "请求离开返回的值" + flg, "放行日志" + (temp.Agv_ID), "插件日志"); } //放行 if (flg) { WriteTxt.SaveLog1("=========》(13):" + location_temp, temp.Agv_ID + "#Agv", "调试日志"); //登记第三方交管路口占用 TrafficModel trafficModel = Frm_A17168.autoLeavedDic.Find(_ => _.areaCode.Equals(location_temp.ToString())); if (trafficModel != null) { trafficModel.agvId = temp.Agv_ID; trafficModel.InArea = DateTime.Now; } WriteTxt.SaveLog1(location_temp.ToString() + "路口添加" + trafficModel?.areaCode + "(" + trafficModel.agvId + ")", "放行日志" + (temp.Agv_ID), "插件日志"); if (Link_To_PC.WriteInfo(1, 0, 0) == 0) { Frm_A17168.leavedDic[temp.Agv_ID] = 0; WriteTxt.SaveLog1(location_temp.ToString() + "路口已放行", "放行日志" + (temp.Agv_ID), "插件日志"); t_temp = 0; temp.Cross_Location_Go = location_temp; lockCode = location_temp; } } Frm_A17168.leavedDic[temp.Agv_ID]++; } // else if (location_temp == 271 || location_temp == 273) //{ else if (Frm_A17168.businessCtrl.apiModel.leavedRfidList.Split(',').Contains(location_temp.ToString())) { WriteTxt.SaveLog1("=========》(14):" + location_temp, temp.Agv_ID + "#Agv", "调试日志"); if (Frm_A17168.businessCtrl == null) Frm_A17168.businessCtrl = new BusinessManage(); if (Frm_A17168.leavedDic == null) Frm_A17168.leavedDic = new Dictionary(); if (!Frm_A17168.leavedDic.ContainsKey(temp.Agv_ID)) Frm_A17168.leavedDic.Add(temp.Agv_ID, 0); bool flg = false; WriteTxt.SaveLog1(location_temp.ToString() + "开始请求释放", "放行日志" + (temp.Agv_ID), "插件日志"); if (Frm_A17168.leavedDic[temp.Agv_ID] % Frm_A17168.businessCtrl.apiModel.requestSecond == 0) { flg = Frm_A17168.businessCtrl.ByLeaved(temp.Agv_ID.ToString(), location_temp.ToString(), (location_temp - 1).ToString()); WriteTxt.SaveLog1(location_temp.ToString() + "离开释放返回的值" + flg, "放行日志" + (temp.Agv_ID), "插件日志"); } //归还 if (flg) { WriteTxt.SaveLog1("=========》(15):" + location_temp, temp.Agv_ID + "#Agv", "调试日志"); Frm_A17168.leavedDic[temp.Agv_ID] = 0; temp.Cross_Location_Go = location_temp; lockCode = location_temp; //清除第三方交管路口占用 TrafficModel trafficModel2 = Frm_A17168.autoLeavedDic.Find(_ => _.areaCode.Equals(location_temp.ToString())); if (trafficModel2 != null) { trafficModel2.agvId = 0; } WriteTxt.SaveLog1(location_temp.ToString() + "路口已归还", "放行日志" + (temp.Agv_ID), "插件日志"); } Frm_A17168.leavedDic[temp.Agv_ID]++; } #endregion else { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (location_temp % 2 == 0) { WriteTxt.SaveLog1("准备放行" + location_temp, temp.Agv_ID + "#Agv", "AGV"); if (Link_To_PC.WriteInfo(1, 0, 0) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "路口已复位", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; t_temp = 0; } } else if (t < 0) { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 状态信息 string sWarn = Convert.ToString(param_info[11], 2).PadLeft(8, '0') + Convert.ToString(param_info[10], 2).PadLeft(8, '0') + Convert.ToString(param_info[9], 2).PadLeft(8, '0') + Convert.ToString(param_info[8], 2).PadLeft(8, '0') + Convert.ToString(param_info[7], 2).PadLeft(8, '0') + Convert.ToString(param_info[6], 2).PadLeft(8, '0') + Convert.ToString(param_info[5], 2).PadLeft(8, '0') + Convert.ToString(param_info[4], 2).PadLeft(8, '0'); ThreadPool.QueueUserWorkItem(delegate { Thread.Sleep(100); Warn_Deal(temp, sWarn); }); #endregion } } } else { if (temp.bPlcLinked) { errorcount++; if (errorcount > 10) { temp.Warn_ID = "0000000000000000000000000000000010000000000000000000000000000000"; temp.Warn_Level = 4; temp.Warn_info = "网络异常"; errorcount = 0; temp.bPlcLinked = false; Link_To_PC.CloseLinkPLC(); } } } } catch (Exception Err) { if (Err.TargetSite.Name.ToString() == "ConnectAsync") { temp.Warn_ID = "0000000000000000000000000000000010000000000000000000000000000000"; temp.Warn_Level = 4; temp.Warn_info = "网络异常"; errorcount = 0; temp.bPlcLinked = false; Link_To_PC.CloseLinkPLC(); } if (temp.bPlcLinked) { errorcount++; if (errorcount > 10) { temp.Warn_ID = "0000000000000000000000000000000010000000000000000000000000000000"; temp.Warn_Level = 4; temp.Warn_info = "网络异常"; errorcount = 0; temp.bPlcLinked = false; Link_To_PC.CloseLinkPLC(); //Crossing.Crossing.BlockCross(0, 0, temp.Agv_ID, true, ds_crossInfo, false); } } Thread.Sleep(5000); } Thread.Sleep(1000); } } public void AGV_Data_PLC(object agvinfo) { bool All_Agv_Stop = false;//一键停止 bool All_Agv_Close = false;//一键关机 Cls.AgvInfo temp = (Cls.AgvInfo)agvinfo; int location_temp = 0; int location_value = 0; int[] param_info = { 0 }; int[] Write_re = { 0 }; int lockCode = 0; int errorcount = 0; int t = 0, t_temp = 0; Ping pingAgv = new Ping(); S7_200SocketClient Link_To_PLC = new S7_200SocketClient(temp.IP, temp.Port); temp.Soc = Link_To_PLC.clientSocket; string[] warn_info = { "", "", "", "", "", "", "", "" }; closedFlag = -1; while (true) { if (!temp.Enable) { Thread.Sleep(3000); continue; } if (!startFlag) { Thread.Sleep(500); continue; } try { if (pingAgv.Send(temp.IP, 150).Status == IPStatus.Success) { //来到这里,表示PING成功。 if (!temp.bPlcLinked) { if (Link_To_PLC.OpenLinkPLC()) { //来到这里,表示链路成功打开 temp.bPlcLinked = true; } else { //来到这里,表示要重新建立链路 temp.bPlcLinked = false; Link_To_PLC.CloseLinkPLC(); Link_To_PLC = new S7_200SocketClient(temp.IP, temp.Port); temp.Soc = Link_To_PLC.clientSocket; errorcount = 0; } } else { param_info[0] = 0; //来到这里,表示PLC的链路已经打开,可以直接读写数据 if (0 != Link_To_PLC.ReadVW(2, 1, 104, out param_info)) { //来到这里,表示读数据失败 errorcount--; if (Math.Abs(errorcount) >= 5) { //来到这里,表示一再失败,要重新建立链路 temp.bPlcLinked = false; errorcount = 0; Link_To_PLC.CloseLinkPLC(); Link_To_PLC = new S7_200SocketClient(temp.IP, temp.Port); temp.Soc = Link_To_PLC.clientSocket; temp.Warn_ID = "0000000000000000000000000000000010000000000000000000000000000000"; temp.Warn_Level = 4; temp.Warn_info = "网络异常"; //Func.db_conn.Command("INSERT INTO [Net_Log] ([dt] ,[AGV_ID] ,[Type]) VALUES ('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "','" + temp.Agv_ID + "','Read Fail')"); string strsql = "INSERT INTO[Net_Log] ([dt],[AGV_ID],[Type]) VALUES('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "', '" + temp.Agv_ID + "', 'Read Fail')"; bBllBase.ExecuteNonQuery(strsql); } } else { location_temp = Func.GetLandMarkId(temp.AssemblyLine, param_info[0]); location_value = param_info[0]; if (temp.Location_Display != location_temp && location_temp != 0) { WriteTxt.SaveLog1("AGV经过地标:" + location_temp, temp.Agv_ID + "#Agv", "地标日志"); //地标信息 temp.Location_Display = location_temp; temp.Read_dt = DateTime.Now; //Func.db_conn.Command("INSERT INTO [Location_Log] ([Dt] ,[Agv_ID] ,[Loc_ID] ,[Value] ,[Loc_ID2] ,[Value2],[Value3]) VALUES ('" + // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'," + temp.Agv_ID + "," + location_temp + "," + location_value + "," + 0 + "," + 0 + "," + 0 + ")"); string strsql = "INSERT INTO [Location_Log] ([Dt] ,[Agv_ID] ,[Loc_ID] ,[Value] ,[Loc_ID2] ,[Value2],[Value3]) VALUES ('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'," + temp.Agv_ID + "," + location_temp + "," + location_value + "," + 0 + "," + 0 + "," + 0 + ")"; UPDateDBList.sqlList.Add(strsql); } if (Math.Abs(errorcount) > 0) errorcount = 0; WriteTxt.SaveLog1("=========》(21):" + location_temp, temp.Agv_ID + "#Agv", "调试日志"); #region 路口放行判断 if (temp.Cross_Location_Go != location_temp && location_value > 0 && location_value < 2000) { #region 发动机限流 152 if (temp.AssemblyLine == 1 && temp.Location_Display == 152) { if (Agv_Land_Count(152) == 1) { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (Link_To_PLC.WriteVW(2, 116, temp.Location_Display) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 仪表线限流 136 else if (temp.AssemblyLine == 3 && temp.Location_Display == 136) { if (Agv_Land_Count(136) == 1) { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (Link_To_PLC.WriteVW(2, 116, temp.Location_Display) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 内饰一线 106 182 ,196 else if (temp.AssemblyLine == 5 && new int[] { 106, 182, 196 }.Contains(location_temp)) { if ((temp.Location_Display == 106 && Agv_Land_Count(106) == 1 && Agv_Land_Count(167) == 0) || (temp.Location_Display == 182 && Agv_Land_Count(182) == 1 && Agv_Land_Count(196) == 0) || (temp.Location_Display == 196 && Agv_Land_Count(196) == 1 && Agv_Land_Count(195) == 0)) { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (Link_To_PLC.WriteVW(2, 116, temp.Location_Display) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 内饰二线 180 else if (temp.AssemblyLine == 6 && new int[] { 180 }.Contains(location_temp)) { if ((temp.Location_Display == 180 && Agv_Land_Count(180) == 1)) { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (Link_To_PLC.WriteVW(2, 116, temp.Location_Display) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 最终线限流 500,174,156,154,216 else if (temp.AssemblyLine == 7 && new int[] { 500, 174, 156, 154, 216, 504 }.Contains(location_temp)) { if ((temp.Location_Display == 500 && Agv_Land_Count(500) == 1) || (temp.Location_Display == 174 && Agv_Land_Count(174) == 1) || (temp.Location_Display == 154 && Agv_Land_Count(154) == 1 && Agv_Land_Count(156) == 0) || (temp.Location_Display == 156 && Agv_Land_Count(156) == 1) || (temp.Location_Display == 216 && Agv_Land_Count(216) == 1) || (temp.Location_Display == 504 && Agv_Land_Count(504) == 1)) { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (Link_To_PLC.WriteVW(2, 116, temp.Location_Display) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 新增限流 else if (new int[] { 316, 328, 332, 300, 314 }.Contains(location_temp)) { if ((temp.Location_Display == 316 && GetAreaAGVNum(new int[] { 316, 320 }) == 1) || (temp.Location_Display == 328 && GetAreaAGVNum(new int[] { 328, 332 }) == 1) || (temp.Location_Display == 332 && GetAreaAGVNum(new int[] { 332 }) == 1) || (temp.Location_Display == 300 && GetAreaAGVNum(new int[] { 300 }) == 1) || (temp.Location_Display == 314 && GetAreaAGVNum(new int[] { 314 }) == 1)) { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (Link_To_PLC.WriteVW(2, 116, temp.Location_Display) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 海康交管 else if (Frm_A17168.businessCtrl.apiModel.waitRfidList.Split(',').Contains(location_temp.ToString())) { WriteTxt.SaveLog1("=========》(22):" + location_temp, temp.Agv_ID + "#Agv", "调试日志"); if (Frm_A17168.businessCtrl == null) Frm_A17168.businessCtrl = new BusinessManage(); if (Frm_A17168.leavedDic == null) Frm_A17168.leavedDic = new Dictionary(); if (!Frm_A17168.leavedDic.ContainsKey(temp.Agv_ID)) Frm_A17168.leavedDic.Add(temp.Agv_ID, 0); bool flg = false; WriteTxt.SaveLog1(location_temp.ToString() + "开始请求放行", "放行日志" + (temp.Agv_ID), "插件日志"); //2秒请求一次 if (Frm_A17168.leavedDic[temp.Agv_ID] % Frm_A17168.businessCtrl.apiModel.requestSecond == 0) { flg = Frm_A17168.businessCtrl.RequestLeaved(temp.Agv_ID.ToString(), location_temp.ToString()); WriteTxt.SaveLog1(location_temp.ToString() + "请求离开返回的值" + flg, "放行日志" + (temp.Agv_ID), "插件日志"); } //放行 if (flg) { // if (Link_To_PLC.WriteGo(temp.Agv_ID) == 0) // { // Frm_A17168.leavedDic[temp.Agv_ID] = 0; // WriteTxt.SaveLog1(location_temp.ToString() + "路口已放行", "放行日志" + (temp.Agv_ID), "插件日志"); // t_temp = 0; // temp.Cross_Location_Go = location_temp; // } WriteTxt.SaveLog1("=========》(23):" + location_temp, temp.Agv_ID + "#Agv", "调试日志"); //登记第三方交管路口占用 TrafficModel trafficModel = Frm_A17168.autoLeavedDic.Find(_ => _.areaCode.Equals(location_temp.ToString())); if (trafficModel != null) { trafficModel.agvId = temp.Agv_ID; trafficModel.InArea = DateTime.Now; } WriteTxt.SaveLog1(location_temp.ToString() + "路口添加" + trafficModel?.areaCode + "(" + trafficModel.agvId + ")", "放行日志" + (temp.Agv_ID), "插件日志"); if (Link_To_PLC.WriteVW(2, 116, temp.Location_Display) == 0) { Frm_A17168.leavedDic[temp.Agv_ID] = 0; WriteTxt.SaveLog1(location_temp.ToString() + "路口已放行", "放行日志" + (temp.Agv_ID), "插件日志"); t_temp = 0; temp.Cross_Location_Go = location_temp; lockCode = location_temp; } } Frm_A17168.leavedDic[temp.Agv_ID]++; } // else if (location_temp == 271 || location_temp == 273) //{ else if (Frm_A17168.businessCtrl.apiModel.leavedRfidList.Split(',').Contains(location_temp.ToString())) { WriteTxt.SaveLog1("=========》(24):" + location_temp, temp.Agv_ID + "#Agv", "调试日志"); if (Frm_A17168.businessCtrl == null) Frm_A17168.businessCtrl = new BusinessManage(); if (Frm_A17168.leavedDic == null) Frm_A17168.leavedDic = new Dictionary(); if (!Frm_A17168.leavedDic.ContainsKey(temp.Agv_ID)) Frm_A17168.leavedDic.Add(temp.Agv_ID, 0); bool flg = false; WriteTxt.SaveLog1(location_temp.ToString() + "开始请求释放", "放行日志" + (temp.Agv_ID), "插件日志"); if (Frm_A17168.leavedDic[temp.Agv_ID] % Frm_A17168.businessCtrl.apiModel.requestSecond == 0) { flg = Frm_A17168.businessCtrl.ByLeaved(temp.Agv_ID.ToString(), location_temp.ToString(), (location_temp - 1).ToString()); WriteTxt.SaveLog1(location_temp.ToString() + "离开释放返回的值" + flg, "放行日志" + (temp.Agv_ID), "插件日志"); } //归还 if (flg) { WriteTxt.SaveLog1("=========》(25):" + location_temp, temp.Agv_ID + "#Agv", "调试日志"); Frm_A17168.leavedDic[temp.Agv_ID] = 0; temp.Cross_Location_Go = location_temp; lockCode = location_temp; //清除第三方交管路口占用 TrafficModel trafficModel2 = Frm_A17168.autoLeavedDic.Find(_ => _.areaCode.Equals(location_temp.ToString())); if (trafficModel2 != null) { trafficModel2.agvId = 0; } WriteTxt.SaveLog1(location_temp.ToString() + "路口已归还", "放行日志" + (temp.Agv_ID), "插件日志"); } Frm_A17168.leavedDic[temp.Agv_ID]++; } #endregion else { t = Crossing.Crossing.BlockCross(temp.AssemblyLine, location_temp, temp.Agv_ID, mainfrm.ds_CrossInfo); if (t >= 0) { if (location_temp % 2 == 0) { WriteTxt.SaveLog1("准备放行" + location_temp, temp.Agv_ID + "#Agv", "AGV"); if (Link_To_PLC.WriteVW(2, 116, temp.Location_Display) == 0) { t_temp = 0; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "已放行", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; } } else { Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "路口已复位", temp.Agv_ID, location_value); temp.Cross_Location_Go = location_temp; t_temp = 0; } } else if (t < 0) { if (t != t_temp) { t_temp = t; Func.CrossInfo_Insert(temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, location_temp, "AGV" + t.ToString() + "占用路口", temp.Agv_ID, location_value); } } } } #endregion #region 状态信息 int[] WarnRead = new int[2] { 0, 0 }; Link_To_PLC.ReadVW(2, 2, 3002, out WarnRead); string sWarn = (Convert.ToString(WarnRead[0], 2).PadLeft(8, '0') + Convert.ToString(WarnRead[1], 2).PadLeft(8, '0')).PadLeft(64, '0'); ThreadPool.QueueUserWorkItem(delegate { Thread.Sleep(100); Warn_Deal_Plc(temp, sWarn); }); #endregion } } } else { if (temp.bPlcLinked) { errorcount--; if (Math.Abs(errorcount) > 50) { temp.Warn_ID = "0000000000000000000000000000000010000000000000000000000000000000"; temp.Warn_Level = 4; temp.Warn_info = "网络异常"; errorcount = 0; temp.bPlcLinked = false; Link_To_PLC.CloseLinkPLC(); } } } } catch (Exception Err) { if (Err.TargetSite.Name.ToString() == "ConnectAsync") { temp.Warn_ID = "0000000000000000000000000000000010000000000000000000000000000000"; temp.Warn_Level = 4; temp.Warn_info = "网络异常"; errorcount = 0; temp.bPlcLinked = false; Link_To_PLC.CloseLinkPLC(); Link_To_PLC = new S7_200SocketClient(temp.IP, temp.Port); temp.Soc = Link_To_PLC.clientSocket; } if (temp.bPlcLinked) { errorcount--; if (Math.Abs(errorcount) > 50) { temp.Warn_ID = "0000000000000000000000000000000010000000000000000000000000000000"; temp.Warn_Level = 4; temp.Warn_info = "网络异常"; errorcount = 0; temp.bPlcLinked = false; Link_To_PLC.CloseLinkPLC(); //Crossing.Crossing.BlockCross(0, 0, temp.Agv_ID, true, ds_crossInfo, false); } } Thread.Sleep(3000); } Thread.Sleep(800); } } }//class agv public static int GetAreaAGVNum(int[] num) { int CarNum = 0; for (int i = 0; i < num.Length; i++) { CarNum += Agv_Land_Count(num[i]); } return CarNum; } //报警读取处理 private static void Warn_Deal(Cls.AgvInfo temp, string sWarn) { if (temp.Warn_ID != sWarn) { int warn_level = 0; string[] warn_info = { "", "", "", "", "", "", "", "" }; temp.Warn_ID = sWarn; WarnInfo(temp.Warn_ID, out warn_info, out warn_level); temp.Warn_info = ""; temp.Warn_Level = warn_level; if (warn_level == 1) { temp.Warn_info = "正常"; Func.Agv_Alarm_Insert(temp.Agv_ID, temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, "正常", temp.Agv_ID); } else { for (byte i = 0; i < 8; i++) { if (warn_info[i] != "") { temp.Warn_info = temp.Warn_info + warn_info[i] + "\r\n"; Func.Agv_Alarm_Insert(0, temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, warn_info[i], temp.Agv_ID); } } } } } private static void Warn_Deal_Plc(Cls.AgvInfo temp, string sWarn) { if (temp.Warn_ID != sWarn) { int warn_level = 0; string[] warn_info = { "", "", "", "", "", "", "", "" }; temp.Warn_ID = sWarn; WarnInfo_PLC(temp.Warn_ID, out warn_info, out warn_level); temp.Warn_info = ""; temp.Warn_Level = warn_level; if (warn_level == 1) { temp.Warn_info = "正常"; Func.Agv_Alarm_Insert(temp.Agv_ID, temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, "正常", temp.Agv_ID); } else { for (byte i = 0; i < 8; i++) { if (warn_info[i] != "") { temp.Warn_info = temp.Warn_info + warn_info[i] + "\r\n"; Func.Agv_Alarm_Insert(0, temp.AssemblyName, temp.AssemblyLine.ToString(), temp.Internal_ID, warn_info[i], temp.Agv_ID); } } } } } //电量获取 private static void Voltage_Deal(Cls.AgvInfo temp, string sValue) { //if (temp.Voltage != param_info[12] + param_info[13] / 10) //{ // //电压 // temp.Voltage = param_info[12] + param_info[13] / 10; //} //if (temp.Voltage_precent != param_info[2]) //{ // //电量剩余百分比 // temp.Voltage_precent = param_info[14]; //} } //速度\障碍物获取 private static void Speed_Bank_Deal(Cls.AgvInfo temp, string sValue) { //if (temp.Run_Speed != param_info[0]) //{ // //AGV速度 // temp.Run_Speed = Convert.ToByte(param_info[0]); //} //if (temp.Bank_Model != param_info[1]) //{ // //障碍物模式 // temp.Bank_Model = Convert.ToByte(param_info[1]); //} } //方向、运动状态获取 private static void Direction_Run_Deal(Cls.AgvInfo temp, string sValue) { //if (temp.Go_Direction != param_info[0]) //{ // //定义:1-直行/2-左转/3-右转 // temp.Go_Direction = Convert.ToByte(param_info[0]); //} //if (temp.Run_Status != param_info[1]) //{ // //定义:1-运动、2-停止、0-未启动 // temp.Run_Status = Convert.ToByte(param_info[1]); //} } private static void Move_Forward(Cls.AgvInfo temp, string sValue) { //if (temp.Run_Direction != param_info[0]) //{ // //定义:1-前进/2-后退 // temp.Run_Direction = Convert.ToByte(param_info[0]); //} } private static byte Agv_Count_Area(int worktype, int[] landmark) { byte count = 0; for (byte i = 0; i < Func.AgvInfo.Length; i++) { if (Func.AgvInfo[i].WorkType == worktype) { for (byte j = 0; j < landmark.Length; j++) { if (Func.AgvInfo[i].Location_Display == landmark[j]) count++; } } } return count; } private static byte Agv_Land_Count(int landmark) { byte count = 0; for (byte i = 0; i < Func.AgvInfo.Length; i++) { if (Func.AgvInfo[i].Location_Display == landmark) count++; } return count; } private static byte Agv_Go_Count(int landmark) { byte count = 0; for (byte i = 0; i < Func.AgvInfo.Length; i++) { if (Func.AgvInfo[i].Cross_Location_Go == landmark) count++; } return count; } //本函数为AGV的失联计数器。负几表示失联几次 private static object SEM_LOST_CONNECT = new object(); private static void LostConnect(ref int iStatus) //AGV的状态 { lock (SEM_LOST_CONNECT) { if (iStatus >= 0) iStatus = -1; else iStatus--; } } private static void WarnInfo(string str, out string[] info, out int level) { int count = 0; info = new string[8]; level = 1; for (byte i = 0; i < 8; i++) { info[i] = ""; } if (str == "0000000000000000000000000000000000000000000000000000000000000000") { info[0] = "正常"; return; } for (byte i = 0; i < 64; i++) { if (str.Substring(63 - i, 1) == "1") { if (mainfrm.ds_WarnList.Tables[0].Rows[i][2].ToString() != "0") { info[count] = mainfrm.ds_WarnList.Tables[0].Rows[i][1].ToString(); if (level < Convert.ToInt16(mainfrm.ds_WarnList.Tables[0].Rows[i][2].ToString())) { level = Convert.ToInt16(mainfrm.ds_WarnList.Tables[0].Rows[i][2].ToString()); } count++; } if (count >= 8) return; } } } private static void WarnInfo_PLC(string str, out string[] info, out int level) { int count = 0; info = new string[8]; level = 1; for (byte i = 0; i < 8; i++) { info[i] = ""; } if (str == "0000000000000000000000000000000000000000000000000000000000000000") { info[0] = "正常"; return; } for (byte i = 0; i < 64; i++) { if (str.Substring(63 - i, 1) == "1") { if (mainfrm.ds_WarnList_Plc.Tables[0].Rows[i][2].ToString() != "0") { info[count] = mainfrm.ds_WarnList_Plc.Tables[0].Rows[i][1].ToString(); if (level < Convert.ToInt16(mainfrm.ds_WarnList_Plc.Tables[0].Rows[i][2].ToString())) { level = Convert.ToInt16(mainfrm.ds_WarnList_Plc.Tables[0].Rows[i][2].ToString()); } count++; } if (count >= 8) return; } } } private static object obj_GetMileage = new object(); public static float GetMileage(int point1, int point2) { lock (obj_GetMileage) { float mileage = 0; int t = ds_mileage.Tables[0].Rows.Count; DataView rowfilter = new DataView(ds_mileage.Tables[0]); rowfilter.RowFilter = "(Start_Point=" + point1 + " or Start_Point=" + point2 + ") and (End_Point= " + point1 + " or End_Point= " + point2 + ")"; rowfilter.RowStateFilter = DataViewRowState.OriginalRows; DataTable dt = rowfilter.ToTable(); int t1 = dt.Rows.Count; try { mileage = Convert.ToSingle(dt.Rows[0]["Distance"].ToString()); } catch { } return mileage; } } } }