private byte[] ConvertRequestPacketToArray(MaxPacket packet) { int packetSize = packet.LEN.HasValue ? (7 + packet.LEN.Value) : 6; List <byte> rawPacket = new List <byte>(); rawPacket.AddRange(new byte[] { packet.STX, packet.STYPE, packet.SCODE.Value, packet.PAD.Value }); if (packet.LEN.HasValue) { rawPacket.Add(packet.LEN.Value); rawPacket.AddRange(packet.DATA); } if (!packet.CRCL.HasValue || !packet.CRCH.HasValue) { var crc = OnComputeCRC(rawPacket.ToArray()); if (BitConverter.IsLittleEndian) { Array.Reverse(crc); } packet.CRCL = crc[0]; packet.CRCH = crc[1]; } rawPacket.Add(packet.CRCH.Value); rawPacket.Add(packet.CRCL.Value); return(rawPacket.ToArray()); }
protected override void OnFillRequestDataPacket(MaxPacket requestPacket) { requestPacket.DATA.Add(_interfaceType); requestPacket.DATA.Add((byte)_baudRate); requestPacket.DATA.Add(_communicationTimeout); requestPacket.DATA.AddRange(_password); }
//public virtual bool IsRequestOk(byte ackValue) //{ // return (ackValue == 0x06); //} public virtual void ProcessResponse(byte[] rawResponse) { responsePacket = OnCreateResponsePacket(rawResponse); if (responsePacket.DATA.Count > 0) { OnProcessResponseData(); } }
public virtual void Reset() { if (requestPacket != null) { requestPacket = null; } if (responsePacket != null) { responsePacket = null; } }
protected virtual MaxPacket OnCreateRequestPacket() { MaxPacket packet = OnCreatePacket(); packet.SCODE = (byte)serviceRequestCode; packet.PAD = 0x00; OnFillRequestDataPacket(packet); if (packet.DATA.Count > 0) { packet.LEN = (byte)packet.DATA.Count; } return(packet); }
protected virtual MaxPacket OnCreateResponsePacket(byte[] data) { /* Formato de respuesta: * Sin datos: * STX STYPE ACK|NACK STAT CRCH CRCL * * Con datos: * STX STYPE ACK|NACK STAT LEN DATA CRCH CRCL * * */ if (data == null) { throw new ArgumentNullException("data", "El paquete de datos MeterMAX no puede ser nulo."); } if (data.Length < 6) { throw new MaxServiceException(MaxServiceExceptionType.PacketLenght, "El paquete de datos MeterMAX debe tener un longitud mínima de 6 bytes."); } // verificamos que el CRC sea correcto... if (!CheckCRC(data)) { throw new MaxServiceException(MaxServiceExceptionType.PacketIntegrity, "Error en la integridad del paquete de datos MeterMAX. CRC Inválido."); } MaxPacket packet = OnCreatePacket(); if (data[0] != packet.STX) { throw new MaxServiceException(MaxServiceExceptionType.PacketIntegrity, $"Error en formato del paquete de datos MeterMAX. Cabecera inválida. {data[0]:X2}"); } if (!Enum.IsDefined(typeof(MaxServiceType), data[1])) { throw new MaxServiceException(MaxServiceExceptionType.PacketIntegrity, $"Error en el formato del paquete de datos MeterMax. Tipo de Servicio inválido. {data[1]:X2}"); } if (!Enum.IsDefined(typeof(MaxServiceResponseCode), data[2])) { throw new MaxServiceException(MaxServiceExceptionType.PacketIntegrity, $"Error en el formato del paquete de datos MeterMAX. Código de Respuesta inválido. {data[2]:X2}"); } packet.STYPE = data[1]; packet.ACK = data[2]; packet.STAT = data[3]; if (data.Length == 6) { packet.CRCH = data[4]; packet.CRCL = data[5]; } else { byte len = (byte)(7 + data[4]); if (data.Length != len) { throw new MaxServiceException(MaxServiceExceptionType.PacketLenght, $"Error en la longitud del paquete de datos MeterMAX. Longitud del paquete: {data.Length} - Longitud esperada: {len}"); } packet.LEN = len; byte[] buffer = new byte[len]; Buffer.BlockCopy(data, 5, buffer, 0, len); packet.DATA.AddRange(buffer); packet.CRCH = data[data.Length - 2]; packet.CRCL = data[data.Length - 1]; } return(packet); }
protected virtual void OnFillRequestDataPacket(MaxPacket requestPacket) { }
public virtual byte[] SendRequest() { requestPacket = OnCreateRequestPacket(); return(ConvertRequestPacketToArray(requestPacket)); }