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 S7.Net;
using System.Threading;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Net;
namespace S71
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void skr()
{
while (true)
{
byte[] bb = new byte[1024];
int a = PLC.Sk.Receive(bb);
// string str = BitConverter.ToString(bb,0,a);
string str = System.Text.Encoding.Default.GetString(bb,0,a);
textBox1.AppendText(str+"\r\n");
}
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
PLC.PLc = new Plc(CpuType.S71200,"192.168.0.10",0,1);
PLC.PLc.Open();
textBox1.AppendText("PLC已经成功连接\r\n");
PLC.Sk = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress Ip = IPAddress.Parse("192.168.0.10");
int Port = 2000;
IPEndPoint pop = new IPEndPoint(Ip,Port);
PLC.Sk.Connect(pop);
textBox1.AppendText("socket 连接完成\r\n");
// 线程生成
Thread th = new Thread(skr);
th.IsBackground = true;
th.Start();
}
private void button1_Click(object sender, EventArgs e)
{
PLC.PLc.Write("M0.0",true);
}
private void button2_Click(object sender, EventArgs e)
{
PLC.PLc.Write("M0.1", true);
}
private void button3_Click(object sender, EventArgs e)
{
PLC.PLc.Write("M0.2", true);
}
private void button6_Click(object sender, EventArgs e)
{
PLC.PLc.Write("M0.0", false);
}
private void button5_Click(object sender, EventArgs e)
{
PLC.PLc.Write("M0.1", false);
}
private void button4_Click(object sender, EventArgs e)
{
PLC.PLc.Write("M0.2", false);
}
private void button7_Click(object sender, EventArgs e)
{
ushort A = Convert.ToUInt16(PLC.PLc.Read("DB1.DBW0"));
textBox2.Text = A.ToString();
ushort B = Convert.ToUInt16(PLC.PLc.Read("DB1.DBW2"));
textBox3.Text = B.ToString();
ushort C = Convert.ToUInt16(PLC.PLc.Read("DB1.DBW4"));
textBox4.Text =C.ToString();
textBox5.Text=((uint)PLC.PLc.Read("DB1.DBD6")).ConvertToFloat().ToString();
textBox1.AppendText("数据读取成功成功\r\n");
}
private void button8_Click(object sender, EventArgs e)
{
ushort A = Convert.ToUInt16(textBox2.Text.Trim());
ushort B= Convert.ToUInt16(textBox3.Text.Trim());
ushort C = Convert.ToUInt16(textBox4.Text.Trim());
Single D = Convert.ToSingle(textBox5.Text.Trim());
PLC.PLc.Write("DB1.DBW0",A);
Thread.Sleep(100);
PLC.PLc.Write("DB1.DBW2", B);
Thread.Sleep(100);
PLC.PLc.Write("DB1.DBW4", C);
Thread.Sleep(100);
PLC.PLc.Write("DB1.DBD6", D);
textBox1.AppendText("数据写入成功\r\n");
}
private void button9_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}