2016-03-29 27 views
-2

Visual Studio 2015 kullanarak bir denetleyiciye işlevler gönderen bir C# kodu yazdım ve daha sonra seri bağlantı (RS232) aracılığıyla denetleyiciden veri alacak. Şimdi, C# kodunu Ardino uno'ma yüklemek istiyorum. Bunu tam olarak nasıl yapabilirim? KodumC# kodumu Arduino Uno'ya uygulamamın bir yolu var mı?

(Arduino kullanmaya yeni Im):

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.IO.Ports; 
using System.IO; 
using System.Net; 





namespace serialreadwrite 
{ 

    class Program 
{ 
    static void Main(string[] args) 
    { 
     Program p = new Program(); 

     p.method3(); 
    } 

    private void method3() 
    { 
     SerialPort _serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); 

     _serialPort.Handshake = Handshake.None; 
     _serialPort.ReadTimeout = 10000; 
     _serialPort.WriteTimeout = 10000; 
     _serialPort.RtsEnable = true; 
     _serialPort.DtrEnable = true; 
     _serialPort.Open(); 

     int command = 0; 

     while (true) 
     { 

      SendData(ref command, _serialPort); 

      if (command == 100) 
      { 
       Thread.Sleep(2000); 

       _serialPort.Open(); 
       command = 1; 
      } 

     } 

    } 

    public void SendData(ref int temp2, SerialPort _serialPort) 
    { 
     try 
     { 

      string c = Convert.ToString(temp2); 
      byte[] array_out = Encoding.ASCII.GetBytes(c); 
      _serialPort.Write(array_out, 0, array_out.Length); 
      byte[] array_out2 = new byte[1]; 
      array_out2[0] = 0xD; 
      _serialPort.Write(array_out2, 0, array_out2.Length); 

      _serialPort.Write(array_out, 0, array_out.Length); 
      _serialPort.Write(array_out2, 0, array_out2.Length); 

      int reader = 0; 
      string xstring = string.Empty; 
      while (true) 
      { 
       reader = _serialPort.ReadByte(); 
       char xchar = Convert.ToChar(reader); 

       if (xchar == '\r') 
       { 
        if (ProcessLine(xstring, ref temp2) == true) 
        { 
         if (temp2 == 100) 
         { 
          _serialPort.Close(); 
         } 
         break; 
        } 

        xstring = string.Empty; 
       } 

       if (xchar != '\r') 
        xstring += xchar; 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex); 
     } 
    } 

    private Boolean ProcessLine(string line, ref int myCommand) 
    { 

     string time = Convert.ToString(DateTime.Now); 
     if (myCommand == 0) 
     { 
      myCommand = 1; 
      return true; 
     } 

     else if (line.StartsWith("Array V") && myCommand == 1) 
     { 
      string[] split = line.Split('='); 

      if (split.Count() > 1) 
      { 
       Console.WriteLine(String.Format(time + "\n" + "Array Voltage = {0}", split[1])); 
       myCommand = 2; 
       return true; 
      } 

      else 
      { 
       myCommand = 1; 
       return false; 
      } 
     } 
     else if (line.StartsWith("Array A") && myCommand == 2) 
     { 
      string[] split = line.Split('='); 

      if (split.Count() > 1) 
      { 
       Console.WriteLine(String.Format("Array Amps = {0}", split[1])); 
       myCommand = 3; 
       return true; 
      } 
      else 
      { 
       myCommand = 2; 
       return false; 
      } 

     } 

     else if (line.StartsWith("Auto") && myCommand == 3) 
     { 

      Console.WriteLine(line); 
      myCommand = 4; 
      return true; 
     } 

     else if (line.StartsWith("Motor V") && myCommand == 4) 
     { 
      string[] split = line.Split('='); 

      if (split.Count() > 1) 
      { 
       Console.WriteLine(String.Format("Motor Volt = {0}", split[1])); 
       myCommand = 5; 
       return true; 
      } 
      else 
      { 
       myCommand = 4; 
       return false; 
      } 
     } 
     else if (line.StartsWith("Motor A") && myCommand == 5) 
     { 
      string[] split = line.Split('='); 

      if (split.Count() > 1) 
      { 
       Console.WriteLine(String.Format("Motor Amps = {0}", split[1])); 
       myCommand = 6; 
       return true; 
      } 
      else 
      { 
       myCommand = 5; 
       return false; 
      } 
     } 
     else if (line.StartsWith("Max") && myCommand == 6) 
     { 
      string[] split = line.Split('='); 

      if (split.Count() > 1) 
      { 
       Console.WriteLine(String.Format("Max Motor = {0}", split[1])); 
       myCommand = 7; 
       return true; 
      } 
      else 
      { 
       myCommand = 6; 
       return false; 
      } 
     } 
     else if (line.StartsWith("Motor R") && myCommand == 7) 
     { 
      string[] split = line.Split('='); 

      if (split.Count() > 1) 
      { 
       Console.WriteLine(String.Format("Motor RPM = {0}", split[1])); 
       myCommand = 8; 
       return true; 
      } 
      else 
      { 
       myCommand = 7; 
       return false; 
      } 
     } 

     else if (line.StartsWith("Sn") && myCommand == 8) 
     { 

      Console.WriteLine(line); 
      myCommand = 9; 
      return true; 
     } 

     else if (line.StartsWith("SM") && myCommand == 9) 
     { 

      Console.WriteLine(line); 
      // Thread.Sleep(5000); 
      myCommand = 1; 
      return true; 
     } 

     else if (line.ToLower().Contains("no action")) 
     { 
      myCommand = 100; 
      return true; 
     } 

     return false; 

    } 
} 

} çalışmayacaktır

+2

C# Eğer Program ** mono ** ile uyumludur ve Google'da bulabileceğiniz herhangi bir öğreticiyi takip edin .. –

+0

Geçerli kodunuzu göndermek için yardımcı olur. – LaneL

+0

@LaneL Kod yayınlandı! – ImaNoobAtProgramming32

cevap

1

... Arduino ++ C \ C harflerini ... temel süreç çok farklıdır C# kodu .... Mikrodenetleyici ilk olarak parametreleri başlatmak için bir ayar fonksiyonunu çalıştırır, daha sonra giriş pimlerinden gelen verileri 'dinler' ve çıkış pimlerine veri gönderen bir döngü çalıştırır. Arduino Dil Referansı ..... https://www.arduino.cc/en/Reference/HomePage

Ardu İndir ino Yazılım (Arduino için yazma \ yükleme kodu için IDE) ... https://www.arduino.cc/en/Main/Software

Ben de SO yönelik önceki sorularınızı seçerek önemine ilişkin bazı ek okumalar önerebilir ... https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work..%E2%80%8C%E2%80%8B

İlgili konular