DataLoggerSettings.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Hid;
  10. namespace Project1HostApp
  11. {
  12. public partial class frmDataloggerSettings : Form
  13. {
  14. private const int DEVICE_VID = 0x03EB;
  15. private const int DEVICE_PID = 0x2063;
  16. private struct Device_Report_t
  17. {
  18. public Byte Day;
  19. public Byte Month;
  20. public Byte Year;
  21. public Byte Hour;
  22. public Byte Minute;
  23. public Byte Second;
  24. public Byte LogInterval500MS;
  25. public Byte[] ToReport()
  26. {
  27. Byte[] Report = new Byte[7];
  28. Report[0] = this.Hour;
  29. Report[1] = this.Minute;
  30. Report[2] = this.Second;
  31. Report[3] = this.Day;
  32. Report[4] = this.Month;
  33. Report[5] = this.Year;
  34. Report[6] = this.LogInterval500MS;
  35. return Report;
  36. }
  37. public void FromReport(Byte[] Report)
  38. {
  39. this.Hour = Report[0];
  40. this.Minute = Report[1];
  41. this.Second = Report[2];
  42. this.Day = Report[3];
  43. this.Month = Report[4];
  44. this.Year = Report[5];
  45. this.LogInterval500MS = Report[6];
  46. }
  47. };
  48. private IDevice GetDeviceConnection()
  49. {
  50. IDevice[] ConnectedDevices = DeviceFactory.Enumerate(DEVICE_VID, DEVICE_PID);
  51. IDevice ConnectionHandle = null;
  52. if (ConnectedDevices.Count() > 0)
  53. ConnectionHandle = ConnectedDevices[0];
  54. else
  55. return null;
  56. // Fix report handle under Windows
  57. if (ConnectionHandle is Hid.Win32.Win32DeviceSet)
  58. {
  59. ((Hid.Win32.Win32DeviceSet)ConnectionHandle).AddDevice(0x00,
  60. ((Hid.Win32.Win32DeviceSet)ConnectionHandle).UnallocatedDevices[0]);
  61. }
  62. return ConnectionHandle;
  63. }
  64. public frmDataloggerSettings()
  65. {
  66. InitializeComponent();
  67. }
  68. private void btnSetValues_Click(object sender, EventArgs e)
  69. {
  70. IDevice ConnectionHandle = GetDeviceConnection();
  71. if (ConnectionHandle == null)
  72. {
  73. MessageBox.Show("Error: Cannot connect to Datalogger device.");
  74. return;
  75. }
  76. Device_Report_t DeviceReport = new Device_Report_t();
  77. DeviceReport.Day = (byte)dtpDate.Value.Day;
  78. DeviceReport.Month = (byte)dtpDate.Value.Month;
  79. DeviceReport.Year = (byte)((dtpDate.Value.Year < 2000) ? 0 : (dtpDate.Value.Year - 2000));
  80. DeviceReport.Hour = (byte)dtpTime.Value.Hour;
  81. DeviceReport.Minute = (byte)dtpTime.Value.Minute;
  82. DeviceReport.Second = (byte)dtpTime.Value.Second;
  83. DeviceReport.LogInterval500MS = (byte)(nudLogInterval.Value * 2);
  84. try
  85. {
  86. ConnectionHandle.Write(0x00, DeviceReport.ToReport());
  87. MessageBox.Show("Device parameters updated successfully.");
  88. }
  89. catch (Exception ex)
  90. {
  91. MessageBox.Show("Error: " + ex.Message);
  92. }
  93. }
  94. private void btnGetValues_Click(object sender, EventArgs e)
  95. {
  96. IDevice ConnectionHandle = GetDeviceConnection();
  97. if (ConnectionHandle == null)
  98. {
  99. MessageBox.Show("Error: Cannot connect to Datalogger device.");
  100. return;
  101. }
  102. Device_Report_t DeviceReport = new Device_Report_t();
  103. try
  104. {
  105. Byte[] Report = new Byte[7];
  106. ConnectionHandle.Read(0x00, Report);
  107. DeviceReport.FromReport(Report);
  108. String msgText = "Device parameters retrieved successfully.";
  109. try
  110. {
  111. dtpDate.Value = new DateTime(
  112. (2000 + DeviceReport.Year),
  113. DeviceReport.Month,
  114. DeviceReport.Day);
  115. dtpTime.Value = new DateTime(
  116. DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
  117. DeviceReport.Hour,
  118. DeviceReport.Minute,
  119. DeviceReport.Second);
  120. }
  121. catch (Exception ex)
  122. {
  123. msgText = "Problem reading device:\n" +
  124. ex.Message +
  125. "\nY:" + DeviceReport.Year.ToString() +
  126. " M:" + DeviceReport.Month.ToString() +
  127. " D:" + DeviceReport.Day.ToString() +
  128. "\n\nUsing current date and time.";
  129. dtpDate.Value = DateTime.Now;
  130. dtpTime.Value = DateTime.Now;
  131. }
  132. try
  133. {
  134. nudLogInterval.Value = (DeviceReport.LogInterval500MS / 2);
  135. }
  136. catch (Exception ex)
  137. {
  138. nudLogInterval.Value = nudLogInterval.Minimum;
  139. }
  140. MessageBox.Show(msgText);
  141. }
  142. catch (Exception ex)
  143. {
  144. MessageBox.Show("Error: " + ex.Message);
  145. }
  146. }
  147. private void frmDataloggerSettings_Load(object sender, EventArgs e)
  148. {
  149. }
  150. }
  151. }