基本信息
源码名称:Modbus读取发送
源码大小:0.46M
文件格式:.zip
开发语言:C#
更新时间:2019-03-22
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using Modbus.Device;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private ModbusIpMaster Master;
private TcpClient tcpClient;
private ushort startAddress;
private ushort numOfPoints;
public Form1()
{
InitializeComponent();
}
private void Button5_Click(object sender, EventArgs e)
{
string ipAddress = conText.Text;
tcpClient = new TcpClient();
IAsyncResult asynResult = tcpClient.BeginConnect(ipAddress, 502, null, null);
asynResult.AsyncWaitHandle.WaitOne(2000, true);
if (asynResult.IsCompleted && IsOnline())
{
Master = ModbusIpMaster.CreateIp(tcpClient);
conLab.Text = "已连接";
conLab.ForeColor = System.Drawing.Color.Green;
}
else
{
conLab.Text = "连接失败";
conLab.ForeColor = System.Drawing.Color.Red;
}
}
private void ShowData(ushort[] obj)
{
string ret = "";
foreach (var item in obj)
{
ret = ret item "\r\n";
}
retText.Text = ret;
}
private void ShowData(bool[] obj)
{
string ret = "";
foreach (var item in obj)
{
ret = ret item "\r\n";
}
retText.Text = ret;
}
private void ReadCoilsBtn_Click(object sender, EventArgs e)
{
if (!IsOnline())
{
conLab.Text = "连接断开";
conLab.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("Modbus连接已断开,请重连");
return;
}
startAddress = Convert.ToUInt16(addressText.Text);
ushort numOfPoints = Convert.ToUInt16(lehText.Text);
bool[] Coils = Master.ReadCoils(startAddress,
numOfPoints);
ShowData(Coils);
}
private void ReadHoldBtn_Click(object sender, EventArgs e)
{
if (!IsOnline())
{
conLab.Text = "连接断开";
conLab.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("Modbus连接已断开,请重连");
return;
}
startAddress = Convert.ToUInt16(addressText.Text);
ushort numOfPoints = Convert.ToUInt16(lehText.Text);
ushort[] holding_register = Master.ReadHoldingRegisters(startAddress,
numOfPoints);
this.ShowData(holding_register);
}
private void WriteCoilsBtn_Click(object sender, EventArgs e)
{
if (!IsOnline())
{
conLab.Text = "连接断开";
conLab.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("Modbus连接已断开,请重连");
return;
}
startAddress = Convert.ToUInt16(addressText.Text);
bool[] values = GetBoolDate();
Master.WriteMultipleCoils(startAddress, values);
}
private void WriteRegistersBtn_Click(object sender, EventArgs e)
{
if (!IsOnline())
{
conLab.Text = "连接断开";
conLab.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("Modbus连接已断开,请重连");
return;
}
startAddress = Convert.ToUInt16(addressText.Text);
ushort[] values = GetShortDate() ;
Master.WriteMultipleRegisters(startAddress,values);
}
private ushort[] GetShortDate()
{
string[] values = valText.Text.Split(',');
ushort[] values2 = new ushort[values.Length];
for (int i = 0; i < values.Length; i )
{
if (String.IsNullOrEmpty(values[i]))
continue;
values2[i] = Convert.ToUInt16(values[i]);
}
return values2;
}
private bool[] GetBoolDate()
{
string[] values = valText.Text.Split(',');
bool[] values2 = new bool[values.Length];
for (int i = 0; i < values.Length; i )
{
if (String.IsNullOrEmpty(values[i]))
continue;
values2[i] = bool.Parse(values[i]);
}
return values2;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (Master != null)
Master.Dispose();
}
private Boolean IsOnline()
{
return !((tcpClient.Client.Poll(1000, SelectMode.SelectRead)
&& (tcpClient.Client.Available == 0)) || !tcpClient.Client.Connected);
}
}
}