2
votes

I'm experiencing this problem while opening a serial port in C# (which should be dirt simple; or so I thought).

When I try to open a serial port, I am getting the following exception:

The semaphore time-out period has expired.‎

Here is the method that is doing this.

public static void Open_TheActivePortWeWillUse(String Drone_StringNameFromUser)
{
    var TempSerialPort = new SerialPort (
        Drone_StringNameFromUser, 
        (int) SerialPortParameter.TheSerialPortSpeed);

    // Now we have a name that anybody can see and use
    OurSpecificPorts.TheActivePortWeAreUsing = TempSerialPort;      
    // We'll do 8-N-1 since almost the whole planet does that
    OurSpecificPorts.TheActivePortWeAreUsing.DataBits = 8;  
    // We'll do 8-N-1
    OurSpecificPorts.TheActivePortWeAreUsing.Parity = Parity.None;    
    // We'll do 8-N-1
    OurSpecificPorts.TheActivePortWeAreUsing.StopBits = StopBits.One;    

    OurSpecificPorts.TheActivePortWeAreUsing.DataReceived +=    
        OurBackGroundSerialPortReceiver;
    // We can now open our active port, which is what this line does
    OurSpecificPorts.TheActivePortWeAreUsing.Open();      
}

The strangest thing for me is that I get this error inconsistently. Half the time it works okay, and the other half, it does not.

Does anybody see anything obviously wrong with my code? Am I missing something?

1

1 Answers

2
votes

Declaring your SerialPort object inside the method will not allow access to it after the method closes. Here is a method that works to open the port:

  private void OpenSerialPort(String portName)
    {
        try
        {
            serialPort1.Close();
            serialPort1.PortName = portName;
            serialPort1.BaudRate = 115200;
            serialPort1.DataBits = 8;
            serialPort1.Handshake = Handshake.None;
            serialPort1.Parity = Parity.None;
            serialPort1.RtsEnable = false;
            serialPort1.StopBits = StopBits.One;

            serialPort1.Open();

        }
        catch (Exception ex)
        {
            MessageBox.Show("Could not open serial port " + portName, "Error");
        }
    }

The SerialPort object is declared in the class:

namespace Arcadia
{
    public partial class Form1 : Form
    {
        private SerialPort serialPort1;

And the callback is added in the constructor:

    public Form1()
    {
        InitializeComponent();

        serialPort1.DataReceived += new SerialDataReceivedEventHandler(this.SerialPortReadCallback);

Sending data is handled in a different method:

    private void SerialPortWrite(String writeString)
    {
        if (serialPort1.IsOpen)
        {
            serialPort1.WriteLine(writeString);
        }
    }

And here is the received data callback:

    private void SerialPortReadCallback(object sender, SerialDataReceivedEventArgs args)
    {
        try
        {

            while (serialPort1.BytesToRead > 0)
            {
                // Do something with the data

            }
        }
        catch (Exception ex)
        {
        }
    }