You are on page 1of 5

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 SymbolFactoryDotNet;
using S7.Net;
using S7.Net.Types;

namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Plc plc = new Plc(CpuType.S71200, "127.0.0.1", 0, 0);
byte[] color = new byte[20];

private void Form1_Load(object sender, EventArgs e)


{
if (plc.Open() == ErrorCode.NoError)
{
MessageBox.Show("Connect Successfully !", "Nofitication",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Cannot connect to PLC !!!", "Nofitication",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
timer1.Interval = 500;
timer1.Enabled = true;

private void On_Off(StandardControl sd, bool value)


{
if (value == true)
{
sd.DiscreteValue1 = true;
sd.DiscreteValue2 = false;
}
else
{
sd.DiscreteValue2 = true;
sd.DiscreteValue1 = false;
}
}

private void btnOn_Click(object sender, EventArgs e)


{
plc.Write("DB1.DBX0.0", 1);
plc.Write("DB1.DBX0.0", 0);
}

private void btnOff_Click(object sender, EventArgs e)


{

plc.Write("DB1.DBX0.1", 1);
plc.Write("DB1.DBX0.1", 0);
}

private void btnOn_Sensor_High_Click(object sender, EventArgs e)


{

plc.Write("DB1.DBX0.7", 1);
}

private void btnOff_Sensor_High_Click(object sender, EventArgs e)


{
plc.Write("DB1.DBX0.7", 0);
}

private void btnOn_Sensor_Med_Click(object sender, EventArgs e)


{
plc.Write("DB1.DBX1.0", 1);
}

private void btnOff_Sensor_Med_Click(object sender, EventArgs e)


{
plc.Write("DB1.DBX1.0", 0);
}

private void btnOn_Sensor_Low_Click(object sender, EventArgs e)


{
plc.Write("DB1.DBX1.1", 1);
}

private void btnOff_Sensor_Low_Click(object sender, EventArgs e)


{
plc.Write("DB1.DBX1.1", 0);
}

private void btnOn_Sensor_Bottle_Click(object sender, EventArgs e)


{
plc.Write("DB1.DBX1.2", 1);
}

private void btnOff_Sensor_Bottle_Click(object sender, EventArgs e)


{
plc.Write("DB1.DBX1.2", 0);
}

private void timer1_Tick(object sender, EventArgs e)


{
txtTimer1.Text = plc.Read("DB1.DBD2").ToString();
txtTimer2.Text = plc.Read("DB1.DBD10").ToString();
color = plc.ReadBytes(DataType.DataBlock, 1, 0, 2);
if (color[1].SelectBit(1) == true) //Sensor_Low
{
On_Off(sdSensor_Low, true);
}
else
{
On_Off(sdSensor_Low, false);
}

if (color[0].SelectBit(2) == true) //Valve1


{
On_Off(standardControl2, true);
On_Off(standardControl4, true);
On_Off(sdValve1, true);
On_Off(standardControl9, true);
}
else
{
On_Off(standardControl2, false);
On_Off(standardControl4, false);
On_Off(sdValve1, false);
On_Off(standardControl9, false);
}

if (color[1].SelectBit(0) == true) //Sensor_Medium


{
On_Off(sdSensor_Med, true);
}
else
{
On_Off(sdSensor_Med, false);
}

if (color[0].SelectBit(3) == true) //Valve2


{
On_Off(standardControl3, true);
On_Off(standardControl6, true);
On_Off(sdValve2, true);
On_Off(standardControl10, true);
}
else
{
On_Off(standardControl3, false);
On_Off(standardControl6, false);
On_Off(sdValve2, false);
On_Off(standardControl10, false);
}

if (color[0].SelectBit(7) == true) //Sensor_High


{
On_Off(sdSensor_High, true);
}
else
{
On_Off(sdSensor_High, false);
}

if (color[0].SelectBit(5) == true) //Motor


{
On_Off(sdTank, true);
}
else
{
On_Off(sdTank, false);
}

if (color[0].SelectBit(6) == true) //Conveyor


{
On_Off(sdConveyor, true);
}
else
{
On_Off(sdConveyor, false);
}

if (color[1].SelectBit(2) == true) //Sensor_Bottle


{
On_Off(sdSensor_Bottle, true);
On_Off(sdBottle, true);
}
else
{
On_Off(sdSensor_Bottle, false);
On_Off(sdBottle, false);
}

if (color[0].SelectBit(4) == true) //Valve3


{
On_Off(sdValve3, true);
On_Off(standardControl7, true);
On_Off(sdProduct, true);
}
else
{
On_Off(sdValve3, false);
On_Off(standardControl7, false);
On_Off(sdProduct, false);
}
}
}
}

You might also like