RXD接11Pin TXD接10Pin GND接GND VCC接5V LED正極接13 LED負極接GND
Arduino程式碼:
/* Name: Test.ino Created: 2016/8/5 下午 08:42:12 Author: C.Y.Fang */ #include <SoftwareSerial.h> SoftwareSerial BTSerial(10, 11); // RX | TX char status = NULL; void setup() { pinMode(13, OUTPUT); BTSerial.begin(38400); } void loop() { if (BTSerial.available()) //從藍芽接收Client傳送的字元 status = (char)BTSerial.read(); //H為開F為關 if (status == 'H') digitalWrite(13, HIGH); else if (status == 'F') digitalWrite(13, LOW); }
C#.Net程式碼如下:
using System; using System.IO.Ports; using System.Windows.Forms; namespace SendDataForBluetooth { public partial class Form1 : Form { /// <summary> /// SerialPort /// </summary> private SerialPort _serialPort; public Form1() { InitializeComponent(); this._serialPort = new SerialPort("COM16", 9600, Parity.None, 8, StopBits.One) { ReadTimeout = 10000, WriteTimeout = 10000 }; this._serialPort.Open(); } /// <summary> /// 關燈按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { On(); } /// <summary> /// 開燈按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { Off(); } /// <summary> /// 視窗關閉時關燈 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Off(); this._serialPort.Close(); } /// <summary> /// 開燈 /// </summary> private void On() { if (_serialPort.IsOpen) _serialPort.Write("H"); } /// <summary> /// 關燈 /// </summary> private void Off() { if (_serialPort.IsOpen) _serialPort.Write("F"); } } }
執行結果:
開燈前
開燈後