USB上位机开发

您所在的位置:网站首页 libusb开发指南 USB上位机开发

USB上位机开发

2024-07-13 17:11:54| 来源: 网络整理| 查看: 265

上位机开发采用图形化界面,使用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 #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 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软件查看里面的函数 在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