Arduino的LED开/关TCP C# [英] Arduino LED On/Off TCP C#
本文介绍了Arduino的LED开/关TCP C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在的Arduino的,我想尝试打开开关/关闭导致使用C#与Arduino的。所以,我尝试使用串行端口,和它的作品,但我想用以太网盾打开/关闭LED。
使用串口我的code
这Arduino的code
I am now for Arduino, I want try to open switch on/off a led using C# with Arduino. So I try using serial port, and it works, but I want to use an Ethernet shield to switch on/off the led. My code using serial port This Arduino code
#define BaudRate 9600
#define LEDPin 10
char incomingOption;
void setup()
{
pinMode(LEDPin, OUTPUT);
// serial communication
Serial.begin(BaudRate);
}
void loop()
{
//read from serial port getting information from VS 2013
incomingOption = Serial.read();
//verify incomingOption
switch(incomingOption){
case '1':
// Turn ON LED
digitalWrite(LEDPin, HIGH);
break;
case '0':
// Turn OFF LED
digitalWrite(LEDPin, LOW);
break;
}
}
和C#的,我有3个简单的按钮(开,关,并关闭串口)
and for C#, I have 3 simple button (on, off, and close the serial port)
public partial class frmTurnONTurnOFFLED : Form
{
public frmTurnONTurnOFFLED()
{
InitializeComponent();
}
private void btnTurnON_Click(object sender, EventArgs e)
{
try
{
serialPort1.Write("1"); //send 1 to Arduino
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnTurnOFF_Click(object sender, EventArgs e)
{
try
{
serialPort1.Write("0"); //send 0 to Arduino
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void frmTurnONTurnOFFLED_Load(object sender, EventArgs e)
{
serialPort1.Open(); //open serialPort
}
private void btnClosePort_Click(object sender, EventArgs e)
{
serialPort1.Close(); //close serialPort
}
}
那么,我可以修改我的code?使用插座?使用TCP UDP公关?
So where can I modify my code? using socket? using TCP pr UDP?
推荐答案
:可以先尝试打开设置端口名称?
Can you try set port name before open?
serialPort1.PortName = "COM3" // your ardunio port (you can see on device manager)
serialPort1.Open();
这篇关于Arduino的LED开/关TCP C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文