0

I have a program that allows several Arduinos to communicate via Serial Port. For example if Arduino1 want to communicate with Arduino3 the user sends a string from Arduino1 and this string appears on Arduino3 and so on. This is working good with SerialMonitor.

The problem is when I try to do the same in my C# application (nothing appears). I tryed this:

//(...)
comPort1.Open();
//(...)
private void comPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    string inData = comPort1.ReadLine();
    msgBoxLog.AppendText(inData); // msgBoxLog = textBox with data received/sent
}
//(...)
private void sendButton_Click(object sender, EventArgs e)
{
    string my_str = "my string";
    msgBoxLog.AppendText(my_str + "\r\n");
    comPort1.WriteLine(my_str + "\r\n");
}

Some notes:

  • RtsEnable and DtrEnable are both active
  • BaudRate (Arduino / C#) = 1200

Is baudrate value a problem? I must use this value but I'm not sure if it is accepted by C#. I also tryed something like this but with no success.

3
  • Try the solution provided here and state the results Commented Sep 12, 2013 at 13:02
  • @Lizzie Like I said I had already tried that solution before. Your answer didn't work for me too :/ Commented Sep 12, 2013 at 15:43
  • 1
    msdn says that "By default, the ReadLine method will block until a line is received". Maybe this is what causes you the problem. Read the Remarks section. Try reading bytes instead of a line and see whether you can get some results Commented Sep 12, 2013 at 17:36

2 Answers 2

1

I have a vague memory about not being able to access UI controls through the dataReceived event. Try this.

private void comPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    string inData = comPort1.ReadLine();
    this.Invoke(new EventHandler(processData));
}

private void processData(object sender, EventArgs e)
{
    msgBoxLog.AppendText(inData);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I used .ReadExisting() instead of .ReadExisting() and now I'm receiving data but I cannot send anything. If I send data from SerialMonitor (Arduino) to my application it's ok but not the reverse. Rx LED of my Arduino lights but nothing else.
I meant .ReadExisting() instead of .ReadLine()
0

Do you have proper event handler enabled, like in this example?

mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx

p.s. I'm using "dumb" delay in my software, between write and read serial port, and it's worked fine. Like this:

serialPort1.Write(Data, 0, Data.Length);

System.Threading.Thread.Sleep(500);

try
{
    serialPort1.Read(Data2, 0, Data2.Length);
}
catch (TimeoutException)
{
    errorProvider1.SetError(maskedTextBox1, "timeout");
}
catch (ArgumentNullException)
{
    errorProvider1.SetError(maskedTextBox1, "no answer");
}

2 Comments

Yes, I have. I forgot to say I'm using winforms.
I got some errors. This is the code for DataReceived event right? What is Data,Data2 and errorProvider1?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.