2015年5月30日 星期六

C#傳送字元至Arduino、Arduino回傳字串至C#

本篇文章為C#傳送字元至Arduino續文
修改COM port設定方式,改為由下拉式選單選擇
新增Arduino回傳字串至C#的功能

C#
使用
comboBox1.Items.AddRange(SerialPort.GetPortNames());
將電腦中的串列埠加入comboBox選單
使用
serialPort1.PortName = comboBox1.Text;
設定 serialPort1.PortName為comboBox的文字
使用
serialPort1.ReadLine();
讀取Arduino回傳字串

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace _0150529
{
    public partial class Form1 : Form
    {
        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;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            comboBox1.Items.AddRange(SerialPort.GetPortNames());
            label1.Text = "PC狀態:尚未連線";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            serialPort1.PortName = comboBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            button1.Enabled = false;
            button2.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = true;
            label1.Text = "PC狀態:連線中";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            serialPort1.Write("1");
            button4.Enabled = true;
            button3.Enabled = false;
            label1.Text = "送出指令:LED ON";
            label2.Text= "Arduino回傳:"+ serialPort1.ReadLine();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            serialPort1.Write("0");
            button3.Enabled = true;
            button4.Enabled = false;
            label1.Text = "送出指令:LED OFF";
            label2.Text = "Arduino回傳:" + serialPort1.ReadLine();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Write("0");
            serialPort1.Close();
            button1.Enabled = true;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            label1.Text = "PC狀態:斷線中";
            label2.Text = "Arduino回傳:";
        }
    }
}

Arduino程式碼
使用Serial.println傳送字串
此處配合C#端使用serialPort1.ReadLine()

void setup() {
   Serial.begin(9600);     // 開啟 Serial port, 通訊速率為 9600 bps
   // 初始化 LED 接腳
   pinMode(13, OUTPUT);
}       
void loop() {
   // 檢查是否有資料可供讀取
   if (Serial.available() > 0) {
     // 讀取進來的 byte
     char inByte = Serial.read();
     // 根據收到的字元決定要打開或關掉 LED
     switch (inByte) {
     case '0':    
       digitalWrite(13, LOW);
       delay(1);
       Serial.println("LED OFF");
       break;
     case '1': 
       digitalWrite(13, HIGH);
       delay(1);
       Serial.println("LED ON");
       break;
     default:
       // 關掉所有的 LED
       digitalWrite(13, LOW);
     }
   } 
}

執行結果



3 則留言:

  1. 不好意思,請問serialPort1是怎麼來的??
    是private SerialPort serialPort1=new SerialPort();
    我這樣寫對嗎???

    回覆刪除
    回覆
    1. 工具箱就有一個serialPort1直接拉進去就出現囉

      刪除
  2. 作者已經移除這則留言。

    回覆刪除