Form1 Bai3KTTN - Cs
Form1 Bai3KTTN - Cs
Form1 Bai3KTTN - Cs
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;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO.Ports;
using System.Windows.Forms.DataVisualization.Charting;
namespace Bai3KTTN
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private const int READ_BUFFER_SIZE = 50;
private const int WRITE_BUFFER_SIZE = 50;
private byte[] bufferReceiver = null;
private byte[] bufferSender = null;
private Socket mSocket = null;
int counter = 0;
double AI;
int ctnQ00 = 0, ctnQ01 = 0;
public void Connect()
{
this.mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.bufferReceiver = new byte[READ_BUFFER_SIZE];
this.bufferSender = new byte[WRITE_BUFFER_SIZE];
this.mSocket.SendBufferSize = READ_BUFFER_SIZE;
this.mSocket.ReceiveBufferSize = WRITE_BUFFER_SIZE;
IPEndPoint sever = new IPEndPoint(IPAddress.Parse(txtIPAddress.Text), Convert.ToInt16(txtPort.
Text));
this.mSocket.Connect(sever);
}
chartArea.AxisX.Minimum = 0;
chartArea.AxisY.Minimum = 0;
chartArea.AxisX.Maximum = 100;
chartArea.AxisY.Maximum = 12;
chartArea.AxisX.Interval = 20;
chartArea.AxisY.Interval = 20;
chart1.Series[0].ChartType = SeriesChartType.Line;
chart1.Series[0].Color = Color.Red;
chart1.Series[0].BorderWidth = 2;
chart1.Series[0].IsVisibleInLegend = false;
}
D:\STUDY-BKHCM\2022-2023-HK2\DoluongvaDieukhienMT\...\Bai3KTTN\C#\GUIBai3KTTN\Bai3KTTN\Bai3KTTN\Form1.cs 5
//Connecting to PLC
private void butConnect_Click(object sender, EventArgs e)
{
try
{
Connect();
label9.Text = "Connected";
butConnect.Enabled = false;
//Enable timer1
timer1.Enabled = true;
}
catch (Exception ex)
{
label9.Text = "IP Address of Port not Available";
}
}
//Disconnecting to PLC
private void ForMain_FormClosing(object sender, FormClosedEventArgs e)
{
try
{
Disconnect();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Draw chart
PlotChart((double)timer1.Interval / 1000.0 * (double)counter, AI);
counter++;
}
//Co the bo qua doan code check loi nay cho nhanh, neu khong thi cu viet lai
//txtError.Text = "OK";
/*Thread.Sleep(20);
byte[] buffReceiver = this.Read();
int SizeByte = bufferReceiver[8];
byte[] byteMsg = null;
if (function != bufferReceiver[7])
{
byte[] errorbytes = new byte[3];
Array.Copy(bufferReceiver, 6, errorbytes, 0, errorbytes.Length);
this.CheckValidate(errorbytes);
byteMsg = new byte[9];
Array.Copy(bufferReceiver, 0, byteMsg, 0, byteMsg.Length);
}
else
{
byteMsg = new byte[READ_BUFFER_SIZE];
Array.Copy(bufferReceiver, 0, byteMsg, 0, byteMsg.Length);
}
//txtRecMess.Text = Display(byteMsg);*/
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Co the bo qua doan code check loi nay cho nhanh, neu khong thi cu viet lai
//txtError.Text = "OK";
/*Thread.Sleep(20);
byte[] buffReceiver = this.Read();
int SizeByte = bufferReceiver[8];
byte[] byteMsg = null;
if (function != bufferReceiver[7])
{
byte[] errorbytes = new byte[3];
Array.Copy(bufferReceiver, 6, errorbytes, 0, errorbytes.Length);
this.CheckValidate(errorbytes);
byteMsg = new byte[9];
Array.Copy(bufferReceiver, 0, byteMsg, 0, byteMsg.Length);
}
else
{
byteMsg = new byte[READ_BUFFER_SIZE];
Array.Copy(bufferReceiver, 0, byteMsg, 0, byteMsg.Length);
}
//txtRecMess.Text = Display(byteMsg);*/
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Co the bo qua doan code check loi nay cho nhanh, neu khong thi cu viet lai
//txtError.Text = "OK";
/*Thread.Sleep(20);
byte[] buffReceiver = this.Read();
int SizeByte = bufferReceiver[8];
byte[] byteMsg = null;
if (function != bufferReceiver[7])
{
byte[] errorbytes = new byte[3];
Array.Copy(bufferReceiver, 6, errorbytes, 0, errorbytes.Length);
this.CheckValidate(errorbytes);
byteMsg = new byte[9];
Array.Copy(bufferReceiver, 0, byteMsg, 0, byteMsg.Length);
}
else
{
byteMsg = new byte[READ_BUFFER_SIZE];
Array.Copy(bufferReceiver, 0, byteMsg, 0, byteMsg.Length);
}
D:\STUDY-BKHCM\2022-2023-HK2\DoluongvaDieukhienMT\...\Bai3KTTN\C#\GUIBai3KTTN\Bai3KTTN\Bai3KTTN\Form1.cs 9
//txtRecMess.Text = Display(byteMsg);*/
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}