Browse Source

fixed modbus register value type

master
Z 9 months ago
parent
commit
959d5666b6
  1. 2
      Example/Gateway/ModbusGateway.tscn
  2. 6
      Example/Gateway/Script/Data/DataModbusGatewaySettings.cs
  3. 59
      Example/Gateway/Script/View/ViewModbusGateway.cs
  4. 10
      addons/EGFramework/Module/ProtocolExtension/EGModbusExtension.cs

2
Example/Gateway/ModbusGateway.tscn

@ -15,4 +15,4 @@ script = ExtResource("1_34uaa") @@ -15,4 +15,4 @@ script = ExtResource("1_34uaa")
wait_time = 2.0
autostart = true
[connection signal="timeout" from="Timer" to="." method="PushDataToGateway"]
[connection signal="timeout" from="Timer" to="." method="PushTCPDataToGateway"]

6
Example/Gateway/Script/Data/DataModbusGatewaySettings.cs

@ -23,10 +23,16 @@ namespace EGFramework.Examples.Gateway{ @@ -23,10 +23,16 @@ namespace EGFramework.Examples.Gateway{
public class DataModbusValue{
public ushort Address { set; get; }
public ushort Length { set; get; }
public DataModbusValueType ValueType = DataModbusValueType.Float_;
public ModbusRegisterType RegisterType { set; get; } = ModbusRegisterType.HoldingRegister;
public string Name { set; get; }
// public string Unit { set; get; }
}
public enum DataModbusValueType{
UShort_ = 0,
Float_ = 1
}
}

59
Example/Gateway/Script/View/ViewModbusGateway.cs

@ -49,17 +49,17 @@ namespace EGFramework.Examples.Gateway{ @@ -49,17 +49,17 @@ namespace EGFramework.Examples.Gateway{
public void InitSettings(){
Setting = new DataModbusGatewaySetting();
Setting.Delay = 1.0f;
Setting.Devices485.Add("COM4",new DataModbus485Device(){
SerialPort = "COM4",
Address = 0x01,
BaudRate = 9600
});
Setting.Devices485["COM4"].ValueRegisters.Add("表头读数",new DataModbusValue(){
Address = 0x00,
Length = 0x01,
RegisterType = ModbusRegisterType.HoldingRegister,
Name = "表头读数"
});
// Setting.Devices485.Add("COM4",new DataModbus485Device(){
// SerialPort = "COM4",
// Address = 0x01,
// BaudRate = 9600
// });
// Setting.Devices485["COM4"].ValueRegisters.Add("表头读数",new DataModbusValue(){
// Address = 0x00,
// Length = 0x01,
// RegisterType = ModbusRegisterType.HoldingRegister,
// Name = "表头读数"
// });
string IpPort = "192.168.1.170:8234";
Setting.DevicesTCP.Add(IpPort,new DataModbusTCPDevice(){
Host = IpPort.GetHostByIp(),
@ -97,7 +97,42 @@ namespace EGFramework.Examples.Gateway{ @@ -97,7 +97,42 @@ namespace EGFramework.Examples.Gateway{
}
string resultJson = JsonConvert.SerializeObject(pushData,Formatting.Indented);
GD.Print(resultJson);
this.EGTCPClient().SendStringData("192.168.1.170",5501,resultJson);
}
public async void PushTCPDataToGateway(){
if(!this.Visible){
return;
}
JObject pushData = new JObject();
foreach(KeyValuePair<string,DataModbusTCPDevice> deviceTCP in Setting.DevicesTCP){
foreach(KeyValuePair<string,DataModbusValue> register in Setting.DevicesTCP[deviceTCP.Key].ValueRegisters){
ModbusTCP_Response? result = await this.EGModbus().ReadTCPAsync(register.Value.RegisterType,deviceTCP.Key,deviceTCP.Value.Address,register.Value.Address,register.Value.Length);
if(result != null){
if(!((ModbusTCP_Response)result).IsError){
if(register.Value.RegisterType == ModbusRegisterType.HoldingRegister){
object onceValue = 0;
switch(register.Value.ValueType){
case DataModbusValueType.Float_:
onceValue = ((ModbusTCP_Response)result).SourceValueData?.ToFloatArrayBigEndian()[0];
break;
case DataModbusValueType.UShort_:
onceValue = ((ModbusTCP_Response)result).HoldingRegister[0];
break;
}
pushData.Add(register.Key, (float)onceValue);
}
}else{
GD.Print("Error:"+((ModbusTCP_Response)result).ErrorCode);
}
}else{
GD.Print("Timeout!");
}
}
}
string resultJson = JsonConvert.SerializeObject(pushData,Formatting.Indented);
GD.Print(resultJson);
this.EGTCPClient().SendStringData("192.168.1.170",5501,resultJson);
}

10
addons/EGFramework/Module/ProtocolExtension/EGModbusExtension.cs

@ -375,6 +375,7 @@ namespace EGFramework{ @@ -375,6 +375,7 @@ namespace EGFramework{
public uint DataLength { set; get; }
public byte[] SourceData { set; get; }
public byte[] SourceValueData { set; get; }
public ModbusFunctionType FunctionType { set; get; }
public ModbusErrorCode ErrorCode { set; get; }
public bool IsError { set; get; }
@ -407,24 +408,28 @@ namespace EGFramework{ @@ -407,24 +408,28 @@ namespace EGFramework{
byte readCoilLength = protocolBytes[8];
byte[] CoilBytes = new byte[readCoilLength];
Array.Copy(protocolBytes,9,CoilBytes,0,readCoilLength);
SourceValueData = CoilBytes;
Coil = CoilBytes.ToBoolArray();
return true;
case ModbusFunctionType.ReadDiscreteInput:
byte readDiscreteInputLength = protocolBytes[8];
byte[] DiscreteInputBytes = new byte[readDiscreteInputLength];
Array.Copy(protocolBytes,9,DiscreteInputBytes,0,readDiscreteInputLength);
SourceValueData = DiscreteInputBytes;
DiscreteInput = DiscreteInputBytes.ToBoolArray();
return true;
case ModbusFunctionType.ReadHoldingRegisters:
byte readHoldingRegistersLength = protocolBytes[8];
byte[] HoldingRegistersBytes = new byte[readHoldingRegistersLength];
Array.Copy(protocolBytes,9,HoldingRegistersBytes,0,readHoldingRegistersLength);
SourceValueData = HoldingRegistersBytes;
HoldingRegister = HoldingRegistersBytes.ToUShortArray();
return true;
case ModbusFunctionType.ReadInputRegisters:
byte readInputRegistersLength = protocolBytes[8];
byte[] InputRegistersBytes = new byte[readInputRegistersLength];
Array.Copy(protocolBytes,9,InputRegistersBytes,0,readInputRegistersLength);
SourceValueData = InputRegistersBytes;
InputRegister = InputRegistersBytes.ToUShortArray();
return true;
case ModbusFunctionType.WriteSingleCoil:
@ -762,6 +767,7 @@ namespace EGFramework{ @@ -762,6 +767,7 @@ namespace EGFramework{
public ushort RegisterStartAddress { set; get; }
public byte[] SourceData { set; get; }
public byte[] SourceValueData { set; get; }
public ModbusFunctionType FunctionType { set; get; }
public ModbusErrorCode ErrorCode { set; get; }
public bool IsError { set; get; }
@ -798,24 +804,28 @@ namespace EGFramework{ @@ -798,24 +804,28 @@ namespace EGFramework{
byte readCoilLength = protocolBytes[2];
byte[] CoilBytes = new byte[readCoilLength];
Array.Copy(protocolBytes,3,CoilBytes,0,readCoilLength);
SourceValueData = CoilBytes;
Coil = CoilBytes.ToBoolArray();
return true;
case ModbusFunctionType.ReadDiscreteInput:
byte readDiscreteInputLength = protocolBytes[2];
byte[] DiscreteInputBytes = new byte[readDiscreteInputLength];
Array.Copy(protocolBytes,3,DiscreteInputBytes,0,readDiscreteInputLength);
SourceValueData = DiscreteInputBytes;
DiscreteInput = DiscreteInputBytes.ToBoolArray();
return true;
case ModbusFunctionType.ReadHoldingRegisters:
byte readHoldingRegistersLength = protocolBytes[2];
byte[] HoldingRegistersBytes = new byte[readHoldingRegistersLength];
Array.Copy(protocolBytes,3,HoldingRegistersBytes,0,readHoldingRegistersLength);
SourceValueData = HoldingRegistersBytes;
HoldingRegister = HoldingRegistersBytes.ToUShortArray();
return true;
case ModbusFunctionType.ReadInputRegisters:
byte readInputRegistersLength = protocolBytes[2];
byte[] InputRegistersBytes = new byte[readInputRegistersLength];
Array.Copy(protocolBytes,3,InputRegistersBytes,0,readInputRegistersLength);
SourceValueData = InputRegistersBytes;
InputRegister = InputRegistersBytes.ToUShortArray();
return true;
case ModbusFunctionType.WriteSingleCoil:

Loading…
Cancel
Save