Thank you, John, for the clarification regarding the offsets. I have modified the Lua script accordingly to use 0x311E for NAV1 standby.
The current Lua script (RadioTest.lua) is intended solely for testing the setting of the NAV1 standby frequency. Here is the script:
dev = com.open("COM6", 115200, 0) -- Open COM port with baud rate 115200
ipc.display("Lua script is running...", 3)
function processInput()
local datastring, length = com.read(dev, 256)
if length then
ipc.log("Raw data received: '" .. datastring .. "' [" .. length .. "]")
if datastring:match("^NAV1SB") then
local frequencyStr = datastring:match("NAV1SB%s*(%d+%.%d+)") -- Extracts the frequency
if frequencyStr then
ipc.log("Extracted frequency: " .. frequencyStr) -- Debug log
local bcdValue = convertToBCD(frequencyStr)
ipc.log("BCD Converted: " .. string.format("0x%X", bcdValue)) -- Debug log
ipc.writeUD(0x311E, bcdValue) -- Send to FSUIPC
ipc.display("NAV1 standby set: " .. frequencyStr, 3)
ipc.log("NAV1 standby frequency set to BCD: " .. string.format("0x%X", bcdValue))
else
ipc.log("Error: Failed to extract frequency!")
end
end
end
end
-- Convert frequency to BCD (e.g., "113.45" → 0x1345)
function convertToBCD(freq)
local cleanFreq = freq:gsub("%.", "") -- Remove decimal (e.g., "109.2" → "0920")
local bcd = tonumber(cleanFreq, 10) -- Convert to number
return bcd or 0 -- Return 0 if conversion fails
end
while true do
processInput()
ipc.sleep(100)
end
And here is the Arduino code:
#include <Keypad.h>
#include <LiquidCrystal.h>
// Initialize the LCD connected to pins 9, 8, 7, 6, 5, 4 on Arduino
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
// Keypad settings
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colPins[COLS] = {A4, A5, A6, A7};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String frequency = "1"; // Pre-define the first digit as '1'
void setup() {
lcd.begin(20, 4);
// Turn off the display:
lcd.noDisplay(); delay(500);
// Turn on the display:
lcd.display();
// Initial LCD message:
lcd.setCursor(0, 0);
lcd.print("Keypad Radio");
delay(4000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Frequency to send:");
Serial.begin(115200); //start checking serial connection
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == 'A') {
sendCom1FrequencyToSim();
frequency = "1"; // Reset to '1' after sending
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Frequency to send:");
}
else if (key == 'B') {
sendCom2FrequencyToSim();
frequency = "1"; // Reset to '1' after sending
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Frequency to send:");
}
else if (key == 'C') {
sendNav1FrequencyToSim();
frequency = "1"; // Reset to '1' after sending
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Frequency to send:");
}
else if (key == 'D') {
sendNav2FrequencyToSim();
frequency = "1"; // Reset to '1' after sending
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Frequency to send:");
}
else if (key == '*') {
if (frequency.length() > 1) { // Ensure '1' stays as the first digit
frequency.remove(frequency.length() - 1); // Remove last digit
// Clear the previous frequency display and update it
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Frequency to send:");
lcd.setCursor(0, 2);
lcd.print(formatFrequency(frequency));
}
}
// else if (key == 'D') {
// transferFrequency(); // Trigger COM1 STBY RADIO SWAP
// }
else if (isDigit(key)) {
if (frequency.length() < 6) { // limit to 6 digits (including '1')
frequency += key;
// Update the LCD with the new frequency
lcd.setCursor(0, 2);
lcd.print(formatFrequency(frequency));
}
}
}
}
String formatFrequency(String freq) {
// Automatically add the decimal point at the right place
if (freq.length() >= 4) { // Adjusted to account for the pre-defined '1'
return freq.substring(0, 3) + "." + freq.substring(3);
}
return freq; // If less than 3 digits, no decimal
}
void sendCom1FrequencyToSim() {
String formattedFrequency = formatFrequency(frequency); // Add the decimal point
Serial.print("COM1SB "); // Add COM1SB tag
Serial.println(formattedFrequency); // Send formatted frequency (with decimal)
}
void sendCom2FrequencyToSim() {
String formattedFrequency = formatFrequency(frequency); // Add the decimal point
Serial.print("COM2SB "); // Add COM2SB tag
Serial.println(formattedFrequency); // Send formatted frequency (with decimal)
}
void sendNav1FrequencyToSim() {
String formattedFrequency = formatFrequency(frequency); // Add the decimal point
Serial.print("NAV1SB "); // Add NAV1SB tag
Serial.println(formattedFrequency); // Send formatted frequency (with decimal)
}
void sendNav2FrequencyToSim() {
String formattedFrequency = formatFrequency(frequency); // Add the decimal point
Serial.print("NAV2SB "); // Add NAV2SB tag
Serial.println(formattedFrequency); // Send formatted frequency (with decimal)
}
//void transferFrequency() {
// Serial.println("COM1TX"); // Send transfer command
//}
Below is an excerpt from the RadioTest.log file, showing what happens when I attempt to send 109.2, 109.20, and 109.200:
3506422 LUA: Local: datastring = NAV1SB 109.2
3506422 LUA: Local: length = 14
3506438 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:8 fn: processInput
3506438 LUA: Raw data received: 'NAV1SB 109.2
' [14]
3506438 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:10 fn: processInput
3506438 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:11 fn: processInput
3506438 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:13 fn: processInput
3506438 LUA: Local: frequencyStr = 109.2
3506438 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:14 fn: processInput
3506438 LUA: Extracted frequency: 109.2
3506453 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:16 fn: processInput
3506453 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:33 fn: convertToBCD
3506453 LUA: Local: freq = 109.2
3506453 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:34 fn: convertToBCD
3506453 LUA: Local: cleanFreq = 1092
3506453 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:35 fn: convertToBCD
3506453 LUA: Local: bcd = 1092
3506453 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:18 fn: processInput
3506469 LUA: Local: bcdValue = 1092
3506469 LUA: BCD Converted: 0x444
3506469 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:20 fn: processInput
3506485 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:22 fn: processInput
3506500 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:23 fn: processInput
3506500 LUA: NAV1 standby frequency set to BCD: 0x444
3512375 LUA: Local: datastring = NAV1SB 109.20
3512375 LUA: Local: length = 15
3512375 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:8 fn: processInput
3512375 LUA: Raw data received: 'NAV1SB 109.20
' [15]
3512375 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:10 fn: processInput
3512391 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:11 fn: processInput
3512391 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:13 fn: processInput
3512391 LUA: Local: frequencyStr = 109.20
3512391 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:14 fn: processInput
3512391 LUA: Extracted frequency: 109.20
3512391 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:16 fn: processInput
3512391 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:33 fn: convertToBCD
3512391 LUA: Local: freq = 109.20
3512407 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:34 fn: convertToBCD
3512407 LUA: Local: cleanFreq = 10920
3512407 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:35 fn: convertToBCD
3512407 LUA: Local: bcd = 10920
3512407 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:18 fn: processInput
3512407 LUA: Local: bcdValue = 10920
3512407 LUA: BCD Converted: 0x2AA8
3512422 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:20 fn: processInput
3512422 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:22 fn: processInput
3512438 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:23 fn: processInput
3512453 LUA: NAV1 standby frequency set to BCD: 0x2AA8
3518438 LUA: Local: datastring = NAV1SB 109.200
3518438 LUA: Local: length = 16
3518438 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:8 fn: processInput
3518453 LUA: Raw data received: 'NAV1SB 109.200
' [16]
3518453 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:10 fn: processInput
3518453 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:11 fn: processInput
3518453 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:13 fn: processInput
3518453 LUA: Local: frequencyStr = 109.200
3518453 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:14 fn: processInput
3518453 LUA: Extracted frequency: 109.200
3518453 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:16 fn: processInput
3518469 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:33 fn: convertToBCD
3518469 LUA: Local: freq = 109.200
3518469 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:34 fn: convertToBCD
3518469 LUA: Local: cleanFreq = 109200
3518469 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:35 fn: convertToBCD
3518469 LUA: Local: bcd = 109200
3518469 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:18 fn: processInput
3518469 LUA: Local: bcdValue = 109200
3518485 LUA: BCD Converted: 0x1AA90
3518485 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:20 fn: processInput
3518500 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:22 fn: processInput
3518516 LUA: ...\Documents\Prepar3D v5 Add-ons\FSUIPC6\RadioTest.lua:23 fn: processInput
3518516 LUA: NAV1 standby frequency set to BCD: 0x1AA90
Unfortunately nothing happens with the Nav1 standby frequency in the simulator.
Cheers,
Isak