|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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();
- }
- }
- }
|