using Newtonsoft.Json.Linq;
using NModbus;
using System;
using System.Data;
using System.IO;
using System.Net.Sockets;
using System.Text;
namespace Gs.DevApp
{
public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
{
public XtraForm1()
{
InitializeComponent();
}
///
/// 水检
///
///
///
private void simpleButton2_Click(object sender, EventArgs e)
{
string strCsl1 = "";
string strSw1 = "";
string strCsl2 = "";
string strSw2 = "";
string strCsl3 = "";
string strSw3 = "";
string strCsl4 = "";
string strSw4 = "";
string strCsl5 = "";
string strSw5 = "";
string SJJG = "";
try//水检取数,后面补充
{
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
string hostname = "192.168.60.51";
ModbusFactory modbusFactory = new ModbusFactory();
IModbusMaster master = modbusFactory.CreateMaster(new TcpClient(hostname, 502));
master.Transport.ReadTimeout = 10000;
master.Transport.Retries = 10000;
DataTable dt = new DataTable();
//参数(分别为从站地址,起始地址,长度)
byte slaveAddress = byte.Parse(1.ToString());
ushort numberOfPoints = ushort.Parse("1");
float fsw = 0;
DataTable dt1 = new DataTable();
ushort[] csl1 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(32.ToString()), numberOfPoints);
strCsl1 = csl1[0].ToString();
ushort[] sw1 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(31.ToString()), numberOfPoints);
strSw1 = (float.Parse(sw1[0].ToString()) / 10).ToString();
ushort[] csl2 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(34.ToString()), numberOfPoints);
strCsl2 = csl2[0].ToString();
ushort[] sw2 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(33.ToString()), numberOfPoints);
strSw2 = (float.Parse(sw2[0].ToString()) / 10).ToString();
ushort[] csl3 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(36.ToString()), numberOfPoints);
strCsl3 = csl3[0].ToString();
ushort[] sw3 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(35.ToString()), numberOfPoints);
strSw3 = (float.Parse(sw3[0].ToString()) / 10).ToString();
ushort[] csl4 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(38.ToString()), numberOfPoints);
strCsl4 = csl4[0].ToString();
ushort[] sw4 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(37.ToString()), numberOfPoints);
strSw4 = (float.Parse(sw4[0].ToString()) / 10).ToString();
ushort[] csl5 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(40.ToString()), numberOfPoints);
strCsl5 = csl5[0].ToString();
ushort[] sw5 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(39.ToString()), numberOfPoints);
strSw5 = (float.Parse(sw5[0].ToString()) / 10).ToString();
bool[] sjjg = master.ReadCoils(slaveAddress, ushort.Parse(77.ToString()), numberOfPoints);
SJJG = sjjg[0].ToString();
///////////////////
///
// 3. 读取从站设备的寄存器
ushort startAddress = 0; // 寄存器起始地址
ushort numRegisters = 125; // 读取 10 个寄存器
try
{
// 读取保持寄存器(从站地址为 1)
ushort[] registers = master.ReadHoldingRegisters(1, startAddress, numRegisters);
// 输出结果
Console.WriteLine("读取到的寄存器值:");
foreach (var register in registers)
{
Console.WriteLine(register);
stringBuilder.Append(register.ToString());
}
richTextBox1.Text = stringBuilder.ToString();
}
catch (Exception ex)
{
Console.WriteLine($"读取错误: {ex.Message}");
}
}
catch (Exception ex)
{
//VisiblePanel(txtDQBarCode.EditValue.ToString(), ex.Message, "NG", Color.Yellow);
//txtGWM.EditValue = "";
return;
}
}
}
}