The simFlight Network Forums

# Help with syntax when using Lua routine n=ipc.readUB(offset) and offset 3125

## Recommended Posts

I'm new to using offset and counting in binary, so my issue may be my full understanding of Hexidecimal and bits work. I'm trying to read the status of Fuel pump in FSX. with aircraft with 4 engines and I beleive that I am getting cthe orrect results when using offset 3104 which only appies to one engine as follows  n=ipc.readUB(3104).  I'm getting expected result of 1= on and 0 = off. My quetion is surrounding using same logic but now with offset 3125.  My understanding which may be flawed, is that memory location 3125 is an 8 byte location and I think that bit location 1 (2^0) has engine one information, bit location 2 (2^1) has engine two information, bit location 4 (2^2) as engine three information and finally bit location 16 (2^4) has engine four information?

My questions are:

1. My inital thought above was that I am dealing with an 8 byte memory location, but my engine 4 logic (2^4) is calculating a bit 16 location so I think my logic is flawed, how I'm I thinking wrong?
2. How do I fill out the ipc.readUB(offset) when only wanting one specific bit location? I think I need to use a mask, but not sure syntax to use? For engine three for example, would it be n=ipc.readUB(3125&04)?
3. I'm I correct that I should expect to to get the same results as offset 3104 for each engine of 1= on and 0 = off?

Thanks

##### Share on other sites

6 hours ago, oneaokguy said:

My inital thought above was that I am dealing with an 8 byte memory location, but my engine 4 logic (2^4) is calculating a bit 16 location so I think my logic is flawed, how I'm I thinking wrong?

Its a 1-byte or 8-bit address. Bit 1 (counting from 1) holds pump1, bit 2 holds pimp2, etc

6 hours ago, oneaokguy said:

How do I fill out the ipc.readUB(offset) when only wanting one specific bit location? I think I need to use a mask, but not sure syntax to use? For engine three for example, would it be n=ipc.readUB(3125&04)?

You need to read as a byte and use the logic library, e.g.
pump1 = logic.And(n, 0x1) -- 2^0
pump2 = logic.And(n, 0x2) -- 2^1
pump3 = logic.And(n, 0x4) -- 2^2
pump4 = logic.And(n, 0x8) -- 2^3

6 hours ago, oneaokguy said:
1. I'm I correct that I should expect to to get the same results as offset 3104 for each engine of 1= on and 0 = off?

Offset 0x3104 only holds pump1, so that should match the state of the first bit in offset 0x3125.

6 hours ago, oneaokguy said:

I am getting cthe orrect results when using offset 3104 which only appies to one engine as follows  n=ipc.readUB(3104).

But that is not correct should be  n=ipc.readUB(0x3104). You need to precede offset values with 0x, or use the string form, i.e.  n=ipc.readUB("3104")

John

##### Share on other sites

Thanks, I had totally missed the logic library and needed that missing piece. Let me add that logic and see if I have any follow-up questions.

As an aside, my documentation might be outdated and this might have already been corrected, but the callout on my FSUIPC Offset cheat sheet for 3125 looks wrong and should read 2^3 for engine #4 which makes more sense now with 8 bit address. If that has not been updated, suggest it be amended.

##### Share on other sites

Follow-up question on the values I should expect for each engine pump. It was my understanding from documentation that a value of 1=on and 0=off should be expected for each but I'm getting the following

Pump1 - 0=off and 1=on

Pump2 - 0=off and 2=on

Pump3 - 0=off and 4=on

Pump4 - 0=off and 8=on

The On value seems wrong since we are dealing with one bit, so I must have an error again in my understanding and my logic for displaying the value after switch is on

I'm using the following display code to see the value for each pump before and after flipping switch in FSX using the earlier ipc.read and logic.and  code, the full function is listed below.

ipc.display(Pump2,3)

Full Function

function Eng2_Fuel_Pump_On()
Pump2x=logic.And(Pump2, 0x2)
ipc.display(Pump2x,1)
ipc.sleep(3000)
ipc.control(66341)
ipc.sleep(3000)
Pump2z=logic.And(Pump2a, 0x2)
ipc.display(Pump2z,5)

end

As I shared earlier, I'm new in thinking on binary terms and I was trying to understand the highlighted example to better understand this logic routine, but I'm not understanding why the below holds true and may be where my error lies in thinking I can display this value. Are you able to explain why those two binary values = 0010? I'm I somehow adding bits when I am displayng the value for each pump?

##### Share on other sites

3 minutes ago, oneaokguy said:

As an aside, my documentation might be outdated and this might have already been corrected, but the callout on my FSUIPC Offset cheat sheet for 3125 looks wrong and should read 2^3 for engine #4 which makes more sense now with 8 bit address. If that has not been updated, suggest it be amended.

Yes, I noticed that as well and will update.

As a sided-note, its also more common/useful to start counting bits from 0, as then bit 0 is 2*0 (1), bit 1 is 2^1 (2), bit 2 is 2^2(4), etc, so the bit number corresponds to the power of 2 number.

##### Share on other sites

37 minutes ago, oneaokguy said:

Follow-up question on the values I should expect for each engine pump. It was my understanding from documentation that a value of 1=on and 0=off should be expected for each but I'm getting the following

Pump1 - 0=off and 1=on

Pump2 - 0=off and 2=on

Pump3 - 0=off and 4=on

Pump4 - 0=off and 8=on

Yes, of course, as that is the bit value. 0 is always off, and::
1 = 2^0 (bit 0)
2 = 2^1 (bit 1)
4 = 2^2 (bit 2)
8 = 2^3 (bit 3)
...etc

But this shouldn't matter. When testing bits. you just want to know if its off (value = 0) or on (value != 0).

##### Share on other sites

39 minutes ago, oneaokguy said:

As I shared earlier, I'm new in thinking on binary terms and I was trying to understand the highlighted example to better understand this logic routine, but I'm not understanding why the below holds true and may be where my error lies in thinking I can display this value. Are you able to explain why those two binary values = 0010? I'm I somehow adding bits when I am displayng the value for each pump?

Compare each bit position:
bit 0 - first number has 1 in this position, second has 0 - and (&) them and you get 0
bit 1 -                             1                                             1                                               1
bit 2 -                             0                                             0                                               0
bit 3 -                             0                                             1                                               0
result: 0010

But this is not the place for a binary logic tutorial - please try google!

##### Share on other sites

Thanks John for the help.

## Create an account

Register a new account