C#HID应用程序帮助 [英] C# HID application help
问题描述
大家好....
我试图使用USB HID和DLL驱动程序与PC通讯我的PIC(18f4550)单片机.主机应用程序是用C#开发的.使用事件捕获器功能收集发送到PC的数据.通讯,数据接收一切顺利.
尽管我加快了微控制器程序内部的数据传输速度,但C#应用程序并未显示出显着的数据接收速度,但我认为问题可能在事件捕获器功能内部发生.我将两个代码都放在这里...
例如:-我以500uS或1mS的速度将数据缓冲区发送到PC,但是文本框未更新为给定的速度.
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 500uS or 1mS speed to the pc, but the text box is not updated to given speed.
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections.Generic;
using USBHIDDRIVER;
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();
}
}
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 - (just to give an idea, i know this is not the place)
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);
}
}
}
可以给我解释一下如何提高C#代码中的数据接收速度吗?我可以为其修改事件捕获程序代码吗?
谢谢
问候
Anuradha
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 ???
Thanx
Regards
Anuradha
推荐答案
对我来说重要的问题是:您是否在USB 1.1端口上运行微控制器?
如果是,则使用时间控制USB的数据发送/接收.时间单位是帧,每帧的长度由总线时钟(1KHz-> 1000帧/s->这样每毫秒一个)来调节.
请参见框架和微框架"部分:
http://www.usbmadesimple.co.uk/ums_6.htm [
Hi,
important question for me is: do you run your microcontroller at usb 1.1 port?
If yes, data sending/receiving with USB is controlled by using time. The unit of time is the frame and length of each of frame is regulated by the bus clock (1KHz -> 1000 frames/s -> so one per millisecond)
See at Section "Frames and Microframes":
http://www.usbmadesimple.co.uk/ums_6.htm[^]
Do you''ve tried to flush frames after receiving?
Regards
除了已经说过的话,您不可能每秒更新一个文本框来获得1,000帧.
您的文本框应按快照"时间表进行绘制.没有人每秒可以读取1,000帧,那么为什么要尝试以这种速度更新?每隔半秒左右用最新值更新文本框.
On top of what has already been said, there is no way you''re going to get 1,000 frames a second updating a textbox.
Your textbox should be painted on a "snapshot" schedule. Nobody can read 1,000 frames a second, so why are you trying to update at that rate? Update the textbox with the lastest value every half second or so.
乍一看,我认为setText()方法是您的瓶颈.
尝试使用BeginInvoke
代替Invoke
.
this.OutBox
是TextBox吗?
试试:
At first look I think setText() method is your bottleneck.
Try usingBeginInvoke
instead ofInvoke
.
this.OutBox
is a TextBox?
Try:
this.OutBox.AppendText("\r\n" + array[1].ToString());
代替:
instead of:
this.Outbox.Text = this.Outbox.Text + "\r\n" + array[1].ToString();
我对USBHIDDRIVER库不熟悉,但是我会更改myEventCatcher
以在一个锁下完成所有工作:
I''m not familiar with USBHIDDRIVER library, but I would change myEventCatcher
to do all it''s work under a single lock:
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;
lock (USBHIDDRIVER.USBInterface.usbBuffer.SyncRoot){
while ((byte[])USBHIDDRIVER.USBInterface.usbBuffer[counter] == null){
USBHIDDRIVER.USBInterface.usbBuffer.RemoveAt(0);
}
currentRecord = (byte[])USBHIDDRIVER.USBInterface.usbBuffer[0];
USBHIDDRIVER.USBInterface.usbBuffer.RemoveAt(0);
}
setText(currentRecord); //update text box with received data
}
}
这篇关于C#HID应用程序帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!