SerialAccess.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. _serialPort = SetSerialSettings();
  38. try
  39. {
  40. // Opens serial port as a quick test
  41. _serialPort.Open();
  42. }
  43. catch (Exception ex)
  44. {
  45. Logging.Append(LogLevel.Error, "Opening serial port :: " + ex.Message);
  46. MessageBox.Show("Error opening serial port :: " + ex.Message, "Error!");
  47. }
  48. finally
  49. {
  50. _serialPort.Close();
  51. SerialReady = true;
  52. }
  53. }
  54. /// <summary>
  55. /// Set the initial values for the serial port from the saved settings.
  56. /// </summary>
  57. /// <returns>Configured SerialPort.</returns>
  58. private static SerialPort SetSerialSettings()
  59. {
  60. string[] serialSettings = Properties.Settings.Default.PrepmasterMagazineSerial.Split(',');
  61. Logging.Append(LogLevel.Information, "Opening serial port : using " + String.Join(',', serialSettings));
  62. switch (serialSettings.Count())
  63. {
  64. case 1:
  65. _serialPort = new SerialPort(serialSettings[0].Trim());
  66. break;
  67. case 2:
  68. _serialPort = new SerialPort(serialSettings[0].Trim(), int.Parse(serialSettings[1].Trim()));
  69. break;
  70. case 3:
  71. _serialPort = new SerialPort(serialSettings[0].Trim(), int.Parse(serialSettings[1].Trim()), (Parity)Enum.Parse(typeof(Parity), serialSettings[2].Trim()));
  72. break;
  73. case 4:
  74. _serialPort = new SerialPort(serialSettings[0].Trim(), int.Parse(serialSettings[1].Trim()), (Parity)Enum.Parse(typeof(Parity), serialSettings[2].Trim()), int.Parse(serialSettings[3].Trim()));
  75. break;
  76. case 5:
  77. _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()));
  78. break;
  79. default:
  80. _serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
  81. break;
  82. }
  83. _serialPort.Handshake = Handshake.None;
  84. return _serialPort;
  85. }
  86. /// <summary>
  87. /// Send data to the serial connection.
  88. /// </summary>
  89. /// <param name="data">The sample ID to send to Prepmaster.</param>
  90. private static void SendSerial(string data)
  91. {
  92. if (_serialPort == null)
  93. {
  94. Logging.Append(LogLevel.Error, "Opening serial port :: _serialPort is null");
  95. MessageBox.Show("Error opening serial port :: Serial device may have been unpluged.", "Error!");
  96. SerialReady = false;
  97. return;
  98. }
  99. // Makes sure serial port is open before trying to write
  100. try
  101. {
  102. if (!_serialPort.IsOpen)
  103. {
  104. _serialPort.Open();
  105. _serialPort.Write(data + "\r\n");
  106. }
  107. _serialPort.Close();
  108. }
  109. catch (Exception ex)
  110. {
  111. Logging.Append(LogLevel.Error, "Opening serial port :: " + ex.Message);
  112. MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
  113. }
  114. }
  115. }
  116. }