Jump to content
The simFlight Network Forums
Sign in to follow this  
zuby

HID buffer writing problem

Recommended Posts

hello everyone,
I made a program in mikroc compiler that reads hid buffer values and write that values to the 7 segment display.  I copied below ipcready.lua file into fsx modules folder, that file writes air speed values to hid buffer. but com.writefeature is not working i think.

Vendor = 0x1234

	Product = 0x0001

	Device = 0

	Report = 0


dev, rd, wrf, wr, init = com.openhid(Vendor, Product, Device, Report)

if dev == 0 then
   ipc.log("Could not open HID")
   ipc.exit()
end

-- Finish HID Connect

function call_speed (offset, value)
    
    
    com.write(dev,tostring(value), wr) 
    
end



event.offset (0x07E2, "UW", "call_speed") -- aircraft speed


 

In mikroc program im reading buffer and put values into 7 segment display like below:

while(1)
    {

          if(HID_Read() != 0) {
            
            AsciiToChar(&readbuff);
            SevenSegmentPrint(ss,(int)atoi(readbuff));

           }
         SetADCInputToUSBBuffer();
    }

can anyone help how can i resolve the issue?

Share this post


Link to post
Share on other sites

This post seems to be a more elaborate version of a post I already answered.

On 8/30/2017 at 10:00 AM, zuby said:

dev = ipc.get("dev")
wrf = ipc.get("wrf")

Where are these Global variables being set?

Please also see my other reply.

Please stick to one thread per subject in furture to avoid extra work and confusion.

Pete

 

 

Share this post


Link to post
Share on other sites

Hello Pete,

Sorry for late reply, my internet was not working...now i'm online...Pete i'm using custom pic18f4550 joystick board. I want to get airspeed value into that chip or board by hid buffer using the mikroc code in previous post, mikroc is an IDE to write program for embedded system. i edited lua script according to you. now i got it that get and set are sessions variable. now problem is the edited code is not working according to my need.. and also im confused how does com.write function work?

Share this post


Link to post
Share on other sites
23 minutes ago, zuby said:

i edited lua script according to you. now i got it that get and set are sessions variable. now problem is the edited code is not working according to my need.. and also im confused how does com.write function work?

com.write writes the data to a port you've already opened. It just calles the Windows function to do that. How would you expect it to work.

I successfully use Lua to read and write from/to an Arduino in my cockpit.

Please do see the HidDemo Lua example in your FSUIPC documents folder, inside the Example Lua Plugins ZIP, and use the Logging facilities to check what is going on -- log data using ipc.log and enable the Lua trace/debug option in the Logging tab for a line by line trace.

Just saying it is not working is no help to anyone. I can't help with zero information.

Pete

 

Share this post


Link to post
Share on other sites

Hello Pete,

Under of thanks for your response.
Did you mean com port (COM1, COM2)?

My expectation was that it will work like luahidapi library that im using right now that fulfills my requirements. but i want to use default com.write or com.writefeature function of fsuipc to write hid buffer.   

Ofcourse Arduino reads and writes to com port.. but i need to write to HID buffer. I read somewhere that HID is better than serial communication.
 

Here's my ipc log. 

 36129 Running in "Microsoft Flight Simulator X", Version: 10.0.60905.0 (SimConnect: 2.0.60905.0)
200773 SimStart Event: Initialising SimConnect data requests
200789 FSUIPC Menu entry added
201288 C:\Users\szacpp\AppData\Roaming\Microsoft\FSX\Previous flight.FLT
201288 G:\Program Files\Microsoft Games\Microsoft Flight Simulator X\SimObjects\Airplanes\B737_800\Boeing737-800.AIR
201693 System time = 22/09/2017 19:59:38, Simulator time = 19:56:42 (02:56Z)
201803 Aircraft="Boeing 737-800 Paint1"
202598 Starting everything now ...
202723 LUA.0: beginning "G:\Program Files\Microsoft Games\Microsoft Flight Simulator X\Modules\ipcReady.lua"
202785 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:2
202801 LUA.0: Global: ipcPARAM = 0
202801 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:5
202801 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:6
202817 LUA.0: Local: USB_DEVICE_VID = 4660
202817 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:8
202817 LUA.0: Local: USB_DEVICE_PID = 1
202817 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:11
202817 LUA.0: Global: Device = 0
202817 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:18
202832 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:20
202832 LUA.0: Global: Report = 0
203456 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:21
203456 LUA.0: Global: wr = 65
203456 LUA.0: Global: wrf = 0
203472 LUA.0: Global: dev = 152504236
203472 LUA.0: Global: rd = 33
203472 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:29
203472 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:27
203487 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:32
203487 LUA.0: Waiting for an event in "G:\Program Files\Microsoft Games\Microsoft Flight Simulator X\Modules\ipcReady.lua"
203487 LUA.0: Offset Change event: calling "call_speed" in "G:\Program Files\Microsoft Games\Microsoft Flight Simulator X\Modules
cReady.lua"
203487 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:28
203487 LUA.0: Local: offset = 2018
203487 LUA.0: Local: value = 0
203503 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:29
203503 LUA.0: Waiting for an event in "G:\Program Files\Microsoft Games\Microsoft Flight Simulator X\Modules\ipcReady.lua"
205235 Advanced Weather Interface Enabled
215437 *** EVENT: Cntrl= 65561 (0x00010019), Param= 0 (0x00000000) PAUSE_TOGGLE

 

