1、介绍
上节文章已经完成了通讯库的开发,可以看到库还是蛮厉害的,在项目中就可以直接拿来应用,这节要做的就是做一个工具软件,形成自己专业的通讯工具,也是对通讯库的直接利用,本节要写的工具软件是一个winform程序,因为控制台程序是单调的,不方便操作,不直观。
2、开始干
1、添加窗体程序
2、引用通讯库文件.dll
将上节生成的dll文件复制到本项目中
3、连接代码
using Omron.Communimcation.Fins.Omron;
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;
namespace OmronFinsFormApp
{
public partial class Form1 : Form
{
/// <summary>
/// finstcp对象
/// </summary>
FinsTcp finsTcp;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 连接Plc
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpen_Click(object sender, EventArgs e)
{
string ip = txtIPAddress.Text.Trim();
string port = txtPort.Text.Trim();
finsTcp = new FinsTcp(ip, Convert.ToInt32(port), (byte)10, (byte)04);// 创建连接
var result = finsTcp.Connect();// 开始连接PLC
if (!result.IsSuccessed)
{
MessageBox.Show(result.Message);
return;
}
lblMessage.Text = "PLC连接成功";
SetTextDisable(true);
this.btnOpen.Enabled = false;
this.btnClose.Enabled = true;
}
private void SetTextDisable(bool v)
{
txtIPAddress.ReadOnly = v;
txtPort.ReadOnly = v;
}
}
}
4、读取代码
/// <summary>
/// 读取数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void myread_Click(object sender, EventArgs e)
{
//内存地址
string plcAddr = txtMemoryAddr.Text.Trim( );
//读取数量
ushort readCount = ushort.Parse(txtRCount.Text.Trim( ));
//数据类型
string dataType = cbodatatype.Text.Trim();
switch (dataType)
{
case "ushort":
var datas= finsTcp.Read<ushort>(plcAddr, readCount);
if (!datas.IsSuccessed)
{
MessageBox.Show(datas.Message);
return;
}
txtReadDatas1.Text = string.Join(",", datas.Datas);
break;
case "short":
var datas2 = finsTcp.Read<short>(plcAddr, readCount);
if (!datas2.IsSuccessed)
{
MessageBox.Show(datas2.Message);
return;
}
txtReadDatas2.Text = string.Join(",", datas2.Datas);
break;
case "float":
var datas3 = finsTcp.Read<float>(plcAddr, readCount);
if (!datas3.IsSuccessed)
{
MessageBox.Show(datas3.Message);
return;
}
txtReadDatas3.Text = string.Join(",", datas3.Datas);
break;
case "bool":
var datas4 = finsTcp.Read<bool>(plcAddr, readCount);
if (!datas4.IsSuccessed)
{
MessageBox.Show(datas4.Message);
return;
}
txtReadDatas4.Text = string.Join(",", datas4.Datas);
break;
}
}
通讯报文
5、写入代码
/// <summary>
/// 写入数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnWrite_Click(object sender, EventArgs e)
{
//PLC地址
string plcAddr = txtWPLCAddr.Text.Trim();
//数据类型
string dataType = cmddatatype2.Text.Trim();
//实际数量
string objWriteVals = "";
switch (dataType)
{
case "ushort":
objWriteVals = txtWriteDatas1.Text.Trim();
break;
case "short":
objWriteVals = txtWriteDatas2.Text.Trim();
break;
case "float":
objWriteVals = txtWriteDatas3.Text.Trim();
break;
case "bool":
objWriteVals = txtWriteDatas4.Text.Trim();
break;
}
//写入数量
ushort writeCount = ushort.Parse(txtWPLCCount.Text.Trim());
ushort objWCount = (ushort)objWriteVals.Split(',').Length;
//实际数量与要求数量不一致,不允许操作
if (writeCount != objWCount)
{
MessageBox.Show("写入值的数量不正确!");
return;
}
List<string> vals = objWriteVals.Split(',').ToList();
switch (dataType)
{
case "ushort":
//实际数值转换成list集合ushort类型
List<ushort> objushort = new List<ushort>();
vals.ForEach((x) =>
{
objushort.Add(ushort.Parse(x));
});
var finish1 = finsTcp.Write<ushort>(objushort, plcAddr);
if (finish1.IsSuccessed)
{
MessageBox.Show(finish1.Message);
}
break;
case "short":
//实际数值转换成list集合 short类型
List<short> objshort = new List<short>();
vals.ForEach((x) =>
{
objshort.Add(short.Parse(x));
});
var finish2 = finsTcp.Write<short>(objshort, plcAddr);
if (finish2.IsSuccessed)
{
MessageBox.Show(finish2.Message);
}
break;
case "float":
//实际数值转换成list集合 float
List<float> objfloat = new List<float>();
vals.ForEach((x) =>
{
objfloat.Add(float.Parse(x));
});
var finish3 = finsTcp.Write<float>(objfloat, plcAddr);
if (finish3.IsSuccessed)
{
MessageBox.Show(finish3.Message);
}
break;
case "bool":
//实际数值转换成list集合bool
List<bool> objbool = new List<bool>();
vals.ForEach((x) =>
{
if (x == "1")
{
objbool.Add(true);
}
else
{
objbool.Add(false);
}
});
var finish4 = finsTcp.Write<bool>(objbool, plcAddr);
if (finish4.IsSuccessed)
{
MessageBox.Show(finish4.Message);
}
break;
}
}
最后看下报文
3、小结
通讯库100%正确,实现了bool,ushort,short,float类型数据的读取和写入,可以看到封装得超爽,有这个工具,到项目开发中就可以用得上这个工具软件,用来调试测试PLC程序,打通与电气工程师的通讯数据,超强啊。
写作不易,图文不易,思路不易,打字不易,分享不易,多多点赞。