SerialAccess.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.IO.Ports;
  2. namespace crusherScanner
  3. {
  4. /// <summary>
  5. /// For sending data to Prepmaster via serial.
  6. /// nb; The concept here is to NOT hold the serial port open.
  7. /// </summary>
  8. internal class SerialAccess
  9. {
  10. private static bool SerialReady = false;
  11. private static SerialPort? _serialPort;
  12. /// <summary>
  13. /// Send sample ID to the serial port for Prepmaster to receive.
  14. /// </summary>
  15. /// <param name="barcode">The sample ID to send.</param>
  16. public static void SendToPrepmaster(string barcode)
  17. {
  18. if (barcode != "" && !Properties.Settings.Default.PrepmasterConnection)
  19. {
  20. MessageBox.Show($"The crusher flap would\nhave just opened to\naccept sample {barcode}", "Crusher Input");
  21. }
  22. else if (barcode != "")
  23. {
  24. //if (!SerialReady)
  25. //{
  26. SetupSerial();
  27. //}
  28. Logging.Append(LogLevel.Information, "Sending " + barcode + " to crusher input.");
  29. SendSerial(barcode);
  30. }
  31. }
  32. /// <summary>
  33. /// Setup and test the serial port.
  34. /// </summary>
  35. private static void SetupSerial()
  36. {
  37. //TODO: use settings for connection
  38. _serialPort = SetSerialSettings();
  39. try
  40. {
  41. // Opens serial port as a quick test
  42. _serialPort.Open();
  43. }
  44. catch (Exception ex)
  45. {
  46. Logging.Append(LogLevel.Error, "Opening serial port :: " + ex.Message);
  47. MessageBox.Show("Error opening serial port :: " + ex.Message, "Error!");
  48. }
  49. finally
  50. {
  51. _serialPort.Close();
  52. SerialReady = true;
  53. }
  54. }
  55. /// <summary>
  56. /// Set the initial values for the serial port from the saved settings.
  57. /// </summary>
  58. /// <returns>Configured SerialPort.</returns>
  59. private static SerialPort SetSerialSettings()
  60. {
  61. string[] serialSettings = Properties.Settings.Default.PrepmasterMagazineSerial.Split(',');
  62. Logging.Append(LogLevel.Information, "Opening serial port : using " + String.Join(',', serialSettings));
  63. switch (serialSettings.Count())
  64. {
  65. case 1:
  66. _serialPort = new SerialPort(serialSettings[0].Trim());
  67. break;
  68. case 2:
  69. _serialPort = new SerialPort(serialSettings[0].Trim(), int.Parse(serialSettings[1].Trim()));
  70. break;
  71. case 3:
  72. _serialPort = new SerialPort(serialSettings[0].Trim(), int.Parse(serialSettings[1].Trim()), (Parity)Enum.Parse(typeof(Parity), serialSettings[2].Trim()));
  73. break;
  74. case 4:
  75. _serialPort = new SerialPort(serialSettings[0].Trim(), int.Parse(serialSettings[1].Trim()), (Parity)Enum.Parse(typeof(Parity), serialSettings[2].Trim()), int.Parse(serialSettings[3].Trim()));
  76. break;
  77. case 5:
  78. _serialPort = new SerialPort(serialSettings[0].Trim(), int.Parse(serialSettings[1].Trim()), (Parity)Enum.Parse(typeof(Parity), serialSettings[2].Trim()), int.Parse(serialSettings[3].Trim()), (StopBits)Enum.Parse(typeof(StopBits), serialSettings[4].Trim()));
  79. break;
  80. default:
  81. _serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
  82. break;
  83. }
  84. _serialPort.Handshake = Handshake.None;
  85. return _serialPort;
  86. }
  87. /// <summary>
  88. /// Send data to the serial connection.
  89. /// </summary>
  90. /// <param name="data">The sample ID to send to Prepmaster.</param>
  91. private static void SendSerial(string data)
  92. {
  93. if (_serialPort == null)
  94. {
  95. Logging.Append(LogLevel.Error, "Opening serial port :: _serialPort is null");
  96. MessageBox.Show("Error opening serial port :: Serial device may have been unpluged.", "Error!");
  97. SerialReady = false;
  98. return;
  99. }
  100. // Makes sure serial port is open before trying to write
  101. try
  102. {
  103. if (!_serialPort.IsOpen)
  104. {
  105. _serialPort.Open();
  106. _serialPort.Write(data + "\r\n");
  107. }
  108. _serialPort.Close();
  109. }
  110. catch (Exception ex)
  111. {
  112. Logging.Append(LogLevel.Error, "Opening serial port :: " + ex.Message);
  113. MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
  114. }
  115. }
  116. }
  117. }