Share this post


Link to post
Share on other sites

oh i forgot to mention that i read HidDemo.lua, com.write or com.writefeature function was not used in that file. 

Share this post


Link to post
Share on other sites
1 hour ago, zuby said:

Did you mean com port (COM1, COM2)?

No. HID ports are, like all USB ports, are treated like COM in the Windows API for reading and writing, but the port name is much more complicated than that, and obtained by reference to the PID and VID values you need to program in your Lua when using OpenHID.

1 hour ago, zuby said:

My expectation was that it will work like luahidapi library that im using right now

And don't you have to supply such information for that? You need to identify the device somehow!

In your log:

202817 LUA.0: Local: USB_DEVICE_VID = 4660
202817 LUA.0: ...es\Microsoft Flight Simulator X\Modules\ipcReady.lua:8
202817 LUA.0: Local: USB_DEVICE_PID = 1

4660 is hex 0x1234 which seems an very unlikely VID. And a PID of 0x0001 is even more unusual. Are you sure you have those correct? Where are you getting them from?

Without seeing the Lua code you've come up with I can't really interpret the log any further, can I? It is a waste posting it really.

1 hour ago, zuby said:

oh i forgot to mention that i read HidDemo.lua, com.write or com.writefeature function was not used in that file. 

No. I don't think any USB device I've got uses the "feature" option. And since the HIDdemeo is simply showing results of reading data on one specific user device, it isn't likely to write to it and possibly much their device settings up, is it?

You are not picking out the importasnt aspects of the example. You should be able to interpolate for yourself into other read and write facilities.

58 minutes ago, zuby said:

i think diagram will be like this

Why would I need that information?

Pete

 

Share this post


Link to post
Share on other sites

Thanks Pete for your response.
 

2 hours ago, Pete Dowson said:

No. HID ports are, like all USB ports, are treated like COM in the Windows API for reading and writing, but the port name is much more complicated than that, and obtained by reference to the PID and VID values you need to program in your Lua when using OpenHID.

I did not know that. thanks for your information.

2 hours ago, Pete Dowson said:

And don't you have to supply such information for that?

Here is the luahidapi library link: https://github.com/ynezz/luahidapi 

here is the working code of luahidapi...I copied luahidapi.dll into "Modules/lua", also copied hidapi.dll  and lua51.dll into "Modules/dll" folder

local hid = require "luahidapi"


local sfmt, sbyte, schar, srep = string.format, string.byte, string.char, string.rep
------------------------------------------------------------------------
-- initialize
------------------------------------------------------------------------

if not hid.init() then
  print("hid library: init error")
  return
end


--====================================================================--
--** WARNING: Test uses Microchip's VID and a PID from MPLAB tools'  **
--** PID range. DO NOT use outside of a laboratory/personal setting. **
--====================================================================--

local USB_DEVICE_VID = 0x1234
local USB_DEVICE_PID = 0x0001

local USB_REPORT_SIZE = 64

local dev = hid.open(USB_DEVICE_VID, USB_DEVICE_PID)
if not dev then
  print("Open: unable to open test device")
  return
end

function addpadding(str)
   local length = string.len(str)
      
   for i=length, 7 do
		str = str .. " "	
   end
   return str;
end 

function call_speed (offset, value)
 
  local tx = ""
  tx = tx .. tostring(value);
  hidwrite(tx)  
	
end

function hidwrite (buffer)
  buffer = addpadding(buffer)
  buffer = srep(buffer, USB_REPORT_SIZE / 8)
  local res = dev:write(buffer)
  if not res then
    print("Unable to write()")
    print("Error: "..dev:error())
    return
  end  
