版博士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.

преди 2 години
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Timers;
  10. using Newtonsoft.Json.Linq;
  11. using static ControllerDllCSharp.ClassLibControllerDll;
  12. namespace ProductionControl.Device
  13. {
  14. internal class Light : IDisposable
  15. {
  16. private int portHandle;
  17. /// <summary>
  18. /// 光源亮度值
  19. /// </summary>
  20. public int DigitalValue { get; private set; }
  21. public Action<int, string> log;
  22. /// <summary>
  23. /// 光源亮度值<通道号,亮度值0-100>
  24. /// </summary>
  25. public Action<int, int> DigitalEvent;
  26. /// <summary>
  27. /// 是否打开设备成功
  28. /// </summary>
  29. public bool IsInit { get; private set; } = false;
  30. private System.Timers.Timer timer = new System.Timers.Timer();
  31. public Light()
  32. {
  33. }
  34. public void start(int comNum)
  35. {
  36. int result = CreateSerialPort(comNum, ref portHandle); //创建串口
  37. if (result != SUCCESS)
  38. throw new Exception($"打开光源设备(COM{comNum})失败: {result}");
  39. IsInit = true;
  40. //timer.Elapsed += Timer_Elapsed;
  41. //timer.Interval = 100;
  42. //timer.Enabled = true;
  43. }
  44. public void stop()
  45. {
  46. if (!IsInit) return;
  47. IsInit = false;
  48. timer.Elapsed -= Timer_Elapsed;
  49. int result = ReleaseSerialPort(portHandle); //断开串口
  50. }
  51. public int getDigitalValue(int channelIndex)
  52. {
  53. if (!IsInit) return -1;
  54. int value = 0;
  55. if (GetDigitalValue(ref value, channelIndex, portHandle) != SUCCESS)
  56. return -1;
  57. DigitalEvent?.Invoke(channelIndex, value);
  58. return value;
  59. }
  60. /// <summary>
  61. /// 设置亮度值
  62. /// </summary>
  63. /// <param name="channelIndex">通道</param>
  64. /// <param name="value">0-100</param>
  65. /// <returns></returns>
  66. public bool setDigitalValue(int channelIndex, int value)
  67. {
  68. if (!IsInit) return false;
  69. if (SetDigitalValue(channelIndex, value, portHandle) != SUCCESS)
  70. return false;
  71. return true;
  72. }
  73. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  74. {
  75. if (!IsInit) return;
  76. }
  77. public void Dispose()
  78. {
  79. stop();
  80. }
  81. }
  82. }