日期:2014-05-18  浏览次数:20938 次

C#如何实现USB口通信
最近在做一个项目,用到了串口通信和USB数据传输。我知道串口通信用的是serialport。但是我的另一个设备是用的USB口,用测试软件测试并不是用的COM口,不知道有没有从USB读取信息的类库。

------解决方案--------------------
嗯,你根据硬件厂商给你提供的API来,应该还有相应的文档。
给你看一段我做的读取写入USB接口的读卡器:
C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
using System.Runtime.InteropServices;
using DevComponents.DotNetBar;

namespace PlatForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        //c++函数强制声明
        [DllImport("mi.dll", EntryPoint = "API_OpenComm")]
        public static extern IntPtr API_OpenComm(int nCom, int nBaudrate);

        [DllImport("mi.dll", EntryPoint = "API_CloseComm")]
        public static extern bool API_CloseComm(IntPtr commHandle);

        [DllImport("mi.dll", EntryPoint = "API_ControlBuzzer")]
        public static extern int API_ControlBuzzer(IntPtr commHandle, int DeviceAddress, byte freq, byte duration, ref byte buffer);

        [DllImport("mi.dll", EntryPoint = "API_MF_Read")]
        public static extern int API_MF_Read(IntPtr commHandle, int DeviceAddress, byte mode, byte blk_add, byte num_blk, ref byte snr, ref byte buffer);

        private void Form1_Load(object sender, EventArgs e)
        {
            Program.Port = API_OpenComm(4, 9600);
            if (Program.Port.ToInt32() == 0)
            {
                labelControl2.Visible = false;
                labelControl3.Visible = true;
                buttonX1.Visible = true;
                return;

            }
            byte pBuf = new byte();
            Program.G_start = API_ControlBuzzer(Program.Port, 0, 3, 1, ref pBuf);
            if (Program.G_start == 0)
            {
                labelControl2.Visible = true;
                labelControl3.Visible = false;
                buttonX1.Visible = false;
            }
            //启动时间轴

            timer1.Interval = 1000;

                timer1.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
                byte[] hex = new byte[16];
                long ret = long.Parse("281474976710655");
                hex = BitConverter.GetBytes(ret);
                byte[] hex1 = new byte[16];
                byte[] hex2 = new byte[16];
                //Array.Clear(hex1, 0, 16);
                byte[] data = new byte[16];
                
                int idata = API_MF_Read(Program.Port, 0, 0, 10, 1, ref hex[0], ref data[0]);
                //无卡接触时,循环等待
                if (idata != 0)
                {
                    return;
                }
                //取卡号
                string s = BitConverter.ToInt64(data, 0).ToString();
                member mem = new member();
                //卡有效性验证
                timer1.Stop();
                if (mem.Exists(Convert.ToInt32(s)))
                {
                    
                    DialogResult result;
                    result = MessageBoxEx.Show("此卡存在数据,是否重写", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.No)
                    {
                        
                        timer1.Start();
                        return;
                    }
                }

                Form3 form3 = new Form3();
                if ((form3.ShowDialog() == DialogResult.OK))
                {
                    timer1.Start();
                    return;
                }
                timer1.Start();
                return;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            Form1_Load(sender, e);
        }
    }
}

------解决方案--------------------