Jump to content
The simFlight Network Forums

FSUIPC5 Socket - Event issue


Recommended Posts

Hello,

Hoping this is something simple but i am now stuck after several days trying to work this out..

So...

I have registered version of FSUIPC5 installed, with the updated socket/core.dll for 64 bit. (all working)

Created the Lua script to set up the server connection (working), can register my client and send "hello" ack back to the client. 

As the socket client I have an ethernet arduino connected to my lan, this calls the server and registers an "event.offset" 

But...

No matter what offset I listen to, i do not seem to be getting anything sent to the call back function on the offset change.

Airplane is the PDMG 737, but as a test I tried a simple offset to check updates were working. and they are not.

 

So in Lua, via a socket read function, executes: 

 

local function ProcessObserveOffset( offset, oft)
    print(string.format("ArduPilot Register:OBS_OFFSET %x:%s", offset, oft))
    event.offset(offset, oft, "OnOffsetModified")
    print("logged")
end

even tried just hardcode line in Lua to register the event.

event.offset(0x64f6, "UW", "OnOffsetModified")

I get no response in the logging window when this is run, but also don't see any errors, I changed the UW to SB to check and do get an error telling me type or size is wrong. So assuming it is processing the event.offset command.

But callbackfunc is never called. 

If i Monitor this using the FSUIPC addon logging to Sim Connect Window in P3D, i can see the value changing 0:1 when I flip the switch in the cockpit. But again the event.offset callbackfunc is not run.

 

So either the event.offset registration is not working correctly  or the event.offset is not triggered and calling the callbackfunc...

 

Any advice welcome to save the last of my hair being pulled out...

Thanks

Andrew

 

 

 

 

Link to comment
Share on other sites

22 minutes ago, dru5412 said:

I have registered version of FSUIPC5 installed, with the updated socket/core.dll for 64 bit. (all working)

64-bit sockets all working? Have you any details of how to use the 64 bit download so i can document them for others?

24 minutes ago, dru5412 said:

No matter what offset I listen to, i do not seem to be getting anything sent to the call back function on the offset change.

Have you tested with more obviously changing offsets, such as the time seconds value in offset 023A (a "UB").

27 minutes ago, dru5412 said:

Airplane is the PDMG 737, but as a test I tried a simple offset to check updates were working. and they are not.

Which "simple offset"? If you only tested with PMDG offsets, have you enabled these in the PMDG file?

28 minutes ago, dru5412 said:

So in Lua, via a socket read function, executes: 

 


local function ProcessObserveOffset( offset, oft)
    print(string.format("ArduPilot Register:OBS_OFFSET %x:%s", offset, oft))
    event.offset(offset, oft, "OnOffsetModified")
    print("logged")
end

Where does this get the parameters offset and oft from? Is it only called the once, and if so how -- from another event?

31 minutes ago, dru5412 said:

I changed the UW to SB to check and do get an error telling me type or size is wrong. So assuming it is processing the event.offset command.

Er ... what error was that? "SB" is a valid data type. Types are not checked against offsets -- any offset can be read as anything. So i'm puzzled -- just setting it to "SB" will NOT generate an error. You can call it a FLT32 or even as ASCIIZ. It isn't checked. Nothing knows what types any offsets are. They are what you treat them as.

Pete

 

 

Link to comment
Share on other sites

 

1 hour ago, Pete Dowson said:

64-bit sockets all working? Have you any details of how to use the 64 bit download so i can document them for others

64-bit, I downloaded the core.dll shown on another topic on this forum. 

Seems to remove all the Lua error on loading, and can open a valid socket to talk to my ethernet arduino. 

Quote

Have you tested with more obviously changing offsets, such as the time seconds value in offset 023A (a "UB").

No, but will try that now. and report back.

 

Quote

Which "simple offset"? If you only tested with PMDG offsets, have you enabled these in the PMDG file?

I tried with offset for "battery master" on default p3d model (non pdmg) but will try 023A as suggested

 

Quote

Where does this get the parameters offset and oft from? Is it only called the once, and if so how -- from another event?

It comes another function, which is parsing what it receives over the socket from the arduino.  Extract below:

local cmd, offset, len =
        string.match(data, "(OBS_OFFSET) (0x%x+):(%a+)")
    if cmd then
        return {
            action = cmd,
            offset = tonumber(offset, 16),
            length = len
        }
    end

Basically i am trying to program some common functions so that the arduino can do 4 main things

1. Register and open a socket connect

2. Register which Offset or Lvars to be updated on

3. Receive updates from event.offset call back (or event.lvars callback)

4. all linked to hardware items. (buttons, switches, rotary, lcd, led, etc)

So there is data posted from the arduino in the format 

"OBS_OFFSET 0x64F6:UW"

This is then pattern matched with the above parse code, to write the event.offset function. 

Quote

Er ... what error was that? "SB" is a valid data type. Types are not checked against offsets -- any offset can be read as anything. So i'm puzzled -- just setting it to "SB" will NOT generate an error. You can call it a FLT32 or even as ASCIIZ. It isn't checked. Nothing knows what types any offsets are. They are what you treat them as.

OK, then this might just be User Error on the offset or something, again let me test this and get the log if it is relevant, but suspect it was typo in my code...

Post again shortly after testing 023A:UB

thanks

 

Link to comment
Share on other sites

Hmmm ok.

So I hardcoded 023A offset to test, and nothing seems to be happening.

function OnOffsetModified(offset, value)
    local cmd = string.format("EVENT_OFFSET %x %d", offset, value)
    print(string.format("ArduPilot Offset:OBS_OFFSET ",cmd))
    assert(client:send(cmd .. ack));
    
end


local function ProcessObserveOffset( offset, oft)
    print(string.format("ArduPilot Register:OBS_OFFSET 0x%x:%s", offset, oft))
  --  event.offset(offset, oft, "OnOffsetModified")
	event.offset(0x023A, "UB", "OnOffsetModified")
	print("logged")
end

 

From this I would expect to see the log showing 

 

ArduPilot Offset:OBS_OFFSET EVENT_OFFSET 0x023A <sec>

But it just stops after the print "logged", which i added just to debug to make sure it wasnt getting stuck on the event.offset line above.

 

Just a thought, as im no Lua expert at all.. (in fact a beginner)

I have a while 1 routine running at the bottom of the lua code, which is listening and then parsing the incoming data, this wouldnt be blocking would it?

while 1 do
    print("ArduPilot: waiting for client connection...");
    local client = server:accept();
	client:setoption("tcp-nodelay", true);
    if client ~= nil then
		print("ArduPilot: client connected!");
		while 1 do 
			local command, err = client:receive();
			if not err then	
				if command == bye then
					print("ArduPilot: client disconnected");
					client:close();
					break;
				elseif command == hello then
					print ("ArduPilot: replying Hello");
					assert(client:send(hello .. ack));
				else						
					print("ArduPilot: Received: ");
					print(command);
					OnDataReceived(command);
					assert(client:send(ack));
				end	
				
			else
				print("ArduPilot: client error, probably disconnected");
				client:close();
				break;
			end
		end
	end
end

 

onDataReceived(command) is the data parser as described before.

May just try stripping this back to simple lua test.

event.offset

callback that just has print function and remove the local socket = require("socket");

just in case its something strange happening there

 

Below is the Log extract.

     4094 LUA.2: AruPilot: waiting for client connection...
     8797 C:\Program Files\Lockheed Martin\Prepar3D v4\SimObjects\Airplanes\PMDG 737-800NGX WL\B737-800WL.air
     8797 PMDG 737 offsets enabled
    55109 Aircraft loaded: running normally now ...
    55125 User Aircraft ID 1 supplied, now being used
    55938 System time = 20/11/2017 15:48:30, Simulator time = 15:47:40 (15:47Z)
    55938 Aircraft="PMDG 737-800NGX PMDG House Winglets"
    62938 Starting everything now ...
    64109 Advanced Weather Interface Enabled
    74609 LUA.2: AruPilot: client connected!
    74625 LUA.2: AruPilot: replying Hello
    74625 LUA.2: AruPilot: Received:
    74625 LUA.2: OBS_OFFSET 0x023A:UB
    74625 LUA.2: ArduPilot Register:OBS_OFFSET 0x23a:UB
    74625 LUA.2: logged

 

Link to comment
Share on other sites

16 minutes ago, dru5412 said:

It comes another function, which is parsing what it receives over the socket from the arduino.  Extract below:

And how is that called?

You should only ask for each specific event the once. They get registered, then the function gets called every time the offset or whatever changes. Do NOT keep calling event facilities for the same events unless you cancel them first (as per documentation).

3 minutes ago, dru5412 said:

I have a while 1 routine running at the bottom of the lua code, which is listening and then parsing the incoming data, this wouldnt be blocking would it?

Of course! Events can only trigger when your program exits! Your infinite while loop never exits. Events might pile up, but can never execute becuse the thread you have is continually sitting in your loop, piling more and more events up!

You should only use events or loops. To do it the way you are doing it, with a loop, you'd just use the ipc.read... functions for the offsets to see if they'd changed since last time you looked.

Generally event requests are the last thing you do before the end of the program. You thread will simply then be suspended until one of those eventstriggers.

So, make up your mind which method to use. You can use events for your Arduino inputs as well, either via event.com, or a simply timer (event.timer) to call the arduino reading function at reguilar intervals.

If you look at the provided exampls you'll see they are all of one tpe or the other. Most of the looping ones would be better re-written using events, but they pre-date the event system which was added later.

Pete

 

Link to comment
Share on other sites

Thanks Pete.

Knew it was going to be obvious!

I had worked my way through the code, but resorted to posting here 1 step too early!

p.s. The register event is only called once from the Arduino  at time of the client socket registration. 

Done this so that I can have multiple boards, each one with its only set of offsets/lvars associated.

Just need to work out how to trigger a function when client:receive(); 

event.com wont work as i am talking to the arduino over TCPIP via sockets not a com port. (if i understand this correctly)

Thanks for the help will go and learn some more lua!

 

Regards

Andrew

 

 

Link to comment
Share on other sites

1 minute ago, dru5412 said:

event.com wont work as i am talking to the arduino over TCPIP via sockets not a com port.

Oh, of course. Why is that, BTW?  My Arduino is definitely connected via a serial port.

There's no provsion for events on sockets, so you'd need to read the incoming message using a function called by event.timer. 

I notice your infinite loop has no ipc.sleep() in it with a time, so you are trying to get it excuted as fast as possible, though i assume it may "sleep" in the receive function -- or can that return immediately with no data? 

Just select a suitable time ot "poll" the socket reception. Something which relates to how often you expect something to arrive.

Pete

 

 

 

Link to comment
Share on other sites

To be frank, im starting to ask myself that same question....

This is as much a learning exercise for me as it is to build next cockpit panel.

Reading now the Com library, i am thinking I might switch to that rather than ethernet and sockets...

Anyway, will keep going just to prove to myself it works...

And thanks for the tips, i will rethink the way the socket waits for the connection. and also receives data. either via timer or poll or something else i find on google...

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • 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.