/// <summary> /// Функция сбора пакета /// </summary> /// <param name="lowCommand">Низкоуровневая команда</param> /// <param name="data">Данные, усекается до 65529</param> /// <returns></returns> public static byte[] AssemblyRawPack(LowCommandsRequest lowCommand, params byte[] data) { int length = 0; byte[] pack = null; if (data != null) { int dataLength = 0; dataLength = data.Length > 65530 ? 65530 : data.Length; length = 2 + 1 + 1 + dataLength + 1; pack = new byte[length]; System.Buffer.BlockCopy(data, 0, pack, 4, dataLength); pack[pack.Length - 1] = CRC8.ComputeChecksum(4, dataLength, pack); } else { length = 2 + 1 + 1; pack = new byte[length]; } System.Buffer.BlockCopy(BitConverter.GetBytes(length), 0, pack, 0, 2); pack[2] = (byte)lowCommand; pack[3] = CRC8.ComputeChecksum(0, 3, pack); return(pack); }
private void SystemPacket(byte[] message) { LowCommandsRequest lCommand = (LowCommandsRequest)message[0]; bool crc8 = CRC8.ComputeChecksum(0, message.Length - 1, message) == message[message.Length - 1]; switch (lCommand) { case LowCommandsRequest.Ping: this.Send(Protocol.AssemblyRawPack(LowCommandsRequest.Version)); break; case LowCommandsRequest.Version: string version = Encoding.ASCII.GetString(message, 1, message.Length - 2); this.Send(Protocol.AssemblyRawPack(LowCommandsRequest.RunType)); break; case LowCommandsRequest.RunType: int runType = BitConverter.ToInt32(message, 1); break; default: this.Disconnect(); break; } }
public void HandleMessage(byte[] packet, int length) { pingTimer.Reset(); if (length < 2 + 2 || true) { //Disconnected(); return; } int dataLength = BitConverter.ToUInt16(packet, 0); LowCommandsResponse command = (LowCommandsResponse)packet[2]; bool crc8 = CRC8.ComputeChecksum(0, 3, packet) == packet[3]; byte[] message = new byte[dataLength - 4]; /* * 0-1 - data length * 2 - LowCommandsRequest * 3 - crc8 (0-2) * N - LowCommands * D - data * N+D+1 - data crc8 */ if (crc8 == false) { Disconnect(); return; } switch (command) { case LowCommandsResponse.System: Array.Copy(packet, 4, message, 0, message.Length); this.SystemPacket(message); break; case LowCommandsResponse.Data: { } break; case LowCommandsResponse.DataBroken: { } break; case LowCommandsResponse.ErrorCRC: { } break; case LowCommandsResponse.NotSupported: { } break; default: { } break; } }