ras78 Posted March 29, 2010 Report Posted March 29, 2010 Hello, I'm trying to code a dll in order to read the visibility and fill a USRVR5 variabile, thus I'll be able to condition an object in my scenery. Here's the code: #include "fsmodule.h" #include #include "FSUIPC_User.h" LINKAGE Linkage = { //0x000002d9, 0x00000000, module_init, module_deinit, 0, 0, 0x900, NULL }; IMPORTTABLE ImportTable = { { 0x00000000, NULL } }; WORD LowVis=1; UINT_PTR TimerFSUIPC, VisibilityTimer; BYTE FSUIPCMemory[500]; VOID CALLBACK Visibility(HWND hwnd, UINT uMsg,UINT_PTR idEvent, DWORD dwTime) { DWORD dwResult; DWORD IsLowVisibility; FSUIPC_Read(0x0E8A, 2, (DWORD*)&IsLowVisibility, &dwResult); FSUIPC_Process(&dwResult); //visibility is lower than 3 miles if (IsLowVisibility<=300) { FSUIPC_Write(0x0DDE, 2, &LowVis, &dwResult); FSUIPC_Process(&dwResult); } } VOID CALLBACK InitFSUIPC(HWND hwnd, UINT uMsg,UINT_PTR idEvent, DWORD dwTime) { DWORD dwResult; if (FSUIPC_Open2(SIM_ANY, &dwResult, (BYTE*)FSUIPCMemory, 500)) { KillTimer(NULL,TimerFSUIPC); VisibilityTimer=SetTimer(NULL,1,2000,Visibility); } } void FSAPI module_init () { TimerFSUIPC=SetTimer (NULL,0,2000,InitFSUIPC); } void FSAPI module_deinit () { KillTimer (NULL,VisibilityTimer); FSUIPC_Close (); } Basically I would fill that variabiles when the visibility is lower/equal than 3 miles. The compiler create succeasfullt my DLL but nothing runs as expected. Is this code correct? thanks! alberto
Pete Dowson Posted March 29, 2010 Report Posted March 29, 2010 Hello, I'm trying to code a dll in order to read the visibility and fill a USRVR5 variabile, thus I'll be able to condition an object in my scenery. A DLL? It would be easy to do as a Lua plug-in. Here's the code: I'm afraid I'm very rusty with FS DLL coding. Not been involved with that for a few years now. Have you used a debugger to see what is happening? These lines: DWORD IsLowVisibility; FSUIPC_Read(0x0E8A, 2, (DWORD*)&IsLowVisibility, &dwResult); if (IsLowVisibility<=300) { seem to be at odds. You read 2 bytes into the low end of a 4 byte DWORD then check if it is ever less than 300. The top end of that DWORD could be any old rubbish, whatever was on the stack at the time. You either need to initialise it to zero each time, before reading it, or be sensible (as you have with your LowVis variable) and declare it as a WORD in the first place. I don't know why you've cast it as a (DWORD *) in the call either. Since it is declared as a DWORD the cast does nothing, but doesn't it generate a compiler warning? Should be a (BYTE *). Regards Pete
ras78 Posted March 29, 2010 Author Report Posted March 29, 2010 I was not aware about Lua, I'll take a look ant thanks for the tips of code!
ras78 Posted March 30, 2010 Author Report Posted March 30, 2010 I had a quick look at LUA, seems so simple...to simple :) I tryied to port my C code into LUA module. Since I've just to read an offset, have a check and write another offset, the code should be this: visibility = ipc.readSW(0x0E8A) if visibility<=300 then ipc.writeSW(0x0DDE, 999) end Is it so simple or I miss something? the LUAC compiler create succesfully my out file, I placed it into /modules folder of FS9 and updated the FSUIPC version. This should be enough?
Pete Dowson Posted March 30, 2010 Report Posted March 30, 2010 visibility = ipc.readSW(0x0E8A) if visibility<=300 then ipc.writeSW(0x0DDE, 999) end Yes, that's good code to do what you want. But it has to be executed -- i.e. initiated, and kept running in some way. Is it so simple or I miss something? the LUAC compiler create succesfully my out file, I placed it into /modules folder of FS9 and updated the FSUIPC version.This should be enough? Not quite. And you did not need to pre-compile it. FSUIPC compiles source on loading it. First, you need to know that Lua programs can only be used by Registered FSUIPC users. If your add-on needs to be used by all I'm afraid this may be a stopping point for you. Second, I'll explain Methods of Running Lua Just placing the .Lua file into the FS Modules folder makes it available and assignable in FSUIPC's drop-downs, so it can be executed by button or keypresses. It can also be started in other ways, by programs (via an offset), or by placing a macro call like: ipc.macro("Lua ") into a file called "ipcReady.lua", also in the Modules folder. This file is executed when FS is ready to fly. For Lua programs relevant to specific aircraft only, the [Auto.] INI file section can be used to call Lua programs. Third, I'll explain how to keep your program running so it isn't merely executed once then terminated: The easiest way to make it run forever is to put it into an everlasting loop: while true do visibility = ipc.readSW(0x0E8A) if visibility<=300 then ipc.writeSW(0x0DDE, 999) end ipc.sleep(100) end Note I've used a Sleep of 100 milliseconds to avoid it wasting time looking too often. You don't want it hogging the system for nothing! The cleverer way, which wastes no time at all, is to use the event library to get your program run each time the offset you are interested in changes: function checkvis(offset, visibility) if visibility<=300 then ipc.writeSW(0x0DDE, 999) end end event.offset(0x0E8A, "SW", "checkvis") This loads up and simply registers your interest in offset 0E8A, so that function "checkvis" is called whenever it changes. There's one slight bother with that. If the visibility is less than 300 to start will, your offset 0x0DDE won't get set. So a fully working one would be like this, with an extra call to initialise things: function checkvis(offset, visibility) if visibility<=300 then ipc.writeSW(0x0DDE, 999) end end checkvis(0x0E8A, ipc.readSW(0x0E8A)) -- initialise for starting value event.offset(0x0E8A, "SW", "checkvis") Regards Pete
ras78 Posted March 30, 2010 Author Report Posted March 30, 2010 Thank you for complete explanation Pete, but since the LUA handling by FSUIPC is reserved only for registered user, I would prefer to move back to a DLL as I've to distribuite my scenery for free.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now