/// <summary> /// 重启设备; /// </summary> /// <param name="rebootType">设备重启方式类型,如是否保存当前参数、是否恢复默认值等</param> /// <returns>tagErrorCode</returns> public DevControl.tagErrorCode RebootManage(RebootType rebootType) { DevControl.tagErrorCode eCode; switch (rebootType) { case RebootType.DefaultWithoutReboot: eCode = DevControl.DM_LoadDefault(this._devHandle, this.communicationTimeout); break; case RebootType.DefaultAndReboot: eCode = DevControl.DM_LoadDefault(this._devHandle, this.communicationTimeout); if (eCode == DevControl.tagErrorCode.DM_ERR_OK) { eCode = DevControl.DM_ResetDevice(this._devHandle, this.communicationTimeout); } break; case RebootType.RebootWithoutSave: eCode = DevControl.DM_ResetDeviceWithoutSave(this._devHandle, this.communicationTimeout); break; case RebootType.SaveAndReboot: eCode = DevControl.DM_ResetDevice(this._devHandle, this.communicationTimeout); break; default: eCode = DevControl.tagErrorCode.DM_ERR_ARG; Debug.Fail("Not Support this RebootType!"); break; } ; return(eCode); }
/// <summary> /// 集中处理操作返回错误码,并生成错误说明信息; /// </summary> /// <param name="eCode">tagErrorCode</param> /// <returns>错误说明信息</returns> public static string HandleError(DevControl.tagErrorCode eCode) { string errorMsg; switch (eCode) { case DevControl.tagErrorCode.DM_ERR_OK: case DevControl.tagErrorCode.DM_ERR_PARA: case DevControl.tagErrorCode.DM_ERR_AUTHFAIL: case DevControl.tagErrorCode.DM_ERR_TIMEOUT: case DevControl.tagErrorCode.DM_ERR_OPR: errorMsg = GetErrorMsg(eCode); break; case DevControl.tagErrorCode.DM_ERR_NOAUTH: errorMsg = GetErrorMsg(eCode); break; case DevControl.tagErrorCode.DM_ERR_SOCKET: case DevControl.tagErrorCode.DM_ERR_MEM: case DevControl.tagErrorCode.DM_ERR_MATCH: case DevControl.tagErrorCode.DM_ERR_ARG: errorMsg = GetErrorMsg(eCode); Log.WriteError(errorMsg); errorMsg = "Soft internal error!"; break; default: errorMsg = "No support this tagErrorCode!"; Debug.Fail(errorMsg); break; }; return errorMsg; }
/// <summary> /// 依据容器控件中的对应映射控件,向远程设备请求对应的已配置参数; /// </summary> /// <param name="ownerPage">容器控件;</param> /// <param name="channelNum">参数所属通道号;</param> /// <param name="paraList">请求参数列表;</param> public void GetQueryParaList(Control ownerPage, int channelNum, IntPtr paraList) { //遍历容器控件,通过控件名与设备参数间的映射关系,生成向远程设备请求配置参数值的参数列表; foreach (Control control in ownerPage.Controls) { //控件不使能则不需要请求参数(包括其下所有子控件) if ((control.Enabled == false)) { continue; } //控件为非叶节点控件,则使用递归继续向下遍历; if (control.Controls.Count != 0) { GetQueryParaList(control, channelNum, paraList); } //使用control.AccessibleName来标识参与映射的控件; //可依据需求修改为其他标识; if (control.AccessibleName != null) { bool isFind; DevControl.PARA_TYPES paraType = DevControl.PARA_TYPES.END_OF_PARA_TYPES; isFind = this._propertyParaMap.FindParaByProperty(control.AccessibleName, ref paraType); if (isFind == true) { DevControl.paralist_addnode(paraList, channelNum, paraType); } } } }
/// <summary> /// 登出设备; /// </summary> /// <returns>tagErrorCode</returns> public DevControl.tagErrorCode Logout() { DevControl.tagErrorCode eCode = DevControl.tagErrorCode.DM_ERR_OK; if (this._isLogin == true) { eCode = DevControl.DM_LogOutDevice(this._devHandle, this.communicationTimeout); this._isLogin = false; } return(eCode); }
/// <summary> /// 修改设备密码; /// </summary> /// <param name="oldPassword">用户当前使用的密码</param> /// <param name="newPassword">用户新的密码</param> /// <returns>tagErrorCode</returns> public DevControl.tagErrorCode ModifyPassword(string oldPassword, string newPassword) { StringBuilder newPasswordBuf, passwordBuf; DevControl.tagErrorCode eCode; passwordBuf = new StringBuilder(oldPassword); newPasswordBuf = new StringBuilder(newPassword); eCode = DevControl.DM_ModifyPassword(this._devHandle, passwordBuf, newPasswordBuf, this.communicationTimeout); return(eCode); }
/// <summary> /// 集中处理操作返回错误码,并生成错误说明信息; /// </summary> /// <param name="eCode">tagErrorCode</param> /// <returns>错误说明信息</returns> public static string HandleDeviceError(DevControl.tagErrorCode eCode, DeviceClass device) { string errorMsg; if (eCode == DevControl.tagErrorCode.DM_ERR_NOAUTH) { LoginForm loginform = new LoginForm(); DialogResult result = loginform.ShowDialog(); if (result == DialogResult.OK) { eCode = device.Login(loginform.UserName, loginform.Password); } } errorMsg = HandleError(eCode); return errorMsg; }
/// <summary> /// 登录设备; /// </summary> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <returns>tagErrorCode</returns> public DevControl.tagErrorCode Login(string userName, string password) { StringBuilder nameBuf, passwordBuf; DevControl.tagErrorCode eCode; nameBuf = new StringBuilder(userName); passwordBuf = new StringBuilder(password); eCode = DevControl.DM_AuthLogin(this._devHandle, nameBuf, passwordBuf, this.communicationTimeout); if (eCode == DevControl.tagErrorCode.DM_ERR_OK) { this._isLogin = true; } else { this._isLogin = false; } return(eCode); }
/// <summary> /// 依据容器控件中的对应映射控件,将本地配置参数配置到远程设备; /// </summary> /// <param name="ownerPage">容器控件;</param> /// <param name="channelNum">参数所属通道号;</param> /// <returns>tagErrorCode</returns> public DevControl.tagErrorCode SetParameter(Control ownerPage, int channelNum) { IntPtr setParaList; DevControl.tagErrorCode eCode; //将配置参数设置到对应远程设备; setParaList = DevControl.paralistCreate(this._devHandle); if (setParaList == IntPtr.Zero) { return(DevControl.tagErrorCode.DM_ERR_MEM); } eCode = GetParameterFromProperty(ownerPage, channelNum, setParaList); if (eCode == DevControl.tagErrorCode.DM_ERR_OK) { Debug.Assert(setParaList != IntPtr.Zero); eCode = DevControl.DM_SetPara(this._devHandle, setParaList, 1000); DevControl.paralistDestroy(setParaList); } return(eCode); }
/// <summary> /// 依据容器控件中的对应映射控件,从设备获取参数并填充对应映射控件; /// </summary> /// <param name="ownerPage">容器控件;</param> /// <param name="channelNum">参数所属通道号;</param> /// <returns>tagErrorCode</returns> public DevControl.tagErrorCode GetParameter(Control ownerPage, int channelNum) { IntPtr getParaList; //从远程设备获取其当前配置参数; getParaList = DevControl.paralistCreate(this._devHandle); if (getParaList == IntPtr.Zero) { return(DevControl.tagErrorCode.DM_ERR_MEM); } GetQueryParaList(ownerPage, channelNum, getParaList); DevControl.tagErrorCode errCode = DevControl.DM_GetPara(this._devHandle, getParaList, 1000); if (errCode == DevControl.tagErrorCode.DM_ERR_OK) { SetParameterToProperty(getParaList, ownerPage, channelNum); } DevControl.paralistDestroy(getParaList); return(errCode); }
/// <summary> /// 将paralist里的数值填充入容器控件中的对应映射控件; /// </summary> /// <param name="paralist">IntPtr(paralist);</param> /// <param name="ownerPage">容器控件;</param> /// <param name="channelNum">参数所属通道号;</param> /// <returns>是否成功</returns> private bool SetParameterToProperty(IntPtr paralist, Control ownerPage, int channelNum) { //通过控件名与设备参数间的映射关系,将从设备获取的参数值填充至对应的配置界面控件; if (paralist == IntPtr.Zero) { return(false); } //遍历容器控件,通过控件名与设备参数间的映射关系,从参数列表中读取设备参数填充入对应控件; foreach (Control control in ownerPage.Controls) { //控件为非叶节点控件,则使用递归继续向下遍历; if (control.Controls.Count != 0) { SetParameterToProperty(paralist, control, channelNum); } //使用control.AccessibleName来标识参与映射的控件; //可依据需求修改为其他标识; if (control.AccessibleName != null) { bool isFind; //查找控件映射的PARA_TYPES DevControl.PARA_TYPES paraType = DevControl.PARA_TYPES.END_OF_PARA_TYPES; isFind = this._propertyParaMap.FindParaByProperty(control.AccessibleName, ref paraType); if (isFind == true) { byte[] valueBuf = new byte[100]; int valueLen = valueBuf.Length; //使用PARA_TYPES从参数列表中读取设备参数填充入对应控件; DevControl.tagErrorCode eCode = DevControl.paralist_getnode(paralist, channelNum, paraType, ref valueLen, valueBuf); if (eCode == DevControl.tagErrorCode.DM_ERR_OK) { //当前设备不支持此参数; if (valueLen == 0) { control.Enabled = false; continue; } //将获取到的参数统一转换为字符串再按控件类型分别填充; StringBuilder bufferString = new StringBuilder(100); int stringLen = bufferString.Capacity; eCode = DevControl.DM_Value2String(paraType, valueBuf, valueLen, bufferString, ref stringLen); Debug.Assert(eCode == DevControl.tagErrorCode.DM_ERR_OK); Type controlType = control.GetType(); if (controlType == typeof(System.Windows.Forms.TextBox)) { ((System.Windows.Forms.TextBox)control).Text = bufferString.ToString(); } else if (controlType == typeof(System.Windows.Forms.CheckBox)) { if (bufferString[0] == '1') { ((System.Windows.Forms.CheckBox)control).Checked = true; } else if (bufferString[0] == '0') { ((System.Windows.Forms.CheckBox)control).Checked = false; } else { Debug.Fail("out of range!"); } } else if (controlType == typeof(System.Windows.Forms.ComboBox)) { if (valueBuf[0] < ((System.Windows.Forms.ComboBox)control).Items.Count) { ((System.Windows.Forms.ComboBox)control).SelectedIndex = int.Parse(bufferString.ToString()); } else { Debug.Fail("out of range!"); } } else if (controlType == typeof(System.Windows.Forms.NumericUpDown)) { ((System.Windows.Forms.NumericUpDown)(control)).Value = decimal.Parse(bufferString.ToString()); } else { Debug.Fail("must support new controlType!"); continue; } } } } } return(true); }
/// <summary> /// 获取错误说明信息; /// </summary> /// <param name="eCode">tagErrorCode</param> /// <returns>错误说明信息</returns> public static string GetErrorMsg(DevControl.tagErrorCode eCode) { Debug.Assert((uint)eCode < errorMsg.Length); return errorMsg[(uint)eCode]; }
/// <summary> /// 添加控件与PARA_TYPES间的映射关系; /// </summary> /// <param name="propertyName">控件标识,使用控件的AccessibleName属性作为标识;</param> /// <param name="paraType">DevControl.PARA_TYPES</param> /// <returns>是否添加成功</returns> public bool AddPropertyParaMap(string propertyName, DevControl.PARA_TYPES paraType) { //增加控件与设备参数间的映射关系; return this._propertyParaMap.AddPropertyParaMap(propertyName, paraType); }
/// <summary> /// 使用PARA_TYPES查找其所对应的控件标识; /// </summary> /// <param name="paraType">DevControl.PARA_TYPES</param> /// <returns>对应的控件标识</returns> public string FindPropertyByPara(DevControl.PARA_TYPES paraType) { return (string)this.propertyParaMap[(int)paraType]; }
/// <summary> /// 使用控件标识查找其所对应的PARA_TYPES; /// </summary> /// <param name="propertyName">控件标识;</param> /// <param name="paraType">DevControl.PARA_TYPES</param> /// <returns>是否添加成功</returns> public bool FindParaByProperty(string propertyName, ref DevControl.PARA_TYPES paraType) { int index = 0; foreach (string name in this.propertyParaMap) { if (propertyName.Equals(name)) { paraType = (DevControl.PARA_TYPES)index; return true; } index++; } return false; }
/// <summary> /// 添加控件与PARA_TYPES间的映射关系; /// </summary> /// <param name="propertyName">控件标识;</param> /// <param name="paraType">DevControl.PARA_TYPES</param> /// <returns>是否添加成功</returns> public bool AddPropertyParaMap(string propertyName, DevControl.PARA_TYPES paraType) { int mapIndex = (int)paraType; Debug.Assert(mapIndex < this.propertyParaMap.Length) ; Debug.Assert(this.propertyParaMap[mapIndex] == null) ; this.propertyParaMap[mapIndex] = propertyName; return true; }
/// <summary> /// 获取设备所支持串口通道的数量; /// </summary> /// <returns>设备所支持串口通道的数量</returns> public bool IsSupportChannel(int channelNum) { return(DevControl.DM_IsComEnable(this._devHandle, channelNum)); }
/// <summary> /// 从容器控件中读取已加入映射表的子控件的输入值; /// </summary> /// <param name="ownerPage">容器控件;</param> /// <param name="channelNum">参数所属通道号;</param> /// <param name="paraList">IntPtr(paralist);</param> /// <returns>DevControl.tagErrorCode</returns> private DevControl.tagErrorCode GetParameterFromProperty(Control ownerPage, int channelNum, IntPtr paraList) { IntPtr setParaList = paraList; DevControl.tagErrorCode eCode = DevControl.tagErrorCode.DM_ERR_OK; Debug.Assert(ownerPage != null); Debug.Assert(paraList != IntPtr.Zero); //遍历容器控件,通过控件名与设备参数间的映射关系,从配置界面的控件中读取配置设备的参数; foreach (Control control in ownerPage.Controls) { //控件不使能则不需要填充数值(包括其下所有子控件) if ((control.Enabled == false)) { continue; } //控件为非叶节点控件,则使用递归继续向下遍历; if (control.Controls.Count != 0) { eCode = GetParameterFromProperty(control, channelNum, paraList); if (eCode != DevControl.tagErrorCode.DM_ERR_OK) { break; } } //使用control.AccessibleName来标识参与映射的控件; //可依据需求修改为其他标识; if (control.AccessibleName != null) { bool isFind; //查找控件映射的PARA_TYPES DevControl.PARA_TYPES paraType = DevControl.PARA_TYPES.END_OF_PARA_TYPES; isFind = this._propertyParaMap.FindParaByProperty(control.AccessibleName, ref paraType); if (isFind == true) { //依据控件类型,从控件中读取输入参数值,按字符串格式读取; StringBuilder bufferString = new StringBuilder(); Type controlType = control.GetType(); if (controlType == typeof(System.Windows.Forms.TextBox)) { if (((System.Windows.Forms.TextBox)control).ReadOnly == true) { continue; } bufferString.Append(((System.Windows.Forms.TextBox)control).Text.Trim()); } else if (controlType == typeof(System.Windows.Forms.CheckBox)) { if (((System.Windows.Forms.CheckBox)control).Checked) { bufferString.Append('1'); } else { bufferString.Append('0'); } } else if (controlType == typeof(System.Windows.Forms.ComboBox)) { bufferString.Append(((System.Windows.Forms.ComboBox)control).SelectedIndex.ToString()); } else if (controlType == typeof(System.Windows.Forms.NumericUpDown)) { bufferString.Append(((System.Windows.Forms.NumericUpDown)control).Value.ToString()); } else { Debug.Fail("must support new controlType!"); continue; } //将读取的字符串格式输入参数值转换为操作接口输入类型,然后检查输入参数的合法性,合法则加入参数列表; byte[] valueBuf = new byte[100]; int valueLen = valueBuf.Length; eCode = DevControl.DM_String2Value(paraType, bufferString, bufferString.Length, valueBuf, ref valueLen); if (eCode == DevControl.tagErrorCode.DM_ERR_OK) { eCode = DevControl.DM_CheckPara(this._devHandle, channelNum, paraType, valueLen, valueBuf); if (eCode == DevControl.tagErrorCode.DM_ERR_OK) { DevControl.paralist_addnode(setParaList, channelNum, paraType, valueLen, valueBuf); } else { break; } } else { break; } } } } return(eCode); }