如何正确转换通过 modbus 接收到的值?
How to correctly convert the values received through modbus?
我正在通过 modbus rtu 从设备获取带有库 Nmodbus4 的 C# 控制台程序中的数据。格式:32 位浮点数 point.I 是通过寄存器 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
如何正确获取日期值?
你的位解码信息好像有误
如果你把这三个数字写成单独的字节(20、12、14、11、33、10),你会发现你的期望值可以直接从这些字节中提取出来:
DateTime ConvertToDateTime(ushort[] data)
{
var year = (int)(data[0] >> 8);
var month = (int)(data[0] & 0xFF);
var day = (int)(data[1] >> 8);
var hour = (int)(data[1] & 0xFF);
var minute = (int)(data[2] >> 8);
var second = (int)(data[2] & 0xFF);
return new DateTime(2000 + year, month, day, hour, minute, second);
}
产量:2020-12-14 11:33:10
我正在通过 modbus rtu 从设备获取带有库 Nmodbus4 的 C# 控制台程序中的数据。格式:32 位浮点数 point.I 是通过寄存器 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
如何正确获取日期值?
你的位解码信息好像有误
如果你把这三个数字写成单独的字节(20、12、14、11、33、10),你会发现你的期望值可以直接从这些字节中提取出来:
DateTime ConvertToDateTime(ushort[] data)
{
var year = (int)(data[0] >> 8);
var month = (int)(data[0] & 0xFF);
var day = (int)(data[1] >> 8);
var hour = (int)(data[1] & 0xFF);
var minute = (int)(data[2] >> 8);
var second = (int)(data[2] & 0xFF);
return new DateTime(2000 + year, month, day, hour, minute, second);
}
产量:2020-12-14 11:33:10