Form1 Bai4KTTN - Cs
Form1 Bai4KTTN - Cs
Form1 Bai4KTTN - 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 pv;
double sp;
ushort pwm;
bool ctrlen;
int countctrl;
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;
D:\STUDY-BKHCM\2022-2023-HK2\DoluongvaDieukhienMT\...\Bai4KTTN\C#\GUIBai3KTTN\Bai3KTTN\Bai3KTTN\Form1.cs 5
chart1.Series[0].BorderWidth = 2;
chart1.Series[0].IsVisibleInLegend = false;
}
//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);
}
}
//Timer1 for READING actual value, draw chart1 and run ON/OFF controller
private void timer1_Tick(object sender, EventArgs e)
{
//Draw chart
PlotChart((double)timer1.Interval / 1000.0 * (double)counter, pv);
counter++;
if (ctrlen == false)
{
pwm = 0;
}
try
{
byte slaveAddress = 1;
byte function = 16;
ushort id = function;
ushort startAddress = 5;
D:\STUDY-BKHCM\2022-2023-HK2\DoluongvaDieukhienMT\...\Bai4KTTN\C#\GUIBai3KTTN\Bai3KTTN\Bai3KTTN\Form1.cs 7
ushort value1;
value1 = pwm;
ushort[] input = new ushort[1] { value1 };
byte[] values = ConvertIntArrayToByteArray(input);
//txtError.Text = string.Empty;
byte[] frame = WriteMultipleRegistersMsg(id, slaveAddress, startAddress, function,
values);
//txtSendMess.Text = Display(frame);
int code = this.Write(frame);
//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);
}
}
}
}