我正在通过 modbus rtu 从设备获取带有库 Nmodbus4 的 C# 控制台程序中的数据。格式:32 位浮点。我通过寄存器 0x0D – 0x0F 请求设备。我的回答是 5132, 3595, 8458 。这个答案应该与 2020 12 14 11:33:~35 匹配。即数据格式为[年][月][日][小时][分钟][秒]。在我的代码下面:
using Modbus.Device;
using Modbus.Utility;
using System;
using System.Collections;
using System.Globalization;
using System.IO.Ports;
namespace Elemer19
{
class Program
{
static void Main(string[] args)
{
string[] pathFolder = ReadDirectory.readDir();
Console.WriteLine("OPENING COM PORT - {0}", pathFolder[0]);
byte slaveID = 4;
ushort startAddress = 0x0D;
ushort numOfPoints = 3;
SerialPort _serialPort = new SerialPort(pathFolder[0],
9600,
Parity.None,
8,
StopBits.One);
try
{
_serialPort.Open();
ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(
_serialPort);
master.Transport.ReadTimeout = 300;
ushort[] date = master.ReadHoldingRegisters(slaveID, startAddress, numOfPoints);
foreach (int item in date)
{
Console.Write("\r\n{0}", item); //5132, 3595, 8458
}
}
catch (Exception ex)
{
Console.WriteLine(DateTime.Now.ToString() + ":Connect process " + ex.StackTrace +
"==>" + ex.Message);
}
Console.ReadLine();
}
}}
设备中的日期格式:两个寄存器的组合位域中的日期和时间(从最低有效字节的最低有效位开始):
seconds - (bits 0 ... 5 - values 0 ... 59)
minutes - (bits 6 ... 11 - values 0 ... 59)
hours - (bits 12 ... 16, values 0 ... 23)
number - (bits 17 ... 21, values 0 ... 30) - days 1 ... 31
month - (bits 22 ... 25, values 0 ... 11) - months January ... December
year - (bits 26 ... 31, values 0 ... 63) - years 2000 ... 2063
如何正确获取日期值?