Add ReturnStruct to return error message
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<AssemblyName>Modbus.Net.Modbus.NA200H</AssemblyName>
|
||||
<RootNamespace>Modbus.Net.Modbus.NA200H</RootNamespace>
|
||||
<PackageId>Modbus.Net.Modbus.NA200H</PackageId>
|
||||
<Version>1.4.0-beta03</Version>
|
||||
<Version>1.4.0-beta04</Version>
|
||||
<Authors>Chris L.(Luo Sheng)</Authors>
|
||||
<Company>Hangzhou Delian Science Technology Co.,Ltd.</Company>
|
||||
<Product>Modbus.Net.Modbus</Product>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<AssemblyName>Modbus.Net.Modbus</AssemblyName>
|
||||
<RootNamespace>Modbus.Net.Modbus</RootNamespace>
|
||||
<PackageId>Modbus.Net.Modbus</PackageId>
|
||||
<Version>1.4.0-beta03</Version>
|
||||
<Version>1.4.0-beta04</Version>
|
||||
<Authors>Chris L.(Luo Sheng)</Authors>
|
||||
<Company>Hangzhou Delian Science Technology Co.,Ltd.</Company>
|
||||
<Product>Modbus.Net.Modbus</Product>
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace Modbus.Net.Modbus
|
||||
/// <param name="startAddress">起始地址</param>
|
||||
/// <param name="setContent">需要设置的数据</param>
|
||||
/// <returns>设置是否成功</returns>
|
||||
Task<bool> SetSingleDataAsync(string startAddress, object setContent);
|
||||
Task<ReturnStruct<bool>> SetSingleDataAsync(string startAddress, object setContent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -251,7 +251,7 @@ namespace Modbus.Net.Modbus
|
||||
/// 读时间
|
||||
/// </summary>
|
||||
/// <returns>设备的时间</returns>
|
||||
public async Task<DateTime> GetTimeAsync()
|
||||
public async Task<ReturnStruct<DateTime>> GetTimeAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -259,12 +259,24 @@ namespace Modbus.Net.Modbus
|
||||
var outputStruct =
|
||||
await Wrapper.SendReceiveAsync<GetSystemTimeModbusOutputStruct>(
|
||||
Wrapper[typeof(GetSystemTimeModbusProtocol)], inputStruct);
|
||||
return outputStruct?.Time ?? DateTime.MinValue;
|
||||
return new ReturnStruct<DateTime>
|
||||
{
|
||||
Datas = outputStruct?.Time ?? DateTime.MinValue,
|
||||
IsSuccess = true,
|
||||
ErrorCode = 0,
|
||||
ErrorMsg = ""
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (ModbusProtocolErrorException e)
|
||||
{
|
||||
logger.LogError(e, $"ModbusUtility -> GetTime: {ConnectionString} error");
|
||||
return DateTime.MinValue;
|
||||
logger.LogError(e, $"ModbusUtility -> GetTime: {ConnectionString} error: {e.Message}");
|
||||
return new ReturnStruct<DateTime>
|
||||
{
|
||||
Datas = DateTime.MinValue,
|
||||
IsSuccess = false,
|
||||
ErrorCode = e.ErrorMessageNumber,
|
||||
ErrorMsg = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -273,7 +285,7 @@ namespace Modbus.Net.Modbus
|
||||
/// </summary>
|
||||
/// <param name="setTime">需要写入的时间</param>
|
||||
/// <returns>写入是否成功</returns>
|
||||
public async Task<bool> SetTimeAsync(DateTime setTime)
|
||||
public async Task<ReturnStruct<bool>> SetTimeAsync(DateTime setTime)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -281,12 +293,24 @@ namespace Modbus.Net.Modbus
|
||||
var outputStruct =
|
||||
await Wrapper.SendReceiveAsync<SetSystemTimeModbusOutputStruct>(
|
||||
Wrapper[typeof(SetSystemTimeModbusProtocol)], inputStruct);
|
||||
return outputStruct?.WriteCount > 0;
|
||||
return new ReturnStruct<bool>()
|
||||
{
|
||||
Datas = outputStruct?.WriteCount > 0,
|
||||
IsSuccess = outputStruct?.WriteCount > 0,
|
||||
ErrorCode = outputStruct?.WriteCount > 0 ? 0 : -2,
|
||||
ErrorMsg = outputStruct?.WriteCount > 0 ? "" : "Data length zero"
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (ModbusProtocolErrorException e)
|
||||
{
|
||||
logger.LogError(e, $"ModbusUtility -> SetTime: {ConnectionString} error");
|
||||
return false;
|
||||
logger.LogError(e, $"ModbusUtility -> SetTime: {ConnectionString} error: {e.Message}");
|
||||
return new ReturnStruct<bool>
|
||||
{
|
||||
Datas = false,
|
||||
IsSuccess = false,
|
||||
ErrorCode = e.ErrorMessageNumber,
|
||||
ErrorMsg = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,7 +329,7 @@ namespace Modbus.Net.Modbus
|
||||
/// <param name="startAddress">起始地址</param>
|
||||
/// <param name="getByteCount">获取字节个数</param>
|
||||
/// <returns>获取的结果</returns>
|
||||
public override async Task<byte[]> GetDatasAsync(string startAddress, int getByteCount)
|
||||
public override async Task<ReturnStruct<byte[]>> GetDatasAsync(string startAddress, int getByteCount)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -314,12 +338,24 @@ namespace Modbus.Net.Modbus
|
||||
var outputStruct = await
|
||||
Wrapper.SendReceiveAsync<ReadDataModbusOutputStruct>(Wrapper[typeof(ReadDataModbusProtocol)],
|
||||
inputStruct);
|
||||
return outputStruct?.DataValue;
|
||||
return new ReturnStruct<byte[]>
|
||||
{
|
||||
Datas = outputStruct?.DataValue,
|
||||
IsSuccess = true,
|
||||
ErrorCode = 0,
|
||||
ErrorMsg = ""
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (ModbusProtocolErrorException e)
|
||||
{
|
||||
logger.LogError(e, $"ModbusUtility -> GetDatas: {ConnectionString} error");
|
||||
return null;
|
||||
logger.LogError(e, $"ModbusUtility -> GetDatas: {ConnectionString} error: {e.Message}");
|
||||
return new ReturnStruct<byte[]>
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = false,
|
||||
ErrorCode = e.ErrorMessageNumber,
|
||||
ErrorMsg = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,7 +365,7 @@ namespace Modbus.Net.Modbus
|
||||
/// <param name="startAddress">起始地址</param>
|
||||
/// <param name="setContents">需要设置的数据</param>
|
||||
/// <returns>设置是否成功</returns>
|
||||
public override async Task<bool> SetDatasAsync(string startAddress, object[] setContents)
|
||||
public override async Task<ReturnStruct<bool>> SetDatasAsync(string startAddress, object[] setContents)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -338,12 +374,24 @@ namespace Modbus.Net.Modbus
|
||||
var outputStruct = await
|
||||
Wrapper.SendReceiveAsync<WriteDataModbusOutputStruct>(Wrapper[typeof(WriteDataModbusProtocol)],
|
||||
inputStruct);
|
||||
return outputStruct?.WriteCount == setContents.Length;
|
||||
return new ReturnStruct<bool>()
|
||||
{
|
||||
Datas = outputStruct?.WriteCount == setContents.Length,
|
||||
IsSuccess = outputStruct?.WriteCount == setContents.Length,
|
||||
ErrorCode = outputStruct?.WriteCount == setContents.Length ? 0 : -2,
|
||||
ErrorMsg = outputStruct?.WriteCount == setContents.Length ? "" : "Data length mismatch"
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (ModbusProtocolErrorException e)
|
||||
{
|
||||
logger.LogError(e, $"ModbusUtility -> SetDatas: {ConnectionString} error");
|
||||
return false;
|
||||
logger.LogError(e, $"ModbusUtility -> SetDatas: {ConnectionString} error: {e.Message}");
|
||||
return new ReturnStruct<bool>
|
||||
{
|
||||
Datas = false,
|
||||
IsSuccess = false,
|
||||
ErrorCode = e.ErrorMessageNumber,
|
||||
ErrorMsg = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -353,7 +401,7 @@ namespace Modbus.Net.Modbus
|
||||
/// <param name="startAddress">起始地址</param>
|
||||
/// <param name="setContent">需要设置的数据</param>
|
||||
/// <returns>设置是否成功</returns>
|
||||
public async Task<bool> SetSingleDataAsync(string startAddress, object setContent)
|
||||
public async Task<ReturnStruct<bool>> SetSingleDataAsync(string startAddress, object setContent)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -362,12 +410,24 @@ namespace Modbus.Net.Modbus
|
||||
var outputStruct = await
|
||||
Wrapper.SendReceiveAsync<WriteSingleDataModbusOutputStruct>(Wrapper[typeof(WriteSingleDataModbusProtocol)],
|
||||
inputStruct);
|
||||
return outputStruct?.WriteValue.ToString() == setContent.ToString();
|
||||
return new ReturnStruct<bool>()
|
||||
{
|
||||
Datas = outputStruct?.WriteValue.ToString() == setContent.ToString(),
|
||||
IsSuccess = outputStruct?.WriteValue.ToString() == setContent.ToString(),
|
||||
ErrorCode = outputStruct?.WriteValue.ToString() == setContent.ToString() ? 0 : -2,
|
||||
ErrorMsg = outputStruct?.WriteValue.ToString() == setContent.ToString() ? "" : "Data length mismatch"
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (ModbusProtocolErrorException e)
|
||||
{
|
||||
logger.LogError(e, $"ModbusUtility -> SetSingleDatas: {ConnectionString} error");
|
||||
return false;
|
||||
logger.LogError(e, $"ModbusUtility -> SetSingleDatas: {ConnectionString} error: {e.Message}");
|
||||
return new ReturnStruct<bool>
|
||||
{
|
||||
Datas = false,
|
||||
IsSuccess = false,
|
||||
ErrorCode = e.ErrorMessageNumber,
|
||||
ErrorMsg = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<AssemblyName>Modbus.Net.OPC</AssemblyName>
|
||||
<RootNamespace>Modbus.Net.OPC</RootNamespace>
|
||||
<PackageId>Modbus.Net.OPC</PackageId>
|
||||
<Version>1.4.0-beta03</Version>
|
||||
<Version>1.4.0-beta04</Version>
|
||||
<Authors>Chris L.(Luo Sheng)</Authors>
|
||||
<Company>Hangzhou Delian Science Technology Co.,Ltd.</Company>
|
||||
<Product>Modbus.Net.OPC</Product>
|
||||
|
||||
@@ -54,7 +54,7 @@ namespace Modbus.Net.OPC
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">获取字节数个数</param>
|
||||
/// <returns>接收到的byte数据</returns>
|
||||
public override async Task<byte[]> GetDatasAsync(string startAddress, int getByteCount)
|
||||
public override async Task<ReturnStruct<byte[]>> GetDatasAsync(string startAddress, int getByteCount)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -64,12 +64,24 @@ namespace Modbus.Net.OPC
|
||||
await
|
||||
Wrapper.SendReceiveAsync<ReadRequestOpcOutputStruct>(Wrapper[typeof(ReadRequestOpcProtocol)],
|
||||
readRequestOpcInputStruct);
|
||||
return readRequestOpcOutputStruct?.GetValue;
|
||||
return new ReturnStruct<byte[]>
|
||||
{
|
||||
Datas = readRequestOpcOutputStruct?.GetValue,
|
||||
IsSuccess = true,
|
||||
ErrorCode = 0,
|
||||
ErrorMsg = ""
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, $"OpcUtility -> GetDatas: {ConnectionString} error");
|
||||
return null;
|
||||
logger.LogError(e, $"OpcUtility -> GetDatas: {ConnectionString} error: {e.Message}");
|
||||
return new ReturnStruct<byte[]>
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = true,
|
||||
ErrorCode = -100,
|
||||
ErrorMsg = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +91,7 @@ namespace Modbus.Net.OPC
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="setContents">设置数据</param>
|
||||
/// <returns>是否设置成功</returns>
|
||||
public override async Task<bool> SetDatasAsync(string startAddress, object[] setContents)
|
||||
public override async Task<ReturnStruct<bool>> SetDatasAsync(string startAddress, object[] setContents)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -90,12 +102,24 @@ namespace Modbus.Net.OPC
|
||||
await
|
||||
Wrapper.SendReceiveAsync<WriteRequestOpcOutputStruct>(Wrapper[typeof(WriteRequestOpcProtocol)],
|
||||
writeRequestOpcInputStruct);
|
||||
return writeRequestOpcOutputStruct?.WriteResult == true;
|
||||
return new ReturnStruct<bool>
|
||||
{
|
||||
Datas = writeRequestOpcOutputStruct?.WriteResult == true,
|
||||
IsSuccess = writeRequestOpcOutputStruct?.WriteResult == true,
|
||||
ErrorCode = writeRequestOpcOutputStruct?.WriteResult == true ? 0 : 1,
|
||||
ErrorMsg = writeRequestOpcOutputStruct?.WriteResult == true ? "" : "Write Failed"
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, $"OpcUtility -> SetDatas: {ConnectionString} error");
|
||||
return false;
|
||||
logger.LogError(e, $"OpcUtility -> SetDatas: {ConnectionString} error: {e.Message}");
|
||||
return new ReturnStruct<bool>
|
||||
{
|
||||
Datas = false,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -100,
|
||||
ErrorMsg = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<AssemblyName>Modbus.Net.Siemens</AssemblyName>
|
||||
<RootNamespace>Modbus.Net.Siemens</RootNamespace>
|
||||
<PackageId>Modbus.Net.Siemens</PackageId>
|
||||
<Version>1.4.0-beta03</Version>
|
||||
<Version>1.4.0-beta04</Version>
|
||||
<Authors>Chris L.(Luo Sheng)</Authors>
|
||||
<Company>Hangzhou Delian Science Technology Co.,Ltd.</Company>
|
||||
<Description>Modbus.Net Siemens Profinet Implementation</Description>
|
||||
|
||||
@@ -63,7 +63,7 @@ namespace Modbus.Net.Siemens
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
throw new FormatException();
|
||||
throw new FormatException($"Error content code with code {content[5]} {content[8]}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Modbus.Net.Siemens
|
||||
/// <summary>
|
||||
/// MPI
|
||||
/// </summary>
|
||||
Mpi = 1,
|
||||
//Mpi = 1,
|
||||
/// <summary>
|
||||
/// 以太网
|
||||
/// </summary>
|
||||
@@ -201,10 +201,10 @@ namespace Modbus.Net.Siemens
|
||||
break;
|
||||
}
|
||||
//MPI
|
||||
case SiemensType.Mpi:
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
//case SiemensType.Mpi:
|
||||
//{
|
||||
//throw new NotImplementedException();
|
||||
//}
|
||||
//Ethenet
|
||||
case SiemensType.Tcp:
|
||||
{
|
||||
@@ -236,7 +236,7 @@ namespace Modbus.Net.Siemens
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">读取字节个数</param>
|
||||
/// <returns>从设备中读取的数据</returns>
|
||||
public override async Task<byte[]> GetDatasAsync(string startAddress, int getByteCount)
|
||||
public override async Task<ReturnStruct<byte[]>> GetDatasAsync(string startAddress, int getByteCount)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -252,12 +252,35 @@ namespace Modbus.Net.Siemens
|
||||
Wrapper.SendReceiveAsync<ReadRequestSiemensOutputStruct>(
|
||||
Wrapper[typeof(ReadRequestSiemensProtocol)],
|
||||
readRequestSiemensInputStruct);
|
||||
return readRequestSiemensOutputStruct?.GetValue;
|
||||
return new ReturnStruct<byte[]>
|
||||
{
|
||||
Datas = readRequestSiemensOutputStruct?.GetValue,
|
||||
IsSuccess = true,
|
||||
ErrorCode = 0,
|
||||
ErrorMsg = ""
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (SiemensProtocolErrorException e)
|
||||
{
|
||||
logger.LogError(e, $"SiemensUtility -> GetDatas: {ConnectionString} error");
|
||||
return null;
|
||||
logger.LogError(e, $"SiemensUtility -> GetDatas: {ConnectionString} error: {e.Message}");
|
||||
return new ReturnStruct<byte[]>
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = false,
|
||||
ErrorCode = e.ErrorCode,
|
||||
ErrorMsg = e.Message
|
||||
};
|
||||
}
|
||||
catch (FormatException e)
|
||||
{
|
||||
logger.LogError(e, $"SiemensUtility -> GetDatas: {ConnectionString} error: {e.Message}");
|
||||
return new ReturnStruct<byte[]>
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -1,
|
||||
ErrorMsg = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,7 +290,7 @@ namespace Modbus.Net.Siemens
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="setContents">需要写入的数据</param>
|
||||
/// <returns>写入是否成功</returns>
|
||||
public override async Task<bool> SetDatasAsync(string startAddress, object[] setContents)
|
||||
public override async Task<ReturnStruct<bool>> SetDatasAsync(string startAddress, object[] setContents)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -283,12 +306,34 @@ namespace Modbus.Net.Siemens
|
||||
Wrapper.SendReceiveAsync<WriteRequestSiemensOutputStruct>(
|
||||
Wrapper[typeof(WriteRequestSiemensProtocol)],
|
||||
writeRequestSiemensInputStruct);
|
||||
return writeRequestSiemensOutputStruct?.AccessResult == SiemensAccessResult.NoError;
|
||||
return new ReturnStruct<bool> {
|
||||
Datas = writeRequestSiemensOutputStruct?.AccessResult == SiemensAccessResult.NoError,
|
||||
IsSuccess = writeRequestSiemensOutputStruct?.AccessResult == SiemensAccessResult.NoError,
|
||||
ErrorCode = writeRequestSiemensOutputStruct?.AccessResult == SiemensAccessResult.NoError ? 0 : (int)writeRequestSiemensOutputStruct?.AccessResult,
|
||||
ErrorMsg = writeRequestSiemensOutputStruct?.AccessResult.ToString()
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (SiemensProtocolErrorException e)
|
||||
{
|
||||
logger.LogError(e, $"ModbusUtility -> SetDatas: {ConnectionString} error");
|
||||
return false;
|
||||
logger.LogError(e, $"ModbusUtility -> SetDatas: {ConnectionString} error: {e.Message}");
|
||||
return new ReturnStruct<bool>
|
||||
{
|
||||
Datas = false,
|
||||
IsSuccess = false,
|
||||
ErrorCode = e.ErrorCode,
|
||||
ErrorMsg = e.Message
|
||||
};
|
||||
}
|
||||
catch (FormatException e)
|
||||
{
|
||||
logger.LogError(e, $"SiemensUtility -> GetDatas: {ConnectionString} error: {e.Message}");
|
||||
return new ReturnStruct<bool>
|
||||
{
|
||||
Datas = false,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -1,
|
||||
ErrorMsg = e.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,13 +19,13 @@ namespace Modbus.Net
|
||||
/// 读取数据
|
||||
/// </summary>
|
||||
/// <returns>从设备读取的数据</returns>
|
||||
Dictionary<string, ReturnUnit> GetDatas(MachineDataType getDataType);
|
||||
ReturnStruct<Dictionary<string, ReturnUnit>> GetDatas(MachineDataType getDataType);
|
||||
|
||||
/// <summary>
|
||||
/// 读取数据
|
||||
/// </summary>
|
||||
/// <returns>从设备读取的数据</returns>
|
||||
Task<Dictionary<string, ReturnUnit>> GetDatasAsync(MachineDataType getDataType);
|
||||
Task<ReturnStruct<Dictionary<string, ReturnUnit>>> GetDatasAsync(MachineDataType getDataType);
|
||||
|
||||
/// <summary>
|
||||
/// 写入数据
|
||||
@@ -33,7 +33,7 @@ namespace Modbus.Net
|
||||
/// <param name="setDataType">写入类型</param>
|
||||
/// <param name="values">需要写入的数据字典,当写入类型为Address时,键为需要写入的地址,当写入类型为CommunicationTag时,键为需要写入的单元的描述</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
bool SetDatas(MachineDataType setDataType, Dictionary<string, double> values);
|
||||
ReturnStruct<bool> SetDatas(MachineDataType setDataType, Dictionary<string, double> values);
|
||||
|
||||
/// <summary>
|
||||
/// 写入数据
|
||||
@@ -41,6 +41,6 @@ namespace Modbus.Net
|
||||
/// <param name="setDataType">写入类型</param>
|
||||
/// <param name="values">需要写入的数据字典,当写入类型为Address时,键为需要写入的地址,当写入类型为CommunicationTag时,键为需要写入的单元的描述</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
Task<bool> SetDatasAsync(MachineDataType setDataType, Dictionary<string, double> values);
|
||||
Task<ReturnStruct<bool>> SetDatasAsync(MachineDataType setDataType, Dictionary<string, double> values);
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">获取字节数个数</param>
|
||||
/// <returns>接收到的byte数据</returns>
|
||||
byte[] GetDatas(string startAddress, int getByteCount);
|
||||
ReturnStruct<byte[]> GetDatas(string startAddress, int getByteCount);
|
||||
|
||||
/// <summary>
|
||||
/// 获取数据
|
||||
@@ -30,7 +30,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">获取字节数个数</param>
|
||||
/// <returns>接收到的byte数据</returns>
|
||||
Task<byte[]> GetDatasAsync(string startAddress, int getByteCount);
|
||||
Task<ReturnStruct<byte[]>> GetDatasAsync(string startAddress, int getByteCount);
|
||||
|
||||
/// <summary>
|
||||
/// 获取数据
|
||||
@@ -38,7 +38,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getTypeAndCount">获取类型和个数</param>
|
||||
/// <returns>接收到的对应的类型和数据</returns>
|
||||
object[] GetDatas(string startAddress, KeyValuePair<Type, int> getTypeAndCount);
|
||||
ReturnStruct<object[]> GetDatas(string startAddress, KeyValuePair<Type, int> getTypeAndCount);
|
||||
|
||||
/// <summary>
|
||||
/// 获取数据
|
||||
@@ -46,7 +46,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getTypeAndCount">获取类型和个数</param>
|
||||
/// <returns>接收到的对应的类型和数据</returns>
|
||||
Task<object[]> GetDatasAsync(string startAddress, KeyValuePair<Type, int> getTypeAndCount);
|
||||
Task<ReturnStruct<object[]>> GetDatasAsync(string startAddress, KeyValuePair<Type, int> getTypeAndCount);
|
||||
|
||||
/// <summary>
|
||||
/// 获取数据
|
||||
@@ -55,7 +55,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">获取字节数个数</param>
|
||||
/// <returns>接收到的对应的类型和数据</returns>
|
||||
T[] GetDatas<T>(string startAddress, int getByteCount);
|
||||
ReturnStruct<T[]> GetDatas<T>(string startAddress, int getByteCount);
|
||||
|
||||
/// <summary>
|
||||
/// 获取数据
|
||||
@@ -64,7 +64,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">获取字节数个数</param>
|
||||
/// <returns>接收到的对应的类型和数据</returns>
|
||||
Task<T[]> GetDatasAsync<T>(string startAddress, int getByteCount);
|
||||
Task<ReturnStruct<T[]>> GetDatasAsync<T>(string startAddress, int getByteCount);
|
||||
|
||||
/// <summary>
|
||||
/// 获取数据
|
||||
@@ -72,14 +72,14 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getTypeAndCountList">获取类型和个数的队列</param>
|
||||
/// <returns>获取数据的对象数组,请强制转换成相应类型</returns>
|
||||
object[] GetDatas(string startAddress, IEnumerable<KeyValuePair<Type, int>> getTypeAndCountList);
|
||||
ReturnStruct<object[]> GetDatas(string startAddress, IEnumerable<KeyValuePair<Type, int>> getTypeAndCountList);
|
||||
|
||||
/// <summary>
|
||||
/// 获取数据
|
||||
/// </summary>
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getTypeAndCountList">获取类型和个数的队列</param>
|
||||
Task<object[]> GetDatasAsync(string startAddress, IEnumerable<KeyValuePair<Type, int>> getTypeAndCountList);
|
||||
Task<ReturnStruct<object[]>> GetDatasAsync(string startAddress, IEnumerable<KeyValuePair<Type, int>> getTypeAndCountList);
|
||||
|
||||
/// <summary>
|
||||
/// 设置数据
|
||||
@@ -87,7 +87,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="setContents">设置数据</param>
|
||||
/// <returns>是否设置成功</returns>
|
||||
bool SetDatas(string startAddress, object[] setContents);
|
||||
ReturnStruct<bool> SetDatas(string startAddress, object[] setContents);
|
||||
|
||||
/// <summary>
|
||||
/// 设置数据
|
||||
@@ -95,7 +95,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="setContents">设置数据</param>
|
||||
/// <returns>是否设置成功</returns>
|
||||
Task<bool> SetDatasAsync(string startAddress, object[] setContents);
|
||||
Task<ReturnStruct<bool>> SetDatasAsync(string startAddress, object[] setContents);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -107,13 +107,13 @@ namespace Modbus.Net
|
||||
/// 获取PLC时间
|
||||
/// </summary>
|
||||
/// <returns>PLC时间</returns>
|
||||
Task<DateTime> GetTimeAsync();
|
||||
Task<ReturnStruct<DateTime>> GetTimeAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 设置PLC时间
|
||||
/// </summary>
|
||||
/// <param name="setTime">设置PLC时间</param>
|
||||
/// <returns>设置是否成功</returns>
|
||||
Task<bool> SetTimeAsync(DateTime setTime);
|
||||
Task<ReturnStruct<bool>> SetTimeAsync(DateTime setTime);
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ namespace Modbus.Net
|
||||
/// <summary>
|
||||
/// 返回的数据值
|
||||
/// </summary>
|
||||
public Dictionary<string, ReturnUnit> ReturnValues { get; set; }
|
||||
public ReturnStruct<Dictionary<string, ReturnUnit>> ReturnValues { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -430,7 +430,7 @@ namespace Modbus.Net
|
||||
/// <param name="onFailure">失败回调方法,参数为设备ID</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NullReferenceException"></exception>
|
||||
public async Task<MachineSetJobScheduler> Deal(string queryId = null, Func<string, Task> onSuccess = null, Func<string, Task> onFailure = null)
|
||||
public async Task<MachineSetJobScheduler> Deal(string queryId = null, Func<string, Task> onSuccess = null, Func<string, int, string, Task> onFailure = null)
|
||||
{
|
||||
return await Deal<string>(queryId, onSuccess, onFailure);
|
||||
}
|
||||
@@ -443,7 +443,7 @@ namespace Modbus.Net
|
||||
/// <param name="onFailure">失败回调方法,参数为设备ID</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NullReferenceException"></exception>
|
||||
public async Task<MachineSetJobScheduler> Deal<TMachineKey>(string queryId = null, Func<string, Task> onSuccess = null, Func<string, Task> onFailure = null) where TMachineKey : IEquatable<TMachineKey>
|
||||
public async Task<MachineSetJobScheduler> Deal<TMachineKey>(string queryId = null, Func<string, Task> onSuccess = null, Func<string, int, string, Task> onFailure = null) where TMachineKey : IEquatable<TMachineKey>
|
||||
{
|
||||
if (queryId == null) return new MachineSetJobScheduler(_scheduler, _trigger, _parentJobKey);
|
||||
JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name);
|
||||
@@ -504,7 +504,7 @@ namespace Modbus.Net
|
||||
|
||||
if (QueryMethod != null && values != null)
|
||||
{
|
||||
context.JobDetail.JobDataMap.Put("SetValue", QueryMethodDispatch(new DataReturnDef() { MachineId = machine == null ? null : ((IMachineProperty<TMachineKey>)machine).GetMachineIdString(), ReturnValues = (Dictionary<string, ReturnUnit>)values }));
|
||||
context.JobDetail.JobDataMap.Put("SetValue", QueryMethodDispatch(new DataReturnDef() { MachineId = machine == null ? null : ((IMachineProperty<TMachineKey>)machine).GetMachineIdString(), ReturnValues = (ReturnStruct<Dictionary<string, ReturnUnit>>)values }));
|
||||
await context.Scheduler.AddJob(context.JobDetail, true, false);
|
||||
}
|
||||
}
|
||||
@@ -527,7 +527,7 @@ namespace Modbus.Net
|
||||
context.JobDetail.JobDataMap.TryGetValue("Value", out values);
|
||||
context.JobDetail.JobDataMap.TryGetValue("SetValue", out valuesSet);
|
||||
if (valuesSet == null && values != null)
|
||||
valuesSet = ((Dictionary<string, ReturnUnit>)values).MapGetValuesToSetValues();
|
||||
valuesSet = ((ReturnStruct<Dictionary<string, ReturnUnit>>)values).Datas.MapGetValuesToSetValues();
|
||||
|
||||
if (valuesSet == null)
|
||||
{
|
||||
@@ -556,14 +556,14 @@ namespace Modbus.Net
|
||||
context.JobDetail.JobDataMap.TryGetValue("Success", out success);
|
||||
context.JobDetail.JobDataMap.TryGetValue("OnSuccess", out onSuccess);
|
||||
context.JobDetail.JobDataMap.TryGetValue("OnFailure", out onFailure);
|
||||
bool? successValue = (bool?)success;
|
||||
if (successValue == true && onSuccess != null)
|
||||
ReturnStruct<bool> successValue = (ReturnStruct<bool>)success;
|
||||
if (successValue.IsSuccess == true && onSuccess != null)
|
||||
{
|
||||
await ((Func<string, Task>)onSuccess)(((IMachineProperty<TMachineKey>)machine).GetMachineIdString());
|
||||
}
|
||||
if (successValue == false && onFailure != null)
|
||||
if (successValue.IsSuccess == false && onFailure != null)
|
||||
{
|
||||
await ((Func<string, Task>)onFailure)(((IMachineProperty<TMachineKey>)machine).GetMachineIdString());
|
||||
await ((Func<string, int, string, Task>)onFailure)(((IMachineProperty<TMachineKey>)machine).GetMachineIdString(), successValue.ErrorCode, successValue.ErrorMsg);
|
||||
}
|
||||
|
||||
context.JobDetail.JobDataMap.Remove("Success");
|
||||
|
||||
@@ -193,7 +193,7 @@ namespace Modbus.Net
|
||||
/// 读取数据
|
||||
/// </summary>
|
||||
/// <returns>从设备读取的数据</returns>
|
||||
public Dictionary<string, ReturnUnit> GetDatas(MachineDataType getDataType)
|
||||
public ReturnStruct<Dictionary<string, ReturnUnit>> GetDatas(MachineDataType getDataType)
|
||||
{
|
||||
return AsyncHelper.RunSync(() => GetDatasAsync(getDataType));
|
||||
}
|
||||
@@ -203,7 +203,7 @@ namespace Modbus.Net
|
||||
/// 读取数据
|
||||
/// </summary>
|
||||
/// <returns>从设备读取的数据</returns>
|
||||
public async Task<Dictionary<string, ReturnUnit>> GetDatasAsync(MachineDataType getDataType)
|
||||
public async Task<ReturnStruct<Dictionary<string, ReturnUnit>>> GetDatasAsync(MachineDataType getDataType)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -212,7 +212,13 @@ namespace Modbus.Net
|
||||
if (!BaseUtility.IsConnected)
|
||||
await BaseUtility.ConnectAsync();
|
||||
//如果无法连接,终止
|
||||
if (!BaseUtility.IsConnected) return null;
|
||||
if (!BaseUtility.IsConnected) return
|
||||
new ReturnStruct<Dictionary<string, ReturnUnit>>() {
|
||||
Datas = null,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -1,
|
||||
ErrorMsg = "Connection Error"
|
||||
};
|
||||
//遍历每一个实际向设备获取数据的连续地址
|
||||
foreach (var communicateAddress in CommunicateAddresses)
|
||||
{
|
||||
@@ -229,12 +235,31 @@ namespace Modbus.Net
|
||||
|
||||
|
||||
//如果没有数据,终止
|
||||
if (datas == null || datas.Length != 0 && datas.Length <
|
||||
if (datas.IsSuccess == false || datas.Datas == null)
|
||||
{
|
||||
return new ReturnStruct<Dictionary<string, ReturnUnit>>()
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = false,
|
||||
ErrorCode = datas.ErrorCode,
|
||||
ErrorMsg = datas.ErrorMsg
|
||||
};
|
||||
}
|
||||
else if (datas.Datas.Length != 0 && datas.Datas.Length <
|
||||
(int)
|
||||
Math.Ceiling(communicateAddress.GetCount *
|
||||
BigEndianValueHelper.Instance.ByteLength[
|
||||
communicateAddress.DataType.FullName]))
|
||||
return null;
|
||||
{
|
||||
return new ReturnStruct<Dictionary<string, ReturnUnit>>()
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -2,
|
||||
ErrorMsg = "Data length mismatch"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
foreach (var address in communicateAddress.OriginalAddresses)
|
||||
@@ -283,7 +308,7 @@ namespace Modbus.Net
|
||||
try
|
||||
{
|
||||
//如果没有数据返回空
|
||||
if (datas.Length == 0)
|
||||
if (datas.Datas.Length == 0)
|
||||
ans.Add(key, new ReturnUnit
|
||||
{
|
||||
DeviceValue = null,
|
||||
@@ -296,7 +321,7 @@ namespace Modbus.Net
|
||||
DeviceValue =
|
||||
Convert.ToDouble(
|
||||
ValueHelper.GetInstance(BaseUtility.Endian)
|
||||
.GetValue(datas, ref localMainPos, ref localSubPos,
|
||||
.GetValue(datas.Datas, ref localMainPos, ref localSubPos,
|
||||
address.DataType)) * address.Zoom,
|
||||
AddressUnit = address.MapAddressUnitTUnitKeyToAddressUnit(),
|
||||
});
|
||||
@@ -308,7 +333,13 @@ namespace Modbus.Net
|
||||
|
||||
if (ErrorCount >= _maxErrorCount)
|
||||
Disconnect();
|
||||
return null;
|
||||
return new ReturnStruct<Dictionary<string, ReturnUnit>>()
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -3,
|
||||
ErrorMsg = "Data translation mismatch"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -318,7 +349,13 @@ namespace Modbus.Net
|
||||
//返回数据
|
||||
if (ans.All(p => p.Value.DeviceValue == null)) ans = null;
|
||||
ErrorCount = 0;
|
||||
return ans;
|
||||
return new ReturnStruct<Dictionary<string, ReturnUnit>>
|
||||
{
|
||||
Datas = ans,
|
||||
IsSuccess = true,
|
||||
ErrorCode = 0,
|
||||
ErrorMsg = ""
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -327,7 +364,13 @@ namespace Modbus.Net
|
||||
|
||||
if (ErrorCount >= _maxErrorCount)
|
||||
Disconnect();
|
||||
return null;
|
||||
return new ReturnStruct<Dictionary<string, ReturnUnit>>()
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -100,
|
||||
ErrorMsg = "Unknown Exception"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,7 +380,7 @@ namespace Modbus.Net
|
||||
/// <param name="setDataType">写入类型</param>
|
||||
/// <param name="values">需要写入的数据字典,当写入类型为Address时,键为需要写入的地址,当写入类型为CommunicationTag时,键为需要写入的单元的描述</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
public bool SetDatas(MachineDataType setDataType, Dictionary<string, double> values)
|
||||
public ReturnStruct<bool> SetDatas(MachineDataType setDataType, Dictionary<string, double> values)
|
||||
{
|
||||
return AsyncHelper.RunSync(() => SetDatasAsync(setDataType, values));
|
||||
}
|
||||
@@ -348,7 +391,7 @@ namespace Modbus.Net
|
||||
/// <param name="setDataType">写入类型</param>
|
||||
/// <param name="values">需要写入的数据字典,当写入类型为Address时,键为需要写入的地址,当写入类型为CommunicationTag时,键为需要写入的单元的描述</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
public async Task<bool> SetDatasAsync(MachineDataType setDataType, Dictionary<string, double> values)
|
||||
public async Task<ReturnStruct<bool>> SetDatasAsync(MachineDataType setDataType, Dictionary<string, double> values)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -356,7 +399,13 @@ namespace Modbus.Net
|
||||
if (!BaseUtility.IsConnected)
|
||||
await BaseUtility.ConnectAsync();
|
||||
//如果设备无法连接,终止
|
||||
if (!BaseUtility.IsConnected) return false;
|
||||
if (!BaseUtility.IsConnected) return new ReturnStruct<bool>()
|
||||
{
|
||||
Datas = false,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -1,
|
||||
ErrorMsg = "Connection Error"
|
||||
};
|
||||
var addresses = new List<AddressUnit<TUnitKey>>();
|
||||
//遍历每个要设置的值
|
||||
foreach (var value in values)
|
||||
@@ -434,12 +483,28 @@ namespace Modbus.Net
|
||||
var datas = datasReturn;
|
||||
|
||||
//如果没有数据,终止
|
||||
if (datas == null || datas.Length <
|
||||
if (datas.IsSuccess == false || datas.Datas == null)
|
||||
{
|
||||
return new ReturnStruct<bool>()
|
||||
{
|
||||
Datas = false,
|
||||
IsSuccess = false,
|
||||
ErrorCode = datas.ErrorCode,
|
||||
ErrorMsg = datas.ErrorMsg
|
||||
};
|
||||
}
|
||||
else if(datas.Datas.Length <
|
||||
(int)
|
||||
Math.Ceiling(communicateAddress.GetCount *
|
||||
BigEndianValueHelper.Instance.ByteLength[
|
||||
communicateAddress.DataType.FullName]))
|
||||
return false;
|
||||
return new ReturnStruct<bool>()
|
||||
{
|
||||
Datas = false,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -2,
|
||||
ErrorMsg = "Data length not match"
|
||||
};
|
||||
|
||||
foreach (var addressUnit in communicateAddress.OriginalAddresses)
|
||||
{
|
||||
@@ -507,13 +572,19 @@ namespace Modbus.Net
|
||||
//将要写入的值加入队列
|
||||
var data = Convert.ChangeType(value.Value / addressUnit.Zoom, dataType);
|
||||
|
||||
if (!valueHelper.SetValue(datas, mainByteCount, localByteCount, data))
|
||||
return false;
|
||||
if (!valueHelper.SetValue(datas.Datas, mainByteCount, localByteCount, data))
|
||||
return new ReturnStruct<bool>()
|
||||
{
|
||||
Datas = false,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -3,
|
||||
ErrorMsg = "Data translation mismatch"
|
||||
}; ;
|
||||
}
|
||||
//写入数据
|
||||
await
|
||||
BaseUtility.GetUtilityMethods<IUtilityMethodData>().SetDatasAsync(addressStart,
|
||||
valueHelper.ByteArrayToObjectArray(datas,
|
||||
valueHelper.ByteArrayToObjectArray(datas.Datas,
|
||||
new KeyValuePair<Type, int>(communicateAddress.DataType, communicateAddress.GetCount)));
|
||||
}
|
||||
//如果不保持连接,断开连接
|
||||
@@ -527,9 +598,21 @@ namespace Modbus.Net
|
||||
|
||||
if (ErrorCount >= _maxErrorCount)
|
||||
Disconnect();
|
||||
return false;
|
||||
return new ReturnStruct<bool>()
|
||||
{
|
||||
Datas = false,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -100,
|
||||
ErrorMsg = "Unknown Exception"
|
||||
};
|
||||
}
|
||||
return true;
|
||||
return new ReturnStruct<bool>()
|
||||
{
|
||||
Datas = true,
|
||||
IsSuccess = true,
|
||||
ErrorCode = 0,
|
||||
ErrorMsg = ""
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<AssemblyName>Modbus.Net</AssemblyName>
|
||||
<RootNamespace>Modbus.Net</RootNamespace>
|
||||
<PackageId>Modbus.Net</PackageId>
|
||||
<Version>1.4.0-beta03</Version>
|
||||
<Version>1.4.0-beta04</Version>
|
||||
<Product>Modbus.Net</Product>
|
||||
<Authors>Chris L.(Luo Sheng)</Authors>
|
||||
<Company>Hangzhou Delian Science Technology Co.,Ltd.</Company>
|
||||
|
||||
32
Modbus.Net/Modbus.Net/ReturnStruct/ReturnStruct.cs
Normal file
32
Modbus.Net/Modbus.Net/ReturnStruct/ReturnStruct.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// 返回引用类型
|
||||
/// </summary>
|
||||
/// <typeparam name="TDataType"></typeparam>
|
||||
public struct ReturnStruct<TDataType>
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据
|
||||
/// </summary>
|
||||
public TDataType Datas { get; set; }
|
||||
/// <summary>
|
||||
/// 操作是否成功
|
||||
/// </summary>
|
||||
public bool IsSuccess { get; set; }
|
||||
/// <summary>
|
||||
/// 错误代码
|
||||
/// </summary>
|
||||
public int ErrorCode { get; set; }
|
||||
/// <summary>
|
||||
/// 错误详细信息
|
||||
/// </summary>
|
||||
public string ErrorMsg { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Policy;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
/// <summary>
|
||||
/// 端格式
|
||||
@@ -85,7 +87,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">获取字节数个数</param>
|
||||
/// <returns>接收到的byte数据</returns>
|
||||
public virtual byte[] GetDatas(string startAddress, int getByteCount)
|
||||
public virtual ReturnStruct<byte[]> GetDatas(string startAddress, int getByteCount)
|
||||
{
|
||||
return AsyncHelper.RunSync(() => GetDatasAsync(startAddress, getByteCount));
|
||||
}
|
||||
@@ -96,7 +98,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">获取字节数个数</param>
|
||||
/// <returns>接收到的byte数据</returns>
|
||||
public abstract Task<byte[]> GetDatasAsync(string startAddress, int getByteCount);
|
||||
public abstract Task<ReturnStruct<byte[]>> GetDatasAsync(string startAddress, int getByteCount);
|
||||
|
||||
/// <summary>
|
||||
/// 获取数据
|
||||
@@ -104,7 +106,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getTypeAndCount">获取类型和个数</param>
|
||||
/// <returns>接收到的对应的类型和数据</returns>
|
||||
public virtual object[] GetDatas(string startAddress,
|
||||
public virtual ReturnStruct<object[]> GetDatas(string startAddress,
|
||||
KeyValuePair<Type, int> getTypeAndCount)
|
||||
{
|
||||
return AsyncHelper.RunSync(() => GetDatasAsync(startAddress, getTypeAndCount));
|
||||
@@ -116,7 +118,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getTypeAndCount">获取类型和个数</param>
|
||||
/// <returns>接收到的对应的类型和数据</returns>
|
||||
public virtual async Task<object[]> GetDatasAsync(string startAddress,
|
||||
public virtual async Task<ReturnStruct<object[]>> GetDatasAsync(string startAddress,
|
||||
KeyValuePair<Type, int> getTypeAndCount)
|
||||
{
|
||||
try
|
||||
@@ -126,12 +128,34 @@ namespace Modbus.Net
|
||||
var getReturnValue = await GetDatasAsync(startAddress,
|
||||
(int)Math.Ceiling(bCount * getTypeAndCount.Value));
|
||||
var getBytes = getReturnValue;
|
||||
return ValueHelper.GetInstance(Endian).ByteArrayToObjectArray(getBytes, getTypeAndCount);
|
||||
if (getBytes.IsSuccess == false || getBytes.Datas == null)
|
||||
{
|
||||
return new ReturnStruct<object[]>
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = getBytes.IsSuccess,
|
||||
ErrorCode = getBytes.ErrorCode,
|
||||
ErrorMsg = getBytes.ErrorMsg
|
||||
};
|
||||
}
|
||||
return new ReturnStruct<object[]>
|
||||
{
|
||||
Datas = ValueHelper.GetInstance(Endian).ByteArrayToObjectArray(getBytes.Datas, getTypeAndCount),
|
||||
IsSuccess = getBytes.IsSuccess,
|
||||
ErrorCode = getBytes.ErrorCode,
|
||||
ErrorMsg = getBytes.ErrorMsg
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, $"ModbusUtility -> GetDatas: {ConnectionString} error");
|
||||
return null;
|
||||
return new ReturnStruct<object[]>
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -100,
|
||||
ErrorMsg = "Unknown Error"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +166,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">获取字节数个数</param>
|
||||
/// <returns>接收到的对应的类型和数据</returns>
|
||||
public virtual T[] GetDatas<T>(string startAddress,
|
||||
public virtual ReturnStruct<T[]> GetDatas<T>(string startAddress,
|
||||
int getByteCount)
|
||||
{
|
||||
return AsyncHelper.RunSync(() => GetDatasAsync<T>(startAddress, getByteCount));
|
||||
@@ -155,19 +179,41 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">获取字节数个数</param>
|
||||
/// <returns>接收到的对应的类型和数据</returns>
|
||||
public virtual async Task<T[]> GetDatasAsync<T>(string startAddress,
|
||||
public virtual async Task<ReturnStruct<T[]>> GetDatasAsync<T>(string startAddress,
|
||||
int getByteCount)
|
||||
{
|
||||
try
|
||||
{
|
||||
var getBytes = await GetDatasAsync(startAddress,
|
||||
new KeyValuePair<Type, int>(typeof(T), getByteCount));
|
||||
return ValueHelper.GetInstance(Endian).ObjectArrayToDestinationArray<T>(getBytes);
|
||||
if (getBytes.IsSuccess == false || getBytes.Datas == null)
|
||||
{
|
||||
return new ReturnStruct<T[]>
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = getBytes.IsSuccess,
|
||||
ErrorCode = getBytes.ErrorCode,
|
||||
ErrorMsg = getBytes.ErrorMsg
|
||||
};
|
||||
}
|
||||
return new ReturnStruct<T[]>
|
||||
{
|
||||
Datas = ValueHelper.GetInstance(Endian).ObjectArrayToDestinationArray<T>(getBytes.Datas),
|
||||
IsSuccess = getBytes.IsSuccess,
|
||||
ErrorCode = getBytes.ErrorCode,
|
||||
ErrorMsg = getBytes.ErrorMsg
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, $"ModbusUtility -> GetDatas Generic: {ConnectionString} error");
|
||||
return null;
|
||||
return new ReturnStruct<T[]>
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -100,
|
||||
ErrorMsg = "Unknown Error"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +223,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getTypeAndCountList">获取类型和个数的队列</param>
|
||||
/// <returns>获取数据的对象数组,请强制转换成相应类型</returns>
|
||||
public virtual object[] GetDatas(string startAddress,
|
||||
public virtual ReturnStruct<object[]> GetDatas(string startAddress,
|
||||
IEnumerable<KeyValuePair<Type, int>> getTypeAndCountList)
|
||||
{
|
||||
return
|
||||
@@ -189,7 +235,7 @@ namespace Modbus.Net
|
||||
/// </summary>
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getTypeAndCountList">获取类型和个数的队列</param>
|
||||
public virtual async Task<object[]> GetDatasAsync(string startAddress,
|
||||
public virtual async Task<ReturnStruct<object[]>> GetDatasAsync(string startAddress,
|
||||
IEnumerable<KeyValuePair<Type, int>> getTypeAndCountList)
|
||||
{
|
||||
try
|
||||
@@ -203,12 +249,34 @@ namespace Modbus.Net
|
||||
select (int)Math.Ceiling(bCount * getTypeAndCount.Value)).Sum();
|
||||
var getReturnValue = await GetDatasAsync(startAddress, bAllCount);
|
||||
var getBytes = getReturnValue;
|
||||
return ValueHelper.GetInstance(Endian).ByteArrayToObjectArray(getBytes, translateTypeAndCount);
|
||||
if (getBytes.IsSuccess == false || getBytes.Datas == null)
|
||||
{
|
||||
return new ReturnStruct<object[]>
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = getBytes.IsSuccess,
|
||||
ErrorCode = getBytes.ErrorCode,
|
||||
ErrorMsg = getBytes.ErrorMsg
|
||||
};
|
||||
}
|
||||
return new ReturnStruct<object[]>
|
||||
{
|
||||
Datas = ValueHelper.GetInstance(Endian).ByteArrayToObjectArray(getBytes.Datas, translateTypeAndCount),
|
||||
IsSuccess = getBytes.IsSuccess,
|
||||
ErrorCode = getBytes.ErrorCode,
|
||||
ErrorMsg = getBytes.ErrorMsg
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, $"ModbusUtility -> GetDatas pair: {ConnectionString} error");
|
||||
return null;
|
||||
return new ReturnStruct<object[]>
|
||||
{
|
||||
Datas = null,
|
||||
IsSuccess = false,
|
||||
ErrorCode = -100,
|
||||
ErrorMsg = "Unknown Error"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,7 +286,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="setContents">设置数据</param>
|
||||
/// <returns>是否设置成功</returns>
|
||||
public virtual bool SetDatas(string startAddress, object[] setContents)
|
||||
public virtual ReturnStruct<bool> SetDatas(string startAddress, object[] setContents)
|
||||
{
|
||||
return AsyncHelper.RunSync(() => SetDatasAsync(startAddress, setContents));
|
||||
}
|
||||
@@ -229,7 +297,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="setContents">设置数据</param>
|
||||
/// <returns>是否设置成功</returns>
|
||||
public abstract Task<bool> SetDatasAsync(string startAddress, object[] setContents);
|
||||
public abstract Task<ReturnStruct<bool>> SetDatasAsync(string startAddress, object[] setContents);
|
||||
|
||||
/// <summary>
|
||||
/// 协议是否遵循小端格式
|
||||
|
||||
@@ -59,11 +59,11 @@ namespace AnyType.Controllers
|
||||
returnValues =>
|
||||
{
|
||||
//唯一的参数包含返回值,是一个唯一标识符(machine的第二个参数),返回值(类型ReturnUnit)的键值对。
|
||||
if (returnValues.ReturnValues != null)
|
||||
if (returnValues.ReturnValues.Datas != null)
|
||||
{
|
||||
lock (values)
|
||||
{
|
||||
var unitValues = from val in returnValues.ReturnValues
|
||||
var unitValues = from val in returnValues.ReturnValues.Datas
|
||||
select
|
||||
new Tuple<AddressUnit, double?>(
|
||||
addressUnits.FirstOrDefault(p => p.CommunicationTag == val.Key)!, val.Value.DeviceValue);
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace CrossLamp.Controllers
|
||||
await _utility.ConnectAsync();
|
||||
}
|
||||
Lamp light = new Lamp();
|
||||
object[] lampsbyte = await _utility.GetDatasAsync("0X 1", new KeyValuePair<Type, int>(typeof(bool), 7));
|
||||
object[] lampsbyte = (await _utility.GetDatasAsync("0X 1", new KeyValuePair<Type, int>(typeof(bool), 7))).Datas;
|
||||
bool[] lamps = BigEndianValueHelper.Instance.ObjectArrayToDestinationArray<bool>(lampsbyte);
|
||||
if (lamps[0])
|
||||
{
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace MachineJob.Service
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task OnFailure(string machineId)
|
||||
public Task OnFailure(string machineId, int errorCode, string errorMsg)
|
||||
{
|
||||
_logger.LogError("Machine {0} set failure", machineId);
|
||||
return Task.CompletedTask;
|
||||
@@ -72,7 +72,7 @@ namespace MachineJob.Service
|
||||
|
||||
private Dictionary<string, double> QueryConsole(DataReturnDef dataReturnDef)
|
||||
{
|
||||
var values = dataReturnDef.ReturnValues;
|
||||
var values = dataReturnDef.ReturnValues.Datas;
|
||||
foreach (var value in values)
|
||||
{
|
||||
_logger.LogInformation(dataReturnDef.MachineId + " " + value.Key + " " + value.Value.DeviceValue);
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace TripleAdd.Controllers
|
||||
utility.AddressTranslator = new AddressTranslatorModbus();
|
||||
await utility.ConnectAsync();
|
||||
}
|
||||
object[] getNum = await utility.GetDatasAsync("4X 1", new KeyValuePair<Type, int>(typeof(ushort), 4));
|
||||
object[] getNum = (await utility.GetDatasAsync("4X 1", new KeyValuePair<Type, int>(typeof(ushort), 4))).Datas;
|
||||
ushort[] getNumUshorts = BigEndianValueHelper.Instance.ObjectArrayToDestinationArray<ushort>(getNum);
|
||||
return SetValue(getNumUshorts);
|
||||
}
|
||||
@@ -64,7 +64,7 @@ namespace TripleAdd.Controllers
|
||||
machine.AddressCombiner = new AddressCombinerContinus(machine.AddressTranslator, 100000);
|
||||
machine.AddressCombinerSet = new AddressCombinerContinus(machine.AddressTranslator, 100000);
|
||||
}
|
||||
var resultFormat = (await machine.GetDatasAsync(MachineDataType.CommunicationTag)).MapGetValuesToSetValues();
|
||||
var resultFormat = (await machine.GetDatasAsync(MachineDataType.CommunicationTag)).Datas.MapGetValuesToSetValues();
|
||||
return SetValue(new ushort[4] { (ushort)resultFormat["Add1"], (ushort)resultFormat["Add2"], (ushort)resultFormat["Add3"], (ushort)resultFormat["Ans"] });
|
||||
}
|
||||
|
||||
|
||||
@@ -48,8 +48,8 @@ namespace Modbus.Net.Tests
|
||||
await _modbusTcpMachine.SetDatasAsync(MachineDataType.Address, dic1);
|
||||
var ans = await _modbusTcpMachine.GetDatasAsync(MachineDataType.Address);
|
||||
var ans2 = await _modbusTcpMachine2.GetDatasAsync(MachineDataType.Address);
|
||||
Assert.AreEqual(ans["4X 1.0"].DeviceValue, dic1["4X 1"]);
|
||||
Assert.AreEqual(ans2["4X 1.0"].DeviceValue, (ushort)dic1["4X 1"] % 256 * 256 + (ushort)dic1["4X 1"] / 256);
|
||||
Assert.AreEqual(ans.Datas["4X 1.0"].DeviceValue, dic1["4X 1"]);
|
||||
Assert.AreEqual(ans2.Datas["4X 1.0"].DeviceValue, (ushort)dic1["4X 1"] % 256 * 256 + (ushort)dic1["4X 1"] / 256);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace Modbus.Net.Tests
|
||||
var success = await baseMachine.BaseUtility.GetUtilityMethods<IUtilityMethodTime>().SetTimeAsync(DateTime.Now);
|
||||
Assert.AreEqual(success, true);
|
||||
var time = await baseMachine.BaseUtility.GetUtilityMethods<IUtilityMethodTime>().GetTimeAsync();
|
||||
Assert.AreEqual((time.ToUniversalTime() - DateTime.Now.ToUniversalTime()).Seconds < 10, true);
|
||||
Assert.AreEqual((time.Datas.ToUniversalTime() - DateTime.Now.ToUniversalTime()).Seconds < 10, true);
|
||||
baseMachine.Disconnect();
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ namespace Modbus.Net.Tests
|
||||
});
|
||||
Assert.AreEqual(success, true);
|
||||
var datas = await baseMachine.GetMachineMethods<IMachineMethodData>().GetDatasAsync(MachineDataType.Address);
|
||||
Assert.AreEqual(datas["0X 1.0"].DeviceValue, 1);
|
||||
Assert.AreEqual(datas.Datas["0X 1.0"].DeviceValue, 1);
|
||||
success = await baseMachine.GetMachineMethods<IMachineMethodData>().SetDatasAsync(
|
||||
MachineDataType.Address,
|
||||
new Dictionary<string, double>
|
||||
|
||||
@@ -135,18 +135,18 @@ namespace Modbus.Net.Tests
|
||||
_modbusRtuMachine1.Disconnect();
|
||||
_modbusRtuMachine2.Disconnect();
|
||||
|
||||
Assert.AreEqual(ans["A1"].DeviceValue, dic1["A1"]);
|
||||
Assert.AreEqual(ans2["A1"].DeviceValue, dic2["A1"]);
|
||||
Assert.AreEqual(ans["A2"].DeviceValue, dic1["A2"]);
|
||||
Assert.AreEqual(ans2["A2"].DeviceValue, dic2["A2"]);
|
||||
Assert.AreEqual(ans["A3"].DeviceValue, dic1["A3"]);
|
||||
Assert.AreEqual(ans2["A3"].DeviceValue, dic2["A3"]);
|
||||
Assert.AreEqual(ans["A4"].DeviceValue, dic1["A4"]);
|
||||
Assert.AreEqual(ans2["A4"].DeviceValue, dic2["A4"]);
|
||||
Assert.AreEqual(ans["A5"].DeviceValue, dic1["A5"]);
|
||||
Assert.AreEqual(ans2["A5"].DeviceValue, dic2["A5"]);
|
||||
Assert.AreEqual(ans["A6"].DeviceValue, dic1["A6"]);
|
||||
Assert.AreEqual(ans2["A6"].DeviceValue, dic2["A6"]);
|
||||
Assert.AreEqual(ans.Datas["A1"].DeviceValue, dic1["A1"]);
|
||||
Assert.AreEqual(ans2.Datas["A1"].DeviceValue, dic2["A1"]);
|
||||
Assert.AreEqual(ans.Datas["A2"].DeviceValue, dic1["A2"]);
|
||||
Assert.AreEqual(ans2.Datas["A2"].DeviceValue, dic2["A2"]);
|
||||
Assert.AreEqual(ans.Datas["A3"].DeviceValue, dic1["A3"]);
|
||||
Assert.AreEqual(ans2.Datas["A3"].DeviceValue, dic2["A3"]);
|
||||
Assert.AreEqual(ans.Datas["A4"].DeviceValue, dic1["A4"]);
|
||||
Assert.AreEqual(ans2.Datas["A4"].DeviceValue, dic2["A4"]);
|
||||
Assert.AreEqual(ans.Datas["A5"].DeviceValue, dic1["A5"]);
|
||||
Assert.AreEqual(ans2.Datas["A5"].DeviceValue, dic2["A5"]);
|
||||
Assert.AreEqual(ans.Datas["A6"].DeviceValue, dic1["A6"]);
|
||||
Assert.AreEqual(ans2.Datas["A6"].DeviceValue, dic2["A6"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,9 +56,9 @@ namespace Modbus.Net.Tests
|
||||
var ans = await _modbusTcpMachine.GetDatasAsync(MachineDataType.Address);
|
||||
var ans2 = await _modbusRtuMachine.GetDatasAsync(MachineDataType.Address);
|
||||
var ans3 = await _modbusAsciiMachine.GetDatasAsync(MachineDataType.Address);
|
||||
Assert.AreEqual(ans["0X 1.0"].DeviceValue, dic1["0X 1.0"]);
|
||||
Assert.AreEqual(ans2["0X 1.0"].DeviceValue, dic1["0X 1.0"]);
|
||||
Assert.AreEqual(ans3["0X 1.0"].DeviceValue, dic1["0X 1.0"]);
|
||||
Assert.AreEqual(ans.Datas["0X 1.0"].DeviceValue, dic1["0X 1.0"]);
|
||||
Assert.AreEqual(ans2.Datas["0X 1.0"].DeviceValue, dic1["0X 1.0"]);
|
||||
Assert.AreEqual(ans3.Datas["0X 1.0"].DeviceValue, dic1["0X 1.0"]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -83,9 +83,9 @@ namespace Modbus.Net.Tests
|
||||
var ans = await _modbusTcpMachine.GetDatasAsync(MachineDataType.Address);
|
||||
var ans2 = await _modbusRtuMachine.GetDatasAsync(MachineDataType.Address);
|
||||
var ans3 = await _modbusAsciiMachine.GetDatasAsync(MachineDataType.Address);
|
||||
Assert.AreEqual(ans["1X 1.0"].DeviceValue, 0);
|
||||
Assert.AreEqual(ans2["1X 1.0"].DeviceValue, 0);
|
||||
Assert.AreEqual(ans3["1X 1.0"].DeviceValue, 0);
|
||||
Assert.AreEqual(ans.Datas["1X 1.0"].DeviceValue, 0);
|
||||
Assert.AreEqual(ans2.Datas["1X 1.0"].DeviceValue, 0);
|
||||
Assert.AreEqual(ans3.Datas["1X 1.0"].DeviceValue, 0);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -110,9 +110,9 @@ namespace Modbus.Net.Tests
|
||||
var ans = await _modbusTcpMachine.GetDatasAsync(MachineDataType.Address);
|
||||
var ans2 = await _modbusRtuMachine.GetDatasAsync(MachineDataType.Address);
|
||||
var ans3 = await _modbusAsciiMachine.GetDatasAsync(MachineDataType.Address);
|
||||
Assert.AreEqual(ans["3X 1.0"].DeviceValue, 0);
|
||||
Assert.AreEqual(ans2["3X 1.0"].DeviceValue, 0);
|
||||
Assert.AreEqual(ans3["3X 1.0"].DeviceValue, 0);
|
||||
Assert.AreEqual(ans.Datas["3X 1.0"].DeviceValue, 0);
|
||||
Assert.AreEqual(ans2.Datas["3X 1.0"].DeviceValue, 0);
|
||||
Assert.AreEqual(ans3.Datas["3X 1.0"].DeviceValue, 0);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -149,9 +149,9 @@ namespace Modbus.Net.Tests
|
||||
var ans = await _modbusTcpMachine.GetDatasAsync(MachineDataType.Address);
|
||||
var ans2 = await _modbusRtuMachine.GetDatasAsync(MachineDataType.Address);
|
||||
var ans3 = await _modbusAsciiMachine.GetDatasAsync(MachineDataType.Address);
|
||||
Assert.AreEqual(ans["4X 1.0"].DeviceValue, dic1["4X 1"]);
|
||||
Assert.AreEqual(ans2["4X 1.0"].DeviceValue, dic1["4X 1"]);
|
||||
Assert.AreEqual(ans3["4X 1.0"].DeviceValue, dic1["4X 1"]);
|
||||
Assert.AreEqual(ans.Datas["4X 1.0"].DeviceValue, dic1["4X 1"]);
|
||||
Assert.AreEqual(ans2.Datas["4X 1.0"].DeviceValue, dic1["4X 1"]);
|
||||
Assert.AreEqual(ans3.Datas["4X 1.0"].DeviceValue, dic1["4X 1"]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -250,24 +250,24 @@ namespace Modbus.Net.Tests
|
||||
var ans2 = await _modbusRtuMachine.GetDatasAsync(MachineDataType.CommunicationTag);
|
||||
var ans3 = await _modbusAsciiMachine.GetDatasAsync(MachineDataType.CommunicationTag);
|
||||
|
||||
Assert.AreEqual(ans["A1"].DeviceValue, dic1["A1"]);
|
||||
Assert.AreEqual(ans["A2"].DeviceValue, dic1["A2"]);
|
||||
Assert.AreEqual(ans["A3"].DeviceValue, dic1["A3"]);
|
||||
Assert.AreEqual(ans["A4"].DeviceValue, dic1["A4"]);
|
||||
Assert.AreEqual(ans["A5"].DeviceValue, dic1["A5"]);
|
||||
Assert.AreEqual(ans["A6"].DeviceValue, dic1["A6"]);
|
||||
Assert.AreEqual(ans2["A1"].DeviceValue, dic1["A1"]);
|
||||
Assert.AreEqual(ans2["A2"].DeviceValue, dic1["A2"]);
|
||||
Assert.AreEqual(ans2["A3"].DeviceValue, dic1["A3"]);
|
||||
Assert.AreEqual(ans2["A4"].DeviceValue, dic1["A4"]);
|
||||
Assert.AreEqual(ans2["A5"].DeviceValue, dic1["A5"]);
|
||||
Assert.AreEqual(ans2["A6"].DeviceValue, dic1["A6"]);
|
||||
Assert.AreEqual(ans3["A1"].DeviceValue, dic1["A1"]);
|
||||
Assert.AreEqual(ans3["A2"].DeviceValue, dic1["A2"]);
|
||||
Assert.AreEqual(ans3["A3"].DeviceValue, dic1["A3"]);
|
||||
Assert.AreEqual(ans3["A4"].DeviceValue, dic1["A4"]);
|
||||
Assert.AreEqual(ans3["A5"].DeviceValue, dic1["A5"]);
|
||||
Assert.AreEqual(ans3["A6"].DeviceValue, dic1["A6"]);
|
||||
Assert.AreEqual(ans.Datas["A1"].DeviceValue, dic1["A1"]);
|
||||
Assert.AreEqual(ans.Datas["A2"].DeviceValue, dic1["A2"]);
|
||||
Assert.AreEqual(ans.Datas["A3"].DeviceValue, dic1["A3"]);
|
||||
Assert.AreEqual(ans.Datas["A4"].DeviceValue, dic1["A4"]);
|
||||
Assert.AreEqual(ans.Datas["A5"].DeviceValue, dic1["A5"]);
|
||||
Assert.AreEqual(ans.Datas["A6"].DeviceValue, dic1["A6"]);
|
||||
Assert.AreEqual(ans2.Datas["A1"].DeviceValue, dic1["A1"]);
|
||||
Assert.AreEqual(ans2.Datas["A2"].DeviceValue, dic1["A2"]);
|
||||
Assert.AreEqual(ans2.Datas["A3"].DeviceValue, dic1["A3"]);
|
||||
Assert.AreEqual(ans2.Datas["A4"].DeviceValue, dic1["A4"]);
|
||||
Assert.AreEqual(ans2.Datas["A5"].DeviceValue, dic1["A5"]);
|
||||
Assert.AreEqual(ans2.Datas["A6"].DeviceValue, dic1["A6"]);
|
||||
Assert.AreEqual(ans3.Datas["A1"].DeviceValue, dic1["A1"]);
|
||||
Assert.AreEqual(ans3.Datas["A2"].DeviceValue, dic1["A2"]);
|
||||
Assert.AreEqual(ans3.Datas["A3"].DeviceValue, dic1["A3"]);
|
||||
Assert.AreEqual(ans3.Datas["A4"].DeviceValue, dic1["A4"]);
|
||||
Assert.AreEqual(ans3.Datas["A5"].DeviceValue, dic1["A5"]);
|
||||
Assert.AreEqual(ans3.Datas["A6"].DeviceValue, dic1["A6"]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -295,18 +295,18 @@ namespace Modbus.Net.Tests
|
||||
var ans = await _modbusTcpMachine.BaseUtility.GetUtilityMethods<IUtilityMethodData>().GetDatasAsync<ushort>("4X 1", 1);
|
||||
var ans2 = await _modbusRtuMachine.BaseUtility.GetUtilityMethods<IUtilityMethodData>().GetDatasAsync<ushort>("4X 1", 1);
|
||||
var ans3 = await _modbusAsciiMachine.BaseUtility.GetUtilityMethods<IUtilityMethodData>().GetDatasAsync<ushort>("4X 1", 1);
|
||||
Assert.AreEqual(ans[0], dic1["4X 1"]);
|
||||
Assert.AreEqual(ans2[0], dic1["4X 1"]);
|
||||
Assert.AreEqual(ans3[0], dic1["4X 1"]);
|
||||
Assert.AreEqual(ans.Datas[0], dic1["4X 1"]);
|
||||
Assert.AreEqual(ans2.Datas[0], dic1["4X 1"]);
|
||||
Assert.AreEqual(ans3.Datas[0], dic1["4X 1"]);
|
||||
await _modbusTcpMachine.BaseUtility.GetUtilityMethods<IUtilityMethodWriteSingle>().SetSingleDataAsync("0X 1", dic2["0X 1"] >= 1);
|
||||
await _modbusAsciiMachine.BaseUtility.GetUtilityMethods<IUtilityMethodWriteSingle>().SetSingleDataAsync("0X 1", dic2["0X 1"] >= 1);
|
||||
await _modbusRtuMachine.BaseUtility.GetUtilityMethods<IUtilityMethodWriteSingle>().SetSingleDataAsync("0X 1", dic2["0X 1"] >= 1);
|
||||
var ans21 = await _modbusTcpMachine.BaseUtility.GetUtilityMethods<IUtilityMethodData>().GetDatasAsync<bool>("0X 1", 1);
|
||||
var ans22 = await _modbusRtuMachine.BaseUtility.GetUtilityMethods<IUtilityMethodData>().GetDatasAsync<bool>("0X 1", 1);
|
||||
var ans23 = await _modbusAsciiMachine.BaseUtility.GetUtilityMethods<IUtilityMethodData>().GetDatasAsync<bool>("0X 1", 1);
|
||||
Assert.AreEqual(ans21[0] ? 1 : 0, dic2["0X 1"]);
|
||||
Assert.AreEqual(ans22[0] ? 1 : 0, dic2["0X 1"]);
|
||||
Assert.AreEqual(ans23[0] ? 1 : 0, dic2["0X 1"]);
|
||||
Assert.AreEqual(ans21.Datas[0] ? 1 : 0, dic2["0X 1"]);
|
||||
Assert.AreEqual(ans22.Datas[0] ? 1 : 0, dic2["0X 1"]);
|
||||
Assert.AreEqual(ans23.Datas[0] ? 1 : 0, dic2["0X 1"]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace Modbus.Net.Tests
|
||||
await _siemensTcpMachine.SetDatasAsync(MachineDataType.Address, dic1);
|
||||
|
||||
var ans = await _siemensTcpMachine.GetDatasAsync(MachineDataType.Address);
|
||||
Assert.AreEqual(ans["Q 0.0"].DeviceValue, dic1["Q 0.0"]);
|
||||
Assert.AreEqual(ans.Datas["Q 0.0"].DeviceValue, dic1["Q 0.0"]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -64,7 +64,7 @@ namespace Modbus.Net.Tests
|
||||
|
||||
_siemensTcpMachine!.GetAddresses = addresses;
|
||||
var ans = await _siemensTcpMachine.GetDatasAsync(MachineDataType.Address);
|
||||
Assert.AreEqual(ans["I 0.0"].DeviceValue, 0);
|
||||
Assert.AreEqual(ans.Datas["I 0.0"].DeviceValue, 0);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -96,7 +96,7 @@ namespace Modbus.Net.Tests
|
||||
|
||||
await _siemensTcpMachine.SetDatasAsync(MachineDataType.Address, dic1);
|
||||
var ans = await _siemensTcpMachine.GetDatasAsync(MachineDataType.Address);
|
||||
Assert.AreEqual(ans["M 0.0"].DeviceValue, dic1["M 0"]);
|
||||
Assert.AreEqual(ans.Datas["M 0.0"].DeviceValue, dic1["M 0"]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -129,7 +129,7 @@ namespace Modbus.Net.Tests
|
||||
await _siemensTcpMachine.SetDatasAsync(MachineDataType.Address, dic1);
|
||||
|
||||
var ans = await _siemensTcpMachine.GetDatasAsync(MachineDataType.Address);
|
||||
Assert.AreEqual(ans["M 0.0"].DeviceValue, dic1["M 0.0"]);
|
||||
Assert.AreEqual(ans.Datas["M 0.0"].DeviceValue, dic1["M 0.0"]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -161,7 +161,7 @@ namespace Modbus.Net.Tests
|
||||
await _siemensTcpMachine.SetDatasAsync(MachineDataType.Address, dic1);
|
||||
|
||||
var ans = await _siemensTcpMachine.GetDatasAsync(MachineDataType.Address);
|
||||
Assert.AreEqual(ans["DB2 0.0"].DeviceValue, dic1["DB2 0.0"]);
|
||||
Assert.AreEqual(ans.Datas["DB2 0.0"].DeviceValue, dic1["DB2 0.0"]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -252,12 +252,12 @@ namespace Modbus.Net.Tests
|
||||
_siemensTcpMachine!.GetAddresses = addresses;
|
||||
await _siemensTcpMachine.SetDatasAsync(MachineDataType.CommunicationTag, dic1);
|
||||
var ans = await _siemensTcpMachine.GetDatasAsync(MachineDataType.CommunicationTag);
|
||||
Assert.AreEqual(ans["A1"].DeviceValue, dic1["A1"]);
|
||||
Assert.AreEqual(ans["A2"].DeviceValue, dic1["A2"]);
|
||||
Assert.AreEqual(ans["A3"].DeviceValue, dic1["A3"]);
|
||||
Assert.AreEqual(ans["A4"].DeviceValue, dic1["A4"]);
|
||||
Assert.AreEqual(ans["A5"].DeviceValue, dic1["A5"]);
|
||||
Assert.AreEqual(ans["A6"].DeviceValue, dic1["A6"]);
|
||||
Assert.AreEqual(ans.Datas["A1"].DeviceValue, dic1["A1"]);
|
||||
Assert.AreEqual(ans.Datas["A2"].DeviceValue, dic1["A2"]);
|
||||
Assert.AreEqual(ans.Datas["A3"].DeviceValue, dic1["A3"]);
|
||||
Assert.AreEqual(ans.Datas["A4"].DeviceValue, dic1["A4"]);
|
||||
Assert.AreEqual(ans.Datas["A5"].DeviceValue, dic1["A5"]);
|
||||
Assert.AreEqual(ans.Datas["A6"].DeviceValue, dic1["A6"]);
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
|
||||
Reference in New Issue
Block a user