/// <summary>取消编辑,将配置中的参数恢复到界面和对象中</summary> private void btCancel_Click(object sender, EventArgs e) { IJFInitializable dev = JFHubCenter.Instance.InitorManager[dgvDevs.SelectedRows[0].Cells[0].Value.ToString()]; JFXmlSortedDictionary <string, List <object> > devCfg = JFHubCenter.Instance.SystemCfg.GetItemValue(JFHubCenter.CK_InitDevParams) as JFXmlSortedDictionary <string, List <object> >; List <object> initParams = devCfg[dgvDevs.SelectedRows[0].Cells[0].Value.ToString()]; for (int i = 1; i < initParams.Count; i++) { UcJFParamEdit pe = gbParams.Controls[i + 4] as UcJFParamEdit; pe.SetParamValue(initParams[i]); pe.IsValueReadOnly = true; dev.SetInitParamValue(dev.InitParamNames[i - 1], initParams[i]); } btInit.Enabled = !dev.IsInitOK; btAdd.Enabled = true; btRemove.Enabled = true; //btDebug.Enabled = true; isEditting = false; btEditSave.Text = "编辑参数"; btCancel.Enabled = false; dgvDevs.Enabled = true; Type devType = dev.GetType(); if (typeof(IJFDevice_Camera).IsAssignableFrom(devType) || typeof(IJFDevice_LightController).IsAssignableFrom(devType) || typeof(IJFDevice_MotionDaq).IsAssignableFrom(devType) || typeof(IJFDevice_TrigController).IsAssignableFrom(devType) || typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) //提供调试界面 { btDebug.Enabled = true; if (typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) { chkSelfUI.Enabled = true; } else { chkSelfUI.Checked = false; chkSelfUI.Enabled = false; } } else { btDebug.Enabled = false; } if (typeof(IJFConfigUIProvider).IsAssignableFrom(devType)) { btCfg.Enabled = true; } else { btCfg.Enabled = false; } }
private void FormDevMgr_Load(object sender, EventArgs e) { isEditting = false; btCancel.Enabled = false; btInit.Enabled = false; btRemove.Enabled = false; btDebug.Enabled = false; btCfg.Enabled = false; chkSelfUI.Enabled = false; ///加载现有设备到列表中 dgvDevs.Rows.Clear(); string[] devIDs = JFHubCenter.Instance.InitorManager.GetIDs(InitorType); foreach (string devID in devIDs) { IJFInitializable dev = JFHubCenter.Instance.InitorManager[devID]; DataGridViewRow row = new DataGridViewRow(); DataGridViewTextBoxCell cellID = new DataGridViewTextBoxCell(); cellID.Value = devID; row.Cells.Add(cellID); DataGridViewTextBoxCell cellModel = new DataGridViewTextBoxCell(); cellModel.Value = JFinitializerHelper.DispalyTypeName(dev.GetType()); row.Cells.Add(cellModel); DataGridViewTextBoxCell cellType = new DataGridViewTextBoxCell(); cellType.Value = dev.GetType().Name; row.Cells.Add(cellType); dgvDevs.Rows.Add(row); if (!dev.IsInitOK) { row.DefaultCellStyle.ForeColor = Color.Red; } } dgvDevs.ClearSelection(); RemoveAllPEs(); }
/// <summary>确定创建Initor对象</summary> private void btOK_Click(object sender, EventArgs e) { if (dgvTypes.SelectedRows == null || dgvTypes.SelectedRows.Count == 0) { MessageBox.Show("请选择需要创建的设备类型"); return; } if (string.IsNullOrWhiteSpace(tbID.Text)) { MessageBox.Show("设备ID不能为空,请重新输入"); tbID.Focus(); return; } if (!_isFixedID && JFHubCenter.Instance.InitorManager.ContainID(tbID.Text)) { IJFInitializable dev = JFHubCenter.Instance.InitorManager.GetInitor(tbID.Text); string disTypeName = JFinitializerHelper.DispalyTypeName(dev.GetType()); MessageBox.Show(string.Format("Initor列表中已存在ID = \"{0}\" Type = \"{1}\"的设备\n请重新输入ID!", tbID.Text, disTypeName)); tbID.Focus(); return; } //IJFDevice newDev = JFHubCenter.Instance.InitorHelp.CreateInstance(dgvTypes.SelectedRows[0].Cells[0].Value.ToString()) as IJFDevice; //for (int i = 2; i < gbParams.Controls.Count;i++) for (int i = 0; i < panelParams.Controls.Count; i++) { //UcJFParamEdit pe = gbParams.Controls[i] as UcJFParamEdit; UcJFParamEdit pe = panelParams.Controls[i] as UcJFParamEdit; object paramVal = null; if (!pe.GetParamValue(out paramVal)) { MessageBox.Show("初始化参数:\"" + pe.Name + "\"获取失败!Error:" + pe.GetParamErrorInfo()); //newDev.Dispose(); pe.Focus(); return; } if (!initor.SetInitParamValue(initor.InitParamNames[i], paramVal)) { MessageBox.Show(string.Format("设置初始化参数失败:Name = {0},Value = {1}", initor.InitParamNames[i], paramVal.ToString())); //newDev.Dispose(); pe.Focus(); return; } } DialogResult = DialogResult.OK; }
private void btCfg_Click(object sender, EventArgs e) { if (dgvDevs.SelectedRows == null || dgvDevs.SelectedRows.Count == 0) { MessageBox.Show("请选择需要设置的" + InitorCaption + "对象!"); return; } IJFInitializable initor = JFHubCenter.Instance.InitorManager[dgvDevs.SelectedRows[0].Cells[0].Value.ToString()]; Type devType = initor.GetType(); if (!typeof(IJFConfigUIProvider).IsAssignableFrom(devType)) { MessageBox.Show(InitorCaption + "对象未提供参数配置界面!"); return; } (initor as IJFConfigUIProvider).ShowCfgDialog(); }
/// <summary> /// 从系统中删除一个单相机视觉示教助手 /// </summary> /// <param name="name"></param> public void DelSVAssist(string name) { if (!ContainSVAssistName(name)) { return; } IJFInitializable initor = JFHubCenter.Instance.InitorManager.GetInitor(name); if (typeof(JFSingleVisionAssist) != initor.GetType()) { throw new Exception("DelSVAssist(string name = " + name + ") failed by the initor's type is not JFSingleVisionAssist"); } JFHubCenter.Instance.InitorManager.Remove(name); //从设备管理器中删除 JFXmlSortedDictionary <string, List <object> > devCfg = JFHubCenter.Instance.SystemCfg.GetItemValue(JFHubCenter.CK_InitDevParams) as JFXmlSortedDictionary <string, List <object> >; devCfg.Remove(name);//从设备配置文件中删除 JFHubCenter.Instance.SystemCfg.NotifyItemChanged(JFHubCenter.CK_InitDevParams); JFHubCenter.Instance.SystemCfg.Save(); }
/// <summary> /// 注册一个工站(不可删除), /// 在Application.Run()运行之前调用 /// </summary> /// <param name="station"></param> public void DeclearStation(IJFStation station) { if (station == null) { throw new ArgumentNullException("StationManager.DeclearStation(IJFStation station) failed by station = null"); } //if (_lstDeclearStations.Contains(station)) // throw new ArgumentException("StationManager.DeclearStation(IJFStation station) failed by station is already decleared!"); string name = station.Name; if (string.IsNullOrEmpty(name)) { throw new ArgumentException("StationManager.DeclearStation(IJFStation station) failed by station's Name null or empty"); } if (_initorStationNames().Contains(name)) { IJFInitializable existedStation = JFHubCenter.Instance.InitorManager.GetInitor(name); if (existedStation.GetType() != station.GetType()) { throw new Exception("StationManager.DeclearStation(IJFStation station) failed by:Exist a station with same name and type unmatched: Decleared Type = " + station.GetType() + " ; Existed Type = " + existedStation.GetType()); } (existedStation as IJFStation).Name = name; DeclearedStationNames.Add(station.Name); //station.WorkStatusChanged += StationWorkStatusChanged; //station.CustomStatusChanged += StationCustomStatusChanged; //if(station is JFCmdWorkBase) // (station as JFCmdWorkBase).WorkMsg2Outter += StationTxtMsg; return; //throw new ArgumentException("StationManager.DeclearStation(IJFStation station) failed by station's Name = \"" + name + "\" is exsited in InitorStation-List!"); } JFHubCenter.Instance.InitorManager.Add(station.Name, station); DeclearedStationNames.Add(station.Name); //station.WorkStatusChanged += StationWorkStatusChanged; //station.CustomStatusChanged += StationCustomStatusChanged; //if (station is JFCmdWorkBase) // (station as JFCmdWorkBase).WorkMsg2Outter += StationTxtMsg; }
/// <summary> /// 检查设备通道是否存在并且可用 /// </summary> /// <param name="category"></param> /// <param name="cellName"></param> /// <param name="initor"></param> /// <param name="cellInfo"></param> /// <param name="errorInfo"></param> /// <returns></returns> public static bool CheckDevCellName(string category, string cellName, out IJFInitializable initor, out JFDevCellInfo ci, out string errorInfo) { initor = null; ci = null; errorInfo = null; if (string.IsNullOrEmpty(cellName)) { errorInfo = "参数项\"cellName\"为空"; return(false); } JFDevCellNameManeger nameMgr = JFHubCenter.Instance.MDCellNameMgr; JFInitorManager initorMgr = JFHubCenter.Instance.InitorManager; JFDevCellInfo cellInfo = null; if (category == DO) //获取数字量输出通道信息 { cellInfo = nameMgr.GetDoCellInfo(cellName); if (null == cellInfo) { errorInfo = "设备命名表中不存在DO:" + cellName; return(false); } IJFInitializable dev = initorMgr.GetInitor(cellInfo.DeviceID); if (null == dev) { errorInfo = "DO:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"在设备列表中不存在"; return(false); } if (!typeof(IJFDevice_MotionDaq).IsAssignableFrom(dev.GetType())) { errorInfo = "DO:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"类型不是Device_MotionDaq "; return(false); } IJFDevice_MotionDaq md = dev as IJFDevice_MotionDaq; if (!md.IsInitOK) { errorInfo = "DO:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"未完成初始化动作 "; return(false); } if (!md.IsDeviceOpen) { errorInfo = "DO:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"未打开 "; return(false); } if (cellInfo.ModuleIndex >= md.DioCount) { errorInfo = "DO:\"" + cellName + "\" ModuleIndex = :" + cellInfo.ModuleIndex + "超出设备DIO模块数量: " + md.DioCount; return(false); } if (cellInfo.ChannelIndex >= md.GetDio(cellInfo.ModuleIndex).DOCount) { errorInfo = "DO:\"" + cellName + "\" Channel = :" + cellInfo.ChannelIndex + "超出模块DO通道数量: " + md.GetDio(cellInfo.ModuleIndex).DOCount; return(false); } initor = dev; ci = cellInfo; errorInfo = "Success"; return(true); } else if (category == DI) //获取数字量输入 { cellInfo = nameMgr.GetDiCellInfo(cellName); if (null == cellInfo) { errorInfo = "设备命名表中不存在DI:" + cellName; return(false); } IJFInitializable dev = initorMgr.GetInitor(cellInfo.DeviceID); if (null == dev) { errorInfo = "DI:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"在设备列表中不存在"; return(false); } if (!typeof(IJFDevice_MotionDaq).IsAssignableFrom(dev.GetType())) { errorInfo = "DI:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"类型不是Device_MotionDaq "; return(false); } IJFDevice_MotionDaq md = dev as IJFDevice_MotionDaq; if (!md.IsInitOK) { errorInfo = "DI:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"未完成初始化动作 "; return(false); } if (!md.IsDeviceOpen) { errorInfo = "DI:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"未打开 "; return(false); } if (cellInfo.ModuleIndex >= md.DioCount) { errorInfo = "DI:\"" + cellName + "\" ModuleIndex = :" + cellInfo.ModuleIndex + "超出设备DIO模块数量: " + md.DioCount; return(false); } if (cellInfo.ChannelIndex >= md.GetDio(cellInfo.ModuleIndex).DICount) { errorInfo = "DI:\"" + cellName + "\" Channel = :" + cellInfo.ChannelIndex + "超出模块DI通道数量: " + md.GetDio(cellInfo.ModuleIndex).DOCount; return(false); } initor = dev; ci = cellInfo; errorInfo = "Success"; return(true); } else if (category == AI) { } else if (category == AO) { } else if (category == Axis) { cellInfo = nameMgr.GetAxisCellInfo(cellName); if (null == cellInfo) { errorInfo = "设备命名表中不存在Axis:" + cellName; return(false); } IJFInitializable dev = initorMgr.GetInitor(cellInfo.DeviceID); if (null == dev) { errorInfo = "Axis:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"在设备列表中不存在"; return(false); } if (!typeof(IJFDevice_MotionDaq).IsAssignableFrom(dev.GetType())) { errorInfo = "Axis:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"类型不是Device_MotionDaq "; return(false); } IJFDevice_MotionDaq md = dev as IJFDevice_MotionDaq; if (!md.IsInitOK) { errorInfo = "Axis:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"未完成初始化动作 "; return(false); } if (!md.IsDeviceOpen) { errorInfo = "Axis:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"未打开 "; return(false); } if (cellInfo.ModuleIndex >= md.McCount) { errorInfo = "Axis:\"" + cellName + "\" ModuleIndex = :" + cellInfo.ModuleIndex + "超出设备轴模块数量: " + md.McCount; return(false); } if (cellInfo.ChannelIndex >= md.GetMc(cellInfo.ModuleIndex).AxisCount) { errorInfo = "Axis:\"" + cellName + "\" Channel = :" + cellInfo.ModuleIndex + "超出模块轴通道数量: " + md.GetMc(cellInfo.ModuleIndex).AxisCount; return(false); } initor = dev; ci = cellInfo; errorInfo = "Success"; return(true); } else if (category == CmpTrig) { cellInfo = nameMgr.GetCmpTrigCellInfo(cellName); if (null == cellInfo) { errorInfo = "设备命名表中不存在CmpTrig:" + cellName; return(false); } IJFInitializable dev = initorMgr.GetInitor(cellInfo.DeviceID); if (null == dev) { errorInfo = "CmpTrig:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"在设备列表中不存在"; return(false); } if (!typeof(IJFDevice_MotionDaq).IsAssignableFrom(dev.GetType())) { errorInfo = "CmpTrig:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"类型不是Device_MotionDaq "; return(false); } IJFDevice_MotionDaq md = dev as IJFDevice_MotionDaq; if (!md.IsInitOK) { errorInfo = "CmpTrig:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"未完成初始化动作 "; return(false); } if (!md.IsDeviceOpen) { errorInfo = "CmpTrig:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"未打开 "; return(false); } if (cellInfo.ModuleIndex >= md.CompareTriggerCount) { errorInfo = "CmpTrig:\"" + cellName + "\" ModuleIndex = :" + cellInfo.ModuleIndex + "超出设备比较触发模块数量: " + md.CompareTriggerCount; return(false); } if (cellInfo.ChannelIndex >= md.GetCompareTrigger(cellInfo.ModuleIndex).EncoderChannels) { errorInfo = "CmpTrig:\"" + cellName + "\" Channel = :" + cellInfo.ModuleIndex + "超出模块比较触发通道数量: " + md.GetCompareTrigger(cellInfo.ModuleIndex).EncoderChannels; return(false); } initor = dev; ci = cellInfo; errorInfo = "Success"; return(true); } else if (category == LightCtrl) { cellInfo = nameMgr.GetLightCtrlChannelInfo(cellName); if (null == cellInfo) { errorInfo = "设备命名表中不存在LightCtrl:" + cellName; return(false); } IJFInitializable dev = initorMgr.GetInitor(cellInfo.DeviceID); if (null == dev) { errorInfo = "LightCtrl:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"在设备列表中不存在"; return(false); } if (!typeof(IJFDevice_LightController).IsAssignableFrom(dev.GetType())) { errorInfo = "LightCtrl:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"类型不是IJFDevice_LightController "; return(false); } IJFDevice_LightController md = dev as IJFDevice_LightController; if (!md.IsInitOK) { errorInfo = "LightCtrl:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"未完成初始化动作 "; return(false); } if (!md.IsDeviceOpen) { errorInfo = "LightCtrl:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"未打开 "; return(false); } if (cellInfo.ChannelIndex >= md.LightChannelCount) { errorInfo = "LightCtrl:\"" + cellName + "\" Channel = :" + cellInfo.ChannelIndex + "超出设备光源通道数量: " + md.LightChannelCount; return(false); } initor = dev; ci = cellInfo; errorInfo = "Success"; return(true); } else if (category == LightTrig) { cellInfo = nameMgr.GetTrigCtrlChannelInfo(cellName); if (null == cellInfo) { errorInfo = "设备命名表中不存在LightTrig:" + cellName; return(false); } IJFInitializable dev = initorMgr.GetInitor(cellInfo.DeviceID); if (null == dev) { errorInfo = "LightTrig:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"在设备列表中不存在"; return(false); } if (!typeof(IJFDevice_TrigController).IsAssignableFrom(dev.GetType())) { errorInfo = "LightTrig:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"类型不是IJFDevice_TrigController "; return(false); } IJFDevice_TrigController md = dev as IJFDevice_TrigController; if (!md.IsInitOK) { errorInfo = "LightTrig:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"未完成初始化动作 "; return(false); } if (!md.IsDeviceOpen) { errorInfo = "LightTrig:\"" + cellName + "\" 所属设备:\"" + cellInfo.DeviceID + "\"未打开 "; return(false); } if (cellInfo.ChannelIndex >= md.TrigChannelCount) { errorInfo = "LightTrig:\"" + cellName + "\" Channel = :" + cellInfo.ChannelIndex + "超出设备触发通道数量: " + md.TrigChannelCount; return(false); } initor = dev; ci = cellInfo; errorInfo = "Success"; return(true); } else if (category == Cmr) { IJFInitializable dev = initorMgr.GetInitor(cellName); if (null == dev) { errorInfo = "Camera:\"" + cellName + "\" 在设备列表中不存在"; return(false); } if (!typeof(IJFDevice_Camera).IsAssignableFrom(dev.GetType())) { errorInfo = "Camera:\"" + cellName + "\" 设备类型不是IJFDevice_Camera "; return(false); } initor = dev; errorInfo = "Success"; return(true); } else { errorInfo = "不支持的参数项\"category\" = " + category; } return(false); }
/// <summary>选择设备</summary> private void dgvDevs_CellClick(object sender, DataGridViewCellEventArgs e) { if (dgvDevs.SelectedRows.Count == 0) { return; } btRemove.Enabled = true; RemoveAllPEs(); DataGridViewRow row = dgvDevs.SelectedRows[0]; IJFInitializable dev = JFHubCenter.Instance.InitorManager[row.Cells[0].Value.ToString()]; Type devType = dev.GetType(); if (typeof(IJFDevice_Camera).IsAssignableFrom(devType) || typeof(IJFDevice_LightController).IsAssignableFrom(devType) || typeof(IJFDevice_MotionDaq).IsAssignableFrom(devType) || typeof(IJFDevice_TrigController).IsAssignableFrom(devType) || typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) //提供调试界面 { btDebug.Enabled = true; if (typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) { chkSelfUI.Enabled = true; } else { chkSelfUI.Checked = false; chkSelfUI.Enabled = false; } } else { btDebug.Enabled = false; } if (typeof(IJFConfigUIProvider).IsAssignableFrom(devType)) { btCfg.Enabled = true; } else { btCfg.Enabled = false; } tbDevID.Text = row.Cells[0].Value.ToString(); btInit.Enabled = !dev.IsInitOK; string[] iniParamNames = dev.InitParamNames; if (null == iniParamNames) { btEditSave.Enabled = false; return; } btEditSave.Enabled = true; int locY = btInit.Location.Y + btInit.Size.Height + 5; foreach (string ipName in iniParamNames) { UcJFParamEdit pe = new UcJFParamEdit(); pe.Width = gbParams.Width - 1; pe.Location = new Point(4, locY); pe.SetParamDesribe(dev.GetInitParamDescribe(ipName)); locY += pe.Height; pe.SetParamValue(dev.GetInitParamValue(ipName)); pe.IsValueReadOnly = true; gbParams.Controls.Add(pe); } }
bool isEditting; ///是否处于编辑初始化参数状态 /// <summary>编辑/保存 初始化参数</summary> private void btEditSave_Click(object sender, EventArgs e) { if (!isEditting)//参数未编辑状态 { if (dgvDevs.SelectedRows == null || 0 == dgvDevs.SelectedRows.Count) { MessageBox.Show("请选择需要编辑参数的对象"); return; } if (gbParams.Controls.Count <= 5) //没有初始化参数 { return; } for (int i = 5; i < gbParams.Controls.Count; i++) { UcJFParamEdit pe = gbParams.Controls[i] as UcJFParamEdit; pe.IsValueReadOnly = false; } dgvDevs.Enabled = false; btEditSave.Text = "保存"; btCancel.Enabled = true; btInit.Enabled = false; btAdd.Enabled = false; btRemove.Enabled = false; btDebug.Enabled = false; isEditting = true; } else //编辑完成,需要存储编辑后的参数 { IJFInitializable dev = JFHubCenter.Instance.InitorManager[dgvDevs.SelectedRows[0].Cells[0].Value.ToString()]; List <object> initParams = new List <object>(); initParams.Add(dev.GetType().AssemblyQualifiedName);//DevType for (int i = 5; i < gbParams.Controls.Count; i++) { object paramVal = null; UcJFParamEdit pe = gbParams.Controls[i] as UcJFParamEdit; if (!pe.GetParamValue(out paramVal)) { pe.Focus(); MessageBox.Show("参数\"" + dev.InitParamNames[i - 5] + "\"格式错误:" + pe.GetParamErrorInfo()); return; } if (!dev.SetInitParamValue(dev.InitParamNames[i - 5], paramVal)) { pe.Focus(); MessageBox.Show("设置参数\"" + dev.InitParamNames[i - 5] + "\"失败:" + dev.GetInitErrorInfo()); return; } //initParams.Add(paramVal); //替换为以下代码,适应数组/基类型混编的参数列表 //paramsInCfg.Add(dev.GetInitParamValue(dev.InitParamNames[i])); 本行代码用以下代码代替,以支持有限的 简单类型 + 数组(列表)类型的组合 //暂时的解决方法:将数组类型转化为字符串存储 Type paramType = dev.GetInitParamDescribe(dev.InitParamNames[i - 5]).ParamType; SerializableAttribute[] sas = paramType.GetCustomAttributes(typeof(SerializableAttribute), false) as SerializableAttribute[]; if (sas != null && sas.Length > 0) //如果是可序列化的类型,直接保存序列化后的文本 { StringBuilder buffer = new StringBuilder(); XmlSerializer serializer = new XmlSerializer(paramType); using (TextWriter writer = new StringWriter(buffer)) { serializer.Serialize(writer, paramVal); } string xmlTxt = buffer.ToString(); initParams.Add(xmlTxt); // paramsInCfg.Add(xmlTxt); } else { if (paramType.IsValueType || paramType == typeof(string)) //单值对象和字符串对象直接添加 { initParams.Add(paramVal); } else //目前支持Array 和 List { if (paramType.IsArray) //参数类型是数组 { if (null == paramVal) { initParams.Add(""); } else { StringBuilder sb = new StringBuilder(); string splitString = "$"; for (int j = 0; j < (paramVal as Array).Length; j++) { sb.Append((paramVal as Array).GetValue(j).ToString()); if (j < (paramVal as Array).Length - 1) { sb.Append(splitString); } } initParams.Add(sb.ToString());//paramsInCfg.Add(string.Join(splitString, paramVal)); //使用ASC 响铃符作为间隔 } } else if (typeof(IList).IsAssignableFrom(paramType)) //除了数组之外的队列类型 { if (null == paramVal) { initParams.Add(""); } else { StringBuilder sb = new StringBuilder(); string splitString = "$"; for (int j = 0; j < (paramVal as IList).Count; j++) { sb.Append((paramVal as IList)[j].ToString()); if (j < (paramVal as IList).Count - 1) { sb.Append(splitString); } } initParams.Add(sb.ToString()); //paramsInCfg.Add(string.Join(System.Text.Encoding.ASCII.GetString(new byte[] { 0x07 }), paramVal)); //使用ASC 响铃符作为间隔 } } } } } for (int i = 5; i < gbParams.Controls.Count; i++) { UcJFParamEdit pe = gbParams.Controls[i] as UcJFParamEdit; pe.IsValueReadOnly = true; } if (!dev.Initialize()) { MessageBox.Show("用当前参数初始化设备失败:" + dev.GetInitErrorInfo()); } JFXmlSortedDictionary <string, List <object> > devCfg = JFHubCenter.Instance.SystemCfg.GetItemValue(JFHubCenter.CK_InitDevParams) as JFXmlSortedDictionary <string, List <object> >; devCfg[dgvDevs.SelectedRows[0].Cells[0].Value.ToString()] = initParams; JFHubCenter.Instance.SystemCfg.NotifyItemChanged(JFHubCenter.CK_InitDevParams); JFHubCenter.Instance.SystemCfg.Save(); btInit.Enabled = true; btAdd.Enabled = true; btRemove.Enabled = true; //btDebug.Enabled = true; isEditting = false; btEditSave.Text = "编辑参数"; btCancel.Enabled = false; dgvDevs.Enabled = true; Type devType = dev.GetType(); if (typeof(IJFDevice_Camera).IsAssignableFrom(devType) || typeof(IJFDevice_LightController).IsAssignableFrom(devType) || typeof(IJFDevice_MotionDaq).IsAssignableFrom(devType) || typeof(IJFDevice_TrigController).IsAssignableFrom(devType) || typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) //提供调试界面 { btDebug.Enabled = true; if (typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) { chkSelfUI.Enabled = true; } else { chkSelfUI.Checked = false; chkSelfUI.Enabled = false; } } else { btDebug.Enabled = false; } if (typeof(IJFConfigUIProvider).IsAssignableFrom(devType)) { btCfg.Enabled = true; } else { btCfg.Enabled = false; } } }
/// <summary>调试所选设备</summary> private void btDebug_Click(object sender, EventArgs e) { if (null == dgvDevs.SelectedRows || 0 == dgvDevs.SelectedRows.Count) { MessageBox.Show("请先选择需要调试的" + InitorCaption + "对象"); return; } IJFInitializable initor = JFHubCenter.Instance.InitorManager[dgvDevs.SelectedRows[0].Cells[0].Value.ToString()]; Type devType = initor.GetType(); if (chkSelfUI.Checked) { if (!typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) { MessageBox.Show(InitorCaption + "对象未提供自带调试界面,请取消\"自带界面\"后重试!"); return; } if (typeof(IJFStation).IsAssignableFrom(devType)) { Form fmStationDebug = (initor as IJFStation).GenForm(); if (null == fmStationDebug) //工站自身未提供界面 { if (DialogResult.Cancel == MessageBox.Show("工站对象未提供调试界面,是否使用通用界面?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)) { return; } if (typeof(JFStationBase).IsAssignableFrom(devType)) { FormStationBaseDebug fmSB = new FormStationBaseDebug(); fmSB.Text = (initor as JFStationBase).Name + " Debug"; fmSB.SetStation(initor as JFStationBase); fmSB.TopMost = true; fmSB.Show(); return; } else { //JFRealtimeUI ucST = (initor as IJFRealtimeUIProvider).GetRealtimeUI(); //ucST.Dock = DockStyle.Fill; UcStationRealtimeUI ucST = new UcStationRealtimeUI(); ucST.Dock = DockStyle.Fill; ucST.AllowedStartStopCmd = true; ucST.SetStation(initor as IJFStation); FormJFRealTimeUI fmST = new FormJFRealTimeUI(); fmST.Text = dgvDevs.SelectedRows[0].Cells[0].Value.ToString() + " Debug"; fmST.Size = ucST.Size; fmST.SetRTUI(ucST); //fm.Controls.Add(uc); fmST.Show(); return; } } else { fmStationDebug.FormBorderStyle = FormBorderStyle.Sizable; fmStationDebug.TopMost = true; fmStationDebug.Show(); return; } } JFRealtimeUI uc = (initor as IJFRealtimeUIProvider).GetRealtimeUI(); uc.Dock = DockStyle.Fill; FormJFRealTimeUI fm = new FormJFRealTimeUI(); fm.Text = dgvDevs.SelectedRows[0].Cells[0].Value.ToString() + " Debug"; fm.Size = uc.Size; fm.SetRTUI(uc); //fm.Controls.Add(uc); fm.Show(); } else { if (typeof(IJFDevice_Camera).IsAssignableFrom(devType) || typeof(IJFDevice_LightController).IsAssignableFrom(devType) || typeof(IJFDevice_MotionDaq).IsAssignableFrom(devType) || typeof(IJFDevice_TrigController).IsAssignableFrom(devType) || typeof(IJFStation).IsAssignableFrom(devType)) { if (typeof(JFStationBase).IsAssignableFrom(devType)) { FormStationBaseDebug fmSB = new FormStationBaseDebug(); fmSB.Text = (initor as JFStationBase).Name + " Debug"; fmSB.SetStation(initor as JFStationBase); fmSB.Show(); return; } if (typeof(IJFStation).IsAssignableFrom(devType)) { UcStationRealtimeUI ucST = new UcStationRealtimeUI(); ucST.Dock = DockStyle.Fill; ucST.AllowedStartStopCmd = true; ucST.SetStation(initor as IJFStation); FormJFRealTimeUI fmST = new FormJFRealTimeUI(); fmST.Text = dgvDevs.SelectedRows[0].Cells[0].Value.ToString() + " Debug"; fmST.Size = ucST.Size; fmST.SetRTUI(ucST); //fm.Controls.Add(uc); fmST.Show(); return; } FormJFRealTimeUI fm = new FormJFRealTimeUI(); fm.Text = dgvDevs.SelectedRows[0].Cells[0].Value.ToString() + " Debug" + "待实现!!"; if (typeof(IJFDevice_MotionDaq).IsAssignableFrom(devType)) { UcMotionDaq uc = new UcMotionDaq(); fm.Text = dgvDevs.SelectedRows[0].Cells[0].Value.ToString() + "调试窗口"; //uc.Parent = fm; uc.SetDevice(initor as IJFDevice_MotionDaq, dgvDevs.SelectedRows[0].Cells[0].Value.ToString()); //fm.Size = new Size(uc.Width + 20, uc.Height + 20); //fm.Controls.Add(uc); fm.SetRTUI(uc); } else if (typeof(IJFDevice_TrigController).IsAssignableFrom(devType))//HTM触发板控制器,一进多出(控制触发强度/时间) { fm.Text = dgvDevs.SelectedRows[0].Cells[0].Value.ToString() + "调试窗口"; if (typeof(IJFDevice_LightController).IsAssignableFrom(devType))//带触发功能的光源控制器 { UcLightCtrl_T uc = new UcLightCtrl_T(); uc.SetModule(initor as IJFDevice_LightControllerWithTrig, null, null); uc.Dock = DockStyle.Fill; fm.SetRTUI(uc); } else //单纯的触发控制器 { UcTrigCtrl uc = new UcTrigCtrl(); uc.SetModuleInfo(initor as IJFDevice_TrigController); fm.SetRTUI(uc); } } else if (typeof(IJFDevice_LightController).IsAssignableFrom(devType)) //光源控制器 { fm.Text = dgvDevs.SelectedRows[0].Cells[0].Value.ToString() + "调试窗口"; UcLightCtrl uc = new UcLightCtrl(); uc.SetModuleInfo(initor as IJFDevice_LightController); fm.SetRTUI(uc); } else if (typeof(IJFDevice_Camera).IsAssignableFrom(devType)) { fm.Text = dgvDevs.SelectedRows[0].Cells[0].Value.ToString() + "调试窗口"; UcCmr uc = new UcCmr(); uc.SetCamera(initor as IJFDevice_Camera); fm.SetRTUI(uc); } fm.Show(); } else { MessageBox.Show("无法为" + InitorCaption + "类型的对象提供调试界面"); return; } } }
/// <summary>添加新设备</summary> private void btAdd_Click(object sender, EventArgs e) { FormCreateInitor fm = new FormCreateInitor(); fm.Text = "创建" + InitorCaption + "对象"; fm.MatchType = InitorType; if (DialogResult.OK == fm.ShowDialog()) { IJFInitializable newDevice = fm.Initor; string devID = fm.ID; JFHubCenter.Instance.InitorManager.Add(devID, newDevice); DataGridViewRow row = new DataGridViewRow(); DataGridViewTextBoxCell cellID = new DataGridViewTextBoxCell(); cellID.Value = devID; row.Cells.Add(cellID); DataGridViewTextBoxCell cellModel = new DataGridViewTextBoxCell(); cellModel.Value = JFinitializerHelper.DispalyTypeName(newDevice.GetType()); row.Cells.Add(cellModel); DataGridViewTextBoxCell cellType = new DataGridViewTextBoxCell(); cellType.Value = newDevice.GetType().Name; row.Cells.Add(cellType); dgvDevs.Rows.Add(row); newDevice.Initialize(); if (!newDevice.IsInitOK) { btInit.Enabled = true; row.DefaultCellStyle.ForeColor = Color.Red; } else { btInit.Enabled = false; } /// 更新参数到界面 dgvDevs.Rows[dgvDevs.Rows.Count - 1].Selected = true; RemoveAllPEs(); string[] iniParamNames = newDevice.InitParamNames; if (null == iniParamNames) { btEditSave.Enabled = false; return; } btEditSave.Enabled = true; int locY = btInit.Location.Y + btInit.Size.Height + 5; foreach (string ipName in iniParamNames) { UcJFParamEdit pe = new UcJFParamEdit(); pe.Width = gbParams.Width - 1; pe.Location = new Point(4, locY); pe.SetParamDesribe(newDevice.GetInitParamDescribe(ipName)); locY += pe.Height; pe.SetParamValue(newDevice.GetInitParamValue(ipName)); pe.IsValueReadOnly = true; gbParams.Controls.Add(pe); } Type devType = newDevice.GetType(); if (typeof(IJFDevice_Camera).IsAssignableFrom(devType) || typeof(IJFDevice_LightController).IsAssignableFrom(devType) || typeof(IJFDevice_MotionDaq).IsAssignableFrom(devType) || typeof(IJFDevice_TrigController).IsAssignableFrom(devType) || typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) //提供调试界面 { btDebug.Enabled = true; if (typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) { chkSelfUI.Enabled = true; } else { chkSelfUI.Checked = false; chkSelfUI.Enabled = false; } } else { btDebug.Enabled = false; } if (typeof(IJFConfigUIProvider).IsAssignableFrom(devType)) { btCfg.Enabled = true; } else { btCfg.Enabled = false; } } }
//internal bool SaveCfgWhenAdd { get; set; } internal void Add(string id, IJFInitializable dev) { dictInitors.Add(id, dev); JFXmlSortedDictionary <string, List <object> > dictInitorParam = JFHubCenter.Instance.SystemCfg.GetItemValue(JFHubCenter.CK_InitDevParams) as JFXmlSortedDictionary <string, List <object> >; List <object> paramsInCfg = new List <object>(); paramsInCfg.Add(dev.GetType().AssemblyQualifiedName); for (int i = 0; i < dev.InitParamNames.Length; i++) { //paramsInCfg.Add(dev.GetInitParamValue(dev.InitParamNames[i])); 本行代码用以下代码代替,以支持有限的 简单类型 + 数组(列表)类型的组合 //暂时的解决方法:将数组类型转化为字符串存储 object paramVal = dev.GetInitParamValue(dev.InitParamNames[i]); Type paramType = dev.GetInitParamDescribe(dev.InitParamNames[i]).ParamType; SerializableAttribute[] sas = paramType.GetCustomAttributes(typeof(SerializableAttribute), false) as SerializableAttribute[]; if (sas != null && sas.Length > 0) //如果是可序列化的类型,直接保存序列化后的文本 { StringBuilder buffer = new StringBuilder(); XmlSerializer serializer = new XmlSerializer(paramType); using (TextWriter writer = new StringWriter(buffer)) { serializer.Serialize(writer, paramVal); } string xmlTxt = buffer.ToString(); paramsInCfg.Add(xmlTxt); } else { if (paramType.IsValueType || paramType == typeof(string)) //单值对象和字符串对象直接添加 { paramsInCfg.Add(paramVal); } else //目前支持Array 和 List { if (paramType.IsArray) //参数类型是数组 { if (null == paramVal) { paramsInCfg.Add(""); } else { StringBuilder sb = new StringBuilder(); string splitString = "$"; for (int j = 0; j < (paramVal as Array).Length; j++) { sb.Append((paramVal as Array).GetValue(j).ToString()); if (j < (paramVal as Array).Length - 1) { sb.Append(splitString); } } paramsInCfg.Add(sb.ToString());//paramsInCfg.Add(string.Join(splitString, paramVal)); //使用ASC 响铃符作为间隔 } } else if (typeof(IList).IsAssignableFrom(paramType)) //除了数组之外的队列类型 { if (null == paramVal) { paramsInCfg.Add(""); } else { StringBuilder sb = new StringBuilder(); string splitString = "$"; for (int j = 0; j < (paramVal as IList).Count; j++) { sb.Append((paramVal as IList)[j].ToString()); if (j < (paramVal as IList).Count - 1) { sb.Append(splitString); } } paramsInCfg.Add(sb.ToString()); //paramsInCfg.Add(string.Join(System.Text.Encoding.ASCII.GetString(new byte[] { 0x07 }), paramVal)); //使用ASC 响铃符作为间隔 } } } } } dictInitorParam.Add(id, paramsInCfg); JFHubCenter.Instance.SystemCfg.NotifyItemChanged(JFHubCenter.CK_InitDevParams); JFHubCenter.Instance.SystemCfg.Save(); }