C#控制只要使用 Serial Port 來溝通,讓電腦端可以控制 Arduino 的 LED
Serial Port必須要引入 using System.IO.Ports才能使用,有些人在使用VS時會缺少Serial Port元件如下圖到 : 工具 > NuGet套件管理員 > 管理方案的NuGet套件
Serial Port的錯誤選項就會消失了
看到此視窗請到瀏覽位置 > 在收尋的地方打上Serial Port > 點選System.IO.Ports > 勾選並選擇安裝
介下來就是開始撰寫程式
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;
using System.Threading;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
// static SerialPort _serialPort;
private SerialPort serialPort1 = new SerialPort();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
serialPort1.BaudRate = 9600;
// serialPort1.Parity = Parity.None;
// serialPort1.DataBits = 8;
// serialPort1.StopBits = StopBits.One;
btnDisconnect.Enabled = false;
btnLedOpen.Enabled = false;
btnLedClose.Enabled = false;
cbPortNumber.Items.AddRange(SerialPort.GetPortNames());
label1.Text = "PC狀態:尚未連線";
}
private void cbPortNumber_SelectedIndexChanged(object sender, EventArgs e)
{
serialPort1.PortName = cbPortNumber.Text;
}
/*private void btnConnect_Click(object sender, EventArgs e)
{
serialPort1.Open();
btnConnect.Enabled = false;
btnDisconnect.Enabled = true;
btnLedOpen.Enabled = true;
btnLedClose.Enabled = true;
label1.Text = "PC狀態:連線中";
}*/
private void btnDisconnect_Click(object sender, EventArgs e)
{
serialPort1.Write("0");
serialPort1.Close();
btnConnect.Enabled = true;
btnDisconnect.Enabled = false;
btnLedOpen.Enabled = false;
btnLedClose.Enabled = false;
label1.Text = "PC狀態:斷線中";
label2.Text = "Arduino回傳:";
label3.Text = serialPort1.IsOpen.ToString();
}
private void btnLedOpen_Click(object sender, EventArgs e)
{
serialPort1.Write("1");
btnLedClose.Enabled = true;
btnLedOpen.Enabled = false;
label1.Text = "送出指令:LED ON";
label2.Text = "Arduino回傳:" + serialPort1.ReadLine();
}
private void btnLedClose_Click(object sender, EventArgs e)
{
serialPort1.Write("0");
btnLedOpen.Enabled = true;
btnLedClose.Enabled = false;
label1.Text = "送出指令:LED OFF";
label2.Text = "Arduino回傳:" + serialPort1.ReadLine();
}
private void btnConnect_Click_1(object sender, EventArgs e)
{
serialPort1.PortName = cbPortNumber.Text.ToString();
serialPort1.Parity = Parity.None;
serialPort1.DataBits = 8;
serialPort1.StopBits = StopBits.One;
serialPort1.Open();
btnConnect.Enabled = false;
btnDisconnect.Enabled = true;
btnLedOpen.Enabled = true;
btnLedClose.Enabled = true;
label1.Text = "PC狀態:連線中";
label3.Text = serialPort1.IsOpen.ToString();
// serialPort1.Write("1");
}
}
}
Arduino的程式碼 :
int led1=13;
void setup() {
Serial.begin(9600); // 開啟 Serial port, 通訊速率為 9600 bps
// 初始化 LED 接腳
pinMode(led1, OUTPUT);
digitalWrite(led1, LOW);
Serial.println("please select '1' or '0' to control led:");
}
void loop() {
// 檢查是否有資料可供讀取
if (Serial.available() > 0) {
// 讀取進來的 byte
char inByte = Serial.read();
// 根據收到的字元決定要打開或關掉 LED
Serial.println(inByte);
switch (inByte) {
case '0':
digitalWrite(led1, LOW);
delay(1000);
Serial.println("LED OFF");
break;
case '1':
digitalWrite(led1, HIGH);
delay(10000);
Serial.println("LED ON");
break;
default:
// 關掉所有的 LED
digitalWrite(led1, LOW);
delay(10000);
}
}
}