Jump to content
The simFlight Network Forums

Isak

Members
  • Posts

    14
  • Joined

  • Last visited

  • Days Won

    1

Isak last won the day on February 8

Isak had the most liked content!

Profile Information

  • Gender
    Male
  • Location
    Athens, Greece

Isak's Achievements

Rookie

Rookie (2/14)

  • One Month Later Rare
  • Collaborator Rare
  • Week One Done
  • Dedicated Rare
  • First Post Rare

Recent Badges

1

Reputation

  1. Hi John, As I mentioned, I wouldn’t call myself a "coder," so I often rely on ChatGPT for help. But when I get stuck, I turn to the forum for deeper insights. Thanks to your hints and guidance, I now have a working script again. I really appreciate your help—thank you so much! Best regards, Isak Here is the working lua 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 decimalNumber = tonumber(cleanFreq) -- Convert to number if not decimalNumber then return 0 end -- Return 0 if conversion fails local bcd = 0 local shift = 0 -- Convert each decimal digit to BCD format while decimalNumber > 0 do local digit = decimalNumber % 10 bcd = bcd + (digit * (16 ^ shift)) -- Use base 16 shift instead of bitwise left shift decimalNumber = math.floor(decimalNumber / 10) shift = shift + 1 end return bcd end while true do processInput() ipc.sleep(100) end
  2. Thank you for your help! I’m not very experienced in coding, so the error wasn’t obvious to me. But with your explanation, I now see where I went wrong and should be able to get it working. I’ll report back soon.
  3. 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
  4. Hi again, Now I would like to also be able to set the NAV1 Standby frequency in the same manner. The FSUIPC6.log file shows the following string when I send the NAV1 frequency 109.2: 314110 LUA.7: Local: datastring = NAV1SB 109.2 However in the simulator the NAV1 freqeuncy is not beeing set. Here is my lua 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.display("Data received: " .. datastring, 2) ipc.log("Data received: '" .. datastring .. "' [" .. length .. "]") -- Check if data is for NAV1 if datastring:match("^NAV1SB") then local frequencyStr = datastring:sub(8) -- e.g., "113.45" local bcdValue = convertToBCD(frequencyStr) -- Write to NAV1 standby frequency offset (0x0352) ipc.writeUD(0x0352, bcdValue) ipc.display("NAV1 standby frequency set: " .. frequencyStr, 3) ipc.log("NAV1 standby frequency BCD: " .. string.format("0x%X", bcdValue)) end end end -- Convert frequency to 4-digit BCD (leading '1' assumed) function convertToBCD(freq) local cleanFreq = freq:gsub("%.", "") -- Remove the decimal local bcd = tonumber(cleanFreq, 10) -- Convert to integer return bcd end while true do processInput() ipc.sleep(100) end Any ideas on how to get it working? Cheers, Isak
  5. I just got back from work and tried multiplying by 1000000, and it worked! Thank you so much for helping me out. Now, I’m planning to optimize the Lua script further by using the event.com function to handle reads and call back when there’s data to process. I’ll also look into modifying the C code to send the frequency directly to the simulator. Again, thank you so much for your help! Cheers, Isak
  6. With the code you suggested (with no minimum), I now receive a SimConnect message when I try to send a frequency. For example, when I type in 120.800 and send it, the following message appears: SimConnect Message Window COM1 standby frequency set to: 120800 Or if I send 118.100, I get: SimConnect Message Window COM1 standby frequency set to: 118100 However, the COM1 standby frequency in the aircraft does not actually update. I am using the standard Lockheed Martin AC-130H aircraft for testing. Any insights on what might be missing? I attached the script and log file again. Isak RadioTest.log RadioTest.lua
  7. Here I have attached the separate log-file and the lua script. Isak RadioTest.lua RadioTest.log
  8. Here is the FSUIPC6.log that shows what happens when I start the script and try to send/set a frequency. The script is called RadioTest.lua. FSUIPC6.log
  9. So, if I change the C code and use the FSUIPC SDK to update the offset with the COM! stndby frequency, there is no need for a lua script?
  10. Here is the working Arduino code that allows me to type in a frequency, which then shows up on the LCD screen. When I press the "#" key, the frequency is sent over the serial port as shown below (output from the serial monitor): 17:12:34.711 -> COM1SB 120.800 #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 == '#') { sendFrequencyToSim(); 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 (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 sendFrequencyToSim() { String formattedFrequency = formatFrequency(frequency); // Add the decimal point Serial.print("COM1SB "); // Add COM1SB tag Serial.println(formattedFrequency); // Send formatted frequency (with decimal) } Now, I am struggling with the Lua script. I’m not very experienced with coding and would really appreciate any guidance. My current attempt connects to the serial port, but the frequency is not being set: ---------------------------------------------------------------- ---------------------------------------------------------------- dev = com.open("COM6", 115200, 0) -- Open COM port with baud rate 115200 ---------------------------------------------------------------- ---------------------------------------------------------------- -- Display a message to indicate that the script is running ipc.display("Lua script is running...", 3) -- Message stays for 3 seconds function processInput() local datastring = com.read(dev, 256, 100) -- Read up to 256 bytes from the serial port, 100ms timeout if datastring ~= "" then ipc.display("Data received: " .. datastring, 2) -- Display the received data for debugging -- Example of parsing the received string to set COM1 standby frequency if datastring:match("^COM1SB") then local frequency = tonumber(datastring:sub(8)) * 1000 -- Extract frequency and convert to Hz ipc.control(67363, frequency) -- COM1_STBY_RADIO_HZ_SET ipc.display("COM1 standby frequency set to: " .. frequency, 3) end end end -- Main loop to continuously check for serial input while true do processInput() -- Call the function to process any incoming serial data ipc.sleep(100) -- Small delay to avoid high CPU usage end Cheers, Isak
  11. Thank you for the insights, John! I'm currently traveling, but as soon as I'm back, I'll share my Arduino code and my current Lua script attempt. Looking forward to any additional feedback you might have once I post those details. Cheers, Isak
  12. Hi John, Thank you for your detailed response and for pointing out my mistake with the forum and the license file. Apologies for that! I appreciate the clarification on the BCD16 format. That example definitely helps. Regarding the Lua script for detecting "COM1SB," I can see how handling all those key presses might get complicated. I'll check out the Lua script you mentioned for keyboard entry of radios and see if I can adapt it to my needs. Thanks again for your help! Best regards, Isak
  13. *** Moved from FSUIPC7 / MSFS sub-forum to main support forum *** Hi everyone, I’m currently working on a small Arduino project that allows me to input a radio frequency using a keypad. The frequency is displayed on an LCD screen. For example, if I type 120.800 and press the "#" key, the following appears in the serial monitor: COM1SB 120.800 What I want to achieve is setting the COM1 standby frequency in the simulator. I believe I’ll need to write a Lua script to detect the string "COM1SB", extract the frequency, and send it to the simulator using ipc.control. Am I correct that 66371 (COM_STBY_RADIO_SET) is the right ipc.control command to use? Could someone share an example of a Lua script that would accomplish this? Best regards, Isak
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.