2014-10-08 update 3
This commit is contained in:
@@ -100,7 +100,7 @@ namespace ModBus.Net
|
||||
FunctionCode = (byte)functionCode;
|
||||
StartAddress = AddressTranslator.Instance.AddressTranslate(startAddress);
|
||||
WriteCount = (ushort)writeValue.Length;
|
||||
WriteByteCount = (byte)(WriteCount * 2);
|
||||
WriteByteCount = 0;
|
||||
WriteValue = writeValue.Clone() as object[];
|
||||
}
|
||||
|
||||
@@ -145,8 +145,10 @@ namespace ModBus.Net
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (WriteDataInputStruct)message;
|
||||
return Format(r_message.BelongAddress, r_message.FunctionCode,
|
||||
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;
|
||||
}
|
||||
|
||||
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
|
||||
|
||||
@@ -212,11 +212,11 @@ namespace ModBus.Net
|
||||
lastIsBool = true;
|
||||
if (_littleEndian)
|
||||
{
|
||||
boolToByteTemp += (byte) ((bool) content ? Math.Pow(2, boolToByteCount) : 0);
|
||||
boolToByteTemp = (byte)(boolToByteTemp * 2 + ((bool)content ? 1 : 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
boolToByteTemp = (byte) (boolToByteTemp*2 + ((bool) content ? 1 : 0));
|
||||
{
|
||||
boolToByteTemp += (byte)((bool)content ? Math.Pow(2, boolToByteCount) : 0);
|
||||
}
|
||||
boolToByteCount++;
|
||||
}
|
||||
@@ -284,6 +284,10 @@ namespace ModBus.Net
|
||||
}
|
||||
}
|
||||
//最后把队列转换为数组
|
||||
if (lastIsBool)
|
||||
{
|
||||
translateTarget.Add(boolToByteTemp);
|
||||
}
|
||||
return translateTarget.ToArray();
|
||||
}
|
||||
|
||||
|
||||
@@ -74,89 +74,49 @@ namespace NA200H.UI.ConsoleApp
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
/*ReadInputStatusModbusProtocal.ReadInputStatusInputStruct readInputStatusInputStruct = new ReadInputStatusModbusProtocal.ReadInputStatusInputStruct(0x02, "I20", 0x25);
|
||||
ReadInputStatusModbusProtocal.ReadInputStatusOutputStruct readInputStatusOutputStruct = (ReadInputStatusModbusProtocal.ReadInputStatusOutputStruct)wrapper.SendReceive(wrapper["ReadInputStatusModbusProtocal"], readInputStatusInputStruct);
|
||||
for (int i = 0; i < readInputStatusOutputStruct.InputStatus.Length; i++)
|
||||
ReadDataInputStruct readHoldRegisterInputStruct = new ReadDataInputStruct(0x02, ModbusProtocalReadDataFunctionCode.ReadHoldRegister, "NW1", 4);
|
||||
ReadDataOutputStruct readHoldRegisterOutputStruct = (ReadDataOutputStruct)wrapper.SendReceive(wrapper["ReadDataModbusProtocal"], readHoldRegisterInputStruct);
|
||||
ushort[] array2 =
|
||||
ValueHelper.Instance.ObjectArrayToDestinationArray<ushort>(
|
||||
ValueHelper.Instance.ByteArrayToObjectArray(readHoldRegisterOutputStruct.DataValue,
|
||||
new List<KeyValuePair<Type, int>>() {new KeyValuePair<Type, int>(typeof (ushort), 8)}));
|
||||
for (int i = 0; i < array2.Length; i++)
|
||||
{
|
||||
Console.WriteLine(readInputStatusOutputStruct.InputStatus[i]);
|
||||
Console.WriteLine(array2[i]);
|
||||
}
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct readHoldRegisterInputStruct = new ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct(0x02, "0", 8);
|
||||
ReadHoldRegisterModbusProtocal.ReadHoldRegisterOutputStruct readHoldRegisterOutputStruct = (ReadHoldRegisterModbusProtocal.ReadHoldRegisterOutputStruct)wrapper.SendReceive(wrapper["ReadHoldRegisterModbusProtocal"], readHoldRegisterInputStruct);
|
||||
for (int i = 0; i < readHoldRegisterOutputStruct.HoldRegisterStatus.Length; i++)
|
||||
{
|
||||
Console.WriteLine(readHoldRegisterOutputStruct.HoldRegisterStatus[i]);
|
||||
}
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
ReadInputRegisterModbusProtocal.ReadInputRegisterInputStruct readInputRegisterInputStruct = new ReadInputRegisterModbusProtocal.ReadInputRegisterInputStruct(0x02, "IW1", 3);
|
||||
ReadInputRegisterModbusProtocal.ReadInputRegisterOutputStruct readInputRegisterOutputStruct = (ReadInputRegisterModbusProtocal.ReadInputRegisterOutputStruct)wrapper.SendReceive(wrapper["ReadInputRegisterModbusProtocal"], readInputRegisterInputStruct);
|
||||
for (int i = 0; i < readInputRegisterOutputStruct.InputRegisterStatus.Length; i++)
|
||||
{
|
||||
Console.WriteLine(readInputRegisterOutputStruct.InputRegisterStatus[i]);
|
||||
}
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
ReadInputRegisterModbusProtocal.ReadInputRegisterInputStruct readInputRegisterInputStruct2 = new ReadInputRegisterModbusProtocal.ReadInputRegisterInputStruct(0x02, "E38", 8);
|
||||
ReadInputRegisterModbusProtocal.ReadEventOutputStruct readEventOutputStruct = (ReadInputRegisterModbusProtocal.ReadEventOutputStruct)wrapper.SendReceive(wrapper["ReadInputRegisterModbusProtocal"], readInputRegisterInputStruct2);
|
||||
Console.WriteLine(readEventOutputStruct.SoeEvent);
|
||||
Console.WriteLine(readEventOutputStruct.TestTime);
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
WriteOneCoilModbusProtocal.WriteOneCoilInputStruct writeOneCoilInputStruct = new WriteOneCoilModbusProtocal.WriteOneCoilInputStruct(0x02, "Q173", true);
|
||||
WriteOneCoilModbusProtocal.WriteOneCoilOutputStruct writeOneCoilOutputStruct = (WriteOneCoilModbusProtocal.WriteOneCoilOutputStruct)wrapper.SendReceive(wrapper["WriteOneCoilModbusProtocal"], writeOneCoilInputStruct);
|
||||
Console.WriteLine(writeOneCoilOutputStruct.StartAddress);
|
||||
Console.WriteLine(writeOneCoilOutputStruct.WriteValue);
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
WriteOneRegisterModbusProtocal.WriteOneRegisterInputStruct writeOneRegisterInputStruct = new WriteOneRegisterModbusProtocal.WriteOneRegisterInputStruct(0x02, "NW1", 100);
|
||||
WriteOneRegisterModbusProtocal.WriteOneRegisterOutputStruct writeOneRegisterOutputStruct = (WriteOneRegisterModbusProtocal.WriteOneRegisterOutputStruct)wrapper.SendReceive(wrapper["WriteOneRegisterModbusProtocal"], writeOneRegisterInputStruct);
|
||||
Console.WriteLine(writeOneRegisterOutputStruct.StartAddress);
|
||||
Console.WriteLine(writeOneRegisterOutputStruct.WriteValue);
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
WriteMultiCoilModbusProtocal.WriteMultiCoilInputStruct writeMultiCoilInputStruct = new WriteMultiCoilModbusProtocal.WriteMultiCoilInputStruct(0x02, "Q20", new bool[] { true, false, true, true, false, false, true, true, true, false });
|
||||
WriteMultiCoilModbusProtocal.WriteMultiCoilOutputStruct writeMultiCoilOutputStruct = (WriteMultiCoilModbusProtocal.WriteMultiCoilOutputStruct)wrapper.SendReceive(wrapper["WriteMultiCoilModbusProtocal"], writeMultiCoilInputStruct);
|
||||
WriteDataInputStruct writeMultiCoilInputStruct = new WriteDataInputStruct(0x02, ModbusProtocalWriteDataFunctionCode.WriteMultiCoil, "Q20", new object[] { true, false, true, true, false, false, true, true, true, false });
|
||||
WriteDataOutputStruct writeMultiCoilOutputStruct = (WriteDataOutputStruct)wrapper.SendReceive(wrapper["WriteDataModbusProtocal"], writeMultiCoilInputStruct);
|
||||
Console.WriteLine(writeMultiCoilOutputStruct.StartAddress);
|
||||
Console.WriteLine(writeMultiCoilOutputStruct.WriteCount);
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
WriteMultiRegisterModbusProtocal.WriteMultiRegisterInputStruct writeMultiRegisterInputStruct = new WriteMultiRegisterModbusProtocal.WriteMultiRegisterInputStruct(0x02, "MW2", new ushort[] { 0x000A, 0x0102 });
|
||||
WriteMultiRegisterModbusProtocal.WriteMultiRegisterOutputStruct writeMultiRegisterOutputStruct = (WriteMultiRegisterModbusProtocal.WriteMultiRegisterOutputStruct)wrapper.SendReceive(wrapper["WriteMultiRegisterModbusProtocal"], writeMultiRegisterInputStruct);
|
||||
WriteDataInputStruct writeMultiRegisterInputStruct = new WriteDataInputStruct(0x02, ModbusProtocalWriteDataFunctionCode.WriteMultiRegister, "NW1", new object[] { (ushort)25, (ushort)18, (ushort)17 });
|
||||
WriteDataOutputStruct writeMultiRegisterOutputStruct = (WriteDataOutputStruct)wrapper.SendReceive(wrapper["WriteDataModbusProtocal"], writeMultiRegisterInputStruct);
|
||||
Console.WriteLine(writeMultiRegisterOutputStruct.StartAddress);
|
||||
Console.WriteLine(writeMultiRegisterOutputStruct.WriteCount);
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
GetSystemTimeModbusProtocal.GetSystemTimeInputStruct getSystemTimeInputStruct = new GetSystemTimeModbusProtocal.GetSystemTimeInputStruct(0x02);
|
||||
GetSystemTimeModbusProtocal.GetSystemTimeOutputStruct getSystemTimeOutputStruct = (GetSystemTimeModbusProtocal.GetSystemTimeOutputStruct)wrapper.SendReceive(wrapper["GetSystemTimeModbusProtocal"], getSystemTimeInputStruct);
|
||||
GetSystemTimeInputStruct getSystemTimeInputStruct = new GetSystemTimeInputStruct(0x02);
|
||||
GetSystemTimeOutputStruct getSystemTimeOutputStruct = (GetSystemTimeOutputStruct)wrapper.SendReceive(wrapper["GetSystemTimeModbusProtocal"], getSystemTimeInputStruct);
|
||||
Console.WriteLine(getSystemTimeOutputStruct.Time);
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
SetSystemTimeModbusProtocal.SetSystemTimeInputStruct setSystemTimeInputStruct = new SetSystemTimeModbusProtocal.SetSystemTimeInputStruct(0x02, DateTime.Now);
|
||||
SetSystemTimeModbusProtocal.SetSystemTimeOutputStruct setSystemTimeOutputStruct = (SetSystemTimeModbusProtocal.SetSystemTimeOutputStruct)wrapper.SendReceive(wrapper["SetSystemTimeModbusProtocal"], setSystemTimeInputStruct);
|
||||
SetSystemTimeInputStruct setSystemTimeInputStruct = new SetSystemTimeInputStruct(0x02, DateTime.Now);
|
||||
SetSystemTimeOutputStruct setSystemTimeOutputStruct = (SetSystemTimeOutputStruct)wrapper.SendReceive(wrapper["SetSystemTimeModbusProtocal"], setSystemTimeInputStruct);
|
||||
Console.WriteLine(setSystemTimeOutputStruct.StartAddress);
|
||||
Console.WriteLine(setSystemTimeOutputStruct.WriteCount);
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user