2012-05-02 15 views
13

Witam Jestem nowy w kontrolowaniu sprzętu za pomocą połączenia USB. Mam mikrokontroler Arduino UNO i szukałem zasobów, aby zacząć. Programuję w języku C# (Visual Studio 2010) i zastanawiałem się, czy są pewne podstawy, których mógłbym użyć do konfigurowania/testowania mojego połączenia. Szukam czegoś tak prostego, jak pole wyboru w moim WinForm przełączanie pin cyfrowy we/wy na Arduino między wysokim i niskim. Na początku nie mogłem znaleźć wiele.Arduino Podstawy UNO dla C#

Z góry dziękuję.

Odpowiedz

6

Jestem pewien, że wiesz, że Arduino ma kilka próbek, które można wykorzystać w C#

Here's their C# page

+0

z jakiegoś powodu, moje poszukiwania nie załatwił mi tę stronę. dzięki. – ikathegreat

11

Istnieje wiele sposobów wysłania polecenia z komputera do Arduino. Sandeep Bansil stanowi dobry przykład podłączenia i odczytu portu szeregowego.

Poniżej znajduje się działający przykład zapisu na porcie szeregowym w zależności od stanu pola wyboru na formularzu Windows oraz sposobu przetwarzania żądania z komputera na arduino.

Jest to pełny przykład, istnieją bardziej ekologiczne rozwiązania, ale jest to bardziej przejrzyste.

W tym przykładzie arduino czeka na "a" lub "b" z komputera. komputer wysyła "a", gdy pole wyboru jest zaznaczone i wysyła "b", gdy pole wyboru jest odznaczone. Przykład zakłada cyfrowy pin 4 na arduino.

kod 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; 
      } 
     } 
    } 
} 

systemu Windows C#

Kod ten będzie znajdować się w pliku formularza .cs. W przykładzie założono, że do pola wyboru dołączono zdarzenia formularzy dla OnOpenForm, OnCloseForm i zdarzenia OnClick. Z każdego ze zdarzeń można nazwać odpowiednich metodach poniżej ....

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

Wskazówka:

Jeśli chcesz przeczytać wiadomość od Arduino następnie dodać czasomierza do formularza z przerwą 50 lub 100 milisekund.

W przypadku OnTick na czasowy należy sprawdzić dla danych za pomocą następującego kodu:

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

Wynik readLine() będzie, że myVar zawiera Hello World.

1

Pracuję nad biblioteką C#, aby połączyć się z Arduinos, jest tam wiele dobrych przykładów kodu i powinno być wystarczająco dobrze skomentowane, aby wszystko miało sens, mam nadzieję, że pomaga!

github repo: https://github.com/qwertykeith/ArduinoLibrary

0

Podstawowym sposobem komunikacji między komputerem a Arduino jest utworzyć 2 przyciski na komputerze i włączyć/wyłączyć światło na Arduino. Użyj portwrite();

Oto najprostszy demo: https://www.instructables.com/id/C-Serial-Communication-With-Arduino/ To absolutnie co chcesz!

C# Kod:

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 szkic:

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);  
} 
Powiązane problemy