I. Thiết kế giao diện chính và trang trí
Như những bài trước, các bạn sẽ thiết kế 1 giao diện cho app của mình như sau:Ngoài ra các bạn kéo thêm
timer1
vào, serialPort1
, StatusTrip
....ngoài ra có thể trang trí bằng lịch, hay hình ảnh cho nguy hiểm!!! timer1
các bạn thiết lập Properties như sau:II. Code
Sau khi xong phần thiết kế giao diện các bạn nạp code cho từng Form:Form1:
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; // Thêm 3 em này vào là OK, để sài SerialPort using System.IO; using System.IO.Ports; using System.Xml; using System.Threading;// khởi tạo hàm Threading namespace nhandulieuCOM { public partial class Form1 : Form { string InputData = String.Empty; // Khai báo string buff dùng cho hiển thị dữ liệu sau này. delegate void SetTextCallback(string text); // Khai bao delegate SetTextCallBack voi tham so string public Form1() { //cái Thread thực ra để chạy Form3 khi ms zô...cho ngầu Thread t = new Thread(new ThreadStart(SplashScreen));//tạo 1 Thread t là SplashScreen t.Start();// khi chương trình bật thỳ chạy t...tức là gọi SplashScreen Thread.Sleep(4500);//ngủ 4,5s InitializeComponent(); t.Abort();//sau đó đóng t // Khai báo hàm delegate bằng phương thức DataReceived của Object SerialPort; // Cái này khi có sự kiện nhận dữ liệu sẽ nhảy đến phương thức DataReceive // Nếu ko hiểu đoạn này bạn có thể tìm hiểu về Delegate, còn ko cứ COPY . Ko cần quan tâm serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceive); string[] BaudRate = { "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" }; comboBox2.Items.AddRange(BaudRate);// Thiết lập cho comboBox2 } private void SplashScreen() { Application.Run(new Form3());//chạy Form3 } private void Form1_Load(object sender, EventArgs e) { comboBox1.DataSource = SerialPort.GetPortNames(); comboBox2.SelectedIndex = 3; } private void timer1_Tick(object sender, EventArgs e) { if (!serialPort1.IsOpen) { button2.Enabled = true; button1.Enabled = false; Thongbao.Text = ("Bạn chưa kết nối, hãy chọn một cổng COM để kết nối nhé!!! keke !! Arduino.vn"); Thongbao.ForeColor = Color.Red; } else if (serialPort1.IsOpen) { button2.Enabled = false; button1.Enabled = true; Thongbao.Text = ("Đã kết nối nhé!!! keke"); Thongbao.ForeColor = Color.Green; } } private void DataReceive(object obj, SerialDataReceivedEventArgs e) { InputData = serialPort1.ReadExisting(); if (InputData != String.Empty) { // txtIn.Text = InputData; // Ko dùng đc như thế này vì khác threads . SetText(InputData); // Chính vì vậy phải sử dụng ủy quyền tại đây. Gọi delegate đã khai báo trước đó. } } // Hàm của em nó là ở đây. Đừng hỏi vì sao lại thế. private void SetText(string text) { if (this.textBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); // khởi tạo 1 delegate mới gọi đến SetText this.Invoke(d, new object[] { text }); } else this.textBox1.Text += text; } private void button2_Click(object sender, EventArgs e) { if (!serialPort1.IsOpen) { serialPort1.PortName = comboBox1.Text; serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text); serialPort1.Open(); } } private void button1_Click(object sender, EventArgs e) { serialPort1.Close(); } private void button5_Click(object sender, EventArgs e) { // Hiện tí thông báo..cho ngầu if (serialPort1.IsOpen)// đã kết nối thì { //nếu chưa nhập if (textBox2.Text == "") MessageBox.Show("Nhập dữ liệu cần gửi vào nhé!!!", "Đỗ Hữu Toàn - Thông báo"); else serialPort1.Write(textBox2.Text);// ngược lại...gửi giá trị từ textBox tới cổng COM } // Nếu cổng COM chưa kết nối else MessageBox.Show("Kết nối cổng COM đi bạn", "Đỗ Hữu Toàn - Thông báo"); textBox2.Clear(); } private void button3_Click(object sender, EventArgs e) { textBox2.Clear(); textBox1.Clear(); //Ấn xóa clear hết textbox } private void button4_Click(object sender, EventArgs e) { DialogResult stt = MessageBox.Show("Bạn chắc chắn muốn thoát chương trình???", "Đỗ Hữu Toàn - Arduino.vn", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (stt == DialogResult.Yes ) MessageBox.Show("Cảm ơn bạn đã sử dụng chương trình, nhớ Rate Note ủng hộ mình nhé","Đỗ Hữu Toàn - Arduino.vn"); Close(); } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { Form2 tacgia = new Form2(); tacgia.ShowDialog(); // chạy form2 khi ấn nút tác giả ( linklabel1) } } }
Form 2:
(chỉ là phần logo..k cóa code nhé)
Form 3:
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; namespace nhandulieuCOM { public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { //Khi timer làm tươi...tăng 1 đơn vị cho progessBar progressBar1.Increment(1); if(progressBar1.Value == 100) { // Nếu ProgessBar = 100..Tức là Max r đó timer1.Stop();// thì sop timer..ngừng làm tươi..ngừng tăng processBar } } } }
III. Kết quả
Trên đây là cách chế tạo Serial Monitor của mình...Các bạn xem và góp ý nhé!!! Chúc các bạn thành công!!!Theo Arduino.VN
No comments:
Post a Comment