banboshi_V1/halftoneproject-master/Code/Device/Light.cs
2023-10-31 13:19:29 +08:00

93 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Newtonsoft.Json.Linq;
using static ControllerDllCSharp.ClassLibControllerDll;
namespace ProductionControl.Device
{
internal class Light : IDisposable
{
private int portHandle;
/// <summary>
/// 光源亮度值
/// </summary>
public int DigitalValue { get; private set; }
public Action<int, string> log;
/// <summary>
/// 光源亮度值<通道号,亮度值0-100>
/// </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 Light()
{
}
public void start(int comNum)
{
int result = CreateSerialPort(comNum, ref portHandle); //创建串口
if (result != SUCCESS)
throw new Exception($"打开光源设备(COM{comNum})失败: {result}");
IsInit = true;
//timer.Elapsed += Timer_Elapsed;
//timer.Interval = 100;
//timer.Enabled = true;
}
public void stop()
{
if (!IsInit) return;
IsInit = false;
timer.Elapsed -= Timer_Elapsed;
int result = ReleaseSerialPort(portHandle); //断开串口
}
public int getDigitalValue(int channelIndex)
{
if (!IsInit) return -1;
int value = 0;
if (GetDigitalValue(ref value, channelIndex, portHandle) != SUCCESS)
return -1;
DigitalEvent?.Invoke(channelIndex, value);
return value;
}
/// <summary>
/// 设置亮度值
/// </summary>
/// <param name="channelIndex">通道</param>
/// <param name="value">0-100</param>
/// <returns></returns>
public bool setDigitalValue(int channelIndex, int value)
{
if (!IsInit) return false;
if (SetDigitalValue(channelIndex, value, portHandle) != SUCCESS)
return false;
return true;
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (!IsInit) return;
}
public void Dispose()
{
stop();
}
}
}