stm32制作CAN适配器5--WinUsb上位机编写

发布于:2024-12-20 ⋅ 阅读:(16) ⋅ 点赞:(0)
  • 上次我们要stm32制作了一个基于winusb有canfd适配器,今天我们来制作一个上位机程序来进行报文收发。
    上位机还是用以前写好的,只是更改下dll文件。

项目链接器,输入,附加依赖项中增加winusb.lib
在这里插入图片描述

winusb初始化:
#include "zlgcan.h"

#ifdef WIN32
#include <windows.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
#include <winusb.h>
#include <setupapi.h>
#include <devguid.h>

#pragma connent(lib,"winusb.lib")
#pragma connent(lib,"setupapi.lib")

GUID guid = { 0x13eb360b, 0xbc1e, 0x46cb, { 0xac ,0x8b,0xef,0x3d,0xa4,0x7b,0x40,0x62 } };
HDEVINFO deviceInfoSet;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA  deviceInterfaceDetailData = NULL;
HANDLE deviceHandle = INVALID_HANDLE_VALUE;
WINUSB_INTERFACE_HANDLE winusbHandle = NULL;

DEVICE_HANDLE FUNC_CALL  ZCAN_OpenDevice(UINT device_type, UINT device_index, UINT reserved)
{
	// 初始化设备信息集
    deviceInfoSet = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    if (deviceInfoSet == INVALID_HANDLE_VALUE)
    {		
		//file << "SetupDiGetClassDevs failed" << std::endl;	
        return 0;
    }

    // 枚举设备接口
    deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    if (!SetupDiEnumDeviceInterfaces(deviceInfoSet, NULL, &guid, 0, &deviceInterfaceData))
    {
		//file << "SetupDiEnumDeviceInterfaces failed" << std::endl;
        SetupDiDestroyDeviceInfoList(deviceInfoSet);
		return 0;
    }

    // 获取设备路径
    DWORD requiredLength = 0;
    SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, NULL, 0, &requiredLength, NULL);
    deviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiredLength);
    deviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
    if (!SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, deviceInterfaceDetailData, requiredLength, NULL, NULL))
    {
		//file << "SetupDiGetDeviceInterfaceDetail failed" << std::endl;		
        free(deviceInterfaceDetailData);
        SetupDiDestroyDeviceInfoList(deviceInfoSet);
		return 0;
    }

    // 打开设备句柄
    deviceHandle = CreateFile(deviceInterfaceDetailData->DevicePath,
                              GENERIC_READ | GENERIC_WRITE,
                              FILE_SHARE_READ | FILE_SHARE_WRITE,
                              NULL,
                              OPEN_EXISTING,
                              FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
                              NULL);
    if (deviceHandle == INVALID_HANDLE_VALUE)
    {
		//file << "CreateFile failed" << std::endl;
        free(deviceInterfaceDetailData);
        SetupDiDestroyDeviceInfoList(deviceInfoSet);
		return 0;
    }

    // 初始化WinUSB
    if (!WinUsb_Initialize(deviceHandle, &winusbHandle))
    {
		//file << "WinUsb_Initialize failed" << std::endl;
        CloseHandle(deviceHandle);
        free(deviceInterfaceDetailData);
        SetupDiDestroyDeviceInfoList(deviceInfoSet);
		return 0;
    }
	
	return (void*)winusbHandle;
}

WinUsb读写

int winusb_write(WINUSB_INTERFACE_HANDLE winusbHandle,const BYTE* buffer,DWORD size)
{
	BOOL result = FALSE;
	ULONG byteswritten = 0;

	result = WinUsb_WritePipe(winusbHandle, EP0ADDR, (PUCHAR)buffer, size, &byteswritten, NULL);

	return byteswritten;
}

int winusb_read(WINUSB_INTERFACE_HANDLE winusbHandle, BYTE* buffer, DWORD size)
{
	BOOL result = FALSE;
	DWORD bytesRead = 0;

	result = WinUsb_ReadPipe(winusbHandle, EP1ADDR, buffer, size, &bytesRead, NULL);

	return bytesRead;
}