最近剛接觸C# windows form這一塊 對於C#和Arduino通訊還不是很了解
目的是想寫一個人機介面跟Arduino 溝通 …
以下是我遇到的狀況和windows form程式碼 麻煩各位了
form1就不提 因為只是設定點一個按鈕後顯示form2
form2的程式碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading;
using System.IO;
using Firmata.NET;
namespace owei02
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
string[] serialPort1 = System.IO.Ports.SerialPort.GetPortNames();
comboBox1.Items.AddRange(serialPort1);
}
private void Form2_Load(object sender, EventArgs e)
{
timer1.Start();
timer1.Interval = 1;
timer2.Start();
timer2.Interval = 1;
}
private void tabPage1_Click(object sender, EventArgs e)
{
}
private void tabPage2_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = “X座標:” + Cursor.Position.X.ToString();
}
private void timer2_Tick(object sender, EventArgs e)
{
label2.Text = “Y座標:” + Cursor.Position.Y.ToString();
}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
toolStripLabel1.Text = “Set Arduino Port.”;
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.PortName = comboBox1.SelectedItem.ToString();
serialPort1.BaudRate = 9600;
serialPort1.Open();
if (serialPort1.IsOpen)
{
toolStripLabel1.Text = “Connect to Arduino.”;
serialPort1.Write(“hello”);
}
}
}
}
Run之後沒有問題
這個是第一個Tab
這是第二個Tab 視窗會自動偵測serial ports給我選 圖中是arduino UNO com7
當我按下 OK按鈕(程式中的button2)的時候
就跑出下面這個方塊
上網查了還是不太懂
我有插入arduino.cs檔進入參考
參考至下面這個網址
不知道是不是我的程式有問題 懇請各位大大幫我檢查一下
非常謝謝