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;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Linq.Expressions;
namespace WindowsFormsApp1
public partial class Form1 : Form
IPEndPoint ipe;
Socket server;
Socket client;
byte[] datasend = new byte[1];
byte[] datareceive = new byte[1];
public Form1()
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
private void Form1_Load(object sender, EventArgs e)
textBox1.Text = "192";
textBox2.Text = "168";
textBox3.Text = "130";
textBox4.Text = "119";
textBox5.Text = "8000";
timer1.Interval = 1000; // Cập nhật mỗi giây
timer1.Enabled = true; // Bật timer
private void Form1_FormClosing(object sender, FormClosingEventArgs
e)
DialogResult answer = MessageBox.Show("Do yu want to exit the
program?", "Question", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (answer == DialogResult.No)
e.Cancel = true;
else
{
if (button1.Enabled == true)
server.Close();
client.Close();
private void nutnhan_ketnoi_Click(object sender, EventArgs e)
Thread thread = new Thread(Endpoint_Thread);
thread.IsBackground = true;
thread.Start();
textBox6.BackColor = Color.Yellow;
textBox6.Text = "đang chờ các thiết bị kết nối";
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox4.Enabled = false;
textBox5.Enabled = false;
void Endpoint_Thread()
{
try
string ip = textBox1.Text.Trim() + "." +
textBox2.Text.Trim() + "." +
textBox3.Text.Trim() + "." +
textBox4.Text.Trim();
int port = int.Parse(textBox5.Text.Trim());
ipe = new IPEndPoint(IPAddress.Parse(ip), port);
server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
server.Bind(ipe);
server.Listen(10);
client = server.Accept();
textBox6.BackColor = Color.Lime;
textBox6.Text = "KẾT NỐI VỚI: " +
client.RemoteEndPoint.ToString();
Thread thread = new Thread(Receive);
thread.IsBackground = true;
thread.Start();
nutnhan_ketnoi.Enabled = false;
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
catch (Exception)
MessageBox.Show("check the connection again.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox6.BackColor = Color.Red;
textBox6.Text = "KHÔNG KẾT NỐI !";
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = true;
nutnhan_ketnoi.Enabled = true;
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
private void button1_Click(object sender, EventArgs e)
try
server.Close();
client.Close();
catch (Exception)
MessageBox.Show("KIỂM TRA LẠI KẾT NỐI.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox6.BackColor = Color.Red;
textBox6.Text = "KHÔNG KẾT NỐI !";
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
nutnhan_ketnoi.Enabled = true;
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
private void Receive()
try
while (true)
// Receive data from the client
int temp = client.Receive(datareceive);
string s = Encoding.ASCII.GetString(datareceive, 0, temp);
// Update traffic light images based on received data
if (s == "A" || s.Contains("A")) // Đèn đỏ
pictureBox1.Image =
WindowsFormsApp1.Properties.Resources.den_do;
pictureBox2.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
pictureBox3.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
else if (s == "B" || s.Contains("B")) // Đèn vàng
pictureBox1.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
pictureBox2.Image =
WindowsFormsApp1.Properties.Resources.den_vang;
pictureBox3.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
else if (s == "D" || s.Contains("D")) // Đèn xanh
pictureBox1.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
pictureBox2.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
pictureBox3.Image =
WindowsFormsApp1.Properties.Resources.den_xanh;
else if (s == "I" || s.Contains("I")) // Đèn tắt
pictureBox1.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
pictureBox2.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
pictureBox3.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
}
}
catch (Exception)
client.Close();
private void textBox1_TextChanged(object sender, EventArgs e)
if (textBox1.Text == "")
MessageBox.Show("Blank is not a valid entry.Please specitfy a
value between 1 and 223", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox1.Text = "192";
textBox1.Focus();
else if (Int16.Parse(textBox1.Text) < 1)
MessageBox.Show(textBox1.Text + "is not a valid entry.Please
specify a value between 1 and 223", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox1.Text = "192";
textBox1.Focus();
}
else if (Int16.Parse(textBox1.Text) > 223)
MessageBox.Show(textBox1.Text + "is not a valid entry.Please
specify a value between 1 and 223", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox1.Text = "192";
textBox1.Focus();
private void textBox2_TextChanged(object sender, EventArgs e)
if (textBox2.Text == "")
MessageBox.Show("Blank is not a valid entry.Please specitfy a
value between 1 and 223", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox2.Text = "168";
textBox2.Focus();
else if (Int16.Parse(textBox2.Text) < 1)
MessageBox.Show(textBox1.Text + "is not a valid entry.Please
specify a value between 1 and 223", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox2.Text = "168";
textBox2.Focus();
}
else if (Int16.Parse(textBox2.Text) > 223)
MessageBox.Show(textBox1.Text + "is not a valid entry.Please
specify a value between 1 and 223", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox2.Text = "168";
textBox2.Focus();
private void textBox3_TextChanged(object sender, EventArgs e)
if (textBox3.Text == "")
MessageBox.Show("Blank is not a valid entry.Please specitfy a
value between 1 and 223", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox3.Text = "130";
textBox3.Focus();
else if (Int16.Parse(textBox2.Text) < 1)
MessageBox.Show(textBox1.Text + "is not a valid entry.Please
specify a value between 1 and 223", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox3.Text = "130";
textBox3.Focus();
}
else if (Int16.Parse(textBox2.Text) > 223)
MessageBox.Show(textBox1.Text + "is not a valid entry.Please
specify a value between 1 and 223", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox3.Text = "130";
textBox3.Focus();
private void textBox4_TextChanged(object sender, EventArgs e)
if (textBox4.Text == "")
MessageBox.Show("Blank is not a valid entry.Please specitfy a
value between 1 and 223", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox4.Text = "119";
textBox4.Focus();
else if (Int16.Parse(textBox2.Text) < 1)
MessageBox.Show(textBox1.Text + "is not a valid entry.Please
specify a value between 1 and 223", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox4.Text = "119";
textBox4.Focus();
}
else if (Int16.Parse(textBox2.Text) > 223)
MessageBox.Show(textBox1.Text + "is not a valid entry.Please
specify a value between 1 and 223", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox4.Text = "119";
textBox4.Focus();
private void textBox5_TextChanged(object sender, EventArgs e)
if (textBox5.Text == "")
MessageBox.Show("Blank is not a valid entry.Pease speccify a
value bettween 0 and 65535", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox5.Text = "8000";
textBox5.Focus();
else if (Int32.Parse(textBox5.Text) < 0)
MessageBox.Show(textBox5.Text + "is not a valid entry.Please
specify a value between 0 and 65535", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox5.Text = "8000";
textBox5.Focus();
}
else if (Int32.Parse(textBox5.Text) > 65535)
MessageBox.Show(textBox5.Text + "is not a valid entry.Please
specify a value between 0 and 65535", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
textBox5.Text = "8000";
textBox5.Focus();
private void textBox6_TextChanged(object sender, EventArgs e)
private void label4_Click(object sender, EventArgs e)
private void button2_Click(object sender, EventArgs e)
try
{
datasend = Encoding.ASCII.GetBytes("@");
client.Send(datasend, datasend.Length, SocketFlags.None);
catch (Exception)
MessageBox.Show("check the connection again.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
private void button3_Click(object sender, EventArgs e)
try
datasend = Encoding.ASCII.GetBytes("#");
client.Send(datasend, datasend.Length, SocketFlags.None);
catch (Exception)
MessageBox.Show("check the connection again.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void button4_Click(object sender, EventArgs e)
try
datasend = Encoding.ASCII.GetBytes("$");
client.Send(datasend, datasend.Length, SocketFlags.None);
catch (Exception)
MessageBox.Show("check the connection again.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
private void timer1_Tick(object sender, EventArgs e)
label4.Text = DateTime.Now.ToString("HH:mm:ss");
// Check if it's day or night
bool isNight = (DateTime.Now.Hour >= 22 || DateTime.Now.Hour <
5);
// Add a textBox for day/night status if it doesn't exist
// Assuming you'll add a textBox7 to your form for this purpose
if (textBox7 != null)
if (isNight)
textBox7.Text = "ĐÊM";
textBox7.BackColor = Color.DarkBlue;
textBox7.ForeColor = Color.White;
else
textBox7.Text = "NGÀY";
textBox7.BackColor = Color.LightSkyBlue;
textBox7.ForeColor = Color.Black;
// Send mode to connected device if connected
if (client != null && client.Connected)
try
string mode = isNight ? "T" : "N";
datasend = Encoding.ASCII.GetBytes(mode);
client.Send(datasend, datasend.Length, SocketFlags.None);
catch (Exception ex)
// You might want to handle this silently or with a status update
// rather than showing a message box every second if there's an
issue
Console.WriteLine("Error sending data: " + ex.Message);
private void button5_Click(object sender, EventArgs e)
try
// Tắt đèn
textBox11.BackColor = Color.Red; // Đổi màu nền thành đỏ
textBox11.Text = "TẮT"; // Hiển thị nội dung
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
if (client != null && client.Connected)
{
datasend = Encoding.ASCII.GetBytes("O");
client.Send(datasend, datasend.Length, SocketFlags.None);
else
MessageBox.Show("No device connected!", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
catch (Exception ex)
MessageBox.Show("Error: " + ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
private void button6_Click(object sender, EventArgs e)
try
// Chế độ bằng tay
textBox11.BackColor = Color.Yellow; // Đổi màu nền thành vàng
textBox11.Text = "CHẾ ĐỘ BẰNG TAY"; // Hiển thị nội dung
// Vô hiệu hóa các nút Chế độ 1, 2, 3
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
if (client != null && client.Connected)
datasend = Encoding.ASCII.GetBytes("O");
client.Send(datasend, datasend.Length, SocketFlags.None);
else
MessageBox.Show("No device connected!", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
catch (Exception ex)
MessageBox.Show("Error: " + ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
private void button7_Click(object sender, EventArgs e)
try
// Chế độ tự động
textBox11.BackColor = Color.Lime; // Đổi màu nền thành xanh lá
textBox11.Text = "CHẾ ĐỘ TỰ ĐỘNG"; // Hiển thị nội dung
// Bật các nút 2, 3, 4 (tương đương với nút 1, 2, 3 trong code
Bluetooth)
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
if (client != null && client.Connected)
datasend = Encoding.ASCII.GetBytes("F");
client.Send(datasend, datasend.Length, SocketFlags.None);
else
MessageBox.Show("No device connected!", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
catch (Exception ex)
MessageBox.Show("Error: " + ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
private void textBox8_TextChanged(object sender, EventArgs e)
// Green light timing adjustment
if (client == null || !client.Connected)
MessageBox.Show("No connection established. Please connect
first.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
// Check input value
if (!int.TryParse(textBox8.Text, out int greenTime) || greenTime < 3 ||
greenTime > 10)
MessageBox.Show("Green light time must be an integer from 3 to
10 seconds!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
try
// Map time to corresponding character
char cmd = (char)('a' + (greenTime - 3));
datasend = Encoding.ASCII.GetBytes(cmd.ToString());
client.Send(datasend, datasend.Length, SocketFlags.None);
MessageBox.Show($"Green light time updated: {greenTime}
seconds", "Notification", MessageBoxButtons.OK,
MessageBoxIcon.Information);
catch (Exception ex)
{
MessageBox.Show("Error sending data: " + ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
private void textBox9_TextChanged(object sender, EventArgs e)
if (client == null || !client.Connected)
MessageBox.Show("No connection established. Please connect
first.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
// Check input value
if (!int.TryParse(textBox9.Text, out int yellowTime) || yellowTime < 3 ||
yellowTime > 10)
MessageBox.Show("Yellow light time must be an integer from 3 to
10 seconds!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
try
// Map time to corresponding character for yellow light
char cmd;
if (yellowTime == 10)
cmd = 'i';
else
cmd = (char)('j' + (yellowTime - 3));
datasend = Encoding.ASCII.GetBytes(cmd.ToString());
client.Send(datasend, datasend.Length, SocketFlags.None);
MessageBox.Show($"Yellow light time updated: {yellowTime}
seconds", "Notification", MessageBoxButtons.OK,
MessageBoxIcon.Information);
catch (Exception ex)
MessageBox.Show("Error sending data: " + ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
private void textBox10_TextChanged(object sender, EventArgs e)
// Red light timing adjustment
if (client == null || !client.Connected)
MessageBox.Show("No connection established. Please connect
first.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
// Check input value
if (!int.TryParse(textBox10.Text, out int redTime) || redTime < 3 ||
redTime > 10)
MessageBox.Show("Red light time must be an integer from 3 to 10
seconds!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
try
// Map time to corresponding character
char cmd = (char)('q' + (redTime - 3));
datasend = Encoding.ASCII.GetBytes(cmd.ToString());
client.Send(datasend, datasend.Length, SocketFlags.None);
MessageBox.Show($"Red light time updated: {redTime} seconds",
"Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
catch (Exception ex)
MessageBox.Show("Error sending data: " + ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);