Anton
Anton

Reputation: 313

Error timeout execution of method Read class SerialPort

Good afternoon.

In Windows 10 Enterprise, I have a COM1 device.

To send and receive data, I use the following code:

        using (var serialPort = new SerialPort(SerialPort.GetPortNames()[0])
        {
            BaudRate = 9600,                                    
            Parity = Parity.None,                                
            StopBits = StopBits.One,                            
            DataBits = 8,                                       
            Handshake = Handshake.None,                           
            ReadTimeout = 50000
        })
        {


            serialPort.Open();

            var inputBuffer = new byte[] { 1, 3, 8, 10, 4 };
            var outputBuffer = new byte[inputBuffer.Length];

            serialPort.Write(inputBuffer, 0, inputBuffer.Length);

            serialPort.Read(outputBuffer, 0, inputBuffer.Length);

        }

However, after reaching 50 seconds, a timeout error occurs in the "Read" method.

Questions:

  1. What settings do I need to make for SerialPort so that the "Read" method receives data that was written using the "Write" method?
  2. How to check that the data was successfully written as a result of executing the "Write" method?
  3. Maybe you need to make some settings in the KOM at the OS level, or there are problems with rights, or something else?

P.S. Never worked with COM before.

Upvotes: 0

Views: 67

Answers (1)

kunif
kunif

Reputation: 4350

There is no API that allows the Windows serial port to have a loopback function.

One of the following is possible.

  • Find and use device drivers and/or hardware that informally support the loopback feature.
  • Set the loopback connector that physically connects the TxD pin and RxD pin to the serial port.
  • Prepare another PC at the end of the serial cable, or prepare two serial ports on one PC and run a program that echoes back the data read at that connection destination.

Upvotes: 1

Related Questions