C#和Arduino之间的通信

时间:2018-01-23 02:00:24

标签: c# arduino-uno

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();
        }

Arduino代码

"#"include "MOVIShield.h"

MOVI recognizer(true); 

循环内的代码

signed int res=recognizer.poll();

  if(Serial.available() > 0){

    String data = Serial.readString();

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

我尝试点击btnStart发送" START"到我的Arduino程序和Arduino程序应该在收到C#程序的数据后运行询问("你好。我的名字是John")。但是当我点击btnStart时,什么也没发生。

3 个答案:

答案 0 :(得分:1)

你可以尝试不同的几件事:

1-确保两端的COM端口参数相同

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

所述

将其添加到循环外的Arduino C代码中:

Serial.begin(9600);

将您的C#更改为类似于以下的代码:

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-使用与C#不同的工具来测试您是否可以与Arduino进行通信。

e.g。这个工具有15天的演示:https://www.eltima.com/rs232-testing-software/

答案 1 :(得分:0)

我想这行中的单一等于可能与它有关。 if(data =“START”)

答案 2 :(得分:0)

也许您应该使用PortWrite();而不是port.write();

这是一个可以帮助您的类似演示:

C#代码:

using System;
using System.Windows.Forms;
using System.IO.Ports;
namespace lightcontrol
{
    public partial class Form1 : Form
    {
        SerialPort port;
        public Form1()
        {
            InitializeComponent();
            this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
            if (port==null)
            {
                port = new SerialPort("COM7", 9600);//Set your board COM
                port.Open();
            }
        }
        void Form1_FormClosed(object sender,FormClosedEventArgs e)
        {
            if(port !=null &&port.IsOpen)
            {
                port.Close();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            PortWrite("1");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PortWrite("0");
        }
        private void PortWrite(string message)
        {
            port.Write(message);
        }
    }
}

Arduino代码:

const int LedPin = 13;  
int ledState = 0;  

void setup()  
{   
  pinMode(LedPin, OUTPUT);  

  Serial.begin(9600);    
}  

void loop()  
{   
    char receiveVal;     

    if(Serial.available() > 0)  
    {          
        receiveVal = Serial.read();  

       if(receiveVal == '1')      
          ledState = 1;     
       else  
          ledState = 0;       
    }     

    digitalWrite(LedPin, ledState);   

    delay(50);      
}  

这是教程: http://www.lattepanda.com/topic-f6t1534p6387.html?sid=8abb18a037a0308db9e4d5825a0aebcd#p6387