繁体   English   中英

有没有办法让我在 Arduino Uno 上实现我的 c# 代码?

[英]Is there a way for me to implement my c# code on an Arduino Uno?

我使用 Visual Studio 2015 编写了一个 C# 代码,该代码将函数发送到控制器,然后通过串行连接 (RS232) 从控制器接收数据。 现在,我想在我的 Ardino uno 上上传 C# 代码。 我怎么能做到这一点? (我是使用 Arduino 的新手)

我的代码:

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;

    }
}

}

那行不通……Arduino 使用 C\\C++……基本过程与 C# 代码非常不同……微控制器首先运行一个设置函数来初始化参数,然后它不断运行一个循环,“监听” ' 用于从其输入引脚传入数据并在其输出引脚上发送数据...... Arduino 语言参考...... https://www.arduino.cc/en/Reference/HomePage

下载 Arduino 软件(用于编写\\上传代码到 Arduino 的 IDE)... https://www.arduino.cc/en/Main/Software

我还可以建议进一步阅读关于选择之前关于 SO 的问题的答案的重要性... https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work..% E2%80%8C%E2%80%8B

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM