1

我使用 LibUSbDotNet 使用事件驱动操作从我的硬件读取 USB 数据。我的硬件以两个不同的时间间隔抽出数据。(2000 毫秒和 300 毫秒)。缓冲区大小为 7 个字节。

该代码工作正常,有时读取速度会变慢。数据在 4000 和 2000 毫秒接收,而不是 2000 和 300 毫秒。

请各位大侠帮我解决这个问题...

问候约翰

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using LibUsbDotNet;
    using LibUsbDotNet.DeviceNotify;
    using LibUsbDotNet.Main;

    namespace ATE_BackEnd
    {
        public partial class Main_Form : Form
        {
           public static IDeviceNotifier UsbDeviceNotifier = DeviceNotifier.OpenDeviceNotifier();
            UsbDeviceFinder MyUsbFinder;
            UsbDevice MyUsbDevice;
            UsbEndpointReader EPReader;
            UsbEndpointWriter EPWriter;
            int bytesWritten;
            public DateTime LastDataEventDate = DateTime.Now;

            public Main_Form()
            {
                InitializeComponent();
                Control.CheckForIllegalCrossThreadCalls = false;
                UsbDeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent;
            }
    void OpenUSB(int VendorID, int ProductID)
    {
        toolStripStatusLabel.Text = "Opening USB";
        MyUsbFinder = new UsbDeviceFinder(VendorID, ProductID);
        MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

        if (MyUsbDevice == null) { USBConnection_label.Text = "USB Not Connected"; CloseUSB(); }
        else
        {
            USBConnection_label.Text = "USB Connected";
            USBInfo_label.Text = "VID = " + MyUsbDevice.Info.Descriptor.VendorID.ToString() + 
                ", PID = " + MyUsbDevice.Info.Descriptor.ProductID.ToString();
            IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
            if (!ReferenceEquals(wholeUsbDevice, null))
            {
                wholeUsbDevice.SetConfiguration(1);
                wholeUsbDevice.ClaimInterface(0);
            }
            EPReader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01, 7);
            EPWriter = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);

            EPReader.DataReceived += OnUsbDataReceived;
            EPReader.DataReceivedEnabled = true;
        }
    }
    void WriteUSB(int Site)
    {
        toolStripStatusLabel.Text = "Writing Data...";
        ErrorCode ECWriter = EPWriter.Write(Encoding.Default.GetBytes(Site.ToString()), 100, out bytesWritten);
        if (ECWriter != ErrorCode.None) throw new Exception(UsbDevice.LastErrorString);
    }
    void CloseUSB()
    {
        toolStripStatusLabel.Text = "Closing USB";
        if (MyUsbDevice != null)
        {
            if (MyUsbDevice.IsOpen)
            {
                IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    wholeUsbDevice.ReleaseInterface(0);
                }
                MyUsbDevice.Close();
            }
            EPReader.DataReceived -= OnUsbDataReceived;
            EPReader.DataReceivedEnabled = false;
            EPReader.Dispose();
            EPWriter.Dispose();

        }
        MyUsbDevice = null;
        UsbDevice.Exit();
    }
    void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
    {
        toolStripStatusLabel.Text = "Device Notify Message: " + e.EventType.ToString();
        if (e.EventType.ToString() == "DeviceRemoveComplete") { USBConnection_label.Text = "USB Disconnected"; CloseUSB(); }
        else if (e.EventType.ToString() == "DeviceArrival") { USBConnection_label.Text = "USB Connected"; OpenUSB(4660, 1); }
    }
    void OnUsbDataReceived(object sender, EndpointDataEventArgs e)
    {
        toolStripStatusLabel.Text = "Receiving Data!!!";
        byte[] s1stat = e.Buffer;
        S1SOT_textBox.Text = s1stat[0].ToString();
        S2SOT_textBox.Text = s1stat[1].ToString();
        S1EOT_textBox.Text = s1stat[2].ToString();
        S2EOT_textBox.Text = s1stat[3].ToString();
        S1BIN_textBox.Text = s1stat[4].ToString();
        S2BIN_textBox.Text = s1stat[5].ToString();
        TowerLamp_textBox.Text = s1stat[6].ToString();

        Time_label.Text = (DateTime.Now - LastDataEventDate).TotalMilliseconds.ToString();
        LastDataEventDate = DateTime.Now;
    }

    private void Main_Form_FormClosing(object sender, FormClosingEventArgs e)
    {
        toolStripStatusLabel.Text = "Closing App";
        CloseUSB();
    }
    private void Main_Form_Load(object sender, EventArgs e)
    {
        toolStripStatusLabel.Text = "Opening App";
        OpenUSB(4660, 1);
    }

    private void Write_button_Click(object sender, EventArgs e)
    {
        CloseUSB();
        OpenUSB(4660, 1);
        WriteUSB(Write_comboBox.SelectedIndex+1);
    }
}

}

4

0 回答 0