Skip to content

Instantly share code, notes, and snippets.

@ggalmazor
Last active July 16, 2021 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggalmazor/09411acd1e4bfc0eb28464f9ee6390af to your computer and use it in GitHub Desktop.
Save ggalmazor/09411acd1e4bfc0eb28464f9ee6390af to your computer and use it in GitHub Desktop.
funcion para leer la frecuencia de transceptor por puerto serie (Arduino)
void read_frequency() {
if (Serial.available() <= 0)
return;
String description = read_serial_message(0xFD);
if (!is_valid(description))
return;
return parse_frequency(description);
}
int parse_frequency(const String &description) {
frequency = 0;
for (byte i = 9 - mode_735; i >= 5; i--)
frequency = (frequency * 100) + hex_2_dec((byte) description.charAt(i));
return frequency;
}
int hex_2_dec(byte hex_number) {
return (hex_number >> 4) * 10 + (hex_number & 0x0F);
}
bool is_valid(const String &description) {
if (description.length() + mode_735 == 11)
return false;
char char_at_2 = description.charAt(2);
char char_at_3 = description.charAt(3);
char char_at_4 = description.charAt(4);
// bool valid_CIV_address_at_2 = char_at_2 == CIV_Address && (char_at_4 == 0x00 || char_at_4 == 0x05);
// bool valid_CIV_address_at_3 = char_at_3 == CIV_Address && (char_at_4 == 0x00 || char_at_4 == 0x03);
// return valid_CIV_address_at_3 || valid_CIV_address_at_2;
return char_at_3 == CIV_Address && (char_at_4 == 0x00 || char_at_4 == 0x03)
||
char_at_2 == CIV_Address && (char_at_4 == 0x00 || char_at_4 == 0x05);
}
String read_serial_message(int end_character) {
char character;
String description = "";
unsigned long serial_read_watchdog = millis();
while ((byte) character != end_character) {
if (Serial.available() > 0) {
character = Serial.read();
description.concat(character);
}
// Timeout if 500 millis have passed and we got no 0xFD
if (millis() - serial_read_watchdog >= 500)
return "";
}
return description;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment