2012-05-02 17 views
13

こんにちは、私はUSB接続でハードウェアを制御するのが初めてです。私はArduino UNOマイクロコントローラを持っていて、私を始めるためのリソースを探していました。私はC#(Visual Studio 2010)でプログラムを作成し、接続の設定やテストに使用できる基本があるかどうか疑問に思っていました。 WinFormのチェックボックスのような単純なものを探して、ArduinoのDigital I/Oピンを高低の間で切り替えます。まずは多くのことを見つけることができていない。C#のArduino UNOの基本

ありがとうございます。

答えて

6

私はあなたがArduinoのは、あなたがVisual Studioを使用しているので、あなたがのために、このクールなVisual Studioのプラグインに興味があるかもしれないC#

Here's their C# page

+0

何らかの理由で、私の検索は私にそのページをやったことがなかったです。ありがとう。 – ikathegreat

11

PCからarduinoにコマンドを送信する方法はたくさんあります。 Sandeep Bansilは、シリアルポートの接続と読み取りの良い例を示しています。

以下は、Windowsフォーム上のチェックボックスの状態に基づいてシリアルポートに書き込む方法の実例です。arduinoのPCからの要求をどのように処理するかを示しています。

これは冗長な例ですが、クリーナーソリューションがありますが、これは明確です。

この例では、arduinoはpcから 'a'または 'b'のいずれかを待ちます。チェックボックスをオンにするとPCは 'a'を送信し、チェックボックスをオフにすると 'b'を送信します。この例では、arduinoのデジタルピン4を想定しています。このコードは、フォームは.csファイルに存在します

Arduinoのコード

#define DIGI_PIN_SOMETHING 4 
unit8_t commandIn; 
void setup() 
{ 
    //create a serial connection at 57500 baud 
    Serial.begin(57600); 
} 

void loop() 
{ 
    //if we have some incomming serial data then.. 
    if (Serial.available() > 0) 
    { 
     //read 1 byte from the data sent by the pc 
     commandIn = serial.read(); 
     //test if the pc sent an 'a' or 'b' 
     switch (commandIn) 
     { 
      case 'a': 
      { 
       //we got an 'a' from the pc so turn on the digital pin 
       digitalWrite(DIGI_PIN_SOMETHING,HIGH); 
       break; 
      } 
      case 'b': 
      { 
       //we got an 'b' from the pc so turn off the digital pin 
       digitalWrite(DIGI_PIN_SOMETHING,LOW); 
       break; 
      } 
     } 
    } 
} 

WindowsのC#

。この例では、OnOpenForm、OnCloseFormおよびOnClickイベントのフォームイベントをチェックボックスに添付していることを前提としています。イベントのそれぞれからは、以下の....それぞれのメソッドを呼び出すことができます

using System; 
using System.IO.Ports; 

class fooForm and normal stuff 
{ 
    SerialPort port; 

    private myFormClose() 
    { 
     if (port != null) 
     port.close(); 
    } 

    private myFormOpen() 
    { 
     port = new SerialPort("COM4", 57600); 
     try 
     { 
      //un-comment this line to cause the arduino to re-boot when the serial connects 
      //port.DtrEnabled = true; 

      port.Open(); 
     } 
     catch (Exception ex) 
     { 
      //alert the user that we could not connect to the serial port 
     } 
    } 

    private void myCheckboxClicked() 
    { 
     if (myCheckbox.checked) 
     { 
      port.Write("a"); 
     } 
     else 
     { 
      port.Write("b");  
     } 
    } 
} 

ヒント:

あなたは、その後の間隔でフォームにタイマーを追加するのArduinoからのメッセージを読みたい場合は50または100ミリ秒。

タイマーのOnTickイベントでは、次のコードを使用してデータを確認する必要があります:readLine()の結果はmyVarHello Worldが含まれていることになります

//this test is used to see if the arduino has sent any data 
if (port.BytesToRead > 0) 

//On the arduino you can send data like this 
Serial.println("Hellow World") 

//Then in C# you can use 
String myVar = port.ReadLine(); 

1

私はArduinosとインターフェイスするためにC#ライブラリに取り組んでいます。そこにはたくさんの良いコード例がありますし、意味のあることを助けるのに十分なコメントが必要です。

GitHubのレポ:https://github.com/qwertykeith/ArduinoLibrary

0

PCとArduinoの間の通信の基本的な方法は、PC上で2つのボタンを作成して、Arduinoの上のライトをオン/オフされます。 https://www.instructables.com/id/C-Serial-Communication-With-Arduino/ あなたが欲しいものを絶対にだ:ここでは最も簡単なデモだportwrite();

を使用してください!

C#コード:

using System; 
using System.Windows.Forms; 
using System.IO.Ports; 
namespace ledcontrol 
{ 
    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);  
} 
関連する問題