public static byte[] CreateBuffer(ushort receiver, ushort sender, ushort parameter, bool write, bool read, bool master, bool error, bool file, bool vdew, byte[] value) { int num = value.Length; if (num != 4 && num != 128) { throw new ArgumentOutOfRangeException("value", string.Concat(new object[] { "value must have ", 4, " or ", 128, " bytes" })); } if (parameter > 1023) { throw new ArgumentOutOfRangeException("parameter", parameter, "only 10 bits (0 - 1023) allowed"); } byte[] array = new byte[9 + value.Length]; ushort num2 = parameter; if (write) { num2 += 32768; } if (read) { num2 += 16384; } if (master) { num2 += 8192; } if (error) { num2 += 4096; } if (file) { num2 += 2048; } if (vdew) { num2 += 1024; } array[0] = 2; array[1] = (byte)(receiver >> 8); array[2] = (byte)receiver; array[3] = (byte)(sender >> 8); array[4] = (byte)sender; array[5] = (byte)(num2 >> 8); array[6] = (byte)num2; Array.Copy(value, 0, array, 7, num); array[7 + num] = GlobalData.CalcChecksum(array, 1, 6 + num); array[8 + num] = 3; return(array); }
public ProtocolFrame(byte[] data, int startIndex, int length) { this.Time = DateTime.Now; if (length < 13) { throw new ArgumentOutOfRangeException("length", length, "must not be less then " + 13); } if (data[startIndex] != 2) { throw new ApplicationException("FrameErrorException"); } this.State = TransmissionState.Ok; this.Receiver = (ushort)(((int)data[startIndex + 1] << 8) + (int)data[startIndex + 2]); this.Sender = (ushort)(((int)data[startIndex + 3] << 8) + (int)data[startIndex + 4]); ushort num = (ushort)(((int)data[startIndex + 5] << 8) + (int)data[startIndex + 6]); this.Parameter = (ushort)(num & 1023); this.Write = ((num & 32768) == 32768); this.Read = ((num & 16384) == 16384); this.Master = ((num & 8192) == 8192); this.Error = ((num & 4096) == 4096); this.File = ((num & 2048) == 2048); this.Vdew = ((num & 1024) == 1024); int num2; if (this.File) { if (length < 137) { throw new ApplicationException("FrameErrorException"); } num2 = 137; } else { num2 = 13; } if (data[startIndex + num2 - 1] != 3) { throw new ApplicationException("FrameErrorException"); } this.Checksum = data[startIndex + num2 - 2]; this.RatedChecksum = GlobalData.CalcChecksum(data, startIndex + 1, startIndex + num2 - 3); if (this.RatedChecksum != this.Checksum) { this.State = TransmissionState.ChecksumError; } if (this.State == TransmissionState.Ok && this.Error) { switch (data[startIndex + 10]) { case 1: this.State = TransmissionState.UnknownParameter; break; case 2: this.State = TransmissionState.PasswordProtected; break; case 3: this.State = TransmissionState.MaxValueError; break; case 4: this.State = TransmissionState.MinValueError; break; case 5: this.State = TransmissionState.ParameterIsReadOnly; break; case 6: this.State = TransmissionState.AccessCode1; break; case 7: this.State = TransmissionState.AccessCode2; break; case 8: this.State = TransmissionState.NoCommand; break; case 9: this.State = TransmissionState.WrongValue; break; case 10: this.State = TransmissionState.NoAnswer; break; case 11: this.State = TransmissionState.NoFWFile; break; default: this.State = TransmissionState.Error; break; } } if (this.File) { this.DataLength = 128; this.buffer = new byte[137]; } else { this.DataLength = 4; this.buffer = new byte[13]; } Array.Copy(data, startIndex, this.buffer, 0, this.buffer.Length); }
private TransmissionState ReadFrame(int blockLength, byte[] outBuffer, byte[] inBuffer, int timeout) { TcpClient tcpClient = null; NetworkStream networkStream = null; Queue <byte> queue = new Queue <byte>(blockLength); DateTime now = DateTime.Now; byte[] array; DateTime t; tcpClient = _client; networkStream = _clientStream; array = new byte[tcpClient.ReceiveBufferSize]; t = now.AddMilliseconds((double)Math.Max(Settings.Default.ReadFrameTcpTimeout, timeout)); while (DateTime.Now < t) { int num = 0; while (queue.Count + num < blockLength && DateTime.Now < t) { if (tcpClient != null) { num = tcpClient.Available; } if (num == 0) { Thread.Sleep(50); } } if (tcpClient != null) { num = tcpClient.Available; } if (queue.Count + num >= blockLength) { int num3 = blockLength - queue.Count; if (tcpClient != null && networkStream != null) { networkStream.Read(array, 0, num3); } if (this.DataReceived != null) { byte[] array2 = new byte[num3]; Array.Copy(array, array2, num3); try { this.DataReceived(array2); } catch { } } for (int i = 0; i < num3; i++) { queue.Enqueue(array[i]); } while (queue.Count > 0 && queue.Peek() != 2) { queue.Dequeue(); if (tcpClient != null) { if (tcpClient.Available > 0 && networkStream != null) { array[0] = (byte)networkStream.ReadByte(); queue.Enqueue(array[0]); } } } if (queue.Count >= blockLength) { queue.CopyTo(inBuffer, 0); if (inBuffer[0] != 2 || inBuffer[blockLength - 1] != 3) { return(TransmissionState.FrameError); } if (inBuffer[blockLength - 2] != GlobalData.CalcChecksum(inBuffer, 1, blockLength - 3)) { return(TransmissionState.ChecksumError); } if (inBuffer[1] != outBuffer[3] || inBuffer[2] != outBuffer[4]) { return(TransmissionState.AddressError); } if (inBuffer[3] != outBuffer[1] || inBuffer[4] != outBuffer[2]) { return(TransmissionState.AddressError); } if ((inBuffer[5] & 3) != (outBuffer[5] & 3) || inBuffer[6] != outBuffer[6]) { return(TransmissionState.WrongAnswer); } if ((inBuffer[5] & 16) == 0) { return(TransmissionState.Ok); } switch (inBuffer[10]) { case 1: return(TransmissionState.UnknownParameter); case 2: return(TransmissionState.PasswordProtected); case 3: return(TransmissionState.MaxValueError); case 4: return(TransmissionState.MinValueError); case 5: return(TransmissionState.ParameterIsReadOnly); case 6: return(TransmissionState.AccessCode1); case 7: return(TransmissionState.AccessCode2); case 8: return(TransmissionState.NoCommand); case 9: return(TransmissionState.WrongValue); case 10: return(TransmissionState.NoAnswer); case 11: return(TransmissionState.NoFWFile); default: return(TransmissionState.Error); } } } Thread.Sleep(0); } return(TransmissionState.ReadTimeout); }