简体   繁体   中英

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

I wrote a C# code using Visual Studio 2015 that sends functions to a controller and then will receive data back from the controller via serial connection (RS232). Now, I will like to upload the C# code on my Ardino uno. How exactly can I do this? (Im new to using the Arduino)

My code:

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;

    }
}

}

That will not work... the Arduino uses C\\C++... the fundamental process is very different to C# code.... the Microcontroller first runs a set-up function to initialise parameters then it continually runs a loop that 'listens' for incoming data from its input pins and sends data on its output pins..... Arduino Language Reference..... https://www.arduino.cc/en/Reference/HomePage

Download the Arduino Software (IDE for writing\\uploading code to the Arduino)... https://www.arduino.cc/en/Main/Software

May I also suggest some further reading regarding the importance of selecting answers to your previous questions on SO... https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work..%E2%80%8C%E2%80%8B

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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