public void SoftBuffer1( ) { SoftBuffer softBuffer = new SoftBuffer(1000); byte[] b1 = new byte[] { 0x13, 0xA6, 0x15, 0x85, 0x5B, 0x05, 0x12, 0x36, 0xF2, 0x27 }; softBuffer.SetBytes(b1, 367); byte[] b2 = softBuffer.GetBytes(367, b1.Length); Assert.IsTrue(SoftBasic.IsTwoBytesEquel(b1, b2), "第一次" + SoftBasic.ByteToHexString(b2)); byte[] b3 = new byte[] { 0x12, 0xC6, 0x25, 0x3C, 0x42, 0x85, 0x5B, 0x05, 0x12, 0x87 }; softBuffer.SetBytes(b3, 367 + b1.Length); byte[] b4 = softBuffer.GetBytes(367 + b1.Length, b3.Length); Assert.IsTrue(SoftBasic.IsTwoBytesEquel(b3, b4), "第二次"); byte[] b5 = SoftBasic.SpliceTwoByteArray(b1, b3); byte[] b6 = softBuffer.GetBytes(367, b1.Length + b3.Length); Assert.IsTrue(SoftBasic.IsTwoBytesEquel(b5, b6), "第三次"); }
public void SoftBufferExample1( ) { // 举例设置100-199长度的数据值 byte[] buffer = new byte[100]; random.NextBytes(buffer); // 本语句是线程安全的,可以在任意的线程进行操作 softBuffer.SetBytes(buffer, 100); // 然后我们把数据进行读取出来,和上述的buffer是一致的 byte[] data = softBuffer.GetBytes(100, 100); }
private byte[] ReadFromModbusCore(byte[] packet) { List <byte> command = new List <byte>(); command.Clear(); var StartAddress = string.Empty; var station = Encoding.ASCII.GetString(packet, 1, 2); var Read = Encoding.ASCII.GetString(packet, 3, 3); var nameLength = Encoding.ASCII.GetString(packet, 6, 2); var DeviceAddress = Encoding.ASCII.GetString(packet, 8, int.Parse(nameLength)); var size = Encoding.ASCII.GetString(packet, 8 + int.Parse(nameLength), 2); //===================================================================================== // Read Response if (Read.Substring(0, 2) == "rS") { command.Add(0x06); // ENQ command.AddRange(SoftBasic.BuildAsciiBytesFrom(byte.Parse(station))); command.Add(0x72); // command r command.Add(0x53); // command type: SB command.Add(0x42); command.AddRange(Encoding.ASCII.GetBytes("01")); StartAddress = DeviceAddress.Remove(0, 3); bool[] data; string txtValue; switch (DeviceAddress.Substring(1, 2)) { case "MB": case "PB": var dbint = Convert.ToInt32(size, 16) * 8; int startIndex = int.Parse(StartAddress); switch (DeviceAddress[1]) { case 'P': data = pBuffer.GetBool(startIndex, dbint); break; case 'Q': data = qBuffer.GetBool(startIndex, dbint); break; case 'M': data = mBuffer.GetBool(startIndex, dbint); break; case 'D': data = dBuffer.GetBool(startIndex, dbint); break; default: throw new Exception(StringResources.Language.NotSupportedDataType); } var data3 = SoftBasic.BoolArrayToByte(data); txtValue = GetValStr(data3, 0, Convert.ToInt32(size, 16)); command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)data3.Length)); command.AddRange(SoftBasic.BytesToAsciiBytes(data3)); command.Add(0x03); // ETX int sum1 = 0; for (int i = 0; i < command.Count; i++) { sum1 += command[i]; } command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)sum1)); break; case "DB": case "TB": var RequestCount = Convert.ToInt32(size, 16); byte[] dataW; var startIndexW = int.Parse(StartAddress); switch (DeviceAddress[1]) { case 'P': dataW = pBuffer.GetBytes(startIndexW, (ushort)RequestCount); break; case 'Q': dataW = qBuffer.GetBytes(startIndexW, (ushort)RequestCount); break; case 'M': dataW = mBuffer.GetBytes(startIndexW, (ushort)RequestCount); break; case 'D': dataW = dBuffer.GetBytes(startIndexW, (ushort)RequestCount); break; default: throw new Exception(StringResources.Language.NotSupportedDataType); } txtValue = GetValStr(dataW, 0, Convert.ToInt32(size, 16)); command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)dataW.Length)); command.AddRange(SoftBasic.BytesToAsciiBytes(dataW)); command.Add(0x03); // ETX int sum = 0; for (int i = 0; i < command.Count; i++) { sum += command[i]; } command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)sum)); break; } return(command.ToArray()); } else { StartAddress = DeviceAddress.Remove(0, 3); command.Add(0x06); // ENQ command.AddRange(SoftBasic.BuildAsciiBytesFrom(byte.Parse(station))); command.Add(0x77); // command w command.Add(0x53); // command type: SB command.Add(0x42); command.Add(0x03); // EOT string Value; if (Read.Substring(0, 3) == "WSS") { //nameLength = packet.Substring(8, 1); //DeviceAddress = packet.Substring(9, Convert.ToInt16(nameLength)); //AddressLength = packet.Substring(9 + Convert.ToInt16(nameLength), 1); nameLength = Encoding.ASCII.GetString(packet, 8, 2); DeviceAddress = Encoding.ASCII.GetString(packet, 10, int.Parse(nameLength)); Value = Encoding.ASCII.GetString(packet, 10 + int.Parse(nameLength), 2); } else { //Value = Encoding.ASCII.GetString(packet, 10 + int.Parse(nameLength), int.Parse(size)); Value = Encoding.ASCII.GetString(packet, 8 + int.Parse(nameLength) + int.Parse(size), int.Parse(size) * 2); var wdArys = HexToBytes(Value); } var startIndex = CheckAddress(StartAddress); switch (DeviceAddress.Substring(1, 2)) { case "MX": // Bit X Value = Encoding.ASCII.GetString(packet, 8 + int.Parse(nameLength) + int.Parse(size), int.Parse(size)); switch (DeviceAddress[1]) { //case 'M': inputBuffer.SetBool(value, startIndex); break; //case 'M': outputBuffer.SetBool(value, startIndex); break; case 'M': mBuffer.SetBool(Value == "01" ? true : false, startIndex); break; case 'D': dBuffer.SetBool(Value == "01" ? true : false, startIndex); break; default: throw new Exception(StringResources.Language.NotSupportedDataType); } return(command.ToArray()); case "DW": //Word Value = Encoding.ASCII.GetString(packet, 8 + int.Parse(nameLength) + int.Parse(size), int.Parse(size) * 2); var wdArys = HexToBytes(Value); switch (DeviceAddress[1]) { case 'C': pBuffer.SetBytes(wdArys, startIndex); break; case 'T': qBuffer.SetBytes(wdArys, startIndex); break; case 'M': mBuffer.SetBytes(wdArys, startIndex); break; case 'D': dBuffer.SetBytes(wdArys, startIndex); break; default: throw new Exception(StringResources.Language.NotSupportedDataType); } return(command.ToArray()); case "DD": //DWord break; case "DL": //LWord break; default: return(null); } } return(command.ToArray()); }