4

I'm trying to read messages sent form my Arduino over a Serial port using baud rate 9600.

My Arduino code is programmed to send a "1" whenever I press a button and a "0" when I release my finger off the button.

So it's not constantly sending data.

My C# Program is to read that message and add it to a ListBox. But whenever I start it, the program hangs.

private void button1_Click(object sender, EventArgs e)
{
    SerialPort port = new SerialPort();
    port.BaudRate = 9600;
    port.PortName = "COM4";
    port.ReadTimeout = 1000;
    port.Open();


    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    try
    {
        ee = port.ReadLine();
        listBox1.Items.Add(ee);
    }
    catch (Exception)
    {
        timer1.Stop();
    }
}

I guess, maybe the reason is that my program should check if there's data available to be received before receiving?

SunAwtCanvas
  • 1,261
  • 1
  • 13
  • 38
  • `ReadLine` reads until there is line terminator (`\n`) in stream. From your description it seems like your device never outputs such terminator, so `ReadLine` blocks forever. – Evk Mar 23 '18 at 08:28
  • Use of `ReadLine` from stream devices (like serial port) is not a good idea, exactly because it freezes the program *(when there is no finished 'line', or no data at all)*. Use `DataReceived` event to ensure there are data and `ReadExisting` instead. – Julo Mar 23 '18 at 08:35

2 Answers2

4

To avoid this problem, you need to add "\n" to your data in your arduino because port.ReadLine(); search for ending line ("\n")

For example, let's say that the data which arduino sends is "1", to read this data with port.ReadLine(); it should be "1\n"

Also, do not worry, port.ReadLine(); doesn't read "\n". Just stops there when it sees "\n".

I hope it helps.

oakar
  • 1,187
  • 2
  • 7
  • 21
4

Try something like this instead. It will at least not hang, and then you can sort out what sort of data your are getting via DataReceived

From there you can determine how to better write your app

private void button1_Click(object sender, EventArgs e)
{
    SerialPort port = new SerialPort();
    port.BaudRate = 9600;
    port.PortName = "COM4";
    port.ReadTimeout = 1000;

   // Attach a method to be called when there
   // is data waiting in the port's buffer
   port.DataReceived += new
      SerialDataReceivedEventHandler(port_DataReceived);

   // Begin communications
   port.Open();
}

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

SerialPort.DataReceived Event

Indicates that data has been received through a port represented by the SerialPort object.

SerialPort.ReadExisting Method ()

Reads all immediately available bytes, based on the encoding, in both the stream and the input buffer of the SerialPort object.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141