Arduino LED 开/关 TCP C# [英] Arduino LED On/Off TCP C#

查看:24
本文介绍了Arduino LED 开/关 TCP C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在是 Arduino,我想尝试使用 C# 和 Arduino 打开/关闭 LED.所以我尝试使用串口,​​它可以工作,但我想使用以太网屏蔽来打开/关闭 LED.我的代码使用串口这个Arduino代码

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
   }        
}

那么我在哪里可以修改我的代码?使用套接字?使用 TCP pr 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