文章目标:
1.与昆仑通态触摸屏完成TCP连接
2.读写0区、3区、4区寄存
NModbus4使用手册
https://wenku.baidu.com/view/1d50bd321ed9ad51f01df2be.html
public static bool[] coil;
public static ModbusIpMaster master;
public static ushort[] holding;
public static float tex1,tex2;
using Modbus.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using Modbus.Utility;
using System.Runtime.InteropServices;
using System.IO.Ports;
using System.Xml.Linq;
using System.Data;
namespace TCP连接测试
{
public partial class Form1 : Form
{
[StructLayout(LayoutKind.Explicit, Size = 4)]//精确布局
struct U
{
[FieldOffset(0)]
public byte b0;
[FieldOffset(1)]
public byte b1;
[FieldOffset(2)]
public byte b2;
[FieldOffset(3)]
public byte b3;
[FieldOffset(0)]
public int i;
[FieldOffset(0)]
public ushort us0;
[FieldOffset(2)]
public ushort us1;
[FieldOffset(0)]
public float f;
}U WR = new U();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox9.Text = "1.完成对触摸屏的TCP连接";
textBox10.Text = "2.完成对0区、3区、4区寄存器的读写";
TcpClient tcpClient = new TcpClient();
tcpClient.BeginConnect("192.168.31.41", 3000, null, null);
CommonVariable.master = ModbusIpMaster.CreateIp(tcpClient);
//if (tcpClient != null)//判断是否连接成功
//{
// MessageBox.Show("连接成功");
//}
}
#region 读写单个0区寄存器
private void button1_Click(object sender, EventArgs e)//读取单个0区寄存器
{
CommonVariable.coil = CommonVariable.master.ReadCoils(1, 0, 2); //读取设备0_读写00001-00002
textBox1.Text = CommonVariable.coil[0].ToString();
textBox2.Text = CommonVariable.coil[1].ToString();
}
private void button2_Click(object sender, EventArgs e)//写入单个0区寄存器
{
bool value1=false,value2=false;
if (textBox6.Text == "关")
{ value1 = false; }
else
{ value1 = true; }
if (textBox5.Text == "关")
{ value2 = false; }
else
{ value2 = true; }
if (textBox6.Text == "0")
{ value1 = false; }
else
{ value1 = true; }
if (textBox5.Text == "0")
{ value2 = false; }
else
{ value2 = true; }
CommonVariable.master.WriteSingleCoil(1, 0, value1); //设备0_读写00001-----通道读写00001
CommonVariable.master.WriteSingleCoil(1, 1,value2); //设备0_读写00002-----通道读写00002
}
#endregion 读写单个0区寄存器
#region 读3区寄存器的值
private void button6_Click(object sender, EventArgs e)//读3区寄存器的值
{
CommonVariable.holding = CommonVariable.master.ReadInputRegisters(1, 0, 4);//读3区寄存器 设备0_DF0001、设备0_3DF003
//ModbusUtility.GetSingle() 该函数是将2个16位的数拼成一个32位的
CommonVariable.tex1 = ModbusUtility.GetSingle(CommonVariable.holding[0], CommonVariable.holding[1]);
CommonVariable.tex2 = ModbusUtility.GetSingle(CommonVariable.holding[2], CommonVariable.holding[3]);
textBox14.Text = CommonVariable.tex1.ToString();
textBox13.Text = CommonVariable.tex2.ToString();
}
#endregion 读3区寄存器的值
#region 读写4区保持寄存器的值
private void button3_Click(object sender, EventArgs e)//读4区保持寄存器的值
{
CommonVariable.holding = CommonVariable.master.ReadHoldingRegisters(1, 0, 4);
CommonVariable.tex1 = ModbusUtility.GetSingle(CommonVariable.holding[0], CommonVariable.holding[1]);
CommonVariable.tex2 = ModbusUtility.GetSingle(CommonVariable.holding[2], CommonVariable.holding[3]);
textBox3.Text = CommonVariable.tex1.ToString();
textBox4.Text = CommonVariable.tex2.ToString();
}
private void button4_Click(object sender, EventArgs e)//写4区保持寄存器的值
{//此处将读写文本框分开,可将读写文本框合并 测试阶段分开比较方便
ushort[] registersNUM = new ushort[28];
//写参数到下位机
if (textBox7.Text == "")
{ WR.f = 0; }//防止文本里没有数据从而造成的程序报错
else
{
WR.f = Convert.ToSingle(textBox7.Text);//传送设备0_4DF0001里的值
registersNUM[0] = WR.us1;
registersNUM[1] = WR.us0;
}
if (textBox8.Text == "") { WR.f = 0; }//防止文本里没有数据从而造成的程序报错
else
{
WR.f = Convert.ToSingle(textBox8.Text);//传送设备0_4DF0003里的值
registersNUM[2] = WR.us1;
registersNUM[3] = WR.us0;
}
CommonVariable.master.WriteMultipleRegisters(1, 0, registersNUM);//写4区保持寄存器命令
//文本框合并后的代码如下:
//ushort[] registersNUM = new ushort[28];
//WR.f = Convert.ToSingle(textBox7.Text);//传送设备0_4DF0001里的值
//registersNUM[0] = WR.us1;
//registersNUM[1] = WR.us0;
//WR.f = Convert.ToSingle(textBox8.Text);//传送设备0_4DF0003里的值
//registersNUM[2] = WR.us1;
//registersNUM[3] = WR.us0;
//CommonVariable.master.WriteMultipleRegisters(1, 0, registersNUM);//写4区保持寄存器命令
}
#endregion 读写4区保持寄存器的值
}
}