Code Visual ETHERNET
Code Visual ETHERNET
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
IPEndPoint ipe;
Socket server;
Socket client;
public Form1()
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
textBox1.Text = "192";
textBox2.Text = "168";
textBox3.Text = "130";
textBox4.Text = "119";
textBox5.Text = "8000";
if (answer == DialogResult.No)
e.Cancel = true;
else
{
if (button1.Enabled == true)
server.Close();
client.Close();
thread.IsBackground = true;
thread.Start();
textBox6.BackColor = Color.Yellow;
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox4.Enabled = false;
textBox5.Enabled = false;
void Endpoint_Thread()
{
try
textBox2.Text.Trim() + "." +
textBox3.Text.Trim() + "." +
textBox4.Text.Trim();
SocketType.Stream, ProtocolType.Tcp);
server.Bind(ipe);
server.Listen(10);
client = server.Accept();
textBox6.BackColor = Color.Lime;
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)
textBox6.BackColor = Color.Red;
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;
try
server.Close();
client.Close();
catch (Exception)
textBox6.BackColor = Color.Red;
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;
try
while (true)
pictureBox1.Image =
WindowsFormsApp1.Properties.Resources.den_do;
pictureBox2.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
pictureBox3.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
pictureBox1.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
pictureBox2.Image =
WindowsFormsApp1.Properties.Resources.den_vang;
pictureBox3.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
pictureBox1.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
pictureBox2.Image =
WindowsFormsApp1.Properties.Resources.den_tat;
pictureBox3.Image =
WindowsFormsApp1.Properties.Resources.den_xanh;
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();
if (textBox1.Text == "")
textBox1.Text = "192";
textBox1.Focus();
textBox1.Text = "192";
textBox1.Focus();
}
else if (Int16.Parse(textBox1.Text) > 223)
textBox1.Text = "192";
textBox1.Focus();
if (textBox2.Text == "")
textBox2.Text = "168";
textBox2.Focus();
textBox2.Text = "168";
textBox2.Focus();
}
else if (Int16.Parse(textBox2.Text) > 223)
textBox2.Text = "168";
textBox2.Focus();
if (textBox3.Text == "")
textBox3.Text = "130";
textBox3.Focus();
textBox3.Text = "130";
textBox3.Focus();
}
else if (Int16.Parse(textBox2.Text) > 223)
textBox3.Text = "130";
textBox3.Focus();
if (textBox4.Text == "")
textBox4.Text = "119";
textBox4.Focus();
textBox4.Text = "119";
textBox4.Focus();
}
else if (Int16.Parse(textBox2.Text) > 223)
textBox4.Text = "119";
textBox4.Focus();
if (textBox5.Text == "")
textBox5.Text = "8000";
textBox5.Focus();
MessageBoxIcon.Exclamation);
textBox5.Text = "8000";
textBox5.Focus();
}
else if (Int32.Parse(textBox5.Text) > 65535)
MessageBoxIcon.Exclamation);
textBox5.Text = "8000";
textBox5.Focus();
try
{
datasend = Encoding.ASCII.GetBytes("@");
catch (Exception)
try
datasend = Encoding.ASCII.GetBytes("#");
catch (Exception)
try
datasend = Encoding.ASCII.GetBytes("$");
catch (Exception)
label4.Text = DateTime.Now.ToString("HH:mm:ss");
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;
try
try
// Tắt đèn
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
{
datasend = Encoding.ASCII.GetBytes("O");
else
try
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
datasend = Encoding.ASCII.GetBytes("O");
else
try
// Chế độ tự động
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
datasend = Encoding.ASCII.GetBytes("F");
else
return;
return;
try
datasend = Encoding.ASCII.GetBytes(cmd.ToString());
{
MessageBox.Show("Error sending data: " + ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
return;
try
char cmd;
if (yellowTime == 10)
cmd = 'i';
else
datasend = Encoding.ASCII.GetBytes(cmd.ToString());
return;
return;
try
datasend = Encoding.ASCII.GetBytes(cmd.ToString());