Server sample fix

This commit is contained in:
luosheng
2023-12-14 15:31:58 +08:00
parent 258da627ac
commit 34ba703696

View File

@@ -1,3 +1,4 @@
using Modbus.Net;
using Modbus.Net.Modbus; using Modbus.Net.Modbus;
using MultipleMachinesJobScheduler = Modbus.Net.MultipleMachinesJobScheduler<Modbus.Net.IMachineMethodDatas, string, double>; using MultipleMachinesJobScheduler = Modbus.Net.MultipleMachinesJobScheduler<Modbus.Net.IMachineMethodDatas, string, double>;
@@ -7,7 +8,12 @@ namespace SampleModbusRtuServer.Service
{ {
private readonly ILogger<Worker> _logger; private readonly ILogger<Worker> _logger;
private byte[] threex = new byte[19998]; private bool[] zerox = new bool[10000];
private byte[] threex = new byte[20000];
private bool _isUpdate = false;
private DateTime _updateTime = DateTime.MinValue;
public Worker(ILogger<Worker> logger) public Worker(ILogger<Worker> logger)
{ {
@@ -19,44 +25,68 @@ namespace SampleModbusRtuServer.Service
ModbusRtuProtocolReceiver receiver = new ModbusRtuProtocolReceiver("COM2", 1); ModbusRtuProtocolReceiver receiver = new ModbusRtuProtocolReceiver("COM2", 1);
receiver.DataProcess = receiveContent => receiver.DataProcess = receiveContent =>
{ {
byte[] returnBytes = null; byte[]? returnBytes = null;
var readContent = new byte[receiveContent.Count * 2]; var readContent = new byte[receiveContent.Count * 2];
var values = receiveContent.WriteContent; var values = receiveContent.WriteContent;
var valueDic = new Dictionary<string, double>();
var redisValues = new Dictionary<string, ReturnUnit<double>>();
if (values != null) if (values != null)
{ {
try try
{ {
/*using (var context = new DatabaseWriteContext()) if (_isUpdate && DateTime.Now - _updateTime > TimeSpan.FromSeconds(9.5))
{ {
context.DatabaseWrites?.Add(new DatabaseWriteEntity _logger.LogDebug($"receive content { String.Concat(receiveContent.WriteContent.Select(p => " " + p.ToString("X2")))}");
}
}
catch (Exception ex)
{ {
Value1 = values["Test1"].DeviceValue, _logger.LogError(ex, "Error");
Value2 = values["Test2"].DeviceValue, }
Value3 = values["Test3"].DeviceValue,
Value4 = values["Test4"].DeviceValue,
Value5 = values["Test5"].DeviceValue,
Value6 = values["Test6"].DeviceValue,
Value7 = values["Test7"].DeviceValue,
Value8 = values["Test8"].DeviceValue,
Value9 = values["Test9"].DeviceValue,
Value10 = values["Test10"].DeviceValue,
UpdateTime = DateTime.Now,
});
context.SaveChanges();
}*/
switch (receiveContent.FunctionCode) switch (receiveContent.FunctionCode)
{ {
case (byte)ModbusProtocolFunctionCode.WriteMultiRegister: case (byte)ModbusProtocolFunctionCode.WriteMultiRegister:
{ {
Array.Copy(receiveContent.WriteContent, 0, threex, receiveContent.StartAddress * 2, receiveContent.Count); Array.Copy(receiveContent.WriteContent, 0, threex, receiveContent.StartAddress * 2, receiveContent.WriteContent.Length);
returnBytes = new WriteDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, receiveContent.StartAddress, receiveContent.Count); returnBytes = new WriteDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, receiveContent.StartAddress, receiveContent.Count);
_isUpdate = true;
break; break;
} }
} case (byte)ModbusProtocolFunctionCode.WriteSingleCoil:
}
catch
{ {
//ignore if (receiveContent.WriteContent[0] == 255)
{
zerox[receiveContent.StartAddress] = true;
}
else
{
zerox[receiveContent.StartAddress] = false;
}
returnBytes = new WriteDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, receiveContent.StartAddress, receiveContent.WriteContent);
_isUpdate = true;
break;
}
case (byte)ModbusProtocolFunctionCode.WriteMultiCoil:
{
var pos = 0;
List<bool> bitList = new List<bool>();
for (int i = 0; i < receiveContent.WriteByteCount; i++)
{
var bitArray = BigEndianLsbValueHelper.Instance.GetBits(receiveContent.WriteContent, ref pos);
bitList.AddRange(bitArray.ToList());
}
Array.Copy(bitList.ToArray(), 0, zerox, receiveContent.StartAddress, bitList.Count);
returnBytes = new WriteDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, receiveContent.StartAddress, receiveContent.Count);
_isUpdate = true;
break;
}
case (byte)ModbusProtocolFunctionCode.WriteSingleRegister:
{
Array.Copy(receiveContent.WriteContent, 0, threex, receiveContent.StartAddress * 2, receiveContent.Count * 2);
returnBytes = new WriteDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, receiveContent.StartAddress, receiveContent.Count);
_isUpdate = true;
break;
}
} }
} }
else else
@@ -69,11 +99,37 @@ namespace SampleModbusRtuServer.Service
returnBytes = new ReadDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, (byte)receiveContent.Count, readContent); returnBytes = new ReadDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, (byte)receiveContent.Count, readContent);
break; break;
} }
case (byte)ModbusProtocolFunctionCode.ReadCoilStatus:
{
var bitCount = receiveContent.WriteByteCount * 8;
var boolContent = new bool[bitCount];
Array.Copy(zerox, receiveContent.StartAddress, boolContent, 0, bitCount);
var byteList = new List<byte>();
for (int i = 0; i < receiveContent.WriteByteCount; i++)
{
byte result = 0;
for (int j = i; j < i + 8; j++)
{
// 将布尔值转换为对应的位
byte bit = boolContent[j] ? (byte)1 : (byte)0;
// 使用左移位运算将位合并到结果字节中
result = (byte)((result << 1) | bit);
}
byteList.Add(result);
}
readContent = byteList.ToArray();
returnBytes = new ReadDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, receiveContent.WriteByteCount, readContent);
break;
}
} }
} }
if (returnBytes != null) return returnBytes; if (returnBytes != null) return returnBytes;
else return null; else return null;
}; };
await receiver.ConnectAsync(); await receiver.ConnectAsync();
} }