1

I've written a small application to test reading serial data from a microcontroller, and for some reason it doesn't seem to be reading anything. I've confirmed using PuTTY and Power Shell that the data is coming in properly from the port being used, at the same baud rate etc. I've confirmed that the port settings in Device Manager are correct. But my app doesn't seem to be reading anything - never reaches the SerialDataReceivedEventHandler. I've tried using both delegate-based and blocking techniques, nothing seems to yield anything. Code below - if someone could point out what I'm doing wrong, I'd appreciate it!

    private void btnDoIt_Click(object sender, EventArgs e)
    {
        serialPort = new SerialPort("COM7", 9600, Parity.None, 8, StopBits.One)
        {
            Handshake = Handshake.None
        };

        serialPort.DataReceived += new
        SerialDataReceivedEventHandler(port_DataReceived);

        // Begin communications
        serialPort.Open();
        Debug.WriteLine("Port opened");
    }

    private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        // Show all the incoming data in the port's buffer in the output window
        SerialPort sp = (SerialPort)sender;
        Debug.WriteLine("data : " + sp.ReadExisting());
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (serialPort.IsOpen)
        {
            serialPort.Close();
        }
    }
Occamman
  • 21
  • 2

1 Answers1

0

Found the answer by following the example at SerialPort not receiving any data. Needed to set the ports DtrEnable and RtsEnable to true after opening the serial port.

Occamman
  • 21
  • 2