You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ran into an exception user-unhandled error, “The BaseStream is only available when the port is open”:
Can we add some try catch like so?
public void Write(byte[] data)
{
try
{
// Ensure the port is open before writing
if (!_serialPort.IsOpen)
{
throw new InvalidOperationException("The serial port is not open. Please open the port before writing.");
}
_serialPort.BaseStream.Write(data, 0, data.Length);
}
catch (InvalidOperationException ex)
{
// Log or handle the exception
Console.WriteLine($"Error: {ex.Message}");
}
catch (IOException ex)
{
// Handle I/O errors
Console.WriteLine($"I/O Error: {ex.Message}");
}
}
The text was updated successfully, but these errors were encountered:
I ran into an exception user-unhandled error, “The BaseStream is only available when the port is open”:
Can we add some try catch like so?
The text was updated successfully, but these errors were encountered: