private byte[] ParsePackage(byte[] package) { byte flag = 0; if (package[0] == Convert.ToByte("01100001", 2)) { flag = package[0]; } byte[] undecodeData = new byte[package.Length - 2]; for (int i = 1, j = 0; i < package.Length - 1; i++, j++) { undecodeData[j] = package[i]; } byte[] decodeData = BitStuffing.DecodeData(undecodeData); byte destinationAddress = decodeData[0]; if (destinationAddress != Convert.ToByte(textBox5.Text.Length > 0 ? textBox5.Text : "0")) { return(null); } byte fcs = package[package.Length - 1]; List <byte> firstListBefore = new List <byte>(new byte[] { flag }); List <byte> middleListBefore = new List <byte>(decodeData); List <byte> lastListBefore = new List <byte>(new byte[] { fcs }); firstListBefore.AddRange(middleListBefore); firstListBefore.AddRange(lastListBefore); byte[] data = firstListBefore.ToArray(); return(data); }
private byte[] CreatePackage(string message) { byte flag = Convert.ToByte("01100001", 2); long sourceAddress = Convert.ToInt64(textBox5.Text.Length > 0 ? textBox5.Text : "0"); if (sourceAddress < 0 || sourceAddress > 255) { this.Invoke((MethodInvoker)(delegate { textBox3.Text += "Source address must be between 0 and 255" + "\r\n"; })); return(null); } long destinationAddress = Convert.ToInt64(textBox4.Text.Length > 0 ? textBox4.Text : "0"); if (destinationAddress < 0 || destinationAddress > 255) { this.Invoke((MethodInvoker)(delegate { textBox3.Text += "Source address must be between 0 and 255" + "\r\n"; })); return(null); } byte destinationByte = (byte)destinationAddress; byte sourceByte = (byte)sourceAddress; if (destinationByte == sourceByte) { this.Invoke((MethodInvoker)(delegate { textBox3.Text += "Source and destinations addresses can't be equals\r\n"; })); return(null); } byte[] bytesSent = Encoding.ASCII.GetBytes(message); byte[] addresses = new byte[] { destinationByte, sourceByte, (byte)bytesSent.Length }; List <byte> addressList = new List <byte>(addresses); List <byte> bytesSentList = new List <byte>(bytesSent); addressList.AddRange(bytesSentList); if (bytesSent.Length > 255) { this.Invoke((MethodInvoker)(delegate { textBox3.Text += "Data length more than 256 bytes" + "\r\n"; })); return(null); } byte[] dataAfterStuffing = BitStuffing.CodeData(addressList.ToArray()); byte fcs = Convert.ToByte("11111111", 2); List <byte> firstListBefore = new List <byte>(new byte[] { flag, destinationByte, sourceByte, (byte)bytesSent.Length }); List <byte> middleListBefore = new List <byte>(Encoding.ASCII.GetBytes(message)); List <byte> lastListBefore = new List <byte>(new byte[] { fcs }); firstListBefore.AddRange(middleListBefore); firstListBefore.AddRange(lastListBefore); byte[] firstPart = new byte[] { flag }; byte[] lastPart = new byte[] { fcs }; List <byte> firstList = new List <byte>(firstPart); List <byte> middleList = new List <byte>(dataAfterStuffing); List <byte> lastList = new List <byte>(lastPart); firstList.AddRange(middleList); firstList.AddRange(lastPart); byte[] package = firstList.ToArray(); return(package); }