2012-05-02 81 views
13

Xin chào tôi mới để kiểm soát phần cứng với kết nối USB. Tôi có một bộ vi điều khiển Arduino UNO và đang tìm kiếm tài nguyên để tôi bắt đầu. Tôi chương trình trong C# (Visual Studio 2010) và đã tự hỏi nếu có một số vấn đề cơ bản tôi có thể sử dụng để thiết lập/thử nghiệm kết nối của tôi. Tôi đang tìm một cái gì đó đơn giản như một hộp kiểm trong WinForm của tôi toggling kỹ thuật số I/O pin trên Arduino giữa cao và thấp. Đã không thể tìm thấy nhiều để bắt đầu.Arduino UNO vấn đề cơ bản cho C#

Xin cảm ơn trước.

Trả lời

6

tôi chắc chắn rằng bạn biết rằng Arduino có một vài mẫu mà bạn có thể sử dụng với C#

Here's their C# page

+0

đối với một số lý do, tôi không bao giờ tìm kiếm đã cho tôi trang đó. cảm ơn. – ikathegreat

11

Có nhiều cách để gửi lệnh từ máy tính đến Arduino. Sandeep Bansil cung cấp một ví dụ tốt về kết nối và đọc một cổng nối tiếp.

Dưới đây là ví dụ làm việc về cách ghi vào cổng nối tiếp dựa trên trạng thái hộp kiểm trên biểu mẫu cửa sổ amd cách xử lý yêu cầu từ máy tính trên Arduino.

Đây là ví dụ tiết, có các giải pháp rõ ràng hơn nhưng điều này rõ ràng hơn.

Trong ví dụ, arduino đợi hoặc là 'a' hoặc 'b' từ máy tính. máy tính sẽ gửi 'a' khi hộp kiểm được chọn và gửi 'b' khi hộp kiểm không được chọn. Ví dụ giả sử pin kỹ thuật số 4 trên Arduino.

Arduino đang

#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#

Mã này sẽ nằm trong file mẫu của bạn cs. Ví dụ giả định rằng bạn đã đính kèm các sự kiện biểu mẫu cho OnOpenForm, OnCloseForm và sự kiện OnClick vào hộp kiểm. Từ mỗi sự kiện bạn có thể gọi các phương pháp tương ứng bên dưới ....

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

Mẹo:

Nếu bạn muốn đọc một tin nhắn từ Arduino sau đó thêm một bộ đếm thời gian để hình của bạn với một khoảng thời gian 50 hoặc 100 mili giây.

Trong trường hợp OnTick của Timer bạn nên kiểm tra dữ liệu bằng cách sử dụng đoạn mã sau:

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

Kết quả của readLine() sẽ là myVar chứa Hello World.

1

Tôi đã làm việc trên thư viện C# để giao tiếp với Arduinos, có rất nhiều ví dụ mã tốt ở đó và sẽ có nhận xét đủ tốt để giúp mọi thứ có ý nghĩa, hy vọng sẽ hữu ích!

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

0

Cách cơ bản để giao tiếp giữa PC và Arduino là tạo 2 nút On PC và bật/tắt đèn trên Arduino. Sử dụng portwrite();

Dưới đây là bản demo đơn giản nhất: https://www.instructables.com/id/C-Serial-Communication-With-Arduino/ Đó là hoàn toàn những gì bạn muốn!

C# Code:

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 phác thảo:

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);  
} 
Các vấn đề liên quan