USB上位机开发

上位机开发采用图形化界面,使用C#语言进行开发。调用C++编写的Dll动态库,实现与STM32进行USB通信的目的。

首先,编写Dll动态库。
开发环境采用VS2017,语言C++
在这里插入图片描述

将lusb0_usb.h和libusb.lib文件拷贝到工程目录下。
在这里插入图片描述
libusb-win32-bin-1.2.6.0下载地址

拷贝后,在项目属性 -》VC++目录 -》库目录添加刚才拷贝后文件路径
在这里插入图片描述
项目属性 -》链接器 -》 输入 -》附加依赖项 添加libusb.lib,如图所示
在这里插入图片描述
Dll2.cpp引入添加的头文件lusb_usb.h
在这里插入图片描述

// Dll1.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "Dll2.h"
#include "lusb0_usb.h"

#include <iostream>
#define PID 0x6668
#define VID 0x8886

using namespace std;

usb_dev_handle * my_device;


int __stdcall Find_MyDevice()
{
    
    
	
	usb_init();
	usb_find_busses();
	usb_find_devices();

	struct usb_bus * bus;
	for (bus = usb_busses; bus; bus->next)
	{
    
    
		struct usb_device * device;
		for (device = bus->devices; device; device->next)
		{
    
    
			if (device->descriptor.idProduct == PID
				&& device->descriptor.idVendor == VID)
			{
    
    
				//cout << "find my device" << endl;
				my_device = usb_open(device);
				int ret = usb_claim_interface(my_device, 0);
				//cout << "usb_claim_interface:" << ret << endl;
				return 1;
			}
			else
			{
    
    
				return -1;
			}
		}
	}
	

}

int __stdcall USB_BulkWrite(int ep, char *bytes, int size, int timeout)
{
    
    
	int ret = 0;
	ret = usb_bulk_write(my_device, ep, bytes, size, timeout);
	return ret;
}

int __stdcall USB_BulkRead(int ep, char *bytes, int size, int timeout)
{
    
    
	int ret = 0;
	ret = usb_bulk_read(my_device, ep, bytes, size, timeout);
	return ret;
}

在cpp文件中,实现了三个函数,Find_MyDevice, USB_BulkWrite,USB_BulkRead
在Dll2.h文件中:

#pragma once
#ifdef DLL2_EXPORTS
#define USB_DRIVES_API __declspec(dllexport)
#else
#define USB_DRIVES_API __declspec(dllimport)
#endif



extern "C"  USB_DRIVES_API int  __stdcall Find_MyDevice();
extern "C"  USB_DRIVES_API int  __stdcall USB_BulkWrite(int ep, char *bytes, int size, int timeout);
extern "C"  USB_DRIVES_API int  __stdcall USB_BulkRead(int ep, char *bytes, int size, int timeout);

声明这三个函数是用于导出的__declspec(dllexport),extern "C"是说明是以C语言格式
宏定义DLL2_EXPORTS在VS中默认已经定义好
在这里插入图片描述
然后点击生成 -》生成解决方案。在Debug文件夹下生成了Dll文件。
在这里插入图片描述
在这里插入图片描述
然后新建一个工程,编写界面,语言C#
在这里插入图片描述
在这里插入图片描述
将Dll2.dll拷贝到此工程目录下
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsUSB
{
    
    
    public partial class Form1 : Form
    {
    
    
        bool param_set = false;
        UInt32 counter = 0;
        float[] data = new float[100];
        UInt32 x = 0;

        [DllImport("Dll2.dll", EntryPoint = "Find_MyDevice", ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        public static extern int Find_MyDevice();
        [DllImport("Dll2.dll", EntryPoint = "USB_BulkWrite", ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        public static extern int USB_BulkWrite(int ep, ref byte bytes, int size, int timeout);
        [DllImport("Dll2.dll", EntryPoint = "USB_BulkRead", ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        public static extern int USB_BulkRead(int ep, ref byte bytes, int size, int timeout);

将之前创建的函数导入,然后就可以使用了。
Dll文件可以用ViewDll软件查看里面的函数
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43527214/article/details/105916564
今日推荐