-1

I am having issues with serial communication to an embedded device in Visual Studio C# environment, current solution is to communicate with a putty session with the following configs:

  • baud > 9600
  • data bits > 8
  • stop bits > 1
  • parity > none
  • flow control > RTS/CTS

Every time the connection is established a menu is displayed on the putty terminal which shows the controller's available commands.

I have tried the following which I have borrowed from the MS example:


public class PortChat
{
    static bool _continue;
    static SerialPort _serialPort;

    public static void Main()
    {

        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

        _serialPort.PortName = "COM4";
        _serialPort.BaudRate = 9600;
        _serialPort.Parity = Parity.None;
        _serialPort.DataBits = 8;
        _serialPort.StopBits = StopBits.One;
        _serialPort.Handshake = Handshake.RequestToSend;


        // Set the read/write timeouts
        _serialPort.ReadTimeout = 5000;
        _serialPort.WriteTimeout = 5000;

        _serialPort.Open();
        Thread.Sleep(1000);
    }

}

Reading from the port after "Port.Open" results in an exception, see 1st screenshot. Writing to the port also results in an exception, see 2nd screenshot. What am I missing here?

Thank you

Read error

Write error

user3255590
  • 29
  • 1
  • 4
  • https://stackoverflow.com/questions/13999439/the-semaphore-timeout-period-has-expired-error-for-usb-connection maybe it would help. – Louis Go Feb 11 '20 at 08:35
  • @Louis Go Thanks Louis but this is not the same thing, I can communicate with putty with the same port settings without any issues and there is no exception thrown with port.open – user3255590 Feb 13 '20 at 02:49
  • Is your .Net framework newer than 4.0? I recalled older version is buggy. Sorry, not much could help. – Louis Go Feb 13 '20 at 03:08
  • @Louis Go, it's framework ver 4.5 – user3255590 Feb 16 '20 at 16:59

1 Answers1

1

ReadLine waits until it sees the SerialPort.NewLine string. If this doesn't arrive within SerialPort.ReadTimeout the TimeoutException is thrown. Either increase your timeout or use ReadExisting().

See: Serial port communication throwing TimeoutException

Per MSDN: [SerialPort.ReadTimeout] Gets or sets the number of milliseconds before a time-out occurs when a read operation does not finish.

See: https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.readtimeout?view=netframework-4.8

By default, the ReadLine method will block until a line is received. If this behavior is undesirable, set the ReadTimeout property to any non-zero value to force the ReadLine method to throw a TimeoutException if a line is not available on the port.

See: https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.readline?view=netframework-4.8

Baddack
  • 1,947
  • 1
  • 24
  • 33
  • if I do a _serialPort.ReadExisting(); after opening the port I always get a "" string. If I set the read/write timeouts to 2sec which is plenty, I get timeouts for both. I am wondering if this has anything to do with the RTS/CTS flow control? But how come putty is handling this so smoothly? – user3255590 Feb 16 '20 at 17:45
  • @user3255590 Is there raw data in `_serialPort.Read`? – Louis Go Feb 17 '20 at 01:03
  • The `ReadExisting()` method returns all the characters in the serialport data received buffer. So you could get only half the message when calling this method. The best thing to do is use the `DataReceived` event, which fires when characters come through the serial port and use `ReadExisting` to get the characters. I have a pretty good example here: https://stackoverflow.com/questions/59187451/why-serialdatareceivedeventargs-receive-incomplete-data/59203139#59203139 – Baddack Feb 17 '20 at 16:09
  • @Baddack, I am not sure how I can integrate your example into my code: how do we your function? private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) – user3255590 Feb 24 '20 at 21:26
  • `mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);` Will create your DataReceived Event. See the Microsoft Example: https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.datareceived?view=netframework-4.8 – Baddack Feb 25 '20 at 17:45
  • @Baddack, I tried the MS example, the code opens a serial connection but doesn't return anything from my device. The device's normal behavior (with putty) is to display a prompt every time a new connection is made to it. Any thought's? – user3255590 Feb 27 '20 at 22:27