版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

164 lines
5.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Timers;
  11. using System.Windows.Forms;
  12. using Newtonsoft.Json.Linq;
  13. using static ControllerDllCSharp.ClassLibControllerDll;
  14. namespace ProductionControl.Device
  15. {
  16. public class LightDev : IDisposable
  17. {
  18. #region 第二款光源(Rsee) X86DLL 64位可用吗?
  19. //comHandle = RseeController_OpenCom(PortName, 19200, true); //Com_Handle!=0成功
  20. [DllImport(@"RseeController.dll", EntryPoint = "RseeController_OpenCom", CallingConvention = CallingConvention.Cdecl)]
  21. private extern static int RseeController_OpenCom(string szPort, int Baud_rate, bool overlapped);
  22. //关闭传上面打开返回的comHandle
  23. [DllImport(@"RseeController.dll", EntryPoint = "RseeController_CloseCom", CallingConvention = CallingConvention.Cdecl)]
  24. private extern static bool RseeController_CloseCom(int comHandle);
  25. //设置新值 channel通道号:1-4
  26. [DllImport(@"RseeController.dll", EntryPoint = "RseeController_PM_D_BRTSetChannel", CallingConvention = CallingConvention.Cdecl)]
  27. private extern static int RseeController_PM_D_BRTSetChannel(int comHandle, int channel, int range);
  28. //读取当前值
  29. [DllImport(@"RseeController.dll", EntryPoint = "RseeController_PM_D_BRTReadChannel", CallingConvention = CallingConvention.Cdecl)]
  30. private static extern unsafe int RseeController_PM_D_BRTReadChannel(int comHandle, int channel, int* range);
  31. #endregion
  32. private int Com_Handle;
  33. private LightDevNameEnum devName;
  34. private long portHandle;
  35. /// <summary>
  36. /// 光源亮度值
  37. /// </summary>
  38. public int DigitalValue { get; private set; }
  39. public Action<WarningEnum, string> WarningEvent;
  40. /// <summary>
  41. /// 光源亮度值<通道号,亮度值0-255>
  42. /// </summary>
  43. public Action<int, int> DigitalEvent;
  44. /// <summary>
  45. /// 是否打开设备成功
  46. /// </summary>
  47. public bool IsInit { get; private set; } = false;
  48. //private System.Timers.Timer timer = new System.Timers.Timer();
  49. public LightDev(LightDevNameEnum _devName)
  50. {
  51. devName = _devName;
  52. }
  53. public bool start(int comNum)
  54. {
  55. try
  56. {
  57. if (devName == LightDevNameEnum.康仕达)
  58. {
  59. int result = CreateSerialPort(comNum, ref portHandle); //创建串口
  60. if (result != SUCCESS)
  61. throw new Exception($"打开光源设备(COM{comNum})失败: {result}");
  62. }
  63. else if (devName == LightDevNameEnum.锐视)
  64. {
  65. Com_Handle = RseeController_OpenCom("COM"+ comNum, 19200, true);
  66. if (Com_Handle == 0)
  67. throw new Exception($"打开光源设备(COM{comNum})失败;");
  68. }
  69. else
  70. throw new Exception($"打开光源设备失败,未知设备型号!");
  71. IsInit = true;
  72. //timer.Elapsed += Timer_Elapsed;
  73. //timer.Interval = 100;
  74. //timer.Enabled = true;
  75. return true;
  76. }
  77. catch(Exception ex)
  78. {
  79. WarningEvent?.Invoke(WarningEnum.High,ex.Message);
  80. return false;
  81. }
  82. }
  83. public void stop()
  84. {
  85. if (!IsInit) return;
  86. try
  87. {
  88. IsInit = false;
  89. //timer.Elapsed -= Timer_Elapsed;
  90. //timer.Stop();
  91. if (devName == LightDevNameEnum.康仕达)
  92. ReleaseSerialPort(portHandle); //断开串口
  93. else if (devName == LightDevNameEnum.锐视)
  94. RseeController_CloseCom(Com_Handle);
  95. }
  96. catch { }
  97. }
  98. public unsafe int getDigitalValue(int channelIndex)
  99. {
  100. if (!IsInit) return -1;
  101. int value = 0;
  102. if (devName == LightDevNameEnum.康仕达)
  103. {
  104. if (GetDigitalValue(ref value, channelIndex, portHandle) != SUCCESS)
  105. return -1;
  106. }
  107. else if (devName == LightDevNameEnum.锐视)
  108. {
  109. if (RseeController_PM_D_BRTReadChannel(Com_Handle, channelIndex, &value) != 0)
  110. return -1;
  111. }
  112. else
  113. return -1;
  114. DigitalEvent?.Invoke(channelIndex, value);
  115. return value;
  116. }
  117. /// <summary>
  118. /// 设置亮度值
  119. /// </summary>
  120. /// <param name="channelIndex">通道</param>
  121. /// <param name="value">0-255</param>
  122. /// <returns></returns>
  123. public bool setDigitalValue(int channelIndex, int value)
  124. {
  125. if (!IsInit) return false;
  126. Task.Factory.StartNew(() => {
  127. if (devName == LightDevNameEnum.康仕达)
  128. {
  129. if (SetDigitalValue(channelIndex, value, portHandle) != SUCCESS)
  130. return;// false;
  131. }
  132. else if (devName == LightDevNameEnum.锐视)
  133. {
  134. if (RseeController_PM_D_BRTSetChannel(Com_Handle, channelIndex, value) != 0)
  135. return;// false;
  136. }
  137. });
  138. return true;
  139. }
  140. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  141. {
  142. if (!IsInit) return;
  143. }
  144. public void Dispose()
  145. {
  146. stop();
  147. }
  148. }
  149. }