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

Software

The document is a C# code for a Windows Forms application that interfaces with serial ports. It includes functionalities for connecting and disconnecting from COM ports, sending commands based on time, and handling data received from the serial port to update the UI. The application supports both manual and automatic modes for sending commands to connected devices.

Uploaded by

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

Software

The document is a C# code for a Windows Forms application that interfaces with serial ports. It includes functionalities for connecting and disconnecting from COM ports, sending commands based on time, and handling data received from the serial port to update the UI. The application supports both manual and automatic modes for sending commands to connected devices.

Uploaded by

hịhd Hdhc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

using [Link].

Forms;
using [Link];
using System;

namespace baiCOM
{
public partial class Form1 : Form
{
private bool isManualMode = false;
private bool isMode3Active = false;
private int lastHour = -1;

public Form1()
{
InitializeComponent();
[Link] += new EventHandler(this.button1_Click);
[Link] += new EventHandler(this.button2_Click);
[Link] += new EventHandler(this.button3_Click_1);
[Link] += new EventHandler(this.button6_Click_1);
[Link] += new EventHandler(this.button7_Click_1);
[Link] += serialPort1_DataReceived;
[Link] += timer1_Tick;
[Link] = 1000;
[Link] = true;
}

private void Form1_Load(object sender, EventArgs e)


{
string[] ports = [Link]();
foreach (string port in ports)
{
[Link](port);
}
}

private void timer1_Tick(object sender, EventArgs e)


{
[Link] = [Link]("HH:mm:ss");

if (isMode3Active && !isManualMode)


{
int currentHour = [Link];
if (currentHour != lastHour)
{
lastHour = currentHour;
SendMode3CommandByTime();
}
}
}

private void SendMode3CommandByTime()


{
if (![Link]) return;

int hour = [Link];


if (hour >= 5 && hour < 22)
{
[Link]("C");
}
else
{
[Link]("B");
}
}

private void serialPort1_DataReceived(object sender,


SerialDataReceivedEventArgs e)
{
string data = [Link]();
[Link](new Action(() =>
{
if ([Link]("R"))
{
[Link] = [Link];
}
if ([Link]("Y"))
{
[Link] = [Link];
}
if ([Link]("G"))
{
[Link] = [Link];
}
}));
}

private void button4_Click(object sender, EventArgs e)


{
if ([Link] == "")
{
[Link]("Vui lòng chọn cổng COM.",
"Cảnh báo", [Link],
[Link]);
}
else
{
try
{
if (![Link])
{
[Link] = [Link];
[Link]();
[Link] = "Connected";
[Link] = [Link];
[Link] = false;
}
else
{
[Link]("Cổng COM đã được kết nối.",
"Cảnh báo", [Link],
[Link]);
}
}
catch
{
[Link]("Không thể kết nối cổng COM, vui lòng kiểm
tra.",
"Cảnh báo", [Link],
[Link]);
}
}
}

private void button5_Click(object sender, EventArgs e)


{
try
{
if ([Link])
{
[Link]();
[Link] = "Disconnected";
[Link] = [Link];
[Link] = true;
}
else
{
[Link]("Cổng COM đã được ngắt kết nối.",
"Cảnh báo", [Link],
[Link]);
}
}
catch
{
[Link]("Không thể ngắt kết nối cổng COM, vui lòng kiểm
tra.",
"Cảnh báo", [Link],
[Link]);
}
}

private void button1_Click(object sender, EventArgs e)


{
isMode3Active = false;

if (isManualMode) return;

if ([Link])
{
try
{
[Link]("A");
}
catch (Exception ex)
{
[Link]("Lỗi gửi dữ liệu: " + [Link],
"Lỗi", [Link],
[Link]);
}
}
}

private void button2_Click(object sender, EventArgs e)


{
isMode3Active = false;

if (isManualMode) return;
if ([Link])
{
try
{
[Link]("B");
}
catch (Exception ex)
{
[Link]("Lỗi gửi dữ liệu: " + [Link],
"Lỗi", [Link],
[Link]);
}
}
}

private void button3_Click_1(object sender, EventArgs e)


{
if (isManualMode) return;

if ([Link])
{
try
{
isMode3Active = true;
lastHour = -1;
SendMode3CommandByTime();
}
catch (Exception ex)
{
[Link]("Lỗi gửi dữ liệu: " + [Link]);
}
}
}

private void button6_Click_1(object sender, EventArgs e)


{
isManualMode = true;
isMode3Active = false;

[Link] = false;
[Link] = false;
[Link] = false;

[Link] = "Chế độ Thủ công";

if ([Link])
{
try
{
[Link]("M");
}
catch (Exception ex)
{
[Link]("Lỗi gửi M: " + [Link],
"Lỗi", [Link],
[Link]);
}
}
}
private void button7_Click_1(object sender, EventArgs e)
{
isManualMode = false;
[Link] = true;
[Link] = true;
[Link] = true;

[Link] = "Chế độ Giao diện";

if ([Link])
{
try
{
[Link]("G");
}
catch (Exception ex)
{
[Link]("Lỗi gửi G: " + [Link],
"Lỗi", [Link],
[Link]);
}
}
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { }


private void textBox1_TextChanged(object sender, EventArgs e) { }
private void textBox2_TextChanged(object sender, EventArgs e) { }
private void pictureBox2_Click(object sender, EventArgs e) { }
private void button1_Click_1(object sender, EventArgs e) { }
private void button8_Click(object sender, EventArgs e) { }
private void textBox2_TextChanged_1(object sender, EventArgs e) { }
private void textBox3_TextChanged(object sender, EventArgs e) { }
private void textBox4_TextChanged(object sender, EventArgs e) { }
private void textBox6_TextChanged(object sender, EventArgs e) { }
}
}

You might also like