using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace USBTest
{
    public partial class frmUSBExpress : Form
    {
        public frmUSBExpress()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }        private void frmUSBExpress_Load(object sender, EventArgs e)
        {
            int i, DevNum = 0;
            StringBuilder DevStr = new StringBuilder(USBTest.USB08DLL.SI_MAX_DEVICE_STRLEN);
            cboDevice.Items.Clear();
            USBTest.USB08DLL.Status = USBTest.USB08DLL.SI_GetNumDevices(ref DevNum);
            //Add the names of all of the devices found to the combo box list.
            if (USBTest.USB08DLL.Status == USBTest.USB08DLL.SI_SUCCESS)
            {
                for (i = 0; i < DevNum; i++)
                {
                    USBTest.USB08DLL.Status = USBTest.USB08DLL.SI_GetProductString(i,
                                     DevStr, USBTest.USB08DLL.SI_RETURN_SERIAL_NUMBER);
                    cboDevice.Items.Insert(i, DevStr);
                }
                cboDevice.SelectedIndex = 0;  // Set combo list to first item
            }
            else  //otherwise exit
            {
                MessageBox.Show("Error finding USB device.  Aborting application.");
                Application.Exit();
            }
        }        private void btnOK_Click(object sender, EventArgs e)
        {
            USBTest.USB08DLL.Status = USBTest.USB08DLL.SI_SetTimeouts(10000, 10000);
            USBTest.USB08DLL.Status = USBTest.USB08DLL.SI_Open(cboDevice.SelectedIndex,
                                                             ref USBTest.USB08DLL.hUSBDevice);
            if (USBTest.USB08DLL.Status != USBTest.USB08DLL.SI_SUCCESS)
            {
                MessageBox.Show("Error opening device: " + cboDevice.Text +
                            ". Application is aborting. Reset hardware and try again.");
                Application.Exit();
            }
            btnOK.Enabled = false;            Thread tReceive = new Thread(new ThreadStart(ReceiveData));
            tReceive.IsBackground = true;
            tReceive.Start();
        }        private void btnExit_Click(object sender, EventArgs e)
        {
            USBTest.USB08DLL.Status = USBTest.USB08DLL.SI_Close(USBTest.USB08DLL.hUSBDevice);
            Application.Exit(); 
        }        private void ReceiveData()
        {
            Int32 IOBuffSize = 12;
            Byte[] IOBuff = new Byte[IOBuffSize];
            Int32 BytesSucceed = 0;
            Int32 BytesWriteRequest = IOBuffSize-4 ;
            Int32 BytesReadRequest = IOBuffSize-4;
            BytesSucceed = 0;
            
            //read data from the board
            while (true)
            {                for (int i = 0; i < 8; i++)
                {
                    USBTest.USB08DLL.Status = USBTest.USB08DLL.SI_Read(USBTest.USB08DLL.hUSBDevice,
                       ref IOBuff[0], BytesReadRequest, ref BytesSucceed, 0);
                    rtfReceive.AppendText(IOBuff[0].ToString("X"));
                }
                rtfReceive.AppendText(Environment.NewLine);
            }
        }
    }
}我接收到的数据总是有错,我实在看不出是哪里的问题了,帮帮我吧,各位大侠!

解决方案 »

  1.   


     private void ReceiveData()
            {
                Int32 IOBuffSize = 12;
                Byte[] IOBuff = new Byte[IOBuffSize];
                Int32 BytesSucceed = 0;
                Int32 BytesWriteRequest = IOBuffSize-4 ;
                Int32 BytesReadRequest = IOBuffSize-4;
                BytesSucceed = 0;
                
                //read data from the board
                while (true)
                {                for (int i = 0; i < 8; i++)
                    {
                        USBTest.USB08DLL.Status = USBTest.USB08DLL.SI_Read(USBTest.USB08DLL.hUSBDevice,
                           ref IOBuff[0], BytesReadRequest, ref BytesSucceed, 0);
                        rtfReceive.AppendText(IOBuff[0].ToString("X"));
                    }
                    rtfReceive.AppendText(Environment.NewLine);
                }
            }
    接收数据的时候,发送数据本来是6 E0 11 22 33 44 55 66的,但是我接受的确是f8f8f8f8f8f8f8