⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myusb.cpp

📁 针对at91sam7s64芯片实现和下位机之间的usb通讯
💻 CPP
字号:
// myusb.cpp: implementation of the myusb class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "usbdev.h"
#include "myusb.h"
#include "DriverDeviceInterface.h"
#include "driverioctl.h"
#include <winioctl.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError);
GUID ClassGuid = DriverDevice_CLASS_GUID;

myusb::myusb()
{
}

myusb::~myusb()
{
}

DWORD myusb::Close()
{
	CloseHandle(hDevice);
	AfxMessageBox("see you, intllisence!");
	return FALSE;
}

DWORD myusb::IniDevice()
{
	DWORD	dError = 0;
	hDevice= OpenByInterface( &ClassGuid, 0, &dError);
	if (hDevice== INVALID_HANDLE_VALUE)
	{
		CString error;
		error.Format("设备不可用,错误号(%d),请重新连接!", dError);
		AfxMessageBox(error);
		return dError;
	}
	else
	{
		AfxMessageBox("hello, intellisence!");
	}
	return ERROR_SUCCESS;
}

void myusb::doWrite(int n,char *ptr)
{
	char	*buf;
	ULONG	nWritten;
	int		i;
	int		j;

	buf = (char *) malloc(n);
	if (buf == NULL)
	{
		printf("Failed to allocate buffer for write");
	}

	// start with the mod26 letter of the number of bytes to write
	j = (n % 26);
	// load buffer with dummy data (abcdefg...)
	for (i=0; i<n; i++, j=(j + 1)%26)
	{
		buf[i] = 'a' + j;
	}
	buf[3]=*ptr;

	// Write data to driver
	printf("Writing to device - ");
	WriteFile(hDevice, buf, n, &nWritten, NULL);
	printf("%d bytes written to device (%d attempted).\n", nWritten, n);

	// Print what was written
	i = 0;
	while(i < n)
	{
		j = min((i+26),n);
		for(; i < j; i++)
		{
			printf("%c, ", buf[i]);
		}
		printf("\n");
	}

	free(buf);
}

char myusb::doRead(int n)
{			
	char	*buf,datarx;
	ULONG	nRead;
	int		i;
	int		j;

	buf = (char *) malloc(n);
	if (buf == NULL)
	{
		printf("Failed to allocate buffer for read");
	}

	// Read data from driver
	printf("Reading from device - ");
	ReadFile(hDevice, buf, n, &nRead, NULL);
	printf("%d bytes read from device (%d requested).\n", nRead, n);

	// Print what was read
	i = 0;
	while(i < n)
	{
		j = min((i+26),n);
		for(; i < j; i++)
		{
			printf("%c, ", buf[i]);
		}
		printf("\n");
	}
	return datarx=buf[0];
	free(buf);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -