0

I have an issue with opening an STMicro USB virtual COM port.

When I plug the device into my PC, the COM port appears as it should, and the Windows Device Manager indicates it is working properly.

I have a C# program on the PC which selects and opens this port.

However, in about 1 in 10 attempts, the PC program sticks on the port.open() command, and after about half a minute, returns with the error "The semaphore timeout period has expired".

I have written a tiny C# program that does nothing more than open the port. This still gives the behaviour noted.

public partial class Form1 : Form
{
    SerialPort port = new SerialPort();
    string portName = "COM1";  // Give it a default to start with


    public Form1()
    {
        InitializeComponent();

        // Populate the COM port selector combobox with available port names
        cmbPortSelect.Items.Clear();

        string[] activePorts = SerialPort.GetPortNames();
        foreach (string availablePort in activePorts)
        {
            cmbPortSelect.Items.Add(availablePort);
        }

        // Declare the serial port
        port = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One);
        port.ReadTimeout = 100;

    }

    private void cmbPortSelect_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cmbPortSelect.SelectedIndex != -1)
        {   // It will get set to -1 (i.e. no selection) in the catch below - we don’t want this selected item change to do anything
            if (port.IsOpen) port.Close();
            port.PortName = (string)cmbPortSelect.SelectedItem;
            System.Threading.Thread.Sleep(50);
            try
            {
                port.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                cmbPortSelect.SelectedIndex = -1;  // Clear the selected item box
            }
        }

    }
}

If instead of using my C# program to open the port, I use the communications program PuTTY, it works every time.

In addition, if I plug in a device with an FDTI USB virtual COM port, it also works every time.

I'm using Windows 7, with the STMicro VCP drivers ver 1.3.1, but the same behaviour occurs with Windows 10 and the generic Microsoft drivers, which STMicro recommend we use.

There is a version 1.5.1 drivers for Windows 7, but when I installed them, it reported that they had installed correctly, but the Device Manager still reported ver 1.3.1.

Has anyone noted any similar behaviour?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Are you using the correct BaudRate 9600? Also for simplicity try COM1,2,3... all options in a simpler program. Also rather than just changing the PortName. Try initializing a new object. – Prateek Shrivastava Dec 03 '19 at 04:03
  • it looks like you have a driver mismatch, have you tried to track the [error](https://stackoverflow.com/questions/13999439/the-semaphore-timeout-period-has-expired-error-for-usb-connection)? – Marcos G. Dec 03 '19 at 06:19
  • See @Hans Passant reply, it seems to be similar: https://stackoverflow.com/questions/13999439/the-semaphore-timeout-period-has-expired-error-for-usb-connection#comment19343678_13999439 – Baddack Dec 03 '19 at 20:51

1 Answers1

0

That is seemed to be a timing issue. Try to increase your delay from 50 to, say, 200 ms and check the difference. As the doc says: The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly., sadly, there is no actual time specified.

lilo0
  • 895
  • 9
  • 12