Modbus ASCII and Siemens PPI support (not test), ComConnector Remaintainence.
This commit is contained in:
@@ -43,6 +43,8 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="AddressFormaterModbus.cs" />
|
||||
<Compile Include="AddressTranslatorModbus.cs" />
|
||||
<Compile Include="ModbusAsciiProtocal.cs" />
|
||||
<Compile Include="ModbusAsciiProtocalLinker.cs" />
|
||||
<Compile Include="ModbusMachine.cs" />
|
||||
<Compile Include="ModbusProtocal.cs" />
|
||||
<Compile Include="ModbusProtocalLinkerBytesExtend.cs" />
|
||||
|
||||
23
Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocal.cs
Normal file
23
Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocal.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
/// <summary>
|
||||
/// Modbus/Rtu协议
|
||||
/// </summary>
|
||||
public class ModbusAsciiProtocal : ModbusProtocal
|
||||
{
|
||||
public ModbusAsciiProtocal() : this(ConfigurationManager.COM)
|
||||
{
|
||||
}
|
||||
|
||||
public ModbusAsciiProtocal(string com)
|
||||
{
|
||||
ProtocalLinker = new ModbusAsciiProtocalLinker(com);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocalLinker.cs
Normal file
33
Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocalLinker.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
public class ModbusAsciiProtocalLinker : ComProtocalLinker
|
||||
{
|
||||
public override bool? CheckRight(byte[] content)
|
||||
{
|
||||
if (!base.CheckRight(content).Value) return false;
|
||||
//CRC校验失败
|
||||
string contentString = Encoding.ASCII.GetString(content);
|
||||
if (!Crc16.GetInstance().LrcEfficacy(contentString))
|
||||
{
|
||||
throw new ModbusProtocalErrorException(501);
|
||||
}
|
||||
//Modbus协议错误
|
||||
if (byte.Parse(contentString.Substring(3, 2)) > 127)
|
||||
{
|
||||
throw new ModbusProtocalErrorException(byte.Parse(contentString.Substring(5, 2)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public ModbusAsciiProtocalLinker(string com) : base(com, 9600, Parity.None, StopBits.One, 8)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -124,9 +124,10 @@ namespace Modbus.Net.Modbus
|
||||
var translateAddress = addressTranslator.AddressTranslate(startAddress, false);
|
||||
FunctionCode = (byte)translateAddress.Area;
|
||||
StartAddress = (ushort)translateAddress.Address;
|
||||
WriteCount = (ushort)Math.Ceiling(writeValue.Length / 2.0);
|
||||
WriteByteCount = 0;
|
||||
WriteValue = writeValue;
|
||||
var writeByteValue = BigEndianValueHelper.Instance.ObjectArrayToByteArray(writeValue);
|
||||
WriteCount = (ushort)(writeByteValue.Length / 2);
|
||||
WriteByteCount = (byte)writeByteValue.Length;
|
||||
WriteValue = writeByteValue;
|
||||
}
|
||||
|
||||
public byte BelongAddress { get; private set; }
|
||||
@@ -139,7 +140,7 @@ namespace Modbus.Net.Modbus
|
||||
|
||||
public byte WriteByteCount { get; private set; }
|
||||
|
||||
public object[] WriteValue { get; private set; }
|
||||
public byte[] WriteValue { get; private set; }
|
||||
}
|
||||
|
||||
public class WriteDataModbusOutputStruct : OutputStruct
|
||||
@@ -172,7 +173,6 @@ namespace Modbus.Net.Modbus
|
||||
var r_message = (WriteDataModbusInputStruct)message;
|
||||
byte[] formattingBytes = Format(r_message.BelongAddress, r_message.FunctionCode,
|
||||
r_message.StartAddress, r_message.WriteCount, r_message.WriteByteCount, r_message.WriteValue);
|
||||
formattingBytes[6] = (byte)(formattingBytes.Length - 7);
|
||||
return formattingBytes;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
@@ -43,10 +45,42 @@ namespace Modbus.Net.Modbus
|
||||
|
||||
public override byte[] BytesDecact(byte[] content)
|
||||
{
|
||||
//Modbus/Rtu协议收缩,抛弃后面1个字节的内容
|
||||
//Modbus/Rtu协议收缩,抛弃后面2个字节的内容
|
||||
byte[] newContent = new byte[content.Length - 2];
|
||||
Array.Copy(content, 0, newContent, 0, newContent.Length);
|
||||
return newContent;
|
||||
}
|
||||
}
|
||||
|
||||
public class ModbusAsciiProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend
|
||||
{
|
||||
public override byte[] BytesExtend(byte[] content)
|
||||
{
|
||||
List<byte> newContent = new List<byte>();
|
||||
newContent.AddRange(Encoding.ASCII.GetBytes(":"));
|
||||
foreach (var number in content)
|
||||
{
|
||||
newContent.AddRange(Encoding.ASCII.GetBytes(number.ToString()));
|
||||
}
|
||||
newContent.AddRange(Encoding.ASCII.GetBytes(Crc16.GetInstance().GetLRC(content)));
|
||||
newContent.Add(0x0d);
|
||||
newContent.Add(0x0a);
|
||||
return newContent.ToArray();
|
||||
}
|
||||
|
||||
public override byte[] BytesDecact(byte[] content)
|
||||
{
|
||||
List<byte> newContent = new List<byte>();
|
||||
string ans = Encoding.ASCII.GetString(content);
|
||||
var index = ans.IndexOf(Environment.NewLine);
|
||||
ans = ans.Substring(1, index - 1);
|
||||
for (int i = 0; i < ans.Length; i += 2)
|
||||
{
|
||||
var number = byte.Parse(ans.Substring(i, 2));
|
||||
newContent.Add(number);
|
||||
}
|
||||
newContent.RemoveAt(newContent.Count-1);
|
||||
return newContent.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
namespace Modbus.Net.Modbus
|
||||
using System.IO.Ports;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
class ModbusRtuProtocalLinker : ComProtocalLinker
|
||||
{
|
||||
@@ -18,9 +20,9 @@
|
||||
return true;
|
||||
}
|
||||
|
||||
public ModbusRtuProtocalLinker(string com) : base(com)
|
||||
public ModbusRtuProtocalLinker(string com) : base(com, 9600, Parity.None, StopBits.One, 8)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,10 @@ namespace Modbus.Net.Modbus
|
||||
/// Tcp连接
|
||||
/// </summary>
|
||||
Tcp = 1,
|
||||
/// <summary>
|
||||
/// Ascii连接
|
||||
/// </summary>
|
||||
Ascii = 2,
|
||||
}
|
||||
|
||||
public class ModbusUtility : BaseUtility
|
||||
@@ -74,6 +78,11 @@ namespace Modbus.Net.Modbus
|
||||
Wrapper = ConnectionString == null ? new ModbusTcpProtocal() : (ConnectionStringPort == null ? new ModbusTcpProtocal(ConnectionString) : new ModbusTcpProtocal(ConnectionStringIp,ConnectionStringPort.Value));
|
||||
break;
|
||||
}
|
||||
case ModbusType.Ascii:
|
||||
{
|
||||
Wrapper = ConnectionString == null ? new ModbusAsciiProtocal() : new ModbusAsciiProtocal(ConnectionString);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user