1.基础配置:
2.设置本机IPv4码段相同即可。
3.c#界面设计:(已经定义IP格式,程序中可见)
4.c#管理器内引用xktcomm:
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 xktComm;
using xktComm.Common;
using xktComm.PLC;
using System.Text.RegularExpressions;
namespace 西门子plc连接
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
xktComm.SiemensS7 ss = new SiemensS7();
public bool a;
public bool b;
private void btncon_Click(object sender, EventArgs e)
{
bool blnTest;
Regex regex = new Regex("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$");
blnTest = regex.IsMatch(txtIP.Text);
if (blnTest)
{
string[] strTemp = this.txtIP.Text.Split(new char[] { '.' });
for (int i = 0; i < strTemp.Length; i++)
{
if (Convert.ToInt32(strTemp[i]) > 255)
{ //大于255则提示,不符合IP格式
MessageBox.Show("不符合IP格式");
return;
}
}
}
else
{
//输入非数字则提示,不符合IP格式
MessageBox.Show("不符合IP格式");
return;
}
string address = txtIP.Text;
CPU_Type cpu = CPU_Type.S71200;
b = ss.Connect(address, cpu, 0, 1);
if (b)
{
MessageBox.Show("链接成功");
}
else
{
MessageBox.Show("链接失败");
}
}
private void btnread_Click(object sender, EventArgs e)
{
textBox2.Text = ss.Read(textBox1.Text, 0).ToString();
}
private void btnwrite_Click(object sender, EventArgs e)
{
ss.Write(textBox3.Text, (textBox4.Text == "1"));
}
}
}
6.plc编写(点不能设置成0或1)
7.可以直接读取写入。