Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exception user-unhandled error, “The BaseStream is only available when the port is open”: #70

Open
LarryChiem opened this issue Dec 12, 2024 · 1 comment

Comments

@LarryChiem
Copy link

LarryChiem commented Dec 12, 2024

I ran into an exception user-unhandled error, “The BaseStream is only available when the port is open”:

image

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}");
            }
        }
@LarryChiem
Copy link
Author

Can you add me as a contributor @jefffhaynes ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant