|
|
View previous topic :: View next topic |
Author |
Message |
daveh Guest
|
C++ code examples for USB bulk transfer? |
Posted: Wed Jun 04, 2008 12:13 pm |
|
|
Hi, does anyone have any suggestions of sites with c++ code examples for interfacing with a USB device (pref PIC18F4550)? Ideally what I'd like is something like CCS' OScope source code in C++.
I did find some C++ code on Microchip's site but I couldn't compile it since I'm using Visual C++ 2008 Express Edition which doesn't have the MFC library. Maybe I'll have to buy the full version.
The reason I want to use C++ is that we already have a program written in C++ that I'd like to incorporate an interface to access the PIC18F4550 into.
Also, if I search the forum for c++ or "c++" it doesn't return anything. Any suggestions for how to search for that?
Any advice would be appreciated. |
|
|
Indy
Joined: 16 May 2008 Posts: 24
|
Re: C++ code examples for USB bulk tranfer? |
Posted: Wed Jun 04, 2008 3:14 pm |
|
|
daveh wrote: | Also, if I search the forum for c++ or "c++" it doesn't return anything. Any suggestions for how to search for that? | Use Google with the search string: c++ site:http://www.ccsinfo.com/forum
Sorry, I can't help you on the C++ / USB questions. |
|
|
daveh Guest
|
|
Posted: Fri Jul 11, 2008 12:17 pm |
|
|
I'll post my demo code below in case it will help anyone in the future. The code was complied with VC++ 2008 Express Edition. It connects to the Microchip PICDEM FS USB board with the "USB Device - MCHPUSB - Generic Driver - C18 - PICDEM FSUSB.hex" firmware installed.
This code requests and displays the Firmware version number, the Pot value and the temp sensor (U4) value.
Code: | // June18.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "mpusbapi.h"
// Global Vars
HINSTANCE libHandle;
HANDLE myOutPipe;
HANDLE myInPipe;
DWORD current_instance=0;
DWORD temp;
double pot_val;
double temp_val;
char vid_pid[]= "vid_04d8&pid_000c"; // MCHPUSB - Generic Driver vid_pid
char out_pipe[]= "\\MCHP_EP1";
char in_pipe[]= \\MCHP_EP1;
typedef enum
{
READ_VERSION = 0x00,
/*
READ_FLASH = 0x01,
WRITE_FLASH = 0x02,
ERASE_FLASH = 0x03,
READ_EEDATA = 0x04,
WRITE_EEDATA = 0x05,
READ_CONFIG = 0x06,
WRITE_CONFIG = 0x07,
*/
ID_BOARD = 0x31,
UPDATE_LED = 0x32,
SET_TEMP_REAL = 0x33,
RD_TEMP = 0x34,
SET_TEMP_LOGGING= 0x35,
RD_TEMP_LOGGING = 0x36,
RD_POT = 0x37,
RESET = 0xFF
}TYPE_CMD;
// Protoypes ----------------------------------------------------------------
void LoadMPUSBDLL();
void GetSummary();
void SetInstance();
void ReadHWVer();
void ReadPotVal();
void ReadTempVal();
DWORD SendReceivePacket(BYTE *SendData, DWORD SendLength, BYTE *ReceiveData, DWORD *ReceiveLength, UINT SendDelay, UINT ReceiveDelay);
void CheckInvalidHandle();
int _tmain(int argc, _TCHAR* argv[])
{
BOOLEAN bQuit = false;
CHAR selection;
printf("PICDEM FS USB Connection Demo \n\r");
printf("------------------------------\n\r");
LoadMPUSBDLL(); // load DLL
ReadPotVal(); // read Pot Value
ReadTempVal(); // read Temp Value
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
printf("The VID/PID being tested => %s \n\r",vid_pid);
temp = MPUSBGetDLLVersion();
printf("DLL Version => %X.%04X \n\r",HIWORD(temp),LOWORD(temp));
printf("Device Count => %d \n\n\r",MPUSBGetDeviceCount(vid_pid));
while(!bQuit)
{
printf("Select an option\n\r");
printf("[1] Summarize Instances\n\r");
printf("[2] Get USB Demo Firmware Version\n\r");
printf("[3] Read Pot Value: %0.1f%%\n\r",pot_val);
printf("[4] Read Temp Value: %0.1f%cC\n\r",temp_val,0xF8);
printf("[9] Set new Instance: %d\n\r",current_instance);
printf("[Q] Quit\n\r>>");
cin >> selection;
switch(selection)
{
case '1':
GetSummary();
break;
case '2':
ReadHWVer();
break;
case '3':
ReadPotVal();
break;
case '4':
ReadTempVal();
break;
case '9':
SetInstance();
break;
case 'Q':
bQuit = true;
break;
case 'q':
bQuit = true;
break;
}// end of switch
fflush(stdin);
printf("\n\r");
}// end of while
if (myOutPipe != INVALID_HANDLE_VALUE) MPUSBClose(myOutPipe);
if (myInPipe != INVALID_HANDLE_VALUE) MPUSBClose(myInPipe);
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
if (libHandle != NULL) FreeLibrary(libHandle);
return 0;
}
void LoadMPUSBDLL()
{
libHandle = NULL;
libHandle = LoadLibrary(_T("mpusbapi"));
if(libHandle == NULL)
{
printf("LoadLibrary => ERROR \n\r");
}
else
{
printf("LoadLibrary => Success \n\r");
MPUSBGetDLLVersion=(DWORD(*)(void))\
GetProcAddress(libHandle,"_MPUSBGetDLLVersion");
MPUSBGetDeviceCount=(DWORD(*)(PCHAR))\
GetProcAddress(libHandle,"_MPUSBGetDeviceCount");
MPUSBOpen=(HANDLE(*)(DWORD,PCHAR,PCHAR,DWORD,DWORD))\
GetProcAddress(libHandle,"_MPUSBOpen");
MPUSBWrite=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
GetProcAddress(libHandle,"_MPUSBWrite");
MPUSBRead=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
GetProcAddress(libHandle,"_MPUSBRead");
MPUSBReadInt=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
GetProcAddress(libHandle,"_MPUSBReadInt");
MPUSBClose=(BOOL(*)(HANDLE))GetProcAddress(libHandle,"_MPUSBClose");
if((MPUSBGetDeviceCount == NULL) || (MPUSBOpen == NULL) ||
(MPUSBWrite == NULL) || (MPUSBRead == NULL) ||
(MPUSBClose == NULL) || (MPUSBGetDLLVersion == NULL) ||
(MPUSBReadInt == NULL))
printf("GetProcAddress => Error\r\n");
else printf("GetProcAddress => Success \n\r");
}//end if else
}//end LoadDLL
void GetSummary()
{
HANDLE tempPipe = INVALID_HANDLE_VALUE;
DWORD count=0;
DWORD max_count = MPUSBGetDeviceCount(vid_pid);
printf("\n\r%d device(s) with %s currently attached\n\r",max_count,vid_pid);
for(int i=0; i< MAX_NUM_MPUSB_DEV; i++)
{
tempPipe = MPUSBOpen(i,vid_pid,NULL,MP_READ,0);
if(tempPipe != INVALID_HANDLE_VALUE)
{
printf("Instance Index # %d\n\r",i);
MPUSBClose(tempPipe);
count++;
}
if(count == max_count) break;
}//end of for
printf("\n\r");
}//end of GetSummary
void SetInstance()
{
DWORD selection;
fflush(stdin);
printf("Enter a valid instance index to open a USB connection: ");
cin >> selection;
myOutPipe = MPUSBOpen(selection,vid_pid,out_pipe,MP_WRITE,0);
myInPipe = MPUSBOpen(selection,vid_pid,in_pipe,MP_READ,0);
if(myOutPipe == INVALID_HANDLE_VALUE || myInPipe == INVALID_HANDLE_VALUE)
{
printf("Failed to open data pipes.\n\r");
return;
}
else current_instance = selection;
}
void ReadHWVer()
{
myOutPipe = MPUSBOpen(current_instance,vid_pid,out_pipe,MP_WRITE,0);
myInPipe = MPUSBOpen(current_instance,vid_pid,in_pipe,MP_READ,0);
if(myOutPipe == INVALID_HANDLE_VALUE || myInPipe == INVALID_HANDLE_VALUE)
{
printf("Failed to open data pipes.\n\r");
return;
}
BYTE send_buf[64],receive_buf[64];
DWORD RecvLength=4;
send_buf[0] = READ_VERSION; // command
send_buf[1] = 0x02; // expected length of return value
if(SendReceivePacket(send_buf,2,receive_buf,&RecvLength,1000,1000) ==1)
{
if(RecvLength==4 && receive_buf[0] == READ_VERSION && receive_buf[1] == 0x02)
{
printf("\nPICDEM FS Firmware Ver %d.%d\n\r",receive_buf[3],receive_buf[2]);
}
}
else printf("USB Operation Failed\n\r");
MPUSBClose(myOutPipe);
MPUSBClose(myInPipe);
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
}//end ReadHWVer()
void ReadPotVal()
{
myOutPipe = MPUSBOpen(current_instance,vid_pid,out_pipe,MP_WRITE,0);
myInPipe = MPUSBOpen(current_instance,vid_pid,in_pipe,MP_READ,0);
if(myOutPipe == INVALID_HANDLE_VALUE || myInPipe == INVALID_HANDLE_VALUE)
{
printf("Failed to open data pipes.\n\r");
return;
}
BYTE send_buf[64],receive_buf[64];
DWORD RecvLength=3;
send_buf[0] = RD_POT; // command
send_buf[1] = 0x02; // expected length of return value
if(SendReceivePacket(send_buf,2,receive_buf,&RecvLength,1000,1000) ==1)
{
if(RecvLength==3 && receive_buf[0] == RD_POT)
{
pot_val = (receive_buf[2]*256+receive_buf[1])/10.2; // max reading is 1020
}
}
else printf("USB Operation Failed\n\r");
MPUSBClose(myOutPipe);
MPUSBClose(myInPipe);
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
}//end ReadPotVal()
void ReadTempVal()
{
myOutPipe = MPUSBOpen(current_instance,vid_pid,out_pipe,MP_WRITE,0);
myInPipe = MPUSBOpen(current_instance,vid_pid,in_pipe,MP_READ,0);
if(myOutPipe == INVALID_HANDLE_VALUE || myInPipe == INVALID_HANDLE_VALUE)
{
printf("Failed to open data pipes.\n\r");
return;
}
BYTE send_buf[64],receive_buf[64];
DWORD RecvLength=3;
send_buf[0] = RD_TEMP; // command
send_buf[1] = 0x02; // expected length of return value
if(SendReceivePacket(send_buf,2,receive_buf,&RecvLength,1000,1000) ==1)
{
if(RecvLength==3 && receive_buf[0] == RD_TEMP)
{
temp_val = (receive_buf[2]*256+receive_buf[1])/8*0.0625; // strip 3 lowest bits - 1 LSB = 0.0625 degC
}
}
else printf("USB Operation Failed\n\r");
MPUSBClose(myOutPipe);
MPUSBClose(myInPipe);
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
}//end ReadTempVal()
DWORD SendReceivePacket(BYTE *SendData, DWORD SendLength, BYTE *ReceiveData, DWORD *ReceiveLength, UINT SendDelay, UINT ReceiveDelay)
{
DWORD SentDataLength;
DWORD ExpectedReceiveLength = *ReceiveLength;
if(myOutPipe != INVALID_HANDLE_VALUE && myInPipe != INVALID_HANDLE_VALUE)
{
if(MPUSBWrite(myOutPipe,SendData,SendLength,&SentDataLength,SendDelay))
{
if(MPUSBRead(myInPipe,ReceiveData,ExpectedReceiveLength,ReceiveLength,ReceiveDelay))
{
if(*ReceiveLength == ExpectedReceiveLength)
{
return 1; // success
}
else if(*ReceiveLength < ExpectedReceiveLength)
{
return 2; // Partially failed, wrong length
}
}
else CheckInvalidHandle();
}
else CheckInvalidHandle();
}// end if
return 0; // Operation Failed
}// end SendReceivePacket()
void CheckInvalidHandle()
{
if(GetLastError() == ERROR_INVALID_HANDLE)
{
MPUSBClose(myOutPipe);
MPUSBClose(myInPipe);
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
}
else
printf("Error Code %d\n\r",GetLastError());
}
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|