using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Timers; namespace LeatherApp.Device { public class LightDev : IDisposable { #region 第二款光源(Rsee) X86DLL 64位可用吗? //comHandle = RseeController_OpenCom(PortName, 19200, true); //Com_Handle!=0成功 [DllImport(@"RseeController.dll", EntryPoint = "RseeController_OpenCom", CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr RseeController_OpenCom(string szPort, int Baud_rate, bool overlapped); //关闭传上面打开返回的comHandle [DllImport(@"RseeController.dll", EntryPoint = "RseeController_CloseCom", CallingConvention = CallingConvention.Cdecl)] private extern static bool RseeController_CloseCom(string szPort, IntPtr comHandle); //设置新值 channel通道号:1-4 //[DllImport(@"RseeController.dll", EntryPoint = "RseeController_PM_D_BRTSetChannel", CallingConvention = CallingConvention.Cdecl)] //private extern static int RseeController_PM_D_BRTSetChannel(int comHandle, int channel, int range); ////读取当前值 //[DllImport(@"RseeController.dll", EntryPoint = "RseeController_PM_D_BRTReadChannel", CallingConvention = CallingConvention.Cdecl)] //private static extern unsafe int RseeController_PM_D_BRTReadChannel(int comHandle, int channel, int* range); //AHC系列 /// /// 设置通道亮度等级 /// /// /// 通道序号,范围:1-2 /// 亮度等级,范围:0-255 /// 0 - 设置发送成功; -1 - 串口未初始化; -2 - 输入参数范围有误 [DllImport(@"RseeController.dll", EntryPoint = "RseeController_AHC_SetChannel", CallingConvention = CallingConvention.Cdecl)] private extern static int RseeController_AHC_SetChannel(IntPtr comHandle, int channel, int range); /// /// 读取通道亮度等级 /// /// /// 通道序号,范围:1-2 /// 大于等于0 - 通道亮度等级; -1 – 串口/网口未初始化; -2 - 输入参数范围有误; -3 - 读取返回出错 [DllImport(@"RseeController.dll", EntryPoint = "RseeController_AHC_ReadChannel", CallingConvention = CallingConvention.Cdecl)] private extern static int RseeController_AHC_ReadChannel(IntPtr comHandle, int channel); /// /// 设置一个通道常亮/常灭 /// /// /// state - 0:常灭,1:常亮 /// [DllImport(@"RseeController.dll", EntryPoint = "RseeController_AHC_SetOnoff", CallingConvention = CallingConvention.Cdecl)] private extern static int RseeController_AHC_SetOnoff(IntPtr comHandle, bool state); #endregion private IntPtr Com_Handle; private LightDevNameEnum devName; private long portHandle; private string comName; /// /// 光源亮度值 /// public int DigitalValue { get; private set; } public Action WarningEvent; /// /// 光源亮度值<通道号,亮度值0-255> /// public Action DigitalEvent; /// /// 是否打开设备成功 /// public bool IsInit { get; private set; } = false; //private System.Timers.Timer timer = new System.Timers.Timer(); public LightDev(LightDevNameEnum _devName) { devName = _devName; } public bool start(int comNum) { try { if (devName == LightDevNameEnum.海康) { //int result = CreateSerialPort(comNum, ref portHandle); //创建串口 //if (result != SUCCESS) // throw new Exception($"打开光源设备(COM{comNum})失败: {result}"); } else if (devName == LightDevNameEnum.锐视) { Com_Handle = RseeController_OpenCom("COM" + comNum, 19200, true); if (Com_Handle == IntPtr.Zero) throw new Exception($"打开光源设备(COM{comNum})失败;"); comName = "COM" + comNum; //常亮 var res=RseeController_AHC_SetOnoff(Com_Handle,true); WarningEvent?.Invoke(DateTime.Now, WarningEnum.Normal, "常亮res:" + res); } else throw new Exception($"打开光源设备失败,未知设备型号!"); IsInit = true; //timer.Elapsed += Timer_Elapsed; //timer.Interval = 100; //timer.Enabled = true; return true; } catch (Exception ex) { WarningEvent?.Invoke(DateTime.Now,WarningEnum.High, ex.Message); return false; } } public void stop() { if (!IsInit) return; try { IsInit = false; //timer.Elapsed -= Timer_Elapsed; //timer.Stop(); if (devName == LightDevNameEnum.海康) ; //ReleaseSerialPort(portHandle); //断开串口 else if (devName == LightDevNameEnum.锐视) RseeController_CloseCom(comName, Com_Handle); } catch { } } public unsafe int getDigitalValue(int channelIndex) { if (!IsInit) return -1; int value = 0; if (devName == LightDevNameEnum.海康) { //if (GetDigitalValue(ref value, channelIndex, portHandle) != SUCCESS) return -1; } else if (devName == LightDevNameEnum.锐视) { value = RseeController_AHC_ReadChannel(Com_Handle, channelIndex); if (value<0) return -1; } else return -1; DigitalEvent?.Invoke(channelIndex, value); return value; } /// /// 设置亮度值 /// /// 通道 /// 0-255 /// public bool setDigitalValue(int channelIndex, int value) { if (!IsInit) return false; //Task.Factory.StartNew(() => { if (devName == LightDevNameEnum.海康) { //if (SetDigitalValue(channelIndex, value, portHandle) != SUCCESS) return false; } else if (devName == LightDevNameEnum.锐视) { var res = RseeController_AHC_SetChannel(Com_Handle, channelIndex, value); WarningEvent?.BeginInvoke(DateTime.Now,WarningEnum.Normal, res.ToString(),null,null); if (res != 0) return false; } //}); return true; } private void Timer_Elapsed(object sender, ElapsedEventArgs e) { if (!IsInit) return; } public void Dispose() { stop(); } } }