1

イベント ドリブン操作を使用してハードウェアから USB データを読み取るために LibUSbDotNet を使用しました。私のハードウェアは、2 つの異なる間隔でデータを送り出します。(2000 ミリ秒と 300 ミリ秒)。バッファサイズは 7 バイトです。

コードは、後で読み取りが遅くなる場合があるため、正常に機能します。2000 および 300 ミリ秒ではなく、4000 および 2000 ミリ秒でデータを受信します。

この問題を解決するのを手伝ってください...

よろしくジョン

    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