using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Diagnostics; namespace UHFReader288MPDemo { /// /// 设备对象,记录设备信息并提供设备操作接口; /// public class DeviceClass { private int communicationTimeout = 1000; /// /// 广播地址; /// public static uint Broadcast { get { return 0xffffffff; } } private IntPtr _devHandle; /// /// 设备句柄; /// public IntPtr DevHandle { get { return _devHandle; } set { _devHandle = value; } } private uint _devIP; /// /// 设备IP地址; /// public uint DeviceIP { get { return _devIP; } set { _devIP = value; } } private string _devMac; /// /// 设备设备Mac地址; /// public string DeviceMac { get { return _devMac; } set { _devMac = value; } } private string _devName; /// /// 设备名; /// public string DeviceName { get { return _devName; } set { _devName = value; } } private bool _isLogin = false; /// /// 设备登录状态; /// public bool IsLogin { get { return _isLogin; } } /// /// 设备对象构造函数; /// /// 设备句柄 /// 设备IP地址 /// 设备Mac地址 /// 设备名 public DeviceClass(IntPtr devHandle, uint deviceIP, string deviceMac, string deviceName) { this.DevHandle = devHandle; this.DeviceIP = deviceIP; this.DeviceMac = deviceMac; this.DeviceName = deviceName; } /// /// 登录设备; /// /// 用户名 /// 密码 /// tagErrorCode 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; } /// /// 登出设备; /// /// tagErrorCode 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; } /// /// 修改设备密码; /// /// 用户当前使用的密码 /// 用户新的密码 /// tagErrorCode 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; } /// /// 重启设备; /// /// 设备重启方式类型,如是否保存当前参数、是否恢复默认值等 /// tagErrorCode 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; } /// /// 获取设备所支持串口通道的数量; /// /// 设备所支持串口通道的数量 public bool IsSupportChannel(int channelNum) { return DevControl.DM_IsComEnable(this._devHandle, channelNum); } } /// /// 重启方式类型; /// public enum RebootType { /// /// 只恢复默认参数,不重启设备; /// DefaultWithoutReboot, /// /// 重启设备,并恢复默认参数; /// DefaultAndReboot, /// /// 重启设备,不保存未保存参数; /// RebootWithoutSave, /// /// 重启设备,并保存参数; /// SaveAndReboot }; }