private void btnSave_Click(object sender, EventArgs e) { try { if (string.IsNullOrEmpty(txtAssemblyAGVClassName.Text.Trim()) && string.IsNullOrEmpty(txtAssemblyChargeClassName.Text.Trim())) { MsgBox.ShowWarn("请配置程序集!"); return; } IList <DispatchAssembly> assemblys = new List <DispatchAssembly>(); if (!string.IsNullOrEmpty(txtAssemblyAGVClassName.Text.Trim())) { DispatchAssembly agvAssembly = new DispatchAssembly() { AssemblyType = 0, ClassName = txtAssemblyAGVClassName.Text.Trim(), Discript = txtAssemblyAGVDescript.Text.Trim() }; assemblys.Add(agvAssembly); } if (!string.IsNullOrEmpty(txtAssemblyChargeClassName.Text.Trim())) { DispatchAssembly chargeAssembly = new DispatchAssembly() { AssemblyType = 1, ClassName = txtAssemblyChargeClassName.Text.Trim(), Discript = txtAssemblyChargeDescript.Text.Trim() }; assemblys.Add(chargeAssembly); } OperateReturnInfo opr = AGVDAccess.AGVClientDAccess.SaveAssemblySet(assemblys); MsgBox.Show(opr); } catch (Exception ex) { MsgBox.ShowError(ex.Message); } }
/// <summary> /// 通讯初始化 /// </summary> public bool CommInit() { try { #region 小车 DispatchAssembly AgvCommunitClass = CoreData.DispathAssemblies.FirstOrDefault(p => p.AssemblyType == 0); if (AgvCommunitClass == null) { DelegateState.InvokeDispatchStateEvent("未配置AGV通信处理程序!"); LogHelper.WriteLog("未配置AGV通信处理程序!"); return(false); } DispatchAssembly ChargeCommunitClass = CoreData.DispathAssemblies.FirstOrDefault(q => q.AssemblyType == 1); if (ChargeCommunitClass == null) { DelegateState.InvokeDispatchStateEvent("未配置充电桩通信处理程序!"); LogHelper.WriteLog("未配置充电桩通信处理程序!"); return(false); } //先停止清除所有的通信管道 foreach (var item in AGVSessions) { ExcuteRflectMethod(item, "Stop", null); } AGVSessions.Clear(); //初始化所有AGV小车的通信 foreach (CarInfo car in CoreData.CarList) { AGVComPara para = new AGVComPara(); para.ServerIP = car.CarIP; para.Port = Convert.ToInt32(car.CarPort); int agvid = Convert.ToInt32(car.AgvID); Type objType = Type.GetType(AgvCommunitClass.ClassName, true); object obj = Activator.CreateInstance(objType); SetModelValue("DeviceID", agvid, obj); SetModelValue("ComPara", para, obj); //obj.DeviceID = agvid; //obj.ComPara = para; AGVSessions.Add(obj); } foreach (var item in AGVSessions) { bool InitResult = (bool)ExcuteRflectMethod(item, "Init", null); if (InitResult) { DelegateState.InvokeDispatchStateEvent(GetModelValue("DeviceID", item).ToString() + "号AGV通讯初始化成功"); LogHelper.WriteLog(GetModelValue("DeviceID", item).ToString() + "号AGV通讯初始化成功"); } else { DelegateState.InvokeDispatchStateEvent(GetModelValue("DeviceID", item).ToString() + "号AGV通讯初始化失败"); LogHelper.WriteLog(GetModelValue("DeviceID", item).ToString() + "号AGV通讯初始化失败"); } } #endregion #region 充电桩 foreach (var item in ChargeStationSessions) { ExcuteRflectMethod(item, "Stop", null); } ChargeStationSessions.Clear(); foreach (ChargeStationInfo Charge in CoreData.ChargeList) { AGVComPara para = new AGVComPara(); para.ServerIP = Charge.IP; para.Port = Convert.ToInt32(Charge.Port); int ChargeID = Convert.ToInt32(Charge.ID); Type objType = Type.GetType(ChargeCommunitClass.ClassName, true); object obj = Activator.CreateInstance(objType); SetModelValue("DeviceID", ChargeID, obj); SetModelValue("ComPara", para, obj); ChargeStationSessions.Add(obj); } foreach (var item in ChargeStationSessions) { bool InitResult = (bool)ExcuteRflectMethod(item, "Init", null); if (InitResult) { DelegateState.InvokeDispatchStateEvent(GetModelValue("DeviceID", item).ToString() + "号充电桩通讯初始化成功"); } else { DelegateState.InvokeDispatchStateEvent(GetModelValue("DeviceID", item).ToString() + "号充电桩通讯初始化失败"); } } #endregion #region io设备 foreach (var item in IOSessions) { item.Stop(); } IOSessions.Clear(); foreach (IODeviceInfo node in CoreData.IOList) { AGVComPara para = new AGVComPara(); para.ServerIP = node.IP; para.Port = Convert.ToInt32(node.Port); int ioid = Convert.ToInt32(node.ID); IOSessions.Add(new IOSession_Fbell(ioid, para)); } foreach (var item in IOSessions) { if (item.Init()) { DelegateState.InvokeDispatchStateEvent(item.DeviceID.ToString() + "号IO设备通讯初始化成功"); } else { DelegateState.InvokeDispatchStateEvent(item.DeviceID.ToString() + "号IO设备通讯初始化失败"); } } #endregion return(true); } catch (Exception ex) { DelegateState.InvokeDispatchStateEvent("初始化客户端通讯异常"); LogHelper.WriteErrorLog(ex); return(false); } }