//------------------------------------------------------------------------------------------------------------------------------------------- private void SendResponse(byte command_byte, byte[] buffer) { byte[] response = new byte[] { (byte)(command_byte + 0x10), buffer[1], buffer[2] }; Debug.Print("Sending response: "); NumberConversions.PrintByteArray(response); _port.Write(response, 0, response.Length); }
//------------------------------------------------------------------------------------------------------------------------------------------- private void SendResponse(byte command_byte, byte[] buffer, ushort value) { char[] value_array = NumberConversions.ShortToCharArray(value); byte[] response = new byte[] { (byte)(command_byte + 0x10), buffer[1], buffer[2], (byte)value_array[0], (byte)value_array[1], (byte)value_array[2], (byte)value_array[3] }; Debug.Print("Sending response: "); NumberConversions.PrintByteArray(response); _port.Write(response, 0, response.Length); }
//------------------------------------------------------------------------------------------------------------------------------------------- private void SendResponse(byte command_byte, byte[] buffer, byte[] value) { int value_length = value.Length; byte[] response = new byte[3 + value_length * 2 + 1]; // need to create extra space since each byte is broken up into high and low nibbles, plus 1 for null terminator response[0] = (byte)(command_byte + 0x10); response[1] = buffer[1]; response[2] = buffer[2]; for (int i = 0; i < value_length; i++) { char[] element_nibbles = NumberConversions.ByteToCharArray(value[i]); response[3 + (i * 2)] = (byte)element_nibbles[0]; response[3 + (i * 2) + 1] = (byte)element_nibbles[1]; } Debug.Print("Sending response: "); NumberConversions.PrintByteArray(response); _port.Write(response, 0, response.Length); }
//------------------------------------------------------------------------------------------------------------------------------------------- private void SendResponse(byte command_byte, byte[] buffer, string value) { // for null termination ---------------------v int total_response_size = 3 + value.Length + 1; byte[] response = new byte[total_response_size]; response[0] = (byte)(command_byte + 0x10); response[1] = buffer[1]; response[2] = buffer[2]; // get the string characters and cast to byte one by one char[] value_array = value.ToCharArray(); for (int i = 0; i < value.Length; i++) { response[3 + i] = (byte)value_array[i]; } Debug.Print("Sending response: "); NumberConversions.PrintByteArray(response); _port.Write(response, 0, response.Length); }
//------------------------------------------------------------------------------------------------------------------------------------------- public byte[] GetByteVariableArray(byte index) { bool print_array = false; // here we will just return a byte array with 128 random values const int size = 111; byte[] value = new byte[size]; //new Random().NextBytes( value); for (int i = 0; i < size; i++) { // scale result - SPOT.Math returns Sin * 1000, so divide result by 10 and shift up by 100 since LineGraph doesn't seem to do negative numbers? value[i] = (byte)(Microsoft.SPOT.Math.Sin(_sin_int_counter++) / 10 + 100); } Debug.Print("[AmuletTestClient] GetByteVariableArray " + index + ":"); if (print_array) { NumberConversions.PrintByteArray(value); } return(value); }
//------------------------------------------------------------------------------------------------------------------------------------------- /// <summary> /// Parses the serial data buffer and responds as appropriate. Buffer should start with a valid command byte. /// </summary> private void HandleCommand(byte[] buffer) { try { Debug.Print("Command packet contents:"); NumberConversions.PrintByteArray(buffer); byte command_byte = buffer[0]; if (!ValidateCommandPacket(command_byte, buffer)) { return; } byte index = GetVariableIndexFromBuffer(command_byte, buffer); if (!CheckForAmuletClient()) { return; } switch (command_byte) { case (byte)Commands.GetByte: { // acknowledge the command, but need to get data from IAmuletClient try { byte value = _client.GetByte(index); SendResponse(command_byte, buffer, value); } catch (Exception ex) { Debug.Print("ERROR: GetByte failed in client call: " + ex.Message); SendAck(); } break; } case (byte)Commands.GetWord: { // acknowledge the command try { ushort value = _client.GetWord(index); SendResponse(command_byte, buffer, value); } catch (Exception ex) { Debug.Print("ERROR: GetWord failed in client call: " + ex.Message); SendAck(); } break; } case (byte)Commands.GetString: { try { string value = _client.GetString(index); SendResponse(command_byte, buffer, value); } catch (Exception ex) { Debug.Print("ERROR: GetString failed in client call: " + ex.Message); SendAck(); } break; } case (byte)Commands.GetLabel: { try { string value = _client.GetLabel(index); SendResponse(command_byte, buffer, value); } catch (Exception ex) { Debug.Print("ERROR: GetLabel failed in client call: " + ex.Message); SendAck(); } break; } case (byte)Commands.GetByteVariableArray: { try { byte[] value = _client.GetByteVariableArray(index); SendResponse(command_byte, buffer, value); } catch (Exception ex) { Debug.Print("ERROR: GetLabel failed in client call: " + ex.Message); SendAck(); } break; } case (byte)Commands.GetWordVariableArray: { try { ushort[] value = _client.GetWordVariableArray(index); SendResponse(command_byte, buffer, value); } catch (Exception ex) { Debug.Print("ERROR: GetLabel failed in client call: " + ex.Message); SendAck(); } break; } case (byte)Commands.SetByte: { byte value = GetByteValueFromBuffer(command_byte, buffer); // acknowledge the command try { _client.SetByte(index, value); SendResponse(command_byte, buffer, value); } catch (Exception ex) { Debug.Print("ERROR: SetByte failed in client call: " + ex.Message); SendAck(); } break; } case (byte)Commands.SetWord: { ushort value = GetShortValueFromBuffer(command_byte, buffer); // acknowledge the command try { _client.SetWord(index, value); SendResponse(command_byte, buffer, value); } catch (Exception ex) { Debug.Print("ERROR: SetByte failed in client call: " + ex.Message); SendAck(); } break; } case (byte)Commands.SetString: { string value = GetStringValueFromBuffer(command_byte, buffer); // acknowledge the command try { _client.SetString(index, value); SendResponse(command_byte, buffer, value); } catch (Exception ex) { Debug.Print("ERROR: SetString failed in client call: " + ex.Message); SendAck(); } break; } case (byte)Commands.InvokeRpc: { // acknowledge the command, but need to call into IAmuletClient try { _client.InvokeRpc(index); SendResponse(command_byte, buffer); } catch (Exception ex) { Debug.Print("ERROR: GetByte failed in client call: " + ex.Message); SendAck(); } break; } } // now process any remaining commands // we can pass 0 because we know that anytime a command is processed, the next valid // command, if any, will end up at index 0 if (CommandPacketAvailable(0)) { byte[] data = ExtractCommandPacketFromRxBuffer(0); HandleCommand(data); } } catch (Exception ex) { Debug.Print(ex.Message); } }