odwdinc / Python-SimConnect

Python interface for MSFS2020 SimConnect.dll

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python-SimConnect Help with P3D?

vamshichittaluri opened this issue · comments

Hello,
First of all, I really want to thank you for providing such an amazing piece of software.

My name is Vamshi, a PhD student from Ryerson University, Toronto, we are working on a project in which we need to control a Prepar3D(P3D) simulation with external data sources (data coming in from a python program...we are using a gps data log file to send data into the simulation.)

I have landed on Python-SimConnect and have been thanking the gods for it serves the exact purpose. I am trying to run the setup and keep running into the following error:

"SIMCONNECT_EXCEPTION_VERSION_MISMATCH"

This is obviously because the SimConnect version being used and the s/w was designed for is FS2020. The P3D sim i am using is P3D V5 which has a simconnect version of 5.1.0. Thus, the server (P3D) does not match the Client SimConnect version.
Since both FS2020 and P3D all develop with SimConnect to send/receive data, this should work.

I've also tried another thing, I've moved the simconnect.dll files from the P3D path into the python project file so that it can reference a dll generated with the libraries compiled on the P3D side. However, I get the following error:

" File "C:\Users\MIMS-PC\AppData\Local\Programs\Python\Python38\lib\ctypes_init_.py", line 391, in getitem
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'SimConnect_Open' not found"

It says simconnect_open not found, in the attributes.py file, we notice that the:

self.SimConnect = cdll.LoadLibrary(library_path)

SIMCONNECTAPI SimConnect_Open(

HANDLE * phSimConnect,

LPCSTR szName,

HWND hWnd,

DWORD UserEventWin32,

HANDLE hEventHandle,

DWORD ConfigIndex)

SIMCONNECTAPI SimConnect_Open is commented out.

These are two possible methods that I am trying right now. Looking forward to your reply and your guidance.

Can you please please please guide me through this? It will help me so much with my studies in creating an amazing data-driven simulation.

Hello Vamshi, Thanks for your interest! happy to help.
few questions and hopefully some answers.

  1. Where is the P3D server running? As the system is expecting it to run on the system as Python-SimConnect. Note there are ways around this if need just not well documented. as you are receiving this message "SIMCONNECT_EXCEPTION_VERSION_MISMATCH" I am guessing this is not the case though.
  2. The attributes.py file has commented out autogen code form the SDK's SimConnect.h. This is code for the open function.
    self.Open = self.SimConnect.SimConnect_Open
    self.Open.restype = HRESULT
    self.Open.argtypes = [POINTER(HANDLE), LPCSTR, HWND, DWORD, HANDLE, DWORD]
    def connect(self):
    try:
    err = self.dll.Open(
    byref(self.hSimConnect), LPCSTR(b"Request Data"), None, 0, 0, 0
    )
  3. looking at the code for P3D, This may be the root of the problem. https://www.prepar3d.com/SDKv5/sdk/simconnect_api/managed_simconnect_projects.html
    The native function calls SimConnect_Open and SimConnect_Close have been replaced by the SimConnect constructor, and Dispose method respectively.

my suggestions is to test with changing

self.Open = self.SimConnect.SimConnect_Open

to
self.Open = self.SimConnect.SimConnect

and

self.Close = self.SimConnect.SimConnect_Close

to
self.Close = self.SimConnect.Dispose

let me know if this helps. and good luck!

Hey!

Thanks for the reply!

So, the server and client are both running on the same system. Thus, I did not make any change to the xmls/cfgs involved.
However, when i make the changes you suggested, I get the following:

AttributeError: function 'SimConnect' not found

ok, is there another dll or are you using the LockheedMartin.Prepar3D.SimConnect.dll from SDK\lib\SimConnect\managed?
it seem like P3D is moved to a managed dll and to use the full open/close might need the SimConnect.lib form SDK\lib\SimConnect
if you can find an un-managed SimConnect.dll you should be good to go.

Hey, Yeah. So I've been using LockheedMartin.Prepar3D.SimConnect.dll from SDK\lib\SimConnect\managed. I cannot find the un-managed SimConnect.dll anywhere on the SDK other than instances of:

C:\Program Files\Lockheed Martin\Prepar3D v5\redist\Interface, which i guess are legacy versions?
they are named: Microsoft.FlightSimulator.SimConnect.dll. However, these are also nested inside their respective managed folders.

Should I try to find something on google? lol

you could try the Microsoft.FlightSimulator.SimConnect.dll if it works grate, if not then its probley managed as well.

I did find this gem, looks to be
http://prepar3d.com/forum/viewtopic.php?f=6310&t=139299

you cannot run a newer SimConnect client with an older SimConnect Server

https://www.prepar3d.com/forum/viewtopic.php?t=7502
Suggests the simconnect.dll form FSX/ESP should be good as long as it is an older or current gen.