end


-- FSX events
event.offset (0x07E2, "UW", "call_speed") -- aircraft speed

after that I read that air speed value into joystick code (that i wrote in mikroc) and display that value to 7 segment LED.

2 hours ago, Pete Dowson said:

4660 is hex 0x1234 which seems an very unlikely VID. And a PID of 0x0001 is even more unusual. Are you sure you have those correct? Where are you getting them from?

yes those are correct. getting from to run a program named "hid scanner.exe"

2 hours ago, Pete Dowson said:

Why would I need that information?

I was thinking that com.write works like luahidapi.

besides above conversation could you guide me?
How do i display air speed value to 7 segment LED without serial communication?

Share this post


Link to post
Share on other sites
1 hour ago, zuby said:

Here is the luahidapi library link: https://github.com/ynezz/luahidapi 

Sorry, why would I want that?

1 hour ago, zuby said:

I copied luahidapi.dll into "Modules/lua", also copied hidapi.dll  and lua51.dll into "Modules/dll" folder

I'm really not wanting such information. I was only trying to help you make proper use of the FSUIPC Lua HICOM library.

1 hour ago, zuby said:

yes those are correct. getting from to run a program named "hid scanner.exe"

That's my program.

Okay then. Seems as if your HID device is probably using an unofficial ID. Normally I think they have to be issued by some authority - see http://www.usb.org/developers/vendor/, and this costs money to register. Doesn't matter to me or Windows, though. This site: http://pcidatabase.com/vendors.php?sort=id lists your 0x1234 as being ownded by "Technical Corp". Is that right? Seems unlikely as the only device of theirs so far listed is Device ID:0x0866, Chip Number:C79, Chip Description:NVIDIA GeForce 9400M.

2 hours ago, zuby said:

How do i display air speed value to 7 segment LED without serial communication?

 Sorry, I don't know your device. How display devices implement different things is up to them. Surely the device you are using doesn't use "writefeature" for its displays?. It should surely use normal writes?

Often 7-segment displays actually need very low level data, driving the actual voltages on the separate lines feeding each chip. That may or may not be handled by microcode on the device. But I've no idea. I've only ever found out about hardware devices I've actually used or got hold of myself, and even then it is usually first a matter of using other programs, like USB Monitors which log what goes on between PC and devices, to work out how to drive them.

I can support my software, I cannot undertake to support hardware as well I'm afraid. Sorry.

Pete

 

Share this post


Link to post
Share on other sites
11 hours ago, Pete Dowson said:

Sorry, why would I want that?

The purpose to show this library is that you can understand my problem easily...there is a proper documentation for luahidapi.
 
Whereas how to use com.write with HID, there is no example file or documentation to use that function.
 

Could you give me HID code example for com.write. or how do i use com.write for HID?

11 hours ago, Pete Dowson said:

This site: http://pcidatabase.com/vendors.php?sort=id lists your 0x1234 as being ownded by "Technical Corp". Is that right?

yes the id ownded by "Technical Corp" but I'm using default HID example program (that has 0x1234 VID) of mikroc.

Thanks for your reply.

Share this post


Link to post
Share on other sites
20 minutes ago, zuby said:

Whereas how to use com.write with HID, there is no example file or documentation to use that function.

But it's the same as com.read except you provide the data instead of receiving it!

com.read is 

str, n = com.read(handle, max to read)

where you get the data rwad in the string 'str' and the number of bytes read in 'n',  whilst com.write is

n = com.write(handle, "string")

where n returns the number of bytes actually written. An example would be

n = com.write(dev, "this is being written")

What more could you possibly expect me to say? It's effectively a direct call into the Windows API "WriteFile" with the file handle being the one obtained for your HID device.

FSUIPC does place the write data into a buffer first, so that a separate autonomous thread can be used to avoid holding yup other things in FSUIPC.

Here's the Lua for my Parking Brake device, operating the LED indicator via an Arduino. It isn't a "HID" device, but the only difference between ordinary COM and HID is how the device name is obtained, in the opening.

port = "com3"
portspeed = 9600
parkbrkpin = 12
parkbrkoffset = 0xbc8

h = com.open(port, portspeed, 0)

function parkbrk(off, val)
   data = 192 + parkbrkpin;
   if val >= 32767 then data = data + 32 end
   str = string.format("%d\x0d", data)
   com.write(h, str)
end

event.offset(parkbrkoffset, "UW", "parkbrk")


Pete

 

Share this post


Link to post
Share on other sites

Thanks for your reply Pete.
I understood that how to write data to COM3 port....but I want to write data to HID Device.  but how? I dont know.
I think I can not use com.write for HID device. right?

Share this post


Link to post
Share on other sites
5 minutes ago, zuby said:

I understood that how to write data to COM3 port....but I want to write data to HID Device.  but how? I dont know.
I think I can not use com.write for HID device. right?

Of course you use com.read and com.write on HID devices. The ONLY difference is how it is opened, because of the way of identifying it (using VID and PID instead of port number)!!!

The code in FSUIPC and in Windows is the same for all devices, once they are opened and a handle has been obtained. There are some extra features for HID devices, as documented, but the basics are just serial reads and writes.  The "S" is USB is for "serial".

Pete

 

Share this post


Link to post
Share on other sites

it means the code below is right for hid device?
 

Vendor = 0x1234 
Product = 0x0001 
Device = 0 
Report = 0 

dev, rd, wrf, wr, init = com.openhid(Vendor, Product, Device, Report) 

if dev == 0 then    
ipc.log("Could not open HID")    
ipc.exit() 
end -- Finish HID Connect 

function call_speed (offset, value)              
com.write(dev,tostring(value), wr)      
end 

event.offset (0x07E2, "UW", "call_speed") -- aircraft speed

 

Share this post


Link to post
Share on other sites

pete i tried that code but i'm not receiving values into my device..i don't know why?...also i showed you ipc log of that code.

Share this post


Link to post
Share on other sites
2 hours ago, zuby said:

pete i tried that code but i'm not receiving values into my device..

What code? What protocol is the device using. Honstely, you are asking the wrong person in the wrong forum. Try the support forum for that device, or buy uoirself a decent USB monitor, as I did for my devices, to work out what is going on and what is needed for your device.

2 hours ago, zuby said:

also i showed you ipc log of that code.

Why? What's the point of a trace log with no source code to see what was being executed? Or are you talking about the code you posted much later? Did you expect me to magically relate the two with so much intervening goings on?

Sorry, but I am not interested. All I can tell you is what you write to a correctly open COM or HID device IS being sent out to that device. What it does with it, whether it recognises it, is something I cannot possibly help with.

Pete

 

Share this post


Link to post
Share on other sites

Hello Pete,

Finally the below lua code is working now :). but "str = string.char(0,49)" char function's first argument must be 0 I dont know why? it does not work when i remove 0. and also guide me where should i use com.close(handle)? because i will use com.write function into offset event. if i use com.close after event then i need to reopen hid device.
Under of Thanks for your help and time.

local USB_DEVICE_VID = 0x1234
local USB_DEVICE_PID = 0x0001

Device = 0  -- Multiple devices of the same name need increasing Device numbers.


Logging = false



Report = 0  -- I *think* all joystick types use Input Report 0

dev, rd, wrf, wr, init = com.openhid(USB_DEVICE_VID, USB_DEVICE_PID, Device, Report)
if dev == 0 then
   ipc.log("Could not open HID")
   ipc.exit()
end



local str = ""
str = string.char(0,49)
n = com.write(dev,str)
ipc.log("return value = " .. n)  	  	  		


com.close(dev)

 

Share this post


Link to post
Share on other sites
14 hours ago, zuby said:

Finally the below lua code is working now :). but "str = string.char(0,49)" char function's first argument must be 0 I dont know why? it does not work when i remove 0.

Perhaps that's what your device needs, a string starting with a zero character.

"str = string.char(0,49)"  is almost the same as str = "\01". i.e. a zerobyte (\ is the escape character) and the character '1'. But the latter will also be terminated by a zero. I don't think the former is (but I don't know offhand). The string library is a standard Lua library, not my own code.

14 hours ago, zuby said:

guide me where should i use com.close(handle)? because i will use com.write function into offset event. if i use com.close after event then i need to reopen hid device.

You only close the device when you've finished with it. If you have no signal for this, but just want to carry on forever, then don't worry -- it will get closed when the session ends. Or you could do it in another function called by event.sim(CLOSE, "function-name")

Pete

 

Share this post


Link to post
Share on other sites

I got it....Thank you very much Pete for your help and time.

On 9/23/2017 at 9:41 PM, Pete Dowson said:

Why? What's the point of a trace log with no source code to see what was being executed? Or are you talking about the code you posted much later? Did you expect me to magically relate the two with so much intervening goings on?

Sorry for this.

Share this post


Link to post
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

Sign in to follow this  

×