I'm trying to print with a thermal printer which uses a com port. And I would like to detect whether the printer is connected or not.
I was assuming that if I try to run the codes which tries to print without connecting the printer, I would get some kind of error. So I used try-catch block. The following is what I have tried so far.
using System.IO.Ports;
private readonly SerialPort _printer = new SerialPort(Port, BaudRate, Parity, DataBits, Stopbits);
private void Print(string text)
{
try
{
if (!_printer.IsOpen)
_printer.Open();
_printer.WriteLine(text);
_printer.Close();
}
catch (Exception ex)
{
Logging.Log("PRINT ERROR: " + ex.Message);
}
}
In most cases, if you try to print programmatically with the printer being disconnected, the code block which tries to print will throw error.
But in this case, the codes were executed as if the printer was connected and no errors were thrown.
I have also tried SerialPort.ErrorReceived
event but it didn't work either.
So, I was wondering if there's a way to detect the status of the printer (whether it was connected or disconnected). Thanks for the help.