0% found this document useful (0 votes)
18 views3 pages

Form1 Bai2KTTN - Cs

This document contains C# code for a GUI program that communicates with an Arduino board via serial port. It defines event handlers for four buttons that send different characters to the serial port. It also defines an event handler for receiving serial data that changes the background color of four rectangle shapes based on the values of four digital inputs from the Arduino.

Uploaded by

Vine Sandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Form1 Bai2KTTN - Cs

This document contains C# code for a GUI program that communicates with an Arduino board via serial port. It defines event handlers for four buttons that send different characters to the serial port. It also defines an event handler for receiving serial data that changes the background color of four rectangle shapes based on the values of four digital inputs from the Arduino.

Uploaded by

Vine Sandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

D:\STUDY-BKHCM\2022-2023-HK2\DoluongvaDieukhienMT\...\Dethi\Bai2KTTN\C#\GUI TN2_1 - Copy\NewForm\Form1.

cs 1
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;

namespace NewForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int count1 = 0, count2 = 0, count3 = 0, count4 = 0;
private void Form1_Load(object sender, EventArgs e)
{
//Sinh viên thay đổi cổng COM phù hợp với kết nối
serialPort1.PortName = "COM1";
serialPort1.Open();

}
// Sinh vien bo sung chuong trinh xu ly khi bam nut button1, button2, button3, button4
//Button1
private void button1_Click(object sender, EventArgs e)
{
if (count1 == 0) //Nhan lan dau gui xuong ky tu A
{
serialPort1.Write("A");
count1++;
}
else
{
serialPort1.Write("B"); //Nhan lan sau gui xuong ky tu B
count1 = 0;
}
}
//Button2
private void button2_Click(object sender, EventArgs e)
{
if (count2 == 0)
{
serialPort1.Write("C");
count2++;
}
else
{
serialPort1.Write("D");
count2 = 0;
}
}
//Button3
private void button3_Click(object sender, EventArgs e)
{
if (count3 == 0)
{
serialPort1.Write("E");
count3++;
}
else
{
serialPort1.Write("F");
D:\STUDY-BKHCM\2022-2023-HK2\DoluongvaDieukhienMT\...\Dethi\Bai2KTTN\C#\GUI TN2_1 - Copy\NewForm\Form1.cs 2
count3 = 0;
}
}
//Button4
private void button4_Click(object sender, EventArgs e)
{
if (count4 == 0)
{
serialPort1.Write("G");
count4++;
}
else
{
serialPort1.Write("H");
count4 = 0;
}
}

private void DataAvailable(object sender,


System.IO.Ports.SerialDataReceivedEventArgs e)
{
string data;
data = serialPort1.ReadExisting();
this.BeginInvoke(new SetDeleg(si_DataReceived), new object[] { data });
}
private void si_DataReceived(string data)
{
// Sinh vien bo sung chuong trinh xu ly ngat
//I0.0
if (data[0] == 'T')
{
rectangleShape1.BackColor = Color.Green;
}
else if (data[0] == 'F')
{
rectangleShape1.BackColor = Color.White;
}

//I0.1
if (data[1] == 'T')
{
rectangleShape2.BackColor = Color.Green;
}
else if (data[1] == 'F')
{
rectangleShape2.BackColor = Color.White;
}

//I0.2
if (data[2] == 'T')
{
rectangleShape3.BackColor = Color.Green;
}
else if (data[2] == 'F')
{
rectangleShape3.BackColor = Color.White;
}

//I0.3
if (data[3] == 'T')
{
rectangleShape4.BackColor = Color.Green;
}
else if (data[3] == 'F')
{
D:\STUDY-BKHCM\2022-2023-HK2\DoluongvaDieukhienMT\...\Dethi\Bai2KTTN\C#\GUI TN2_1 - Copy\NewForm\Form1.cs 3
rectangleShape4.BackColor = Color.White;
}

}
private delegate void SetDeleg(string text);
}

You might also like