60 lines
2.4 KiB
C#
60 lines
2.4 KiB
C#
using System.Text;
|
|
|
|
namespace Modbus.Net.Modbus
|
|
{
|
|
/// <summary>
|
|
/// Modbus/Ascii码协议连接器Tcp透传
|
|
/// </summary>
|
|
public class ModbusAsciiInTcpProtocolLinker : TcpProtocolLinker
|
|
{
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="ip">IP地址</param>
|
|
public ModbusAsciiInTcpProtocolLinker(string ip)
|
|
: base(ip, int.Parse(ConfigurationReader.GetValueDirect("TCP:" + ip, "ModbusPort") ?? ConfigurationReader.GetValueDirect("TCP:Modbus", "ModbusPort")))
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="ip">ip地址</param>
|
|
/// <param name="port">端口号</param>
|
|
public ModbusAsciiInTcpProtocolLinker(string ip, int port)
|
|
: base(ip, port)
|
|
{
|
|
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "FetchSleepTime")),
|
|
lengthCalc: content =>
|
|
{
|
|
if (content[0] != 0x3a) return 0;
|
|
for (int i = 1; i < content.Length; i++)
|
|
{
|
|
if (content[i - 1] == 0x0D && content[i] == 0x0A) return i + 1;
|
|
}
|
|
return -1;
|
|
},
|
|
checkRightFunc: ContentCheck.LrcCheckRight,
|
|
waitingListMaxCount: ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount") != null
|
|
? int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount"))
|
|
: null
|
|
));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 校验返回数据是否正确
|
|
/// </summary>
|
|
/// <param name="content">返回的数据</param>
|
|
/// <returns>校验是否正确</returns>
|
|
public override bool? CheckRight(byte[] content)
|
|
{
|
|
//ProtocolLinker不会返回null
|
|
if (base.CheckRight(content) != true) return false;
|
|
//Modbus协议错误
|
|
var contentString = Encoding.ASCII.GetString(content);
|
|
if (byte.Parse(contentString.Substring(3, 2)) > 127)
|
|
throw new ModbusProtocolErrorException(byte.Parse(contentString.Substring(5, 2)));
|
|
return true;
|
|
}
|
|
}
|
|
} |