簡體   English   中英

C#與Arduino之間的通信

[英]Communication between C# and Arduino

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程序,並且從C#程序接收到數據后,Arduino程序應運行ask(“ Hello。My name is John”)。 但是,當我單擊btnStart時,沒有任何反應。

您可以嘗試幾種不同的方法:

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通信。

例如,此工具有15天的演示: https : //www.eltima.com/rs232-testing-software/

我想這條線中的單等號可能與此有關。 if(數據=“開始”)

也許您應該使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM