using System.IO.Ports;
namespace crusherScanner
{
///
/// For sending data to Prepmaster via serial.
/// nb; The concept here is to NOT hold the serial port open.
///
internal class SerialAccess
{
private static bool SerialReady = false;
private static SerialPort? _serialPort;
///
/// Send sample ID to the serial port for Prepmaster to receive.
///
/// The sample ID to send.
public static void SendToPrepmaster(string barcode)
{
if (barcode != "" && !Properties.Settings.Default.PrepmasterConnection)
{
MessageBox.Show($"The crusher flap would\nhave just opened to\naccept sample {barcode}", "Crusher Input");
}
else if (barcode != "")
{
//if (!SerialReady)
//{
SetupSerial();
//}
Logging.Append(LogLevel.Information, "Sending " + barcode + " to crusher input.");
SendSerial(barcode);
}
}
///
/// Setup and test the serial port.
///
private static void SetupSerial()
{
_serialPort = SetSerialSettings();
try
{
// Opens serial port as a quick test
_serialPort.Open();
}
catch (Exception ex)
{
Logging.Append(LogLevel.Error, "Opening serial port :: " + ex.Message);
MessageBox.Show("Error opening serial port :: " + ex.Message, "Error!");
}
finally
{
_serialPort.Close();
SerialReady = true;
}
}
///
/// Set the initial values for the serial port from the saved settings.
///
/// Configured SerialPort.
private static SerialPort SetSerialSettings()
{
string[] serialSettings = Properties.Settings.Default.PrepmasterMagazineSerial.Split(',');
Logging.Append(LogLevel.Information, "Opening serial port : using " + String.Join(',', serialSettings));
switch (serialSettings.Count())
{
case 1:
_serialPort = new SerialPort(serialSettings[0].Trim());
break;
case 2:
_serialPort = new SerialPort(serialSettings[0].Trim(), int.Parse(serialSettings[1].Trim()));
break;
case 3:
_serialPort = new SerialPort(serialSettings[0].Trim(), int.Parse(serialSettings[1].Trim()), (Parity)Enum.Parse(typeof(Parity), serialSettings[2].Trim()));
break;
case 4:
_serialPort = new SerialPort(serialSettings[0].Trim(), int.Parse(serialSettings[1].Trim()), (Parity)Enum.Parse(typeof(Parity), serialSettings[2].Trim()), int.Parse(serialSettings[3].Trim()));
break;
case 5:
_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()));
break;
default:
_serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
break;
}
_serialPort.Handshake = Handshake.None;
return _serialPort;
}
///
/// Send data to the serial connection.
///
/// The sample ID to send to Prepmaster.
private static void SendSerial(string data)
{
if (_serialPort == null)
{
Logging.Append(LogLevel.Error, "Opening serial port :: _serialPort is null");
MessageBox.Show("Error opening serial port :: Serial device may have been unpluged.", "Error!");
SerialReady = false;
return;
}
// Makes sure serial port is open before trying to write
try
{
if (!_serialPort.IsOpen)
{
_serialPort.Open();
_serialPort.Write(data + "\r\n");
}
_serialPort.Close();
}
catch (Exception ex)
{
Logging.Append(LogLevel.Error, "Opening serial port :: " + ex.Message);
MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
}
}
}
}