Question

Through testing, I have discovered two laptops that refuse to communicate through a .Net SerialPort object. I should probably start off by saying that the application is using .Net 4.0. This is the setup:

  • Both laptops communicate using these serial ports with Tera Term
  • There is no communication on either the internal serial port on the motherboard or any USB serial port emulators
  • The ports being tested were not already in use.
  • No exceptions are being triggered
  • The software can open the COM ports, but not transmit any data
  • The ErrorReceived event handler is not being called

Here is how the object is initialized prior to use:

serialPort = new SerialPort();
serialPort.PortName = SelectSerialPort.GetSerialPort();
serialPort.BaudRate = 9600;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Handshake = Handshake.RequestToSend;
serialPort.ReadTimeout = 10000;
serialPort.WriteTimeout = 1000;
serialPort.ErrorReceived += OnSerialError;

This setup has been tested on at least twenty other computers and it works just fine. To write to the port I am simply calling:

serialPort.Write(packet, 0, nBytes);

Where packet is a byte[] and nBytes is the length of the data to be sent. It seems like the write timeout is being triggered because the software will open the serial port and after a delay the port closes without transmitting any data.

I was able to capture the following logs with a serial port monitor. The first log is what I expect to see, the COM port is opened and configured then the data is sent. The second log is from one of the laptops. You can see the port opens but the port just closes without transmitting anything.

I noticed that there are two main differences in the log files. The log from the laptops contains RTS off and the good log does not. Also, the log from the laptops seems to be setting the write timeout to zero. In the good log:

Set timeouts: ReadInterval=-1, ReadTotalTimeoutMultiplier=-1, ReadTotalTimeoutConstant=10000, WriteTotalTimeoutMultiplier=0, WriteTotalTimeoutConstant=1000

And the log from the laptops:

Set timeouts: ReadInterval=-1, ReadTotalTimeoutMultiplier=-1, ReadTotalTimeoutConstant=-2, WriteTotalTimeoutMultiplier=0, WriteTotalTimeoutConstant=0

What could be causing this issue, and what can i do to stop it?

Was it helpful?

Solution

It seems like i have found the solution to this problem while I was doing some research for a different project. I stumbled on a post in the Arduino forum where someone was having a similar issue with an Atmega32U4 (which has a proper USART).

It would seem that there were two properties of the SerialPort that I overlooked - SerialPort.DtrEnable, and SerialPort.RtsEnable.

The winning combination seems to be the following configuration:

serialPort = new SerialPort();
serialPort.PortName = SelectSerialPort.GetSerialPort();
serialPort.BaudRate = 9600;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Handshake = Handshake.None;
serialPort.DtrEnable = true;
serialPort.RtsEnable = true;
serialPort.ReadTimeout = 10000;
serialPort.WriteTimeout = 1000;
serialPort.ErrorReceived += OnSerialError;

Once these two properties were set to true, communication with no handshaking started working. However, the executable had to be run as Administrator otherwise I would receive an access denied message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top