//------------------------------------------------------------------------------------------------------------------------------------------- private byte GetVariableIndexFromBuffer(byte command_byte, byte[] buffer) { byte index_high_nibble = buffer[1]; byte index_low_nibble = buffer[2]; byte index = NumberConversions.HexStringToByte(new String(new char[] { (char)index_high_nibble, (char)index_low_nibble })); Debug.Print("Command 0x" + NumberConversions.ByteToHexString(command_byte) + ": variable index = " + index.ToString()); return(index); }
//------------------------------------------------------------------------------------------------------------------------------------------- private byte GetByteValueFromBuffer(byte command_byte, byte[] buffer) { byte value_high_nibble = buffer[3]; byte value_low_nibble = buffer[4]; byte value = NumberConversions.HexStringToByte(new String(new char[] { (char)value_high_nibble, (char)value_low_nibble })); Debug.Print("Command 0x" + NumberConversions.ByteToHexString(command_byte) + ": setting with byte value = " + value.ToString()); return(value); }
//------------------------------------------------------------------------------------------------------------------------------------------- private ushort GetShortValueFromBuffer(byte command_byte, byte[] buffer) { byte msb_value_high_nibble = buffer[3]; byte msb_value_low_nibble = buffer[4]; byte msb = NumberConversions.HexStringToByte(new String(new char[] { (char)msb_value_high_nibble, (char)msb_value_low_nibble })); byte lsb_value_high_nibble = buffer[5]; byte lsb_value_low_nibble = buffer[6]; byte lsb = NumberConversions.HexStringToByte(new String(new char[] { (char)lsb_value_high_nibble, (char)lsb_value_low_nibble })); ushort value = (ushort)(((short)msb << 8) + (short)lsb); Debug.Print("Command 0x" + NumberConversions.ByteToHexString(command_byte) + ": setting with short value = " + value.ToString()); return(value); }
//------------------------------------------------------------------------------------------------------------------------------------------- private bool ValidateCommandPacket(byte command_byte, byte[] buffer) { if (buffer.Length < (int)GetCommandPacketLength(command_byte)) { Debug.Print("ERROR: Command parsing for command 0x" + NumberConversions.ByteToHexString(command_byte) + " (" + ByteToCommandName(command_byte) + ") was skipped because not enough data was available"); return(false); } if (buffer[1] == 0 || buffer[2] == 0) { Debug.Print("ERROR: Command parsing for command 0x" + NumberConversions.ByteToHexString(command_byte) + " (" + ByteToCommandName(command_byte) + ") failed because of null byte(s) in variable index"); return(false); } Debug.Print("Command " + ByteToCommandName(command_byte) + " validation OK"); return(true); }
//------------------------------------------------------------------------------------------------------------------------------------------- /// <summary> /// Returns the command packet and shifts remaining data in RX buffer down /// </summary> /// <param name="index_of_command_byte"></param> /// <returns></returns> private byte[] ExtractCommandPacketFromRxBuffer(int index_of_command_byte) { // the following variables are only used for the debug print statement that is going to // try to help visualize what's in the buffer, and what is getting extracted for command processing, e.g. // | command bytes here | rest of buffer here // print all buffer bytes up to index_of_command_byte // index_of_command_byte: print | before this position // print all buffer bytes up to index_of_command_byte + command_packet_length // print | after last command packet byte // print rest of buffer bytes // determine the number of bytes that should be in the command packet, based on the command byte command_id = _rx_buffer[index_of_command_byte]; byte command_packet_length = GetCommandPacketLength(command_id); Debug.Print("Handling command ID: " + ByteToCommandName(command_id) + ", expected packet length = " + command_packet_length); #region VISUALIZATION DEBUG MESSAGE // now print the debug buffer visualization line string viz = ""; // print all bytes up to command byte position for (int i = 0; i < index_of_command_byte; i++) { viz += NumberConversions.ByteToHexString(_rx_buffer[i]) + " "; } viz += "> "; // print command packet for (int i = index_of_command_byte; i < index_of_command_byte + command_packet_length; i++) { viz += NumberConversions.ByteToHexString(_rx_buffer[i]) + " "; if (i == index_of_command_byte + command_packet_length - 1) { viz += "< "; } } // print remaining bytes in buffer for (int i = index_of_command_byte + command_packet_length; i < RX_BUFFER_SIZE; i++) { if (_rx_buffer[i] == 0) { break; } viz += NumberConversions.ByteToHexString(_rx_buffer[i]) + " "; } Debug.Print(viz); #endregion // now create the array for just the packet byte[] packet = new byte[command_packet_length]; Array.Copy(_rx_buffer, index_of_command_byte, packet, 0, command_packet_length); //Debug.Print( "copied command packet from buffer into new packet array"); // we need to save the current _rx_buffer_pointer, because that's the ONLY way we know what the new // _rx_buffer_pointer should be once we shift down the bits. int old_rx_buffer_pointer = _rx_buffer_pointer; // modify the rx buffer and shift down the remaining bytes _rx_buffer_pointer = index_of_command_byte + command_packet_length; // now it points to the element after the command packet we're handling // the new rx buffer is where the end of the existing data will end up once we shift all of the bytes down int new_rx_buffer_pointer = old_rx_buffer_pointer - _rx_buffer_pointer; Debug.Assert(new_rx_buffer_pointer >= 0, "RX buffer pointer problems!"); //Debug.Print( "rx buffer pointer points at: " + _rx_buffer_pointer); int bytes_remaining_in_buffer = RX_BUFFER_SIZE - _rx_buffer_pointer; //Debug.Print( bytes_remaining_in_buffer.ToString() + " bytes left in buffer"); // shift down //Debug.Print( "shifting down bytes"); for (int i = 0; i < bytes_remaining_in_buffer; i++) { _rx_buffer[i] = _rx_buffer[_rx_buffer_pointer + i]; } //Debug.Print( "clearing remaining space"); // clear remaining space (possibly unnecessary) for (int i = bytes_remaining_in_buffer; i < RX_BUFFER_SIZE; i++) { _rx_buffer[i] = 0; } //Debug.Print( "Setting new RX buffer pointer to " + new_rx_buffer_pointer); _rx_buffer_pointer = new_rx_buffer_pointer; return(packet); }