如何从Arduino sketch“ethernet webserver”中读取输出。使用c#程序 [英] How to read the out put from Arduino sketch "ethernet webserver" using c# program
本文介绍了如何从Arduino sketch“ethernet webserver”中读取输出。使用c#程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经开发了一个草图和c#程序来读取Arduino uno模拟引脚和控制数字引脚的串行。现在我使用盾牌通过Lan连接Arduino uno。那么如何将草图和程序转换为在lan上使用它。
============
sketch
============
int LED = 2 ;
int LED3 = 3 ;
int LED4 = 4 ;
int LED5 = 5 ;
int LED6 = 6 ;
int LED7 = 7 ;
int LED8 = 8 ;
int LED9 = 9 ;
int LED10 = 10 ;
int LED11 = 11 ;
int LED12 = 12 ;
char k = ' F'跨度>;
char a = ' F'跨度>;
char b = ' F'跨度>;
char c = ' F'跨度>;
char d = ' F'跨度>;
char e = ' F'跨度>;
char f = ' F'跨度>;
char g = ' F'跨度>;
char h = ' F'跨度>;
char i = ' F'跨度>;
char j = ' F'跨度>;
char 数据;
char x = ' f'跨度>;
char con [ 80 ];
int analogpin = 3 ;
void setup(){
{
Serial.begin( 9600 );
pinMode(LED,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(LED5,OUTPUT);
pinMode(LED6,OUTPUT);
pinMode(LED7,OUTPUT);
pinMode(LED8,OUTPUT);
pinMode(LED9,OUTPUT);
pinMode(LED10,OUTPUT);
pinMode(LED11,OUTPUT);
pinMode(LED12,OUTPUT);
pinMode(analogpin,INPUT);
Serial.begin( 9600 );
}
}
void loop(){
/// /////////////
int value = analogRead(analogpin);
String stringOne = String ( value 跨度>);
/// /////////////// //////////////
data = Serial.read();
/// /////////////// //////////
if (data == ' o')
{
k = ' O'跨度>;
}
else if (data == ' p')
{
k = ' F';
}
else if (data == ' ')
{
k = ' < span class =code-string> F';
}
if (data == ' o')
{
digitalWrite(LED,HIGH);
x =数据;
}
else if (data == ' p')
{
digitalWrite(LED,LOW);
x =数据;
}
/// ////////// //////////////////////
/ * if(data!='o'|| data!='p')
{
data = x;
} * /
/// //////// ///////////////////////////
/// //////////////////////////
if (data == ' A' )
{
digitalWrite(LED3,HIGH);
a = ' O';
}
else if (data == ' a')
{
digitalWrite(LED3,LOW);
a = ' F';
}
/// ////////////// ///////////
if (data == ' B')
{
digitalWrite(LED4,HIGH);
b = ' O';
}
else if (data == ' b')
{
digitalWrite(LED4,LOW);
b = ' F';
}
/// ////////// ////////////////
if (data == ' C')
{
digitalWrite(LED5,HIGH);
c = ' O';
}
else if (data == ' c')
{
digitalWrite(LED5,LOW);
c = ' F';
}
/// ////////// //////////
if (data == ' D')
{
digitalWrite(LED6,HIGH);
d = ' O';
}
else if (data == ' d')
{
digitalWrite(LED6,LOW);
d = ' F';
}
/// ////////////// //////
if (data == ' E')
{
digitalWrite(LED7,HIGH);
e = ' O';
}
else if (data == ' e')
{
digitalWrite(LED7,LOW);
e = ' F';
}
/// ////////////// ////
if (data == ' F')
{
digitalWrite(LED8,HIGH);
f = ' O';
}
else if (data == ' f')
{
digitalWrite(LED8,LOW);
f = ' F';
}
/// /////////////
if (data == ' G')
{
digitalWrite(LED9,HIGH);
g = ' O';
}
else if (data == ' g')
{
digitalWrite(LED9,LOW);
g = ' F';
}
/// /////////
if (data == ' H')
{
digitalWrite(LED10,HIGH);
h = ' O';
}
else if (data == ' h')
{
digitalWrite(LED10,LOW);
h = ' F';
}
/// ////////
if (data == ' 我')
{
digitalWrite(LED11,HIGH);
i = ' O';
}
else if (data == ' i')
{
digitalWrite(LED11,LOW);
i = ' F';
}
/// ///////////
if (data == ' J')
{
digitalWrite(LED12,HIGH);
j = ' O';
}
else if (data == ' j')
{
digitalWrite(LED12,LOW);
j = ' F';
}
String sco(k);
String scoa(a);
String scob(b);
String scoc(c);
String scod(d);
String scoe(e);
String scof(f);
String scog(g);
String scoh(h);
String scoi(i);
String scoj(j);
String stringTwo = sco;
String string3 = scoa;
String string4 = scob;
String string5 = scoc;
String string6 = scod;
String string7 = scoe;
String string8 = scof;
String string9 = scog;
String string10 = scoh;
String string11 = scoi;
String string12 = scoj;
stringTwo.concat(string3);
stringTwo.concat(string4);
stringTwo.concat(string5);
stringTwo.concat(string6);
stringTwo.concat(string7);
stringTwo.concat(string8);
stringTwo.concat(string9);
stringTwo.concat(string10);
stringTwo.concat(string11);
stringTwo.concat(string12);
stringTwo.concat(stringOne);
Serial.println(stringTwo);
延迟( 100 );
}
====
c#
= ===
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
使用 System.IO.Ports;
命名空间 WindowsFormsApplication3
{
public partial class Form1:表格
{
私人 DateTime日期时间;
private string in_data;
private double con;
string r;
// string lb;
public SerialPort myport;
委托 void SetTextCallback( string text);
string InputData = String .Empty;
public Form1()
{
InitializeComponent();
init();
}
private void button1_Click( object sender,EventArgs e)
{
myport.WriteLine( ○跨度>);
on.Enabled = false ;
off.Enabled = true ;
}
private void Form1_Load( object sender,EventArgs e)
{
/// ///////////////////////////////////////////
string [] ArrayComPortsNames = null ;
int index = -1;
string ComPortName = null ;
// Com Ports
ArrayComPortsNames = SerialPort.GetPortNames();
执行
{
index + = 1 ;
cboPorts.Items.Add(ArrayComPortsNames [index]);
} while (!((ArrayComPortsNames [index] == ComPortName)||(index == ArrayComPortsNames.GetUpperBound( 0 ))));
Array.Sort(ArrayComPortsNames);
if (index == ArrayComPortsNames.GetUpperBound( 0 ))
{
ComPortName = ArrayComPortsNames [ 0 ];
}
尝试
{
myport = 新 SerialPort();
myport.BaudRate = 9600 ;
}
catch (例外)
{
MessageBox.Show( 错误);
}
}
私人 void init( )
{
on.Enabled = false ;
off.Enabled = false ;
}
private void off_Click( object sender,EventArgs e)
{
myport.WriteLine( p跨度>);
on.Enabled = true ;
off.Enabled = false ;
}
private void timer1_Tick( object sender,EventArgs e)
{
r = myport.ReadLine();
label1.Text = r;
}
private void button1_Click_1(< span class =code-keyword> object sender,EventArgs e)
{
try
{
myport = new SerialPort();
myport.BaudRate = 9600 ;
myport.PortName = cboPorts.Text;
myport.Open();
button1.Enabled = false ;
}
catch (例外)
{
MessageBox.Show( 错误);
}
myport.Parity = Parity.None;
myport.DataBits = 8 ;
myport.StopBits = StopBits.One;
myport.DataReceived + = myport_DataReceived;
尝试
{
textBox1.Text = ;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, error);
}
}
void myport_DataReceived( object sender,SerialDataReceivedEventArgs e)
{
in_data = myport.ReadLine();
this .Invoke( new EventHandler(displaydata_event));
}
private void displaydata_event( object senter,EventArgs e)
{
string rd = myport.ReadLine();
string sub = rd.Substring( 0 , 1 跨度>);
/// /////////////////// ////////////////////////
if (sub == F)
{
on.Enabled = true ;
off.Enabled = false ;
}
else if (sub == O)
{
on.Enabled = false < /跨度>;
off.Enabled = true ;
}
label1.Text = sub;
/// /////////////////// ////////////////////////
/// //////// **** LABELS ****** ///////////
label2.Text = rd.Substring( 1 , 1 );
label3.Text = rd.Substring( 2 , 1 );
label4.Text = rd.Substring( 3 , 1 );
label5.Text = rd.Substring( 4 , 1 );
label6.Text = rd.Substring( 5 , 1 );
label7.Text = rd.Substring( 6 , 1 );
label8.Text = rd.Substring( 7 , 1 );
label9.Text = rd.Substring( 8 , 1 );
label10.Text = rd.Substring( 9 , 1 );
label11.Text = rd.Substring( 10 , 1 );
/// /////////////// ///////////////////
// string st = rd.Substring(1);
string st = rd.Remove( 0 , 11 );
datetime = DateTime.Now;
string time = datetime.Hour + : + datetime.Minute + : + datetime.Second;
con =(Convert.ToDouble(st)* 5 / 1024 ) ;
if (con > 3 。 5 )
{System.Media.SystemSounds.Beep.Play(); }
if (con == 0 )
{System.Media.SystemSounds .Asterisk.Play(); }
textBox1.Text = time + \t\t\t + con;
if (con > 2 。 9 & con < 3 。 9 )
{textBox1.BackColor = System.Drawing.Color.Orange; }
if (con > 3 . 9 )
{textBox1.BackColor = System.Drawing.Color.Red; }
if (con < 3 )
{textBox1.BackColor = System.Drawing.Color.Green;}
}
private void comboBox1_SelectedIndexChanged( object sender, EventArgs e)
{
}
private void button2_Click( object sender,EventArgs e)
{
string [] ArrayComPortsNames = null ;
int index = -1;
string ComPortName = null ;
// Com Ports
ArrayComPortsNames = SerialPort.GetPortNames();
执行
{
index + = 1 ;
cboPorts.Items.Add(ArrayComPortsNames [index]);
} while (!((ArrayComPortsNames [index] == ComPortName)||(index == ArrayComPortsNames.GetUpperBound( 0 ))));
Array.Sort(ArrayComPortsNames);
if (index == ArrayComPortsNames.GetUpperBound( 0 ))
{
ComPortName = ArrayComPortsNames [ 0 ];
}
}
private void D3on_Click( object sender,EventArgs e)
{
myport.WriteLine( A跨度>);
}
private void button4_Click( object sender,EventArgs e)
{
myport.WriteLine( 一个跨度>);
}
私有 void D4on_Click( object sender,EventArgs e)
{
myport.WriteLine( B跨度>);
}
private void D4OFF_Click( object sender,EventArgs e)
{
myport.WriteLine( b跨度>);
}
私有 void D5on_Click( object sender,EventArgs e)
{
myport.WriteLine( C跨度>);
}
私有 void D50ff_Click( object sender,EventArgs e)
{
myport.WriteLine( C跨度>);
}
私有 void D6on_Click( object sender,EventArgs e)
{
myport.WriteLine( d跨度>);
}
private void D6off_Click( object sender,EventArgs e)
{
myport.WriteLine( d跨度>);
}
private void D7on_Click( object sender,EventArgs e)
{
myport.WriteLine( E跨度>);
}
private void D7off_Click( object sender,EventArgs e)
{
myport.WriteLine( E跨度>);
}
private void D8On_Click( object sender,EventArgs e)
{
myport.WriteLine( F跨度>);
}
private void D8off_Click( object sender,EventArgs e)
{
myport.WriteLine( F跨度>);
}
private void button11_Click( object sender,EventArgs e)
{
myport.WriteLine( J跨度>);
}
private void button10_Click( object sender,EventArgs e)
{
myport.WriteLine( J跨度>);
}
private void button9_Click( object sender,EventArgs e)
{
myport.WriteLine( i 的跨度>);
}
private void button8_Click( object sender,EventArgs e)
{
myport.WriteLine( I跨度>);
}
private void label2_Click( object sender,EventArgs e)
{
}
private void label7_Click( object sender,EventArgs e)
{
}
private void D9on_Click(对象发件人,EventArgs e)
{
myport.WriteLine( G跨度>);
}
private void D9off_Click( object sender,EventArgs e)
{
myport.WriteLine( 克跨度>);
}
private void D100n_Click( object sender,EventArgs e)
{
myport.WriteLine( H跨度>);
}
私有 void D10off_Click( object sender,EventArgs e)
{
myport.WriteLine( H跨度>);
}
private void textBox1_TextChanged( object sender,EventArgs e)
{
}
}
}
解决方案
你可以尝试一下这个 https:// www。 arduino.cc/en/Tutorial/WebServer [ ^ 一>
i already develpoed a sketch and c# program to read the serial out of Arduino uno analog pin and control digital pin. Now i am using a shield to connect the Arduino uno through Lan. So how can i convert the sketch and program to use it on lan.
============
sketch
============
int LED= 2;
int LED3= 3;
int LED4= 4;
int LED5= 5;
int LED6= 6;
int LED7= 7;
int LED8= 8;
int LED9= 9;
int LED10= 10;
int LED11= 11;
int LED12= 12;
char k='F';
char a='F';
char b='F';
char c='F';
char d='F';
char e='F';
char f='F';
char g='F';
char h='F';
char i='F';
char j='F';
char data;
char x='f';
char con[80];
int analogpin=3;
void setup() {
{
Serial.begin(9600);
pinMode(LED,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(LED5,OUTPUT);
pinMode(LED6,OUTPUT);
pinMode(LED7,OUTPUT);
pinMode(LED8,OUTPUT);
pinMode(LED9,OUTPUT);
pinMode(LED10,OUTPUT);
pinMode(LED11,OUTPUT);
pinMode(LED12,OUTPUT);
pinMode(analogpin,INPUT);
Serial.begin(9600);
}
}
void loop() {
////////////////
int value = analogRead(analogpin);
String stringOne =String(value);
////////////////////////////////
data= Serial.read();
////////////////////////////
if (data == 'o')
{
k='O';
}
else if (data == 'p')
{
k= 'F';
}
else if (data == ' ')
{
k= 'F';
}
if (data=='o')
{
digitalWrite(LED,HIGH);
x = data;
}
else if (data=='p')
{
digitalWrite(LED,LOW);
x = data;
}
///////////////////////////////////
/*if (data !='o'||data !='p')
{
data=x;
}*/
//////////////////////////////////////
/////////////////////////////
if (data=='A')
{
digitalWrite(LED3,HIGH);
a = 'O';
}
else if (data=='a')
{
digitalWrite(LED3,LOW);
a = 'F';
}
////////////////////////////
if (data=='B')
{
digitalWrite(LED4,HIGH);
b = 'O';
}
else if (data=='b')
{
digitalWrite(LED4,LOW);
b = 'F';
}
/////////////////////////////
if (data=='C')
{
digitalWrite(LED5,HIGH);
c = 'O';
}
else if (data=='c')
{
digitalWrite(LED5,LOW);
c = 'F';
}
///////////////////////
if (data=='D')
{
digitalWrite(LED6,HIGH);
d = 'O';
}
else if (data=='d')
{
digitalWrite(LED6,LOW);
d = 'F';
}
///////////////////////
if (data=='E')
{
digitalWrite(LED7,HIGH);
e = 'O';
}
else if (data=='e')
{
digitalWrite(LED7,LOW);
e = 'F';
}
/////////////////////
if (data=='F')
{
digitalWrite(LED8,HIGH);
f = 'O';
}
else if (data=='f')
{
digitalWrite(LED8,LOW);
f = 'F';
}
////////////////
if (data=='G')
{
digitalWrite(LED9,HIGH);
g = 'O';
}
else if (data=='g')
{
digitalWrite(LED9,LOW);
g = 'F';
}
////////////
if (data=='H')
{
digitalWrite(LED10,HIGH);
h = 'O';
}
else if (data=='h')
{
digitalWrite(LED10,LOW);
h = 'F';
}
///////////
if (data=='I')
{
digitalWrite(LED11,HIGH);
i = 'O';
}
else if (data=='i')
{
digitalWrite(LED11,LOW);
i = 'F';
}
//////////////
if (data=='J')
{
digitalWrite(LED12,HIGH);
j = 'O';
}
else if (data=='j')
{
digitalWrite(LED12,LOW);
j = 'F';
}
String sco(k);
String scoa(a);
String scob(b);
String scoc(c);
String scod(d);
String scoe(e);
String scof(f);
String scog(g);
String scoh(h);
String scoi(i);
String scoj(j);
String stringTwo= sco;
String string3=scoa;
String string4=scob;
String string5=scoc;
String string6=scod;
String string7=scoe;
String string8=scof;
String string9=scog;
String string10=scoh;
String string11=scoi;
String string12=scoj;
stringTwo.concat(string3);
stringTwo.concat(string4);
stringTwo.concat(string5);
stringTwo.concat(string6);
stringTwo.concat(string7);
stringTwo.concat(string8);
stringTwo.concat(string9);
stringTwo.concat(string10);
stringTwo.concat(string11);
stringTwo.concat(string12);
stringTwo.concat(stringOne);
Serial.println(stringTwo);
delay(100);
}
====
c#
====
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private DateTime datetime;
private string in_data;
private double con;
string r;
//string lb;
public SerialPort myport;
delegate void SetTextCallback(string text);
string InputData = String.Empty;
public Form1()
{
InitializeComponent();
init();
}
private void button1_Click(object sender, EventArgs e)
{
myport.WriteLine("o");
on.Enabled = false;
off.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
//////////////////////////////////////////////
string[] ArrayComPortsNames = null;
int index = -1;
string ComPortName = null;
//Com Ports
ArrayComPortsNames = SerialPort.GetPortNames();
do
{
index += 1;
cboPorts.Items.Add(ArrayComPortsNames[index]);
} while (!((ArrayComPortsNames[index] == ComPortName) || (index == ArrayComPortsNames.GetUpperBound(0))));
Array.Sort(ArrayComPortsNames);
if (index == ArrayComPortsNames.GetUpperBound(0))
{
ComPortName = ArrayComPortsNames[0];
}
try
{
myport = new SerialPort();
myport.BaudRate = 9600;
}
catch (Exception)
{
MessageBox.Show("Error");
}
}
private void init()
{
on.Enabled = false;
off.Enabled = false;
}
private void off_Click(object sender, EventArgs e)
{
myport.WriteLine("p");
on.Enabled = true;
off.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
r = myport.ReadLine();
label1.Text = r;
}
private void button1_Click_1(object sender, EventArgs e)
{
try
{
myport = new SerialPort();
myport.BaudRate = 9600;
myport.PortName = cboPorts.Text;
myport.Open();
button1.Enabled = false;
}
catch (Exception)
{
MessageBox.Show("Error");
}
myport.Parity = Parity.None;
myport.DataBits = 8;
myport.StopBits = StopBits.One;
myport.DataReceived += myport_DataReceived;
try
{
textBox1.Text = "";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "error");
}
}
void myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
in_data = myport.ReadLine();
this.Invoke(new EventHandler(displaydata_event));
}
private void displaydata_event(object senter, EventArgs e)
{
string rd = myport.ReadLine();
string sub = rd.Substring(0, 1);
//////////////////////////////////////////////
if (sub == "F")
{
on.Enabled = true;
off.Enabled = false;
}
else if (sub == "O")
{
on.Enabled = false;
off.Enabled = true;
}
label1.Text = sub;
//////////////////////////////////////////////
///////////****LABELS******///////////
label2.Text = rd.Substring(1, 1);
label3.Text = rd.Substring(2, 1);
label4.Text = rd.Substring(3, 1);
label5.Text = rd.Substring(4, 1);
label6.Text = rd.Substring(5, 1);
label7.Text = rd.Substring(6, 1);
label8.Text = rd.Substring(7, 1);
label9.Text = rd.Substring(8, 1);
label10.Text = rd.Substring(9, 1);
label11.Text = rd.Substring(10, 1);
/////////////////////////////////////
//string st = rd.Substring(1);
string st = rd.Remove(0, 11);
datetime = DateTime.Now;
string time = datetime.Hour + ":" + datetime.Minute + ":" + datetime.Second;
con = (Convert.ToDouble(st) * 5 / 1024);
if (con > 3.5)
{ System.Media.SystemSounds.Beep.Play(); }
if (con == 0)
{ System.Media.SystemSounds.Asterisk.Play(); }
textBox1.Text = time + "\t\t\t" + con;
if (con > 2.9 & con < 3.9)
{ textBox1.BackColor = System.Drawing.Color.Orange; }
if (con > 3.9)
{ textBox1.BackColor = System.Drawing.Color.Red; }
if (con < 3)
{ textBox1.BackColor = System.Drawing.Color.Green;}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string[] ArrayComPortsNames = null;
int index = -1;
string ComPortName = null;
//Com Ports
ArrayComPortsNames = SerialPort.GetPortNames();
do
{
index += 1;
cboPorts.Items.Add(ArrayComPortsNames[index]);
} while (!((ArrayComPortsNames[index] == ComPortName) || (index == ArrayComPortsNames.GetUpperBound(0))));
Array.Sort(ArrayComPortsNames);
if (index == ArrayComPortsNames.GetUpperBound(0))
{
ComPortName = ArrayComPortsNames[0];
}
}
private void D3on_Click(object sender, EventArgs e)
{
myport.WriteLine("A");
}
private void button4_Click(object sender, EventArgs e)
{
myport.WriteLine("a");
}
private void D4on_Click(object sender, EventArgs e)
{
myport.WriteLine("B");
}
private void D4OFF_Click(object sender, EventArgs e)
{
myport.WriteLine("b");
}
private void D5on_Click(object sender, EventArgs e)
{
myport.WriteLine("C");
}
private void D50ff_Click(object sender, EventArgs e)
{
myport.WriteLine("c");
}
private void D6on_Click(object sender, EventArgs e)
{
myport.WriteLine("D");
}
private void D6off_Click(object sender, EventArgs e)
{
myport.WriteLine("d");
}
private void D7on_Click(object sender, EventArgs e)
{
myport.WriteLine("E");
}
private void D7off_Click(object sender, EventArgs e)
{
myport.WriteLine("e");
}
private void D8On_Click(object sender, EventArgs e)
{
myport.WriteLine("F");
}
private void D8off_Click(object sender, EventArgs e)
{
myport.WriteLine("f");
}
private void button11_Click(object sender, EventArgs e)
{
myport.WriteLine("j");
}
private void button10_Click(object sender, EventArgs e)
{
myport.WriteLine("J");
}
private void button9_Click(object sender, EventArgs e)
{
myport.WriteLine("i");
}
private void button8_Click(object sender, EventArgs e)
{
myport.WriteLine("I");
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void D9on_Click(object sender, EventArgs e)
{
myport.WriteLine("G");
}
private void D9off_Click(object sender, EventArgs e)
{
myport.WriteLine("g");
}
private void D100n_Click(object sender, EventArgs e)
{
myport.WriteLine("H");
}
private void D10off_Click(object sender, EventArgs e)
{
myport.WriteLine("h");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
解决方案
you could try something along the lines of this https://www.arduino.cc/en/Tutorial/WebServer[^]
这篇关于如何从Arduino sketch“ethernet webserver”中读取输出。使用c#程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文