geboshi_V1/LeatherProject/LeatherApp/Device/LightDev.cs
2024-03-07 14:03:22 +08:00

195 lines
7.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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系列
/// <summary>
/// 设置通道亮度等级
/// </summary>
/// <param name="comHandle"></param>
/// <param name="channel">通道序号范围1-2</param>
/// <param name="range">亮度等级范围0-255</param>
/// <returns>0 - 设置发送成功; -1 - 串口未初始化; -2 - 输入参数范围有误</returns>
[DllImport(@"RseeController.dll", EntryPoint = "RseeController_AHC_SetChannel", CallingConvention = CallingConvention.Cdecl)]
private extern static int RseeController_AHC_SetChannel(IntPtr comHandle, int channel, int range);
/// <summary>
/// 读取通道亮度等级
/// </summary>
/// <param name="comHandle"></param>
/// <param name="channel">通道序号范围1-2</param>
/// <returns>大于等于0 - 通道亮度等级; -1 串口/网口未初始化; -2 - 输入参数范围有误; -3 - 读取返回出错</returns>
[DllImport(@"RseeController.dll", EntryPoint = "RseeController_AHC_ReadChannel", CallingConvention = CallingConvention.Cdecl)]
private extern static int RseeController_AHC_ReadChannel(IntPtr comHandle, int channel);
/// <summary>
/// 设置一个通道常亮/常灭
/// </summary>
/// <param name="comHandle"></param>
/// <param name="mode">state - 0常灭1常亮</param>
/// <returns></returns>
[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;
/// <summary>
/// 光源亮度值
/// </summary>
public int DigitalValue { get; private set; }
public Action<DateTime,WarningEnum, string> WarningEvent;
/// <summary>
/// 光源亮度值<通道号,亮度值0-255>
/// </summary>
public Action<int, int> DigitalEvent;
/// <summary>
/// 是否打开设备成功
/// </summary>
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;
}
/// <summary>
/// 设置亮度值
/// </summary>
/// <param name="channelIndex">通道</param>
/// <param name="value">0-255</param>
/// <returns></returns>
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();
}
}
}