Shen
Shen

Reputation: 1

Communication between C# and Arduino

Code for C#

using System;

using System.Windows.Forms; 

using System.IO.Ports;

SerialPort port;

    private void btnStart_Click(object sender, EventArgs e)
        {
            port = new SerialPort("COM6", 9600);
            port.Open();
            port.Write("START");
            port.Close();
        }

Code for Arduino

"#"include "MOVIShield.h"

MOVI recognizer(true); 

Code inside the loop

signed int res=recognizer.poll();

  if(Serial.available() > 0){

    String data = Serial.readString();

    if(data = "START"){
             recognizer.ask("Hello. My name is John");
    }
  }

I tried to click the btnStart to send "START" to my Arduino Program and the Arduino Program should run ask("Hello. My name is John") after received the data from C# program. But when I clicked the btnStart, there is nothing happened.

Upvotes: 0

Views: 3296

Answers (2)

Doug Johnson
Doug Johnson

Reputation: 560

I imagine the single equals in this line may have something to do with it. if(data = "START")

Upvotes: 0

Bishoy
Bishoy

Reputation: 725

You can try different a couple of different things:

1- Make sure COM port parameters are the same on both sides

As explained at http://www.instructables.com/id/How-to-connect-Arduino-to-a-PC-through-the-serial-/

Add this to the Arduino C code outside the loop:

Serial.begin(9600);

And change your C# to a code similar to:

private void btnStart_Click(object sender, EventArgs e)
{
    port = new SerialPort("COM6", 9600);
    port.DataBits = 8;
    port.StopBits = StopBits.One;
    port.Handshake = Handshake.None;
    port.Parity = Parity.None;
    port.Open();
    port.Write("START");
    port.Close();
}

2- Use a different tool than C# to test if you can communicate to the Arduino.

e.g. this tool has 15 days demo: https://www.eltima.com/rs232-testing-software/

Upvotes: 1

Related Questions