General
Banner
Home | Articles | Dutch

 

Basics for cockpit builders.

Writing your own cockpit managements program in Visual Basic 6.

Part 1: Read and write data to FS

Introduction | Required software | Foreknowledge | Read program | Write program.

Introduction

This subject is probably something a lot of people have wondered about and raise a lot of questions. No simple matter to explain in three words. We will try to explain the basics on how to read out variables from FS and write to FS by means of the SDK for programmers from FSUIPC (system development kit for Flight Simulator Universal Inter-Process communication) of Peter Dowson. With this basic knowledge we will then make a program that couples the hardware interface K8061 with toggle switch to FS. As second interface we will use the serial communication port and make a program that send data to a self made instrument. So we have highlighted the two way communication from and to the cockpit.

First of all I'm no programmer myself. All the knowledge I have of this is from individual study. Some time ago I bought myself an elaborate book called "The book Visual Basic 5" from Easy computing. And started experimenting with it. In this book you will find all about the basic things you need to know and it helped me a lot solving many problems. Of course I also had some help from colleges from the Flight Simulator Club Belgium., who helped me get started. Once you understand the basics it is a little step to start making your own cockpit managements program. I will try to explain all this in a simple and understandable way with some examples. To get a general idea on how the connection between FS and the hardware can be made, I would like you to go to article 9 where you can read all about it.

Top

Required software.

Because we are going to program in Visual Basic you need to have Microsoft Visual basic 6 or 5. All examples described in this article are made for version 6.

Further we are going to use FSUIPC.dll. This connection module for MSFS is placed in the folder 'module' in your Flight Simulator and serves as interface so you can read and write variables to FS via the FSUIPC SDK for programmers.
In the page Links of this site you will find a link to the homepage where you can buy this software. Without this dll there is no connection. There are of course other ways to communicate with FS but they are very complicated and above all not universal. So every time Microsoft comes out with a new version of FS you need to start all over again. This is why I stick with FSUIPC. This program has already a windows based program that lets you change all kinds of inputs. We won't make use of this but we will make are own custom made program with the same functionality but only for those things we really need and can be connected to hardware.

FSUIPC System Developer Kit (SDK for programmers). This contains a module (FSUIPC.bas) witch we need in the Visual basic program.

K8000 Visual Basic module. Adapted version for Windows XP to connect the IO card of Velleman to the PC. I adjusted the module myself so it now can be used for 128 bits in stead of the 64 bits in the original module. Normally (according the manufacturer) you can only extend the K8000 to 4 cards(1 master and 3 slaves). But by using a different I²C chip the PCF8574P in stead of the PCF8574AP it is possible to extend it with an other 4 cards. So you get a total of 128 in- or outputs. This module can be downloaded as well in the download page.

DlportIO.dll. This dll is needed to control the parallel port under Windows XP. We only need this dll when we want to work with the K8000. For serial communication we don't need this.

Top

Foreknowledge of Visual Basic.

Some basic knowledge of Visual Basic is advisable but not all necessary to come to a final result. I would recommend you to bay a good book in your own language. The book I mentioned above 'The book Visual Basic 5' from Easy computing is a very good book to learn the basics. I'm sure they also have a book about VB6. You can understand I can't explain you all the basic things, this would mean I could better write a whole book about it. But Visual Basic is fairly understandable. How to make a form, putting on some command buttons, add a module to a project is very simple and easy to learn.

Top

Reading out variables from Flight Simulator.

We start with a new project in VB, we will call it ReadFS. We start by creating a form with two buttons, a combobox and a label field to show the values we will read out. The project will also have two modules, the first is FSUIPC.bas (basic module of the FSUIPC SDK) and second MdlRead.bas (own made module to read out FS data) you can find the source code of this example in the page downloads. You can open it in VB and see how it's made.

The program first initialises and opens FSUIPC by clicking on the button "Start FSUIPC". With this step we check if FS is running. It reads out the FS version and sends it to the label called 'lblFSUIPCcon'. If FS isn't started yet and we can't make a connection due to other problems. A message box will then open and gives us a fault message why we can't connect. Once FSUIPC is open we can start using the read and/or write procedures. In this example we will only use the read function.

In the module MdlRead.bas you can find examples on how to make a read procedure.
In principle it is always the same. We first start by naming a number of global variables we will be using outside the subroutines and modules.

Example:
Public All_LigHt As Integer
The length of the variable is determined by length of the result of a chosen offset. The lengths of the different offsets can be found in the tables of the FSUIPC SDK. The next step is adding a Public procedure that works outside the module.
Within the subroutine we declare the internal variables.
Example:
Dim booResult As Boolean
Dim dwOffset As Long
Dim dwSize As Long
Dim dwResult As Long
Dim Light As Integer

The value of the offset can be found in the tables of the SDK (ligth = 0D0C)
The length of the offset is set as follows (light = 2)

1 as byte
2 as integer
4 as long
8 as double

We now proceed with the FSUIPC Read procedure and declare the offset number together with the offset length.
We will now get the result from FSUIPC after the FSUIPC_Process . This goes as follows

dwOffset = (&HD0C)
dwSize = Len(Light)
If FSUIPC_Read(dwOffset, dwSize, VarPtr(Light), dwResult) Then
If FSUIPC_Process(dwResult) Then
All_LigHt = Light
End If
End If

This way of reading out data is very good for a single offset. For example to test things out and to have a quick result. If on the other hand you have numerous offsets you want to read at the same time, this way of reading out data isn't very good because you will loose to much time by executing the FSUIPC_Process procedure. The next way of programming is then preferred.

