0

I'm writing a sequence to a serial port telling it to send me data. After sending the sequence and trying to read the data from the serial port there is nothing there. However, if I close the program and open the port in HyperTerminal, the data immediately shows up without needed to prompt for it.

private string ReadSerialPort()
    {
       var _comPort = new SerialPort()
       {
            PortName = "COM2",
            BaudRate = 1200,
            DataBits = 7,
            Parity = System.IO.Ports.Parity.Odd,
            StopBits = System.IO.Ports.StopBits.One,
            NewLine = Environment.NewLine,
            WriteTimeout = 500,
            ReadTimeout = 500,
            Handshake = System.IO.Ports.Handshake.None
        };

        if (!_comPort.IsOpen)
        {
            _comPort.Open();
        }

        int unicode = 27;
        char character = (char)unicode;
        string text = character + "P";

        _comPort.WriteLine(text);

        //System.Threading.Thread.Sleep(200); // Didn't help

        string serialData = string.Empty;
        while (serialData.Length == 0)
        {
            serialData = _comPort.ReadLine();
        }

        return serialData;
    }

If I write the escape sequence to the serial port 3 times, I will see nothing in my code but if I open up HyperTerminal it will appear 3 times.

What could be causing the code to not see anything in the buffer, but I can see it once I exit the program?

K Sco
  • 51
  • 7
  • What if you set the ReadTimeout value to `0`? Will `_comPort.ReadLine();` then block or return the expected data? (remove the while loop or augment your code there with additional Console messages that will help you track whether `_comPort.ReadLine();` is blocking or whether the while-loop is spinning.) –  Feb 22 '19 at 20:02
  • Does the code (specifically `_comPort.ReadLine()`) throw a TimeoutException? If not, is your code stuck in the while loop? If it is not stuck in the while loop, how could it ever possibly leave the while loop if it did receive nothing? Leaving the while loop would require _serialData_ to become some non-empty string (i.e., having a length greater than 0), and a non-empty string would be "something" and not nothing... –  Feb 22 '19 at 20:11
  • When I set the `ReadTimeout = 0` it throws a `TimeoutException` on the InternalRead. I tried setting the read in the while loop to `ReadExisting` and the loop continues spinning indefinitely. – K Sco Feb 22 '19 at 20:33
  • Dang. I made a mistake. Try setting ReadTimeout to `-1` or `SerialPort.InfiniteTimeout` (not to `0` as i mistakenly suggested) and see what happens. My apologies... :-( –  Feb 22 '19 at 21:10
  • When I change `ReadTimeout` to `-1` the execution hangs up on `_comPort.ReadLine()` indefinitely. Although, if I stop the code and open up hyperterminal it still happily spits out a line of data. – K Sco Feb 25 '19 at 19:08
  • I would recommend looking into using the `DataReceived` event. This event fires on your `SerialPort` when characters come in. See https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.datareceived?view=netframework-4.7.2 From my experience, `ReadLine` has issues! Maybe try `Read` or `ReadExisting`. – Baddack Feb 25 '19 at 23:36
  • I've implemented the `DataReceived` event elsewhere, unfortunately the buffer never sees anything so it does not fire. My `Read` and `ReadExisting` all similarly fail to see data. For the life of me I'm not sure what to look into. The hyperterminal has been kind enough to pick up all the data my code does not see. I'm not sure what the cause of that might be. – K Sco Feb 26 '19 at 12:54

1 Answers1

2

My solution was to set DtrEnable = true. Everything is now working as intended!
This question helped lead me to the solution

SerialPort.DtrEnable Property

K Sco
  • 51
  • 7