Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C# HID application help

Status
Not open for further replies.

ranaya

Advanced Member level 4
Joined
Jan 22, 2012
Messages
101
Helped
4
Reputation
8
Reaction score
9
Trophy points
1,298
Location
Kelaniya
Activity points
2,164
Hi every1....

Im trying to communicate my PIC (18f4550) micro controller with PC using USB HID, and a DLL driver. 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.

Code:
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 button1_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 button3_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 -

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
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top