用于寄存器读取的C#MODBUS RTU



我目前正在VS中编写一个c#程序,该程序使用Modbus RTU读取Panasonic KW9M-a电表的保持寄存器。我试图读取的保留寄存器是:00A4H至00A5H;无符号32位

注意:这是我在控制台应用程序中写的。我使用了Maxwe11的NModbus4我知道我作为一个编程初学者所缺少的东西。有人能帮我吗?提前感谢:(

以下是我目前所拥有的:

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_ye
{
class Program
{
public static void Main(string[] args)
{
SerialPort serialPort = new SerialPort("COM13", 115200, Parity.Odd, 8, StopBits.One);
serialPort.Open();
Console.WriteLine("This is the beginning: ");
string hex_add = "0x00A4";
ushort dec_add = Convert.ToUInt16(hex_add, 16);
Console.WriteLine("Value of hex: " + hex_add);
Console.WriteLine("Value of ushort: " + dec_add);
byte slaveId = 1;
ushort startAddress = dec_add;
ushort numberOfPoints = 8;
IModbusMaster masterRTU = ModbusSerialMaster.CreateRtu (serialPort);
ushort[] ushortArray = masterRTU.ReadHoldingRegisters(slaveId, startAddress, numberOfPoints);
Console.WriteLine("Here " + ushortArray[0]);
foreach (ushort item in ushortArray)
{
Console.WriteLine(string.Join ("n", item));
}
}
}
} 

对NModbus4一无所知,但如示例程序所示,您可能需要在程序中包含以下一个或多个内容。

using Modbus.Data;
using Modbus.Device;
using Modbus.Utility;
using Modbus.Serial;

您应该更正这一行:Console.WriteLine(string.Join ("n", item));Console.WriteLine(item);

那么它就可行了。