cancel
Showing results for 
Search instead for 
Did you mean: 

Using USB Hid to send float/double

deathclaw
Associate II
Posted on May 29, 2011 at 22:53

Hi,

I just started working with Hid class to communicate via USB with STM32F103VB. I managed to send few bytes of data at once using

UserToPMABufferCopy(Send_Buffer, ENDP1_TXADDR, 5);

where Send_Buffer is declared as

u8 Send_Buffer2[5];

It works fine but is there a simple way to send floats or double variables in a simple way or do I have to make a conversion from seperate bytes to a variable I want to send? I'm real beginner so sorry if this is a silly qustion 🙂

Thanks in advance.

4 REPLIES 4
Posted on May 30, 2011 at 03:25

Probably like this would suffice :

double AssortedMeasurements[12];

UserToPMABufferCopy((void *)AssortedMeasurements, ENDP1_TXADDR, sizeof(AssortedMeasurements) );

That or use some generic struct or union.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
deathclaw
Associate II
Posted on May 31, 2011 at 00:42

Unfortunately that didn't work.. It's impossible to read any data from the device. Any other hints?

Posted on May 31, 2011 at 18:51

Unfortunately that didn't work.. It's impossible to read any data from the device. Any other hints?

Didn't work? The device didn't send it, or you couldn't do anything with it at the receiving end? You couldn't read any data, or decipher it? Please be more specific, and provide example data.

As you're trying to send binary data, you'll have to reconstitute it on the correct boundaries at the destination.

Want it as ASCII, use sprintf(), and send the resultant data.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
deathclaw
Associate II
Posted on June 01, 2011 at 11:44

On the host side I am using ReadFile function to obtain the report but it timeouts and reads no data. So I decided to take a step backwards and try to send an integer the same way you proposed. This time I was able to read data form the device. The code looks like this:

int AssortedMeasurements[2];

AssortedMeasurements[0]=5;

AssortedMeasurements[1]=280;

UserToPMABufferCopy((void *)AssortedMeasurements, ENDP1_TXADDR, sizeof(AssortedMeasurements) );

I've set my report descriptor like this:

const u8 CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] =

  { 

    0x05, 0x8c,            // USAGE_PAGE (ST Page)                          

    0x09, 0x01,            // USAGE (Demo Kit)                  

    0xa1, 0x01,            // COLLECTION (Application)                   

    // 6

                                         // ------ common globals ------

    0x15, 0x00,                         //   LOGICAL_MINIMUM (0)              

    0x26, 0x2c, 0x01,                   //   LOGICAL_MAXIMUM (300)         

    0x75, 32,                         //   REPORT_SIZE (32)   bits          

                                        // ------ input report ------

0x85, 0x05,           //     REPORT_ID (5) 

    0x95, 1,

    

    

    

    

    

    

//   REPORT_COUNT               

    0x09, 0x01,                         //   USAGE (Vendor Usage 1)         

    0x81, 0x02,                         //   INPUT (Data,Var,Abs)         

                                        // ------ output report ------

    0x85, 0x03,          //     REPORT_ID (3)

  0x95, 1,

    

    

    

    

    

//   REPORT_COUNT             

    0x09, 0x01,                         //   USAGE (Vendor Usage 1)           

    0x91, 0x02,                         //   OUTPUT (Data,Var,Abs) 

                                   // ------ output report 2 ------

    0x85, 0x04,          //     REPORT_ID (3)

  0x95, 1,

    

    

    

    

    

//   REPORT_COUNT              

    0x09, 0x02,                         //   USAGE (Vendor Usage 1)           

    0x91, 0x02,                         //   OUTPUT (Data,Var,Abs)               

    0xc0          //     END_COLLECTION           

  };

The problem is that my integer value is being send as one byte. So if I try to send more than 255 it just wraps around itself (f.e. 280 turns to 280-256=24). I am looking for a way to send my integer value as one number, I was hoping that REPORT_SIZE(32) would do the trick but unfortunately it didn't. Any clues please?

EDIT: I've managed to send float using an union. I just had to find out what an union is first. Still, if anyone has a better solution it would be greatly appreciated. Thanks anyways for your help!