http://prepar3d.net/forum/viewtopic.php?t=125348

it is possible for you to build your own simconnect.dll using the SDK files (simconnect.h and simconnect.lib).

So Try google, check out FSX/ESP might be helpful. if push comes to shove try compiling your own simconnect.dll ?? https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-160

good luck!

Hey,

Thank you.

I tried the Microsoft.FlightSimulator.SimConnect.dll. and I get the following error when I run the python script.

OSError: [WinError 193] %1 is not a valid Win32 application, Do you know what this could mean?

https://stackoverflow.com/questions/25651990/oserror-winerror-193-1-is-not-a-valid-win32-application

according to this, it seems like I have to point to the python.exe file. I will check it out and update you.

I will explore this route as well as potentially compiling our own simconnect.dll.

OSError

miss matched python version 64bit vs 32bit.

Found this https://sites.google.com/site/fseoperationsguide/getting-started/using-the-fse-client/simconnect-client

Edit, The bit of python needs to match the bit of the DLL. aka a 64bit DLL needs 64bit python and 32bit DLL needs 32bit python. It looks like the older versions is 32bit so use 32bit python for testing.
found DLL under
C:\Windows\WinSxS\x86_microsoft.flightsimulator.simconnect_*******\

Hey,
So I installed Python 32 bit. And also pulled the DLL from the path you specified.

I get this:
image
Edit' I guess its saying the altitude = aq.get("PLANE_ALTITUDE") is returning a NoneType while it should be a float.

wow that's wonderful!!
if you want to move you could try

# Set pos arund space nedle in WA.
sm.set_pos(
_Altitude=1000.0,
_Latitude=47.614699,
_Longitude=-122.358473,
_Airspeed=130,
_Heading=70.0,
# _Pitch=0.0,
# _Bank=0.0,
# _OnGround=0
)

Edit' I guess its saying the altitude = aq.get("PLANE_ALTITUDE") is returning a NoneType while it should be a float.

Just test altitude for None before adding back to altitude.

if (self.LastData + self.time) < millis():
if self.sm.get_data(self):
self.LastData = millis()
else:
return None
return self.outData

if the data is not found in the timeout then returns NoneType

maybe change _attemps is default to 10.

_time is for cashing results _time=0 disables cashing.
aq = AircraftRequests(sm, _time=0, _attemps=20)

See for more samples.
https://github.com/odwdinc/Python-SimConnect/blob/master/local_example.py

Also you are welcome to test the new Asyncio branch, no NoneType there :)
https://github.com/odwdinc/Python-SimConnect/blob/asyncio-test/local_example.py

Hey!

Thank you so much!!!!! IT works like a charm, I didn't check for the none type stuff yet. But I will do that later.
I went ahead and played around by setting different altitude types... it works just correctly!

Thank you so much for your help! I will write up a report along with instructions that I realized to get this working with P3D. and contribute to this project :)
This is such a great addition to what you have already committed to!

Once again! Thanks!!!!!

Glad to help, Happy Holladay's and all, and good luck with your PhD!

Thank you! And good luck to you too on your endeavors!
I have one question actually: The update rate of the sim, is it stuck to 2000ms? or can we make it any faster? I would like to update data in real time.

Kind Regards
Vamshi

Thank you.

Where are you seeing 2000ms?
There should be no delays in updating data, there might be some in receiving data.

Updates to SimConnect are called every 2ms via:

def _run(self):
while self.quit == 0:
self.dll.CallDispatch(self.hSimConnect, self.my_dispatch_proc_rd, None)
time.sleep(.002)

Data is pushed to SimConnect without delays via:

@value.setter
def value(self, val):
if self._deff_test() and self.settable:
self.outData = val
self.sm.set_data(self)

Then:
def set_data(self, _Request):
rtype = _Request.definitions[0][1].decode()
if 'string' in rtype.lower():
pyarr = bytearray(_Request.outData)
dataarray = (ctypes.c_char * len(pyarr))(*pyarr)
else:
pyarr = list([_Request.outData])
dataarray = (ctypes.c_double * len(pyarr))(*pyarr)
pObjData = cast(
dataarray, c_void_p
)
err = self.dll.SetDataOnSimObject(
self.hSimConnect,
_Request.DATA_DEFINITION_ID.value,
SIMCONNECT_SIMOBJECT_TYPE.SIMCONNECT_SIMOBJECT_TYPE_USER,
0,
0,
sizeof(ctypes.c_double) * len(pyarr),
pObjData
)
if self.IsHR(err, 0):
# LOGGER.debug("Request Sent")
return True
else:
return False

Oh! Nevermind, I thought it was 2secs sorry about that.
Thanks!

Hey Odwdinc,

Sorry to bug you again, The plane's altitude etc changes no problem.
However, I keep getting this exception. Do yu have any idea on why is this is being thrown and how I can resolve it.

image

Thanks, Vamshi

Are you calling sm.quit() or sm.exit()?

def exit(self):
self.quit = 1
self.timerThread.join()
self.dll.Close(self.hSimConnect)

looks like the error is started with call on sm.quit() line 35 on testing.py

Proper exit is:

sm.exit()
quit()

If you able to post some test code when you see the problem, I can take a look.

Hey,

So I am using sm.exit() now. Howeer, I still recieve the following:
image

Edit': Error Text:

Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Program Files (x86)\Python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\SimConnect\SimConnect.py", line 173, in _run
self.dll.CallDispatch(self.hSimConnect, self.my_dispatch_proc_rd, None)
OSError: exception: access violation writing 0x037D0048

Edit2': Sorry I forgot to post the code I was running: very basic
`from SimConnect import *

Create SimConnect link

sm = SimConnect()
aq = AircraftRequests(sm, _time=0)

To find and set timeout of cached data to 200ms:

altitude = aq.find("PLANE_ALTITUDE")

Set the aircraft's current altitude

aq.set("PLANE_ALTITUDE", 10000)

sm.exit()
#quit()`

Are you running form command line or form some IDE? I have had reports that some IDE's (Visual Studio) may cause errors.
Can you test with this code on the command line?

from SimConnect import *
import logging
logging.basicConfig(level=logging.DEBUG)
LOGGER = logging.getLogger(__name__)
LOGGER.info("START")
sm = SimConnect()
aq = AircraftRequests(sm, _time=0, _attemps=20)

pa = aq.find("PLANE_ALTITUDE")
print(pa.get())

sm.exit()
quit()

I am running it on Idle. So it shouldnt be any issues as far as i know. But when i did run it on cmd, line by line and i get the following errors at this point:
sm = SimConnect()
image

So my best guess is that there is some python threading running in the background?? Maybe form the time you where calling sm.quit() vs sm.exit().
I know its silly but have you checked task manager? or a reboot? If nothing else can you test with https://docs.python.domainunion.de/3/library/faulthandler.html#module-faulthandler

also are you using python in a Virtual Environment?

Hey,

Checked with a reboot as well as closing any instances of python on task manager...
And no, not using it on a VE.
Edit' came across the following: if you can make any sense out of it.

pywinauto/pywinauto#451

Best guess is how windows handles 32bit apps in 64bit. As this is a windows only program I think we can get by with
https://docs.python.org/3/library/ctypes.html#ctypes.WINFUNCTYPE

self.DispatchProc = WINFUNCTYPE(c_void_p, POINTER(SIMCONNECT_RECV), DWORD, c_void_p) 

vs

self.DispatchProc = CFUNCTYPE(c_void_p, POINTER(SIMCONNECT_RECV), DWORD, c_void_p)

I tested on the 64bit and see no errors replacing this line, hopefully this will help.

Hey,

No, making the change still throws the same exceptions for some reason.
Its so weird, cause, when i send a request for an altitude change, it does make the change and then throws this response :/

I am clearly lost at what the issue could be. However, it is throwing an OSError(OSError: exception: access violation writing 0x04220048), which in the link you posted and the following
https://docs.python.org/3.4/library/ctypes.html

shows the potential issue:

ctypes exports the cdll, and on Windows windll and oledll objects, for loading dynamic link libraries.You load libraries by accessing them as attributes of these objects. cdll loads libraries which export functions using the standard cdecl calling convention, while windll libraries call functions using the stdcall calling convention. oledll also uses the stdcall calling convention, and assumes the functions return a Windows HRESULT error code. The error code is used to automatically raise an OSError exception when the function call fails.

Thank you for all your kind help.

Hey!!
I managed to get it fixed!!!! I had to change the following:

self.SimConnect = cdll.LoadLibrary(library_path)

-->
self.SimConnect = windll.LoadLibrary(library_path)

and then

self.DispatchProc = CFUNCTYPE(c_void_p, POINTER(SIMCONNECT_RECV), DWORD, c_void_p)

to

self.DispatchProc = WINFUNCTYPE(c_void_p, POINTER(SIMCONNECT_RECV), DWORD, c_void_p)

in the attributes file.

Thank you so much for all the help!!

Kind Regards
Vamshi

wonderful!
All push the changes to the repo, as the have no effect on the 64bit version.
Thank you for working this out!

Thank you so much!!
I will write the "guide" to get it up on P3D as well.
Happy Holidays!
Kind Regards
Vamshi

Thank you so much!! I will write the "guide" to get it up on P3D as well. Happy Holidays! Kind Regards Vamshi

Sorry to bother you but I wonder could you please upload the copy you have changed to match P3D? Thank you!