Author Topic: C# HID application help (Data Rate Problem)  (Read 20279 times)

ranabasu

  • Member
  • ***
  • Posts: 7
C# HID application help (Data Rate Problem)
« on: May 14, 2012, 06:18:29 am »
Hi every1....

Im trying to communicate my PIC (18f4550) micro controller with PC using USB HID, and a DLL driver (Florian Leitner'one). The host application is developed in C#. The data send to the pc is collected using Event catcher function. Communication, data receiving all the stuffs going well.
Though I speedup the data rate inside the micro controller program, the C# app does not display a significant data receiving rate, I think the problem might occur inside the event catcher function. I'll put both codes here...

Eg:- I send my data buffer at 800uS or 1mS speed to the pc, but the text box is not updated to given speed.

----------------------------------------------------------------------------------------------
namespace My_NEWWW_HID_CS{
   
    public partial class Form1 : Form{
       
        USBHIDDRIVER.USBInterface usb = new USBInterface("vid_1234", "pid_1234");
               
        public Form1(){
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e){
 
        }
 
        private void connect_Click(object sender, EventArgs e){
            try {
                usb.Connect();
                if (usb.Connect() == true)
                    MessageBox.Show("Connected !!!");
                else
                    MessageBox.Show("Not Connected !!!");
            }catch(Exception er){
                MessageBox.Show("BAAA");
            }
         }
 
        private delegate void setTextDelegate(byte []array);
       
        // set and update a text box using received data from microcontroller

        private void setText(byte []array){
            if (this.Outbox.InvokeRequired){
                this.Outbox.Invoke(new setTextDelegate(this.setText), array);
            }else{
                this.Outbox.Text = this.Outbox.Text + "\r\n" + array[1].ToString();
                this.Outbox.SelectionStart = Outbox.Text.Length;
                this.Outbox.ScrollToCaret();
                this.Outbox.Refresh();
            }
        }
 
        //write command to activate receiving data
        private void read_Click(object sender, EventArgs e){
            byte[] DataWrite = new byte[64];
            if (usb.Connect() == true){
                DataWrite[0] = byte.Parse(textBox1.Text);
                usb.write(DataWrite);
                usb.enableUsbBufferEvent(new System.EventHandler(myEventCatcher));
                Thread.Sleep(5);
                usb.startRead();
                MessageBox.Show("Data Reading!");
                //Thread.Sleep(2000);
                usb.stopRead();
                DataWrite[0] = 20;
                usb.write(DataWrite);
            }
        }
        //this is the event catcher !!!
        public void myEventCatcher(object sender,System.EventArgs e){
            //MessageBox.Show("Event Caught");
            if (USBHIDDRIVER.USBInterface.usbBuffer.Count > 0){
                byte[] currentRecord = new byte[64];
                int counter = 0;
               
              while ((byte[])USBHIDDRIVER.USBInterface.usbBuffer[counter] == null){
                    lock (USBHIDDRIVER.USBInterface.usbBuffer.SyncRoot){
                        USBHIDDRIVER.USBInterface.usbBuffer.RemoveAt(0);
                    }
              }
               
                currentRecord = (byte[])USBHIDDRIVER.USBInterface.usbBuffer[0];
               
                lock (USBHIDDRIVER.USBInterface.usbBuffer.SyncRoot){
                    USBHIDDRIVER.USBInterface.usbBuffer.RemoveAt(0);
                }
                setText(currentRecord); //update text box with received data
               
            }
         }
 
           }//end class

}//end namespace

---------------------------------------------------------------------------------------------

Micro controller code -

HID_Enable(&readbuff,&writebuff);
 
  while(1){
     if(HID_Read()){
         Lcd_Cmd(_LCD_CLEAR);
         ByteToStr(readbuff[0],txt);
 
         if(readbuff[0]==10){
            writebuff[0]=2;
            while(!HID_Read()){
              HID_Write(&writebuff,64);
              Delay_us(500);
            }
 
         }else{
            while(!HID_Read()){
               Lcd_Out(1,10,txt);
            }
         }
     }

--------------------------------------------------------------------------------------------

Can some1 please explain me how can I speed up data receiving rate in C# code ? Can I modify my event catcher code for it or else is there anything can be done to the MCU code ???

Thanx
Regards
Anuradha

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: C# HID application help (Data Rate Problem)
« Reply #1 on: May 14, 2012, 08:02:10 am »
Every API call to send or retrieve data adds overhead.

So one way to increase throughput is to define longer reports and send more data in each report.

Also, for device-to-host reports, ReadFile returns as many reports as are available and will fit in the buffer. In some cases, increasing ReadFile's buffer size to retrieve multiple reports at once can help. I don't know if that's possible with the DLL you're using.

If your device is NAKing IN or OUT token packets, you can increase the throughput by improving the firmware so it provides data to send and retrieves received data more quickly.

Jan

ranabasu

  • Member
  • ***
  • Posts: 7
Re: C# HID application help (Data Rate Problem)
« Reply #2 on: May 14, 2012, 10:21:21 am »
Hey... thnx Jan for the quick reply  :) I've read ur articles as well as the book. And they helped me lot to figure this out and to bring my project wrk so far. I'll try to modify the things u have mentioned. Btw HID can transfer up to 64kbpS as i remembered. So practically can we achieve this throughput ?

Thanx
Regards
Anuradha 

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: C# HID application help (Data Rate Problem)
« Reply #3 on: May 14, 2012, 03:32:54 pm »
For a full-speed HID with wMaxPacketSize = 64 and bInterval = 1, the standard Windows driver will reserve bandwidth to transfer 64 bytes/millsecond. The amount of data actually transferred depends on what the host controller driver schedules and whether the HID's endpoint accepts (for OUT endpoints) or sends (for IN endpoints) data without NAKing.

Jan

ranabasu

  • Member
  • ***
  • Posts: 7
Re: C# HID application help (Data Rate Problem)
« Reply #4 on: May 17, 2012, 06:44:31 am »
Hi Jan,
I sea... is there any free open source dll file for high throughput application ? Does your C# example code acquire such a throughput ? and I searched for mpusbapi.dll for .net class but i cudnt find it... :( . Or is there away to modify the dll i use (found frm ur page) for higher data rate ??

Thanx
 

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: C# HID application help (Data Rate Problem)
« Reply #5 on: May 17, 2012, 12:04:08 pm »
My HID page has links to all of the application code I'm aware of.

http://www.lvr.com/hidpage.htm

Bulk transfers can be the fastest unless the bus is very busy, but you can't use the HID class for bulk.

Jan