0

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.

6
  • I believe there won't be a straight answer to why it is not working, it will need more debugging from your side and fine tuning every little variable. To start with, what about the other COM port params? Parity, data bits, and stop bits. Are you running the C# on a PC with a hardware COM port and connecting that to Arduino? or is it the USB cable appearing as a COM port? Are you sure it is COM6? Commented Jan 23, 2018 at 2:08
  • I connect my arduino board to my pc by using cable. I am comfirmed that it is COM6 and it's baud rate is correct. I cannot find the parity, data bits and stop bits of the arduino program. Commented Jan 23, 2018 at 2:13
  • Check if this is helpful: instructables.com/id/… Commented Jan 23, 2018 at 2:19
  • I am sorry that I cannot understand the description in the link provided by you nicely. Is it the causes of the problem is the Serial port need to fill in with parity, data bits and stop bits? Commented Jan 23, 2018 at 2:28
  • You cannot assume any of these values except if defaults are stated in the Arduino documentation, and even then you need to state those default into your C# code. But if there are no such thing like defaults you have to set the same values in both C code on the Arduino and C# code on the PC to be able to talk. Commented Jan 23, 2018 at 2:31

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

3 Comments

For the serial.Open part, the meaning of outside the loop is it in the setup?
I guess I was confused a bit which C code is the Arduino code from the link I've looked at, it looks like you only need to set up the baud rate on the Arduino side, which suggests that there are defaults for the other values that you still need to set in C# as I've said before.
I follow the steps provided by you but after I click the btnStart, it still nothing happened.
0

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

1 Comment

I didn't notice that but sure if(data = "START")won't work. You have to use something like ìf (strcmp(data, "START"))

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.