我使用Visual Studio 2015編寫了一個C#代碼,將功能發送給控制器,然後通過串行連接(RS232)從控制器接收數據。現在,我想在我的Ardino uno上上傳C#代碼。我該如何做到這一點? (IM新使用的Arduino)有沒有辦法讓我在Arduino Uno上實現我的C#代碼?
我的代碼:
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;
}
}
}
如果你的C#程序與** mono **兼容,然後按照您可能在Google上找到的任何教程.. –
將有助於發佈您的當前代碼。 – LaneL
@LaneL代碼發佈! – ImaNoobAtProgramming32