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

Software

The document is a C# Windows Forms application that implements a socket server for communication with a client. It includes functionalities for connecting to the client, sending commands based on user input, and receiving data to update the UI. The application supports both manual and automated modes of operation, with a timer to handle periodic tasks.

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)
14 views5 pages

Software

The document is a C# Windows Forms application that implements a socket server for communication with a client. It includes functionalities for connecting to the client, sending commands based on user input, and receiving data to update the UI. The application supports both manual and automated modes of operation, with a timer to handle periodic tasks.

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
You are on page 1/ 5

using System;

using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace baiCOM
{
public partial class Form1 : Form
{
private IPEndPoint ipe;
private Socket server;
private Socket client;
private byte[] datasend = new byte[1];
private byte[] datareceive = new byte[1];

private bool isManualMode = false;


private bool isMode3Active = false;
private int lastHour = -1;

public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;

button1.Click += button1_Click;
button2.Click += button2_Click;
button3.Click += button3_Click;
button6.Click += button6_Click;
button7.Click += button7_Click;
button8.Click += button8_Click;
button4.Click += button4_Click;
button5.Click += button5_Click;

timer1.Tick += timer1_Tick;
timer1.Interval = 1000;
timer1.Enabled = true;
}

private void Form1_Load(object sender, EventArgs e)


{
textBox8.Text = "192";
textBox9.Text = "168";
textBox10.Text = "0";
textBox11.Text = "125";
textBox12.Text = "8000";
}

private void button4_Click(object sender, EventArgs e)


{
string ip = $"{textBox8.Text}.{textBox9.Text}.{textBox10.Text}.
{textBox11.Text}";
if (!int.TryParse(textBox12.Text, out int port))
{
MessageBox.Show("Port không hợp lệ!", "Lỗi", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}

if (server != null)
{
try { server.Close(); } catch { }
server = null;
client = null;
}

try
{
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
server.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true);
server.ExclusiveAddressUse = false;

ipe = new IPEndPoint(IPAddress.Parse(ip), port);


server.Bind(ipe);
server.Listen(1);
}
catch (SocketException)
{
MessageBox.Show("Port đang được sử dụng hoặc chưa giải phóng. Vui
lòng chọn port khác.", "Lỗi kết nối", MessageBoxButtons.OK, MessageBoxIcon.Error);
server = null;
return;
}

textBox1.BackColor = Color.Yellow;
textBox1.Text = "Waiting for device to connect...";
textBox8.Enabled = textBox9.Enabled = textBox10.Enabled =
textBox11.Enabled = textBox12.Enabled = false;
button4.Enabled = false;

var t = new Thread(AcceptThread) { IsBackground = true };


t.Start();
}

private void AcceptThread()


{
try
{
client = server.Accept();

Invoke(new Action(() =>


{
textBox1.BackColor = Color.Lime;
textBox1.Text = "Connected: " + client.RemoteEndPoint;
button5.Enabled = true;
button1.Enabled = button2.Enabled = button3.Enabled =
button6.Enabled = button7.Enabled = button8.Enabled = true;
}));

new Thread(Receive) { IsBackground = true }.Start();


}
catch (SocketException)
{
}
}

private void Receive()


{
try
{
while (true)
{
int len = client.Receive(datareceive);
string s = Encoding.ASCII.GetString(datareceive, 0, len);
Invoke(new Action(() =>
{
if (s.Contains("R")) pictureBox2.Image =
baiEthernet.Properties.Resources.dendo;
if (s.Contains("Y")) pictureBox2.Image =
baiEthernet.Properties.Resources.denvang;
if (s.Contains("G")) pictureBox2.Image =
baiEthernet.Properties.Resources.denxanh;
}));
}
}
catch { }
}

private void button5_Click(object sender, EventArgs e)


{
if (server != null)
{
try { server.Close(); } catch { }
server = null;
}
if (client != null)
{
try { client.Close(); } catch { }
client = null;
}

textBox1.BackColor = Color.Red;
textBox1.Text = "Disconnected";
textBox8.Enabled = textBox9.Enabled = textBox10.Enabled =
textBox11.Enabled = textBox12.Enabled = true;
button4.Enabled = true;
button5.Enabled = button1.Enabled = button2.Enabled = button3.Enabled =
button6.Enabled = button7.Enabled = button8.Enabled = false;
}

private void timer1_Tick(object sender, EventArgs e)


{
textBox2.Text = DateTime.Now.ToString("HH:mm:ss");
if (isMode3Active && !isManualMode)
{
int h = DateTime.Now.Hour;
if (h != lastHour)
{
lastHour = h;
SendMode3CommandByTime();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
isMode3Active = false;
if (!isManualMode) SendCommand("A");
}

private void button2_Click(object sender, EventArgs e)


{
isMode3Active = false;
if (!isManualMode) SendCommand("B");
}

private void button3_Click(object sender, EventArgs e)


{
if (isManualMode) return;
isMode3Active = true;
lastHour = -1;
SendMode3CommandByTime();
}

private void button6_Click(object sender, EventArgs e)


{
isManualMode = true;
isMode3Active = false;
button1.Enabled = button2.Enabled = button3.Enabled = false;
textBox5.Text = "Chế độ Thủ công";
SendCommand("M");
}

private void button7_Click(object sender, EventArgs e)


{
isManualMode = false;
button1.Enabled = button2.Enabled = button3.Enabled = true;
textBox5.Text = "Chế độ Giao diện";
SendCommand("G");
}

private void SendMode3CommandByTime()


{
string cmd = (DateTime.Now.Hour >= 5 && DateTime.Now.Hour < 22) ? "C" :
"B";
SendCommand(cmd);
}

private void SendCommand(string cmd)


{
if (client != null && client.Connected)
{
datasend = Encoding.ASCII.GetBytes(cmd);
client.Send(datasend, 0, datasend.Length, SocketFlags.None);
}
}

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


private void textBox8_TextChanged(object sender, EventArgs e) { }
private void textBox9_TextChanged(object sender, EventArgs e) { }
private void textBox10_TextChanged(object sender, EventArgs e) { }
private void textBox11_TextChanged(object sender, EventArgs e) { }
private void textBox12_TextChanged(object sender, EventArgs e) { }
private void pictureBox2_Click(object sender, EventArgs e) { }
}
}

You might also like