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

📄 test1dlg.cpp

📁 用vc编写的usb转I2c例子
💻 CPP
字号:
// test1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "test1.h"
#include "test1Dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest1Dlg dialog

CTest1Dlg::CTest1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTest1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTest1Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTest1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTest1Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTest1Dlg, CDialog)
	//{{AFX_MSG_MAP(CTest1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDOK, OnExit)
	ON_BN_CLICKED(IDC_BUTTON3, OnButtonReadI2c)
	ON_BN_CLICKED(IDC_BUTTON4, OnButtonWriteI2c)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest1Dlg message handlers

BOOL CTest1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

  hDLL = LoadLibrary("UsbI2cIo.dll");          // attempt load dll
  if( hDLL != NULL) {
    // got handle to dll, now initialize API function pointers
    GetDllVersion=(GETDLLVERSION)GetProcAddress(hDLL,"DAPI_GetDllVersion");
    OpenDeviceInstance=(OPENDEVICEINSTANCE)GetProcAddress(hDLL,"DAPI_OpenDeviceInstance");
    CloseDeviceInstance=(CLOSEDEVICEINSTANCE)GetProcAddress(hDLL,"DAPI_CloseDeviceInstance");
    DetectDevice=(DETECTDEVICE)GetProcAddress(hDLL,"DAPI_DetectDevice");
    GetDeviceCount=(GETDEVICECOUNT)GetProcAddress(hDLL,"DAPI_GetDeviceCount");
    GetDeviceInfo=(GETDEVICEINFO)GetProcAddress(hDLL,"DAPI_GetDeviceInfo");
    OpenDeviceBySerialId=(OPENDEVICEBYSERIALID)GetProcAddress(hDLL,"DAPI_OpenDeviceBySerialId");
    GetSerialId=(GETSERIALID)GetProcAddress(hDLL,"DAPI_GetSerialId");
    ConfigIoPorts=(CONFIGIOPORTS)GetProcAddress(hDLL,"DAPI_ConfigIoPorts"); 
    GetIoConfig=(GETIOCONFIG)GetProcAddress(hDLL,"DAPI_GetIoConfig"); 
    ReadIoPorts=(READIOPORTS)GetProcAddress(hDLL,"DAPI_ReadIoPorts");
    WriteIoPorts=(WRITEIOPORTS)GetProcAddress(hDLL,"DAPI_WriteIoPorts");
    ReadI2c=(READI2C)GetProcAddress(hDLL,"DAPI_ReadI2c");
    WriteI2c=(WRITEI2C)GetProcAddress(hDLL,"DAPI_WriteI2c");
  }
  else {
    // unable to get handle to dll, no sense continuing, exit application
    exit(0);
  }


  return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTest1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTest1Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTest1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTest1Dlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	UpdateData(FALSE);
	
	CDialog::OnTimer(nIDEvent);
}

void CTest1Dlg::OnButton1() 
{
  // uncomment one of the following "Device Open" examples

/*************************************************************************
  Example 1: how to open a handle to a specific UsbI2cIo device (use your serial number)
*************************************************************************/
/*
  hDevice[0] = OpenDeviceBySerialId("UsbI2cIo","01030001");
  if(hDevice == INVALID_HANDLE_VALUE) {
    // error if here
    exit(0);
  }
*/

/*************************************************************************
  Example 2: how to open a handle to a specific device instance
*************************************************************************/
  hDevice[0]=OpenDeviceInstance("UsbI2cIo", 0);
  if(hDevice == INVALID_HANDLE_VALUE) {
    // error if here
    exit(0);
  }

/*************************************************************************
  Example 3: how to open a handle to the first available device
*************************************************************************/
/*
  int i;

  for(i=0; i<128;i++) {
    hDevice[0]=OpenDeviceInstance("UsbI2cIo", i);
    if(hDevice != INVALID_HANDLE_VALUE) {
      // got one!
      break;
    }
  }
  // check after exiting loop to make sure we were successful
  if(hDevice == INVALID_HANDLE_VALUE) {
    // error if here
    exit(0);
  }
*/
}

void CTest1Dlg::OnButton2() 
{
	flag=CloseDeviceInstance(hDevice[0]);
}

void CTest1Dlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	flag=CloseDeviceInstance(hDevice[0]);
  exit(1);
}

void CTest1Dlg::OnButtonReadI2c() 
{
  // setup for I2C Read Transaction, reading 5 byte from device 0x42, no extended addressing

  LONG lReadCnt;

  // initialize I2C transaction structure
  TransI2C.byTransType = I2C_TRANS_NOADR;
  TransI2C.wMemoryAddr = 0;
  TransI2C.bySlvDevAddr = 0x42;
  TransI2C.wCount = 5;      // number of bytes to read

  // call the API funciton to perform the I2C read transaction
  lReadCnt = ReadI2c(hDevice[0], &TransI2C);
  
  if(lReadCnt == TransI2C.wCount) {
     // successfully read 5 bytes, the data is in the byte array TransI2c.Data
  }
  else { 
    // failed to read 5 bytes
  }
}

void CTest1Dlg::OnButtonWriteI2c() 
{

  // setup for I2C Write Transaction, writing 3 bytes to device 0x42, no extended addressing

  LONG lWriteCnt;

  // initialize I2C transaction structure
  TransI2C.byTransType = I2C_TRANS_NOADR;
  TransI2C.wMemoryAddr = 0;
  TransI2C.bySlvDevAddr = 0x42;   // I2C device Id (R/W bit is handled by API function)
  TransI2C.wCount = 3;      // number of bytes to write
  TransI2C.Data[0] = 0x11;  // Data byte 0 value
  TransI2C.Data[1] = 0x22;  // Data byte 1 value
  TransI2C.Data[2] = 0x33;  // Data byte 2 value

  // call the API funciton to perform the I2C write transaction
  lWriteCnt = WriteI2c(hDevice[0], &TransI2C);

  if(lWriteCnt == TransI2C.wCount) {
     // successfully wrote 3 bytes to the device, the data was supplied from the byte array TransI2c.Data
  }
  else { 
    // failed to write 3 bytes
  }
}

⌨️ 快捷键说明

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