上位机软件与PLC通过ProfiNet工业现场总线通讯案例,包括通讯连接模块,系统参数设置模块,不同存储区的读写模块,安全机制模块,界面定制化模块 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using S7.Net;
namespace PrjProfiNetDebug
{
public partial class frmMain : Form
{
S7.Net.Plc PLCType;
int iPLCType;
short iPLCRack;
short iPLCSlot;
string strPLCIPAddress;
bool bLinkStatus;
string strDateTime;
string strPLCType;
string strLinkStatus;
int iDBNumber = 1; //数据块的编号
int iAddressStart = 0; //数据读取的起始地址
int iDataCount = 0; //数据读取的数量
int iVarType = -1; //数据类型
string[] strAddress = null; //地址显示数组
int iBitSelected; //位号
string strCommand = ""; //指令字符串
string strDBNumber = ""; //DB号字符串
string strAStart = ""; //起始地址字符串
string strBit = ""; //位号字符串
bool bSReadValue = true; //位单个读
byte bytSReadValue; //字节单个读
ushort wSReadValue; //字单个读
uint dwSReadValue; //双子单个读
short intSReadValue; //整型单个读
int diSReadValue; //双整型单个读
double realSReadValue; //实数单个读
bool bSWriteValue = true; //位单个写
byte bytSWriteValue; //字节单个写
ushort wSWriteValue; //字单个写
uint dwSWriteValue; //双子单个写
short intSWriteValue; //整型单个写
int diSWriteValue; //双整型单个写
double realSWriteValue; //实数单个写
object[] objAReadValue = null; //对象数组多个读
object[] objBitAReadValue = null; //对象数组位多个读
bool[] bAReadValue = null; //布尔数组位多个读
byte[] bTBReadValue = null; //字节数组位多个读_位转字节
byte[] bytAReadValue = null; //字节数组位多个读
ushort[] wAReadValue = null; //字数组位多个读
uint[] dwAReadValue = null; //双字d数组位多个读
short[] intAReadValue = null; //整型数组位多个读
int[] diAReadValue = null; //双整型数组位多个读
double[] rAReadValue = null; //实数数组位多个读
object[] objAWriteValue = null; //对象数组多个写
object[] objBitAWriteValue = null; //对象数组位多个写
bool[] bAWriteValue = null; //布尔数组位多个写
byte[] bTBWriteValue = null; //字节数组位多个写_位转字节
byte[] bytAWriteValue = null; //字节数组位多个写
ushort[] wAWriteValue = null; //字数组位多个写
uint[] dwAWriteValue = null; //双字d数组位多个写
short[] intAWriteValue = null; //整型数组位多个写
int[] diAWriteValue = null; //双整型数组位多个写
double[] rAWriteValue = null; //实数数组位多个写
DataGridViewColumn dgvColProfiNetAddress = new DataGridViewColumn();
DataGridViewColumn dgvColProfiNetValue = new DataGridViewColumn();
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
cboPLCType.SelectedIndex = 3;
cboPLCIPAddress.SelectedIndex = 0;
txtPLCRack.Text = "0";
txtPLCSlot.Text = "1";
timMain.Enabled = true;
timMain.Interval = 1000;
cboVarType.SelectedIndex = 2;
cboBit.SelectedIndex = 0;
lblBit.Visible = false;
cboBit.Visible = false;
dgvColProfiNetAddress.Name = "ProfiNetAddress";
dgvColProfiNetAddress.HeaderText = "地址编号";
dgvColProfiNetAddress.CellTemplate = new DataGridViewTextBoxCell();
dgvColProfiNetValue.Name = "ProfiNetValue";
dgvColProfiNetValue.HeaderText = "值";
dgvColProfiNetValue.CellTemplate = new DataGridViewTextBoxCell();
dgvShow.Columns.Add(dgvColProfiNetAddress);
dgvShow.Columns.Add(dgvColProfiNetValue);
dgvShow.Columns[0].ReadOnly = true;
//dgvDisplay.ColumnHeadersVisible = false;
dgvShow.RowHeadersVisible = false;
dgvShow.BackgroundColor = Color.WhiteSmoke;
//dgvDisplay.Columns[1].ToolTipText = "该列只能输入数字";
dgvShow.AllowUserToAddRows = false;
}
private void btnConnect_Click(object sender, EventArgs e)
{
iPLCType = cboPLCType.SelectedIndex;
strPLCIPAddress = cboPLCIPAddress.Text.ToString();
iPLCRack = Convert.ToInt16(txtPLCRack.Text);
iPLCSlot = Convert.ToInt16(txtPLCSlot.Text);
switch (iPLCType)
{
case 0:
{
PLCType = new S7.Net.Plc(CpuType.S7200,strPLCIPAddress ,iPLCRack ,iPLCSlot);
break;
}
case 1:
{
PLCType = new S7.Net.Plc(CpuType.S7300, strPLCIPAddress, iPLCRack, iPLCSlot);
break;
}
case 2:
{
PLCType = new S7.Net.Plc(CpuType.S7400, strPLCIPAddress, iPLCRack, iPLCSlot);
break;
}
case 3:
{
PLCType = new S7.Net.Plc(CpuType.S71200, strPLCIPAddress, iPLCRack, iPLCSlot);
break;
}
case 4:
{
PLCType = new S7.Net.Plc(CpuType.S71500, strPLCIPAddress, iPLCRack, iPLCSlot);
break;
}
}
try
{
if (!bLinkStatus)
{
PLCType.Open();
if (PLCType.IsConnected)
{
bLinkStatus = true;
btnConnect.Enabled = false;
btnDisconnect.Enabled = true;
//MessageBox.Show("通讯连接正常", "ProfiNet现场总线");
}
else
{
MessageBox.Show("通讯连接失败", "ProfiNet现场总线");
}
}
else
{
//MessageBox.Show("通讯已连接", "ProfiNet现场总线");
}
}
catch
{
MessageBox.Show(e.ToString(), "ProfiNet-现场总线", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
try
{
if (bLinkStatus)
{
PLCType.Close();
bLinkStatus = false;
btnConnect.Enabled = true;
btnDisconnect.Enabled = false;
//MessageBox.Show("通讯连接关闭", "ProfiNet现场总线");
}
else
{
//MessageBox.Show("通讯连接已断开", "ProfiNet现场总线");
}
}
catch
{
MessageBox.Show(e.ToString(), "ProfiNet-现场总线", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
private void txtPLCRack_TextChanged(object sender, EventArgs e)
{
int iCheckPLCRack;
if (int.TryParse(txtPLCRack.Text, out iCheckPLCRack) == false)
{
txtPLCRack.Text = "0";
MessageBox.Show("PLC机架号-必须输入数字", "ProfiNet-现场总线", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
}
private void txtPLCSlot_TextChanged(object sender, EventArgs e)
{
int iCheckPLCSlot;
if (int.TryParse(txtPLCSlot.Text, out iCheckPLCSlot) == false)
{
txtPLCSlot.Text = "1";
MessageBox.Show("PLC插槽号-必须输入数字", "ProfiNet-现场总线", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
}
private void timMain_Tick(object sender, EventArgs e)
{
strDateTime = DateTime .Now.ToString();
tssDateTime.Text = "日期:" strDateTime;
if (bLinkStatus)
{
strLinkStatus = "网络状态:通讯正常";
tssLinkStatus.Text = strLinkStatus;
}
else
{
strLinkStatus="网络状态:通讯中断";
tssLinkStatus.Text = strLinkStatus;
}
strPLCType = cboPLCType.Text.ToString();
tssPLCType.Text = "PLC型号:" strPLCType;
iVarType = cboVarType.SelectedIndex;
if (iVarType == 0)
{
lblBit.Visible = true;
cboBit.Visible = true;
}
else
{
lblBit.Visible = false;
cboBit.Visible = false;
}
}
private void btnSingleRead_Click(object sender, EventArgs e)
{
if (bLinkStatus == false)
{
MessageBox.Show("网络状态-连接断开!", "网络侦测");
return;
}
strDBNumber = txtDBNumber.Text.Trim().ToString();
strAStart = txtAddressStart.Text.Trim().ToString();
iVarType = cboVarType.SelectedIndex;
switch (iVarType)
{
case 0:
{
iBitSelected = cboBit.SelectedIndex;
strBit = iBitSelected.ToString();
strCommand = "DB" strDBNumber "." "DBX" strAStart "." strBit;
bSReadValue = (bool)PLCType.Read(strCommand);
txtSingleReadValue.Text = bSReadValue.ToString();
break;
}
case 1:
{
strCommand = "DB" strDBNumber "." "DBB" strAStart;
bytSReadValue = (byte)PLCType.Read(strCommand);
txtSingleReadValue.Text = bytSReadValue.ToString();
break;
}
case 2:
{
strCommand = "DB" strDBNumber "." "DBW" strAStart;
wSReadValue = (ushort)PLCType.Read(strCommand);
txtSingleReadValue.Text = wSReadValue.ToString();
break;
}
case 3:
{
strCommand = "DB" strDBNumber "." "DBD" strAStart;
dwSReadValue = (uint)PLCType.Read(strCommand);
txtSingleReadValue.Text = dwSReadValue.ToString();
break;
}
case 4:
{
strCommand = "DB" strDBNumber "." "DBW" strAStart;
intSReadValue = ((ushort)PLCType.Read(strCommand)).ConvertToShort();
txtSingleReadValue.Text = intSReadValue.ToString();
break;
}
case 5:
{
strCommand = "DB" strDBNumber "." "DBD" strAStart;
diSReadValue = ((uint)PLCType.Read(strCommand)).ConvertToInt();
txtSingleReadValue.Text = diSReadValue.ToString();
break;
}
case 6:
{
strCommand = "DB" strDBNumber "." "DBD" strAStart;
realSReadValue = ((uint)PLCType.Read(strCommand)).ConvertToDouble();
txtSingleReadValue.Text = realSReadValue.ToString();
break;
}
}
}
private void btnSingleWrite_Click(object sender, EventArgs e)
{
if (bLinkStatus == false)
{
MessageBox.Show("网络状态-连接断开!", "网络侦测");
return;
}
strDBNumber = txtDBNumber.Text.Trim().ToString();
strAStart = txtAddressStart.Text.Trim().ToString();
iVarType = cboVarType.SelectedIndex;
switch (iVarType)
{
case 0:
{
iBitSelected = cboBit.SelectedIndex;
strBit = iBitSelected.ToString();
strCommand = "DB" strDBNumber "." "DBX" strAStart "." strBit;
if ((txtSingleWriteValue.Text).ToString().ToLower() != "true" && (txtSingleWriteValue.Text).ToString().ToLower() != "false")
{
MessageBox.Show("写入值要为false或true", "格式不符");
return;
}
bSWriteValue = Convert.ToBoolean(txtSingleWriteValue.Text);
PLCType.Write(strCommand, bSWriteValue);
break;
}
case 1:
{
strCommand = "DB" strDBNumber "." "DBB" strAStart;
bytSWriteValue = Convert.ToByte(txtSingleWriteValue.Text);
PLCType.Write(strCommand, bytSWriteValue);
break;
}
case 2:
{
strCommand = "DB" strDBNumber "." "DBW" strAStart;
wSWriteValue = Convert.ToUInt16(txtSingleWriteValue.Text);
PLCType.Write(strCommand, wSWriteValue);
break;
}
case 3:
{
strCommand = "DB" strDBNumber "." "DBD" strAStart;
dwSWriteValue = Convert.ToUInt32(txtSingleWriteValue.Text);
PLCType.Write(strCommand, dwSWriteValue);
break;
}
case 4:
{
strCommand = "DB" strDBNumber "." "DBW" strAStart;
intSWriteValue = Convert.ToInt16(txtSingleWriteValue.Text);
PLCType.Write(strCommand, intSWriteValue);
break;
}
case 5:
{
strCommand = "DB" strDBNumber "." "DBD" strAStart;
diSWriteValue = Convert.ToInt32(txtSingleWriteValue.Text);
PLCType.Write(strCommand, diSWriteValue);
break;
}
case 6:
{
strCommand = "DB" strDBNumber "." "DBD" strAStart;
realSWriteValue = Convert.ToDouble(txtSingleWriteValue.Text).ConvertToUInt();
PLCType.Write(strCommand, realSWriteValue);
break;
}
}
}
private void btnMulRead_Click(object sender, EventArgs e)
{
string strAddressFlag = "";
int intAddressInitial = 0;
int iIncreaseStep = 0;
int iRowCount = 0;
int i, j, t;
if (bLinkStatus == false)
{
MessageBox.Show("网络状态-连接断开!", "网络侦测");
return;
}
iDBNumber = Convert.ToInt16(txtDBNumber.Text.ToString());
iAddressStart = Convert.ToInt16(txtAddressStart.Text.ToString());
iDataCount = Convert.ToInt16(txtDataCount.Text.ToString());
iVarType = cboVarType.SelectedIndex;
//清空数据网格视图里的数据
iRowCount = dgvShow.Rows.Count;
if (iRowCount != 0)
{
for (j = 0; j < iRowCount; j )
{
dgvShow.Rows.RemoveAt(0);
}
}
//数据安全防护
if (iDBNumber < 1)
{
MessageBox.Show("ProfiNet-DB的号不能小于1");
txtDBNumber.Text = "1";
}
if (iAddressStart < 0)
{
MessageBox.Show("ProfiNet-起始地址不能为负数");
txtAddressStart.Text = "0";
}
if (iDataCount < 2)
{
MessageBox.Show("ProfiNet-数据长度不能小于2");
txtDataCount.Text = "10";
}
//数据读操作
switch (iVarType)
{
case 0:
{
break;
}
case 1:
{
strAddressFlag = "DBByte";
iIncreaseStep = 1;
bytAReadValue = (byte[])PLCType.Read(DataType.DataBlock, iDBNumber, iAddressStart, VarType.Byte, iDataCount);
break;
}
case 2:
{
strAddressFlag = "DBWord";
iIncreaseStep = 2;
wAReadValue = (ushort[])PLCType.Read(DataType.DataBlock, iDBNumber, iAddressStart, VarType.Word, iDataCount);
break;
}
case 3:
{
strAddressFlag = "DBDWord";
iIncreaseStep = 4;
dwAReadValue = (uint[])PLCType.Read(DataType.DataBlock, iDBNumber, iAddressStart, VarType.DWord, iDataCount);
break;
}
case 4:
{
strAddressFlag = "DBInt";
iIncreaseStep = 2;
intAReadValue = (short[])PLCType.Read(DataType.DataBlock, iDBNumber, iAddressStart, VarType.Int, iDataCount);
break;
}
case 5:
{
strAddressFlag = "DBDInt";
iIncreaseStep = 4;
diAReadValue = (int[])PLCType.Read(DataType.DataBlock, iDBNumber, iAddressStart, VarType.DInt, iDataCount);
break;
}
case 6:
{
strAddressFlag = "DBReal";
iIncreaseStep = 4;
rAReadValue = (double[])PLCType.Read(DataType.DataBlock, iDBNumber, iAddressStart, VarType.Real, iDataCount);
break;
}
}
//对象数据赋值
strAddress = new string[iDataCount];
objAReadValue = new object[iDataCount];
for (i = 0; i < iDataCount; i )
{
switch (iVarType)
{
case 0:
{
break;
}
case 1:
{
objAReadValue[i] = bytAReadValue[i];
break;
}
case 2:
{
objAReadValue[i] = wAReadValue[i];
break;
}
case 3:
{
objAReadValue[i] = dwAReadValue[i];
break;
}
case 4:
{
objAReadValue[i] = intAReadValue[i];
break;
}
case 5:
{
objAReadValue[i] = diAReadValue[i];
break;
}
case 6:
{
objAReadValue[i] = rAReadValue[i];
break;
}
}
}
//数据显示在数据栅格视图里
strAddress = new string[iDataCount];
for (i = 0; i < iDataCount; i )
{
intAddressInitial = iAddressStart iIncreaseStep * i;
strAddress[i] = strAddressFlag Convert.ToString(intAddressInitial);
dgvShow.Rows.Add(strAddress[i], objAReadValue[i]);
}
}
private void btnMulWrite_Click(object sender, EventArgs e)
{
int i;
int iRowCount;
ushort uiWriteReturn;
if (bLinkStatus == false)
{
MessageBox.Show("网络状态-连接断开!", "网络侦测");
return;
}
iDBNumber = Convert.ToInt16(txtDBNumber.Text.ToString());
iAddressStart = Convert.ToInt16(txtAddressStart.Text.ToString());
iDataCount = Convert.ToInt16(txtDataCount.Text.ToString());
iVarType = cboVarType.SelectedIndex;
objAWriteValue = new object[iDataCount];
//数据安全防护
if (iDBNumber < 1)
{
MessageBox.Show("ProfiNet-DB的号不能小于1");
txtDBNumber.Text = "1";
}
if (iAddressStart < 0)
{
MessageBox.Show("ProfiNet-起始地址不能为负数");
txtAddressStart.Text = "0";
}
if (iDataCount < 2)
{
MessageBox.Show("ProfiNet-数据长度不能小于2");
txtDataCount.Text = "10";
}
switch (iVarType)
{
case 0:
{
break;
}
case 1:
{
bytAWriteValue = new byte[iDataCount];
for (i = 0; i < iDataCount; i )
{
objAWriteValue[i] = dgvShow["ProfiNetValue", i].Value;
bytAWriteValue[i] = Convert.ToByte(objAWriteValue[i]);
}
uiWriteReturn = (ushort)PLCType.Write(DataType.DataBlock, iDBNumber, iAddressStart, bytAWriteValue);
break;
}
case 2:
{
wAWriteValue = new ushort[iDataCount];
for (i = 0; i < iDataCount; i )
{
objAWriteValue[i] = dgvShow["ProfiNetValue", i].Value;
wAWriteValue[i] = Convert.ToUInt16(objAWriteValue[i]);
}
uiWriteReturn = (ushort)PLCType.Write(DataType.DataBlock, iDBNumber, iAddressStart, wAWriteValue);
break;
}
case 3:
{
dwAWriteValue = new uint[iDataCount];
for (i = 0; i < iDataCount; i )
{
objAWriteValue[i] = dgvShow["ProfiNetValue", i].Value;
dwAWriteValue[i] = Convert.ToUInt32(objAWriteValue[i]);
}
uiWriteReturn = (ushort)PLCType.Write(DataType.DataBlock, iDBNumber, iAddressStart, dwAWriteValue);
break;
}
case 4:
{
intAWriteValue = new short[iDataCount];
for (i = 0; i < iDataCount; i )
{
objAWriteValue[i] = dgvShow["ProfiNetValue", i].Value;
intAWriteValue[i] = Convert.ToInt16(objAWriteValue[i]);
}
uiWriteReturn = (ushort)PLCType.Write(DataType.DataBlock, iDBNumber, iAddressStart, intAWriteValue);
break;
}
case 5:
{
diAWriteValue = new int[iDataCount];
for (i = 0; i < iDataCount; i )
{
objAWriteValue[i] = dgvShow["ProfiNetValue", i].Value;
diAWriteValue[i] = Convert.ToInt32(objAWriteValue[i]);
}
uiWriteReturn = (ushort)PLCType.Write(DataType.DataBlock, iDBNumber, iAddressStart, diAWriteValue);
break;
}
case 6:
{
rAWriteValue = new double[iDataCount];
for (i = 0; i < iDataCount; i )
{
objAWriteValue[i] = dgvShow["ProfiNetValue", i].Value;
rAWriteValue[i] = Convert.ToDouble(objAWriteValue[i]);
}
uiWriteReturn = (ushort)PLCType.Write(DataType.DataBlock, iDBNumber, iAddressStart, rAWriteValue);
break;
}
}
}
}
}
评论