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一无所知,但如sample program所示,您可能需要在程序中包含以下一项或多项内容。

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