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