2017-02-27 7 views
1

Arduino 보드와 통신하는 프로그램을 C#으로 설계하려고합니다. 이 프로그램은 Arduino에서 정수 데이터를 받아 값과 관련된 것을 표시해야합니다.arduino에서 int를 PC에있는 C#으로 보내는 방법

유일하게 필요한 것은 C# 및 Arduino Uno의 코드로 arduino에서 pc (노트북 통합 블루투스)의 C#으로 값 (int)을 전송하는 것입니다.

내 프로그램 코드가 필요한지 문의하십시오.

C#에서 프로그램을 완료했습니다. 올바른지 알려주세요.

using System; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO.Ports; 

public class travauxEncadre 
{ 
static public void Main() 
{ 

    string data = "0"; 
    int Consommer = int.Parse(data); 

    //Début Prise des valeurs manuelle 

    Console.Clear(); 


    //Définition du seuil d'avertissement 

    Console.WriteLine("Seuil d'avertissement"); 
    string SeuilAvertissement = Console.ReadLine(); 
    Console.Clear(); 


    // Définition du seuil d'exces 

    Console.WriteLine("Seuil d'exces"); 
    string SeuilExces = Console.ReadLine(); 
    Console.Clear(); 


    //Défintion de la conso actuelle (a enlever) 

    // Console.WriteLine("Consommation"); 
    // string Conso = Console.ReadLine(); 
    // Console.Clear(); 



    int Avertissement = int.Parse(SeuilAvertissement); 
    int Exces = int.Parse(SeuilExces); 
    // int Consommer = int.Parse(Conso); 

    //Fin Prise des valeurs manuelle 



    //Début Bluetooth 

    SerialPort port; 

    port = new SerialPort(); 

    port.BaudRate = 9600; 
    port.DataBits = 8; 
    port.StopBits = StopBits.One; 
    port.Parity = Parity.None; 

    port.PortName = "COM4"; 

    port.DataReceived += Port_DataReceived; 

    port.Open(); 


    //Fin Bluetooth 



    //Début Vérification 

    if (Avertissement >= Exces) 
    { 
     Console.WriteLine("Impossible"); 
     System.Threading.Thread.Sleep(1000); 

    } 

    else 
    { 

     if (Consommer < Avertissement) 
     { 
      Console.WriteLine("Vert"); 
      Console.WriteLine(data + " Kw/H"); 
      System.Threading.Thread.Sleep(1000); 

     } 
     else 
     { 
      if (Consommer >= Exces) 
      { 
       Console.WriteLine("Rouge"); 
       Console.WriteLine(data + "Kw/H"); 
       System.Threading.Thread.Sleep(1000); 

      } 
      else 
      { 
       Console.WriteLine("Jaune"); 
       Console.WriteLine(data + "Kw/H"); 
       System.Threading.Thread.Sleep(1000); 

      } 

      // Fin Vérification 


     } 
    } 



} 
private static void Port_DataReceived(object sender, SerialDataReceivedEventArgs e) 
{ 


    SerialPort port; 
    string data = string.Empty; 

    port = (SerialPort)sender; 

    data = port.ReadExisting(); 

    int Consommer = int.Parse(data); 

} 
} 
+0

항상 작업중인 코드 스 니펫을 게시하십시오. http://stackoverflow.com/help/how-to-ask – matt

+0

프랑스에서 유감스럽게 생각합니다. – Mazeo

+0

나에게 잘 어울리 며, 특정 기기 이름이나 다른 기기를 검색해야 할 필요가있는 것처럼 보이지 않습니다. COM4. 그래, 그래서 kW 시간을 초과하면 빨간색으로 가고 그렇지 않으면 황색 (내 프랑스어는 그다지 좋지 않음). 정말 멋진 프로젝트가있는 것 같습니다. – Snoopy

답변

1

난 당신이 아두 이노 측에하고 싶은 데이터를 전송하는 방법에는 여러 가지가도 있기 때문에, 나는 당신에게 C#을 시리얼 포트 측을 설명 할 것입니다 무엇을 완전히 확실하지 오전입니다. Arduino와 통신하는 가장 쉬운 방법은 RS-232 포트 (또는 직렬 포트 Bluetooth 어댑터 동글)를 사용하는 것입니다.

먼저 Arduino 보드에 내장 된 RS-232를 통해 Arduino와 통신하기 위해 직렬 포트를 열어야합니다. 아래의 직렬 포트 ...

/// <summary> 
/// Read data from Arduino until user presses key. 
/// </summary> 
/// <param name="args">Arguments to the program (we do not take any).</param> 
static void Main(string[] args) 
{ 
    SerialPort port; 

    // first, create a new serial-port 
    port = new SerialPort(); 

    // configure the settings to match the Arduino board 
    // below i've just used some of the most common settings 
    // to get the point across 
    port.BaudRate = 9600; 
    port.DataBits = 8; 
    port.StopBits = StopBits.One; 
    port.Parity = Parity.None; 

    // you'll have to figure out what your actual COM name is 
    // for this example I'll just use COM 11 
    port.PortName = "COM11"; 

    // subscribe to when the data is coming from the port 
    port.DataReceived += Port_DataReceived; 

    // open up communications with the port 
    port.Open(); 

    // continue to receive data until user presses key 
    Console.ReadKey(); 

    // close access to the port when finished 
    port.Close(); 
} 

기타해야 할 일은 구독자 (실제로 데이터를 인쇄하는 방법)를 만드는 것입니다. 나는 당신을 위해 그 일을했습니다 ...

/// <summary> 
/// Methods for handling the incoming data from Arduino. 
/// </summary> 
/// <param name="sender">The port that's getting data from Arduino.</param> 
/// <param name="e">When the new data comes in.</param> 
private static void Port_DataReceived(object sender, SerialDataReceivedEventArgs e) 
{ 
    SerialPort port; 
    string data = string.Empty; 

    // get a reference to the port that's sending the data 
    port = (SerialPort)sender; 

    // read the data from the port 
    data = port.ReadExisting(); 

    // print Arduino data to the screen 
    Console.WriteLine(data); 
} 
+0

감사 스누피! 만약 내가 잘 이해한다면, 나는 내 프로그램에 그 코드를 넣어야하지만, 나는 블루투스 모듈 (이름을 기억하지 못한다)과 노트북과 블루투스 (hp elitebook laptop)를 통합 한 arduino uno를 사용한다. 그래서 당신이 준 코드에서 변경해야합니까? – Mazeo

+0

@Mazeo 직렬 포트를 사용하는 경로를 찾으려면 각각을 쿼리해야합니다. 따라서 각 직렬 포트 장치에서 문자열을 얻는'for' 루프를 작성해야합니다. 장치가 "나는 Arduino"라고 응답하면 올바른 COM 포트에 있다는 것을 알게됩니다. – Snoopy

+0

@Mazeo Arduino에 어떤 종류의 명령/응답 메커니즘이 있는지 확인하십시오. Arduino로 전송할 때 : "안녕하세요, 당신은 누구입니까?" ... 응답 할 것입니다 ... "안녕하세요, 저는 Arduino입니다!"... 그런 식으로 어느 포트에 연결되어 있는지 알 수 있습니다. – Snoopy