Author Topic: Old example code?  (Read 13604 times)

DavidClarke

  • Member
  • ***
  • Posts: 24
Old example code?
« on: May 08, 2019, 06:07:08 pm »
Hi
Do you have any old example code in plain C with Win32API?  One person suggested I order an old copy of your USB book.  I found a volume 3 and it is on its way.  I have to use PowerBasic to add functionality to an existing large bit of code.  PowerBasic can call Win32Api just fine.  You can do most anything in PowerBasic that you can do in C.

I got a Beagle USB 480 but my device doesn't light up when plugged into the Beagle.  Before I purchased I asked the Beagle people if the 480 could supply 300ma to the device and they said no problem.  So protocol analyzer is not helping yet.

I am 50% of the way to having this working - receiving button press events work great with ReadFile + WaitForSingleObject.
Data is simple (01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00) Bytes 2 - 7 are 1 when the buttons are pressed.

I have a software usb analyzer.  I can see the data going to the device as changes are made.  This data (05 55 AA D1 01 64 00 00 00 00 00 00 00 00 00 00 00) should adjust the display brightness from 00 to 64 in byte 6.

I have tried to simply do a WriteFile of the data to adjust brightness but no change in device and no sign of data movement on USB analyzer. 

I have attached a jpg of the trace Seq 1 - 8 are the initial connection.  Seq 9 - 24 are three increase brightness commands.  I sure would have thought I could just push that data towards the device and see a brightness change.

Is there something going on that the analyzer is not showing me?

Thanks!
So frustrating!




Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Old example code?
« Reply #1 on: May 08, 2019, 07:46:25 pm »
If the device is receiving the data, the problem may be that the host application isn't sending the data in the expected format / report type - assuming it's a HID-class device, the report descriptor will describe the amount of data, its format, and whether it uses Output or Feature reports. For WriteFile, the first data byte sent is the report ID.

I have example HID code using ReadFile and WriteFile here:

http://www.janaxelson.com/hidpage.htm#MyExampleCode

towlerg

  • Member
  • ***
  • Posts: 24
Re: Old example code?
« Reply #2 on: May 09, 2019, 09:44:48 am »
"I have tried to simply do a WriteFile" does WriteFile return an error?

DavidClarke

  • Member
  • ***
  • Posts: 24
Re: Old example code?
« Reply #3 on: May 09, 2019, 10:46:46 am »
I get error 6 - ERROR_INVALID_HANDLE.  Sure looks like a valid handle to me...

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Old example code?
« Reply #4 on: May 09, 2019, 03:44:36 pm »
Post your code.

DavidClarke

  • Member
  • ***
  • Posts: 24
Re: Old example code?
« Reply #5 on: May 09, 2019, 04:16:05 pm »
Actually, found a problem. I had an invalid file handle.

I will post if I can't fix it.

Thanks!!

DavidClarke

  • Member
  • ***
  • Posts: 24
Re: Old example code?
« Reply #6 on: May 09, 2019, 04:54:35 pm »
Some concept questions:

File handle questions:
 hReadFile = CreateFile(FILE_NAME_ASCIIZ, %GENERIC_READ OR %GENERIC_WRITE, %FILE_SHARE_READ OR %FILE_SHARE_WRITE, BYVAL %NULL, %OPEN_EXISTING, %FILE_FLAG_OVERLAPPED, %NULL)
Using this file handle I can read just fine.  And the overlapped part works with WaitForSingleObjest.

Should I use the same file handle to WriteFile?  Or should I have a different, not overlapped, handle to write?
hHid = CreateFile(@pz, %GENERIC_READ OR %GENERIC_WRITE, %FILE_SHARE_READ OR %FILE_SHARE_WRITE, BYVAL %NULL, %OPEN_EXISTING, %NULL, %NULL) 

What is the exact sequence that one calls the WinAPI to emulate the the transaction shown in the attached jpg sequence 18876 to 18892.

Should I start with  HidD_SetOutputReport?

I have been trying to:
RESULT = WriteFile(BYVAL GLOBAL_FILE_HANDLE, WRITE_BUFFER, MY_OutputReportByteLength, BYTES_WRITTEN, MY_OVERLAPPED)

Where (in powerbasic)

WRITE_BUFFER = CHR$(&h03, &H05, &H55, &HAA, &HD1, &H03, &H00, &H00, &H00, &H00, &H00, &H00, &H00, &H00, &H00, &H00, &H00, &H00)

So I guess I have a couple of issues, am I using the correct file handle, am I sending the correct data and am I missing steps...
Thanks










 

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Old example code?
« Reply #7 on: May 09, 2019, 07:48:08 pm »
Let us know if you have not resolved this (as reported in other recent question).

DavidClarke

  • Member
  • ***
  • Posts: 24
Re: Old example code?
« Reply #8 on: May 09, 2019, 08:33:33 pm »
Thanks!

towlerg

  • Member
  • ***
  • Posts: 24
Re: Old example code?
« Reply #9 on: May 10, 2019, 08:15:30 am »
"Should I use the same file handle to WriteFile?" I use different ones derived from -

    glHidHandle = CreateFile(tIfcDetail.DevicePath, %GENERIC_READ OR %GENERIC_WRITE, _
      %FILE_SHARE_READ OR %FILE_SHARE_WRITE, security, %OPEN_EXISTING, 0&, 0&)

    glrHidHandle = CreateFile(tIfcDetail.DevicePath, %GENERIC_READ OR %GENERIC_WRITE, _
      %FILE_SHARE_READ OR %FILE_SHARE_WRITE, security, %OPEN_EXISTING, %FILE_FLAG_OVERLAPPED, 0&)

I normally use custom microcontroller firmware so I've never seen the need to use reports.