'read Land Lights
dwOffset = (&HODC)
dwSize = Len(Light)
booResult = FSUIPC_Read(dwOffset, dwSize, VarPtr(Light), dwResult)
'read Flap position
dwOffset = (&HBE0)
dwSize = Len(strFlapPos)
booResult = FSUIPC_Read(dwOffset, dwSize, VarPtr(strFlapPos), dwResult)

booResult = FSUIPC_Process(dwResult)

All_Light = Light
Flap_pos = strFlapPos

When programming this way you only use the FSUIPC_Process procedure once and transfer all the needed offset information in one time to the variables. This speeds up the retraction of data tremendously. Certainly when you have a large number of offsets.

When closing off the form you always need to make sure you also close FSUIPC. This to prevent that when you start a new session, FSUIPC gives you a fault message that it is already open. You won't be able to restart your program or any other program using FSUIPC. The only way is than to reboot your computer.
The subroutine you need for closing FSuipc is described in Form_terminate

Top

Write program:

The preferred way of giving inputs to FS is trough the joystick buttons, but sometimes it can be handy to do this in a different way. I use the K8000 from Velleman for this. In this example we will look at how we can write data to FS. To keep things simple we will not go into dept on how to connect a switch to FS in this part. This is for later

We start a new project in VB and name it WriteFS. In this project we will highlight how to write data to FS trough FSUIPC offsets. A second way to change values in FS is to send keystrokes. ( for example F7 Flaps down). The program is made up out of two modules. First the FSUIPC.bas and second the self made mdlWrite.bas. A program example can be found in the page downloads. You can open it in VB and find out how the program is made.

The program first initialises and opens FSUIPC by clicking on the button "Start FSUIPC". With this step we check if FS is running. It reads out the FS version and sends it to the label called 'lblFSUIPCcon'. If FS isn't started yet and we can't make a connection due to other problems. A message box will then open and gives us a fault message why we can't connect. Once FSUIPC is open we can start using the read and/or write procedures. In this example we will only use the write function.

In the module mdlWrite are two subroutine examples. The first example shows you how to send a key code to FS. The second example shows you how program a offset to send data to FS.

Key code example:

'Write example key code (function code F7 flaps down one step,max 4)
Public Sub Flap_down()
Static strFlp

If strFlp < 4 Then
AppActivate (FSversie)
SendKeys "{F7}"
WaiT (0.5)
strFlp = strFlp + 1
End If

End Sub

In the subroutine we first declare the internal variables. The variable strFlp is declared as static and will keep its last value. This is very handy if want to count the number of times this task is being carried out.
The next step in the program will set the focus on the program we want to write a key code to and not any other program that is active at that moment. We will do this with the procedure AppActivate(Title,[wait]). Between the brackets we will give the name of the program that needs to be in focus. In my example I put in a variable called FSversie. The value of this variable is determined at the start of the program in the subroutine FSversie_controle in the module mdlwrite. The execution of this subroutine takes place on the moment we connect FSUIPC to FS. You can find this step in the Form frmWriteFs under subroutine cmdConnect_Click. Depending on the FS version you use, it will give a different title. In my case it is "Microsoft Flight Simulator 2004".
After this we will send a key code with the procedure Sendkeys string. Depending whether you want to send a character, a number or a special key the string will be different. All the different possibilities are explained in the help files of VB. For a small single character it is very simple you write for example "A". For a function key like in my example it is "{F7}". For all other special keys please look in the help files.
After sending the key code we will wait for half a second to make sure the program received the key code. Experience told me that if you don't do this sometimes the key code isn't processed by lack of time. In this little program it wouldn't be the case but later on if we start using timers to run the management program it will be a must.
As last step we add 1 to the variable strFlp. As long as this variable is smaller then 4 the routine will be executed. From the moment it becomes 4 it will be impossible to go on. This will prevent use from given more than 4 commands to the flap control.

Very simple, with a few instructions we can now send key codes to any selected application.

Offset example:

This is a bit more complicated. It exists out of two main steps. First and biggest part is in the module mdlWriteFs and the second ,and executive, part is in the form frmWriteFs.

First we look at the sub LL2 in the module

Public Sub LL2()

This is build up like the Read subroutine. We first declare the internal variables

Dim booResult As Boolean
Dim dwOffset As Long
Dim dwSize As Long
Dim dwResult As Long
Dim LL2 As Byte

Next we give LL2 a value 1 (Landing light on = 1 or out = 0). You can find these values in the list of offsets in SDK of FSUIPC.
LL2 = 1

After this we declare what offset we want to change togheter with the length of this offset. How this is done or found can be seen in the read part above.


dwOffset = (&H28C)
dwSize = Len(LL2)

The last step is to call the FSUIPC_write procedure and give it all the different values.
booResult = FSUIPC_Write(dwOffset, dwSize, VarPtr(LL2), dwResult)
End Sub

Like you will notice we are not going further with the FSUIPC_process procedure. For the same reason as reading out data, we are going to process all the read and write data in the same time to make the program faster. The second step is done in the form in the sub Write_FSUIPC.

Public Sub Write_FSUIPC()

First we declare all the internal variables
Dim booResult As Boolean
Dim dwResult As Long

Next we call for all the different subs we want to write data to. Of course you can make it conditional. For example if switch 1 is on you can change value x to 1 else etc. We will go further in to dept on this issue in the next chapter when we will speak about the connection with the K8000. In our example we have only one offset to change that is LL2. We will call for the sub with the procedure Call. By doing this the program will jump to the sub LL2 in the module mdlWriteFs, and will execute all the deferent steps.
Call LL2

The last step is to process all the different results in on step. This can of course be write and read instructions together.
booResult = FSUIPC_Process(dwResult)

Like you see it is very simple to change values in FS with FSUIPC.

Top

<< Previous | Home | Articles | Next >

 

 

© Verley Jan 2007-2017