locked
write data to usb RRS feed

  • Question

  •  
    Dear All,

    I've a Bluetooth device, and I'm trying to write a Bluetooth stack..

    It's a USB Device.. and I use C# with P/Invoke..

     

    I open the connection to the device by using CreateFile method with "Async" flag on..

    Then I wrap the handle around FileStream, so I can write and read to the device..

    However, every time I write more than 343 bytes then flush, I got an exception:

     

    "The supplied user buffer is not valid for the requested operation"

    and my code is below, if you like I can send you the bluetooth driver that I m using.
    regards,
    uint 
    OverlappedFlag = 0x40000000;  
     
            private void btnCreatefile_Click(object sender, EventArgs e)  
            {  
                IntPtr deviceHandle;  
     
                // this is a Bluetooth device (usb) . The actual buffer inside the devices I used are between 700 - 2040 bytes. However  
                int deviceID = 0;  
                string driverName = string.Format("\\\\.\\FbtUsb{0}", deviceID.ToString("D2"));  
                deviceHandle = CreateFile(driverName,  
     System.IO.FileAccess.ReadWrite,   
    System.IO.FileShare.None,   
    IntPtr.Zero,   
    System.IO.FileMode.Open,   
    OverlappedFlag,   
    IntPtr.Zero);  
               
                // if handle is not valid for any reason..  
                if (deviceHandle == IntPtr.Zero || deviceHandle.ToInt32() == -1)  
                {  
                    MessageBox.Show("Cannot load the device");  
                }  
     
                // else, write some data  
                  
                // instance is found  
                System.IO.FileStream fs = new System.IO.FileStream(deviceHandle, System.IO.FileAccess.ReadWrite, true, 1024, true);  
     
                // this will work.. =< 343 
                fs.Write(new byte[300], 0, 300);  
                fs.Flush();  
     
                // this will not! > 343  
                fs.Write(new byte[500], 0, 500);  
                fs.Flush();  
     
                fs.Close();  
     
            }  
     
    • Moved by jack 321 Wednesday, June 4, 2008 3:22 AM off topic for C# general
    Saturday, May 31, 2008 9:03 AM

Answers

All replies

  • I am wondering if the numeric value uint OverlappedFlag = 0x40000000 is

    FILE_FLAG_WRITE_THROUGH ?


    AlexB
    Sunday, June 1, 2008 1:30 AM
  • Dear AleeBB,
    I found this value from Winbase.h came with Visual Studio 2008

    #define FILE_FLAG_WRITE_THROUGH         0x80000000
    #define FILE_FLAG_OVERLAPPED                0x40000000
    Sunday, June 1, 2008 6:39 AM
  • This was moved out of the C# forum.

    You can ask this question in another forum.

    Or I can move it into a specific forum if you'd like.

    Thanks!


    Ed Price a.k.a User Ed, Microsoft Experience Program Manager (Blog, Twitter, Wiki)
    Monday, January 16, 2012 7:53 AM