C # communication problem with COM port

I have C # code that communicates with three different COM ports. COM ports are actually three serial ports for USB converters.

Each time you turn off 'on' and 'on', the codes with which it exchanges switch each time, and then initializes three COM ports, tries to send and read data, and then closes the COM port. This continues to continue for a predetermined number of cycles. My problem is that after about 8 or 9 iterations, the COM port connection stops working. Sometimes it throws an error saying that the port is closed, sometimes it does not throw any exceptions, but in fact it does not read or write anything from the COM port. For a while he only wrote, but did not read the data.

What could be the reason and any tips for debugging this problem?

EDIT:

The port closes or stops abruptly even in the middle of the program, as shown below:

SerialPort.Write("ss");
SerialPort.Read("ss"); // FAILS!!

Some part of the code I'm using

public string Read(string readCommand)
        {
            string str = "";
            _port.WriteLine("\r");
            _port.WriteLine(readCommand + "\r");
            Thread.Sleep(0x3e8);
            str = _port.ReadExisting();
            return str;
        }

public void Write(string command)
        {
            _port.WriteLine(command + "\r");
            Thread.Sleep(100);
            if (_port.ReadExisting() == string.Empty)
            {
                throw new IOException("Error writing to COM");
            }
        }

    public void Initialize()
    {
        if (_port == null)
        {
            _port = new SerialPort(this.PortName.ToString(), this.BaudRate, this.Parity, this.DataBits, this.StopBits);
            _port.Handshake = this.Handshake;
        }
        try
        {
            if (!_port.IsOpen)
            {
                _port.Open();
                if (Read("") == string.Empty)
                {
                    throw new IOException("Device not connected or powered on");
                }

            }
        }
        catch (Exception)
        {
            this.Close();
        }
    }

Thank...

0
source share
3 answers
        _port.WriteLine(command + "\r");
        Thread.Sleep(100);
        if (_port.ReadExisting() == string.Empty)
        {
            throw new IOException("Error writing to COM");
        }

This evil code must abandon it sooner or later. Windows cannot provide such a service guarantee. Or, most importantly, the device itself, especially when you turn it on and off. Use SerialPort.ReadTimeout, set it for at least 2 seconds. And make a blocking call like ReadLine ().

    catch (Exception)
    {
        this.Close();
    }

This is the top fragment of the previous fragment. You have no idea what will happen when this happens. And your code will try to use the private port. Just delete statements, it does nothing but harm.

, . SerialPort , Close(), . , , . , .

+2

SetCommTimeouts ( , .NET, .NET API Win32), USB/ .

, USB, .

+1

It’s hard to say what the problem is if you don’t see some code. I assume that you do not wait long enough for the COM port to close after it is reopened. Please note that on the page SerialPort.Close:

The best practice for any application is to wait a while after calling the Close method before trying to call the Open method, since the port cannot be closed immediately.

Can you just open the COM ports and leave them open until you're done? For example, from this message:

using (SerialPort serialPort = new SerialPort("COM1", 9600))
{
    serialPort.Open();
    while (true)
    {
        Thread.Sleep(1000);
        // serialPort.Write();
        Thread.Sleep(1000);
        // serialPort.Read();
        // break at some point to end
    }
    serialPort.Close();
}
0
source

Source: https://habr.com/ru/post/1767552/


All Articles