步骤,先连接PLC,然后在填入对应的点位 D10 然后去读取。
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 ActUtlTypeLib;
namespace MX_Component
{
public partial class Form1 : Form
{
//传教Utl控件
private ActUtlType plc;
public Form1()
{
InitializeComponent();
plc = new ActUtlType();
guna2Button2.Enabled = false;
guna2Button3.Enabled = false;
guna2Button4.Enabled = false;
}
//连接plc
private void guna2Button1_Click(object sender, EventArgs e)
{
int iReturnCode;
int iLogicalStationNumber;
if (guna2TextBox1.Text == "")
{
MessageBox.Show("The TextBox has not been entered.", Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
iLogicalStationNumber = Convert.ToInt32(this.guna2TextBox1.Text);
try
{
plc.ActLogicalStationNumber = iLogicalStationNumber;//输入站号
iReturnCode = plc.Open();//打开连接
//设置控件
if (iReturnCode == 0)
{
this.guna2Button2.Enabled = true;
this.guna2Button1.Enabled = false;
guna2TextBox1.Enabled = false;
guna2TextBox2.Enabled = false;
guna2Button3.Enabled = true;
guna2Button4.Enabled = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
//关闭连接
private void guna2Button2_Click(object sender, EventArgs e)
{
int iReturnCode;
try
{
iReturnCode = plc.Close();//关闭连接
if (iReturnCode == 0)
{
this.guna2Button2.Enabled = false;
this.guna2Button1.Enabled = true;
guna2TextBox1.Enabled = true;
guna2TextBox2.Enabled = true;
guna2Button3.Enabled = false;
guna2Button4.Enabled = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
//软元件批量读取
private void guna2Button3_Click(object sender, EventArgs e)
{
int iReturnCode;
string szDeviceName;
int iNumberOfData;
short[] arrDeviceValue;
string[] arrData;
try
{
if (guna2TextBox4.Text == null || guna2TextBox6.Text == null)//判断TextBox是否为空
{
MessageBox.Show("The TextBox has not been entered.", Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
szDeviceName = string.Join("\n", guna2TextBox4.Lines);
iNumberOfData = Convert.ToInt32(guna2TextBox6.Text);
arrDeviceValue = new short[iNumberOfData];
iReturnCode = plc.ReadDeviceBlock2(szDeviceName, iNumberOfData, out arrDeviceValue[0]);//读取数据
if (iReturnCode == 0)//判断读取结果
{
arrData = new string[iNumberOfData];
//数据分行显示到TextBox中
for (int i = 0; i < iNumberOfData; i++)
{
arrData[i] = arrDeviceValue[i].ToString();
}
guna2TextBox5.Lines = arrData;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
//批量写入数据
private void guna2Button4_Click(object sender, EventArgs e)
{
int iReturnCode;
string szDeviceName;
int iNumberOfData;
short[] lpsData;
if (guna2TextBox4.Text == null || guna2TextBox6.Text == null||guna2TextBox3.Text==null)
{
MessageBox.Show("The TextBox has not been entered.", Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
szDeviceName = guna2TextBox4.Text;
iNumberOfData = Convert.ToInt32(guna2TextBox6.Text);
lpsData = new short[iNumberOfData];
try
{
if (guna2TextBox3.Lines.Length!= iNumberOfData)
{
MessageBox.Show("The element count of the array has to be the same as the size.",
Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//待写数据转为short[]
for (int i = 0; i < iNumberOfData; i++)
{
lpsData[i] = Convert.ToInt16( guna2TextBox3.Lines[i]);
}
iReturnCode = plc.WriteDeviceBlock2(szDeviceName, iNumberOfData, ref lpsData[0]);//写入数据
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
其他参考信息
MX Component函数一览
Open 通信线路的打开
Close 通信线路的关闭
ReadDeviceBlock 软元件的批量读取 (4字节数据)
WriteDeviceBlock 软元件的批量写入 (4字节数据)
ReadDeviceRandom 软元件的随机读取 (4字节数据)
WriteDeviceRandom 软元件的随机写入 (4字节数据)
SetDevice 软元件1点的设置 (4字节数据)
GetDevice 软元件1点的数据获取 (4字节数据)
ReadBuffer 缓冲存储器的读取
WriteBuffer 缓冲存储器的写入
GetClockData CPU模块的时钟数据读取
SetClockData CPU模块的时钟数据写入
GetCpuType CPU模块型号读取
SetCpuStatus PU模块的远程RUN/STOP/PAUSE
EntryDeviceStatus 2 软元件的状态监视登录
FreeDeviceStatus 软元件的状态监视登录的解除
OnDeviceStatus 事件通知
ReadDeviceBlock2 软元件的批量读取 (2字节数据)
WriteDeviceBlock2 软元件的批量写入 (2字节数据)
ReadDeviceRandom2 软元件的随机读取 (2字节数据)
WriteDeviceRandom2 软元件的随机写入 (2字节数据)
SetDevice2 软元件1点的设置 (2字节数据)
GetDevice2 软元件1点的数据获取 (2字节数据)
GetErrorMessage 错误内容及处理方法的获取
ReadFirstFile 文件名/目录名的搜索
ReadNextFile 文件名/目录名的搜索
ReadClose 搜索的结束
GetFile 记录文件的传送
Dispose 存储器的释放