<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[MFC und USB2.0 betreiben]]></title><description><![CDATA[<p>Hi leutz,</p>
<p>mein Problem lautet wie folgt:<br />
Habe eine bestehende Konsolenanwendung(DOS-Shell) in C++ und integriere diese gerade in eine grafische MFC-Umgebung. Eine der Methoden des Programms läd eine Datei über die USB Schnittstelle in ein FPGA. Die dazu nötigen Methoden stimmen. Nun ist es so, das die Konsolenanwendung für diesen Ladevorgang ca. 4 Sekunden, die grafische Oberfläche 20 - 30 Sekunden braucht.</p>
<p>Was mach die MFC Oberfläche anders als die Konsolenanwendung?<br />
Müsste man vielleicht einen eigenen Prozess für die grafische Anwendung starten?</p>
<p>Sorry ich bin MFC noobie <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
<p>Danke für hilfreiche Antworten!</p>
<p>Chiao</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/81659/mfc-und-usb2-0-betreiben</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 05:36:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/81659.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 03 Aug 2004 07:20:36 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to MFC und USB2.0 betreiben on Tue, 03 Aug 2004 07:20:36 GMT]]></title><description><![CDATA[<p>Hi leutz,</p>
<p>mein Problem lautet wie folgt:<br />
Habe eine bestehende Konsolenanwendung(DOS-Shell) in C++ und integriere diese gerade in eine grafische MFC-Umgebung. Eine der Methoden des Programms läd eine Datei über die USB Schnittstelle in ein FPGA. Die dazu nötigen Methoden stimmen. Nun ist es so, das die Konsolenanwendung für diesen Ladevorgang ca. 4 Sekunden, die grafische Oberfläche 20 - 30 Sekunden braucht.</p>
<p>Was mach die MFC Oberfläche anders als die Konsolenanwendung?<br />
Müsste man vielleicht einen eigenen Prozess für die grafische Anwendung starten?</p>
<p>Sorry ich bin MFC noobie <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
<p>Danke für hilfreiche Antworten!</p>
<p>Chiao</p>
]]></description><link>https://www.c-plusplus.net/forum/post/573952</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/573952</guid><dc:creator><![CDATA[curry-king]]></dc:creator><pubDate>Tue, 03 Aug 2004 07:20:36 GMT</pubDate></item><item><title><![CDATA[Reply to MFC und USB2.0 betreiben on Tue, 03 Aug 2004 07:23:34 GMT]]></title><description><![CDATA[<p>Zeig doch mal Quelltext, ist einfacher zu analysieren...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/573957</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/573957</guid><dc:creator><![CDATA[C-O-M-M-A-N-D-E-R]]></dc:creator><pubDate>Tue, 03 Aug 2004 07:23:34 GMT</pubDate></item><item><title><![CDATA[Reply to MFC und USB2.0 betreiben on Tue, 03 Aug 2004 07:38:13 GMT]]></title><description><![CDATA[<p>Den Code kann ich nicht posten, da das ein firmeninternes Programm ist. Ausserdem ist der Code zu lang, um ihn zu posten.</p>
<p>Möchte nur wissen, wie bei oben genannten Problem es bei MFC zu Engpässen in der Programmverarbeitung kommen kann!</p>
<p>Chiao</p>
]]></description><link>https://www.c-plusplus.net/forum/post/573967</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/573967</guid><dc:creator><![CDATA[curry-king]]></dc:creator><pubDate>Tue, 03 Aug 2004 07:38:13 GMT</pubDate></item><item><title><![CDATA[Reply to MFC und USB2.0 betreiben on Tue, 03 Aug 2004 07:44:51 GMT]]></title><description><![CDATA[<p>Zu Engpässen kommt es garnicht außer du macht irgendwas damit was wir nicht wissen weil du entweder den Ablauf genau beschreibst oder einen Teil des Codes postest</p>
]]></description><link>https://www.c-plusplus.net/forum/post/573972</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/573972</guid><dc:creator><![CDATA[Unix-Tom]]></dc:creator><pubDate>Tue, 03 Aug 2004 07:44:51 GMT</pubDate></item><item><title><![CDATA[Reply to MFC und USB2.0 betreiben on Tue, 03 Aug 2004 07:45:08 GMT]]></title><description><![CDATA[<p>naja den part wo du auf die schnittstelle zugreifst kann ja wohl nicht so ..naja ok! ist aber schwer zu sagen. könnte alles mögliche sein, kann auch sein das du unsauber konvertiert hast etc. gibt viele möglichkeiten!</p>
<p>kann aber auch einfach daran liegen das du dabei evtl. auf funktionen der mfc zugreifst etc.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/573973</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/573973</guid><dc:creator><![CDATA[C-O-M-M-A-N-D-E-R]]></dc:creator><pubDate>Tue, 03 Aug 2004 07:45:08 GMT</pubDate></item><item><title><![CDATA[Reply to MFC und USB2.0 betreiben on Tue, 03 Aug 2004 08:20:54 GMT]]></title><description><![CDATA[<p>Ok leute, hier ist der Quellcode</p>
<p>Alle Funktionen, die nicht von MFC Objekten abgeleitet sind, stammen aus der alten Konsolenanwendung. Die sollten funktionieren.</p>
<pre><code>// MchTrDlg.cpp : Implementierungsdatei
//

#include &quot;stdafx.h&quot;
#include &quot;MchTr.h&quot;
#include &quot;MchTrDlg.h&quot;
#include &lt;windows.h&gt;

#include &lt;io.h&gt;
#include &lt;sys\stat.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;stdio.h&gt;
#include &lt;objbase.h&gt;
#include &lt;initguid.h&gt;	//this file is necessary for GUID macros like in Guids.h

#include &lt;mapidefs.h&gt;
#include &lt;Setupapi.h&gt;	//Win32 setup API header file, required library file is &quot;Setupapi.lib&quot;
						//in VS: goto &quot;Project/Settings&quot;, &quot;Link&quot; and  
						//				insert filename &quot;Setupapi.lib&quot; in &quot;Object/library modules&quot;
#include &lt;winioctl.h&gt;	//includes macro definitions for IOCTL operations
						//this file must be included!!

#include &quot;CeUsb2if.h&quot;	//CeUsb2 driver programming interface (structures, definitions and IOCTL codes)
#include &quot;CeFirmware.h&quot; //Definition of the firmware programming interface
#include &quot;CeError.h&quot;	//Status code definitions for CeUSb2 interface
#include &quot;Guids.h&quot;		//CeUsb2 device interface GUIDs are defined in this file

//#include &quot;tcapture.h&quot;
#include &lt;winbase.h&gt;

#include &quot;Usbd.h&quot;		//Necessary to get interface information: GetIFInfo()

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

//classes and defines from tcapture.h

#define CAPTURE_NO_ERROR					0x00
#define CEUSB2_OPEN_FAILED				    0x01
#define CEUSB2_IOCTL_OPERATION_FAILED		0x02
#define CEUSB2_FPGA_CONFIGURATION_FAILED	0x03
#define EXO_FILE_OPEN_FAILED				0x04
#define CEUSB2_RESET_FAILED					0x05
#define PIPE_RESET_FAILED					0x06
#define FILE_NOT_CREATED					0x07
#define PIPE_ABORT_FAILED					0x08
#define TESTMODE_ACTIVATE_FAILED			0x09
#define READ_STREAM_FAILED					0x0A
#define START_TRACER_FAILED					0x0B
#define WRITE_TO_DISK_FAILED				0x0C
#define MANUAL_ABORT						0x0D

//#define CAPTURE_EXO_FILE				&quot;tracer_testmode_off.exo&quot;
//#define CAPTURE_EXO_FILE_TESTMODE		&quot;D:\\ftp\\MchTr_GUI\\Debug\\tracer_testmode_on.exo&quot;

#define BLOCK_SIZE			1024

//Globals
char *ExoFile;
unsigned int CaptureFileSize;
unsigned int StartTriggerPoint;
unsigned int EndTriggerPoint;
bool overwrite=false;
bool start=false;
bool stop=false;
bool filter=false;
bool leave=false;
//End Globals		

class TCapture : public CMchTrDlg
{
public:
	TCapture();

	// Destructor: closes connections to CEUSB2-board
	~TCapture();

	int capture(int total_time, char* file, int channel, int transfer_rate, int testmode);
	bool OpenCeUsb2 (void);
	DWORD OpenUsb2(GUID Guid, UINT DeviceIndex, HANDLE *DevHandle);
	void CloseCeUsb2(HANDLE DevHandle);
	bool ResetPipe(unsigned long pipe);
	bool AbortPipe(unsigned long pipe);
	bool InitFpga(void);
	bool WriteFpgaBuffer(unsigned char *lBuffer, unsigned long Length);
	int IsFpgaConfiguredCorrectly(void);
	bool GpifAbort(void);
	bool ReinitFw(void);
	bool StartTracer(void);
	bool Testmode(void);
	int ReadExoFileBuffer(FILE *file_stream, unsigned char *Buffer, int Length);
	int ConfigureFpga(int testm);
	bool ResetFpga(void);
	bool ReadData(unsigned char *lBuffer, unsigned long Length);
	void error(int e);

protected:

	int filehandle;
	time_t start_time;

	// handle to CEUSB2 device
	HANDLE m_board_handle;
};

TCapture::TCapture()
{
}

int TCapture::capture(int total_time, char *capture_file, int chan, int transfer_rate, int testmode)
{
	int RetCode;
	int i=0, elapsed_time = 0;
	unsigned char Databuffer[BLOCK_SIZE]; //for trace on all channels = 128kbit/s
	unsigned char dummy1[BLOCK_SIZE/2];   //for trace on all channels with 64kbit/s
	unsigned char dummy3[BLOCK_SIZE/128]; //for trace on specific channel with 64kbit/s
	unsigned char dummy4[BLOCK_SIZE/64];  //for trace on specific channel with 128kbit/s
	int tmp_chan=chan;

//	time_t finish_time;
//	double elapsed_time_, Speed = 0;

	// Configure FPGA on CEUSB2 Board ****************************************
	if ( (RetCode = ConfigureFpga (testmode)) != CAPTURE_NO_ERROR )
		return RetCode;

	// Open Handle to CEUSB2 Board *******************************************
	if (!OpenCeUsb2 () )
	{
		CloseCeUsb2 (&amp;m_board_handle) ;
		return CEUSB2_OPEN_FAILED;
	}

	//Delete old capturefile before creating new empty file
	_unlink(capture_file);

	//Create new file
	if ( (filehandle = _open( capture_file, _O_RDWR | _O_BINARY | _O_CREAT, _S_IWRITE )) == -1 )
		return FILE_NOT_CREATED;

	//Reset FPGA design
	if (!ResetFpga () )
		return CEUSB2_RESET_FAILED;
	else
		m_CaptureError+=&quot;FPGA reset successful\r\n&quot;;

	//Abort GPIF waveform transition
	if (!GpifAbort () )
		perror(&quot;GPIF abort failed\n&quot;);
	else
		m_CaptureError+=&quot;GPIF aborted successfully\r\n&quot;;

	// Reset pipe1 for ReadData()
	if (!ResetPipe (1) )
		return PIPE_RESET_FAILED;
	else
		m_CaptureError+=&quot;Pipe reset successful\r\n&quot;;

	// Reinitialize Firmware
	if (ReinitFw () )
		m_CaptureError+=&quot;Firmware reinit successful\r\n&quot;;

	time(&amp;start_time);

	// Write header information
	_write( filehandle, &amp;start_time, 4);
	_write( filehandle, &amp;chan, 1);
	_write( filehandle, &amp;total_time, 11);

	//Reset FPGA design
	if (!ResetFpga () )
		return CEUSB2_RESET_FAILED;
//	else
//		printf(&quot;FPGA reset successful\n&quot;);

	//Testmode or not
	if ( testmode == 1 ) 
	{
		m_CaptureError+=&quot;*** Testmode activated! ***\r\n&quot;;
			//Send start signal
		if (!StartTracer () )
		return START_TRACER_FAILED;
	}

	//	time(&amp;start_time);			

	//Starting capture process ***********************************************
	if ( chan==0 )
	{
		if (transfer_rate==64)
		{
		      MessageBox(&quot;0xff 64&quot;);
			while (elapsed_time &lt; total_time)
			{
				if( !ReadData(Databuffer, BLOCK_SIZE) )
					return READ_STREAM_FAILED;

				for (i=0; i&lt;512; i++)
					dummy1[i]=Databuffer[i*2];

				if ( unsigned(_write( filehandle, dummy1, BLOCK_SIZE/2 )) != BLOCK_SIZE/2 )
					return WRITE_TO_DISK_FAILED;
				elapsed_time++;
			}

		}
		else
		{
			MessageBox(&quot;0xff 128&quot;);
			while (elapsed_time &lt; total_time)
			{
				if( !ReadData(Databuffer, BLOCK_SIZE) )
					return READ_STREAM_FAILED;

				if ( unsigned(_write( filehandle, Databuffer, BLOCK_SIZE )) != BLOCK_SIZE )
					return WRITE_TO_DISK_FAILED;
				elapsed_time++;
			}
		}
	}
	else
	{ // single-channel trace (Parameter -c set)

		if (transfer_rate==64)
		{
			MessageBox(&quot;-c 64&quot;);
			while (elapsed_time &lt; total_time)
			{
				if( !ReadData(Databuffer, BLOCK_SIZE) )
				return READ_STREAM_FAILED;

				for (int i=0; i&lt;BLOCK_SIZE/128; i++)
					dummy3[i] = Databuffer[(128*i)+tmp_chan];

				if ( unsigned(_write( filehandle, dummy3, BLOCK_SIZE/128 )) != BLOCK_SIZE/128 )
					return WRITE_TO_DISK_FAILED;
			   elapsed_time++;
			}
		}
		else
		{
			MessageBox(&quot;-c 64&quot;);
			while (elapsed_time &lt; total_time)
			{
				if( !ReadData(Databuffer, BLOCK_SIZE) )
					return READ_STREAM_FAILED;

				for (i=0; i&lt;(BLOCK_SIZE/128); i++)
				{	
					dummy4[i]=Databuffer[tmp_chan+i*128];
					dummy4[++i]=Databuffer[tmp_chan+i*128+1];
				}

				//write sgl_channel in file
				if ( unsigned(_write( filehandle, dummy4, BLOCK_SIZE/64 )) != BLOCK_SIZE/64 )
					return WRITE_TO_DISK_FAILED;

				/* //write all in alles.txt
				if ( unsigned(_write( fh1, Databuffer, BLOCK_SIZE )) != BLOCK_SIZE )
					return WRITE_TO_DISK_FAILED;
				*/
				elapsed_time++;
			}
		}
	}	

	//calculate elapsed time and speed
/*	time(&amp;finish_time);
	elapsed_time_ = difftime(finish_time, start_time);
	printf(&quot;\nElapsed time while capturing %dKB: %2.2f seconds\n&quot;, (BLOCK_SIZE*total_frames)/1024, elapsed_time_);
	Speed = (BLOCK_SIZE*total_frames)/(elapsed_time_*1024*1024);
	printf(&quot;Speed: %2.4fMB/s&quot;, Speed);
*/

	return CAPTURE_NO_ERROR;
}

TCapture::~TCapture()
{
	// close files
	_close( filehandle );

	// CeUsb2-Close
	CloseCeUsb2 (&amp;m_board_handle) ;
}

//Initialize Fpga device for configuration.
//Returns TRUE if succeeds, otherwise returns FALSE
//
bool TCapture::InitFpga(void)
{
	DWORD BytesReturned;
	unsigned char DummyBuffer[1];	//0x00 if initialization successful, 0xAB if error

	CEUSB2_VENDOR_REQUEST_CONTROL VenRequest;	//vendor request control structure

	//set up the FPGA initialization structure to initialize FPGA device for configuration
	VenRequest.Request = CEUSB2_CMD_FPGA_INIT;	
	VenRequest.Direction = 1;
	VenRequest.Value = 0;
	VenRequest.Index = 0;

	//call the driver
	return  DeviceIoControl(
						m_board_handle,							//	handle to device
						IOCTL_CEUSB2_VENDOR_REQUEST,			//	operation control code
						&amp;VenRequest,							//	input data buffer
						sizeof(CEUSB2_VENDOR_REQUEST_CONTROL),	//	size of input data buffer
						&amp;DummyBuffer,							//	output data buffer
						1,										//	size of output data buffer
						&amp;BytesReturned,							//	byte count
						NULL									//	overlapped information
							)!=0;
}

// Opens the CeUsb2 device with the given index and device interface GUID, and stores its handle in
// DevHandle pointer. This functions return 32 bit status code (0 = success).
//
DWORD TCapture::OpenUsb2(GUID Guid, UINT DeviceIndex, HANDLE *DevHandle)
{
	DWORD dwStatus = 0;		// return status
	SP_INTERFACE_DEVICE_DATA DevInterfaceData;
	PSP_INTERFACE_DEVICE_DETAIL_DATA pDevDetail = NULL;
	SP_DEVINFO_DATA DevInfoData;
	DWORD Size;

	// open a device enumeration handle
	HDEVINFO hDevInfo = SetupDiGetClassDevs(&amp;Guid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE );
	if (hDevInfo == INVALID_HANDLE_VALUE)
		return GetLastError();

	// find the device with DeviceIndex index
	DevInterfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
	if(!SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &amp;Guid, DeviceIndex, &amp;DevInterfaceData ))
	{
		dwStatus = GetLastError();
		goto exit_open;
	}

	// first get the length of detailed information
	SetupDiGetDeviceInterfaceDetail(hDevInfo, &amp;DevInterfaceData, NULL, 0, &amp;Size, NULL );

	pDevDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(Size);
	pDevDetail-&gt;cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
	DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

	// get device details
	if(!SetupDiGetDeviceInterfaceDetail(hDevInfo, &amp;DevInterfaceData, pDevDetail, Size, NULL, &amp;DevInfoData))
	{	
		dwStatus = GetLastError();
		goto exit_open;
	}

	// open the device
	*DevHandle = CreateFile(
						pDevDetail-&gt;DevicePath,			// name of the device
						GENERIC_READ | GENERIC_WRITE,	// access mode
						0,								// share mode
						NULL,							// security desc.
						OPEN_EXISTING,					// how to create
						0,								// file attributes
						NULL							// template file
							);

	if ( *DevHandle == INVALID_HANDLE_VALUE )
		dwStatus = GetLastError();

	exit_open:

	if(pDevDetail)
	{
		free(pDevDetail);
		pDevDetail = NULL;
	}

	SetupDiDestroyDeviceInfoList(hDevInfo);		// destroy device information list

	return dwStatus;
}

//Open handle to CEUSB2 device.
//
bool TCapture::OpenCeUsb2 (void)
{

   	__GUIDS_H__		//start macro

	// open handle to the device
	OpenUsb2(GUID_INTERFACE_CEUSB2, 0, &amp;m_board_handle);

	if ( m_board_handle == INVALID_HANDLE_VALUE )
	{
		//something wrong! There is not a device in the system
		//with this link name
		return FALSE;
	}
	return TRUE;
}

// CloseCeUsb2 function traverses the functionality of OpenCeUsb function, and closes (nullifies) the
// device handle stored during open device process.
//
void TCapture::CloseCeUsb2(HANDLE DevHandle)
{
	if(!DevHandle)
	{
		CloseHandle(DevHandle);
		DevHandle = NULL;
	}
}

// Reset USB pipe, necessary to use it
//
bool TCapture::ResetPipe(unsigned long pipe)
{
	DWORD BytesReturned;
	bool Ret;

    Ret = DeviceIoControl(
					m_board_handle,				// handle to device
					IOCTL_CEUSB2_RESET_PIPE,	// operation control code
					&amp;pipe,						// input data buffer
					sizeof(ULONG),				// size of input data buffer
					NULL,						// output data buffer
					0,				 			// size of output data buffer
					&amp;BytesReturned,				// byte count
					NULL						// overlapped information
						)!=0;	
	return Ret;
}

// Abort USB pipe, might be implemented before reset pipe
//
bool TCapture::AbortPipe(unsigned long pipe)
{
	DWORD BytesReturned;
	bool Ret;

    Ret = DeviceIoControl(
					m_board_handle,				// handle to device
					IOCTL_CEUSB2_ABORT_PIPE,	// operation control code
					&amp;pipe,						// input data buffer
					sizeof(ULONG),				// size of input data buffer
					NULL,						// output data buffer
					0,				 			// size of output data buffer
					&amp;BytesReturned,				// byte count
					NULL						// overlapped information
						)!=0;	
	return Ret;
}

//Write a buffer of bytes to the Fpga.
//Returns TRUE if succeeds, otherwise returns FALSE
//
bool TCapture::WriteFpgaBuffer(unsigned char *lBuffer, unsigned long Length)
{
	DWORD BytesReturned; //dummy
	bool Ret;

	ULONG pipe;

	pipe = 0;  //pipe 0 means: Type Bulk, Direction Out, Endpoint number 2

    Ret = DeviceIoControl(
					m_board_handle,				// handle to device
					IOCTL_CEUSB2_WRITE_BULK,	// operation control code
					&amp;pipe,						// input data buffer
					sizeof(ULONG),				// size of input data buffer
					lBuffer,				    // output data buffer
					Length,				 		// size of output data buffer
					&amp;BytesReturned,				// byte count
					NULL						// overlapped information
						)!=0;	

	return Ret;
}

//Read Fpga's done status.
//Returns -1 if IOCTL call fails, 1 if Fpga is configured successfully and 0 if it
//could not be configured correctly.
//
int TCapture::IsFpgaConfiguredCorrectly(void)
{
	DWORD BytesReturned;
	unsigned char Result;
	BOOL Ret;

	CEUSB2_VENDOR_REQUEST_CONTROL VenRequest;	//vendor request control structure

	//set up the FPGA initialization structure to initialize FPGA device for configuration
	VenRequest.Request = CEUSB2_CMD_FPGA_GET_STATUS;	
	VenRequest.Direction = 1;
	VenRequest.Value = 0;
	VenRequest.Index = 0;

	//call the driver
	Ret = DeviceIoControl(
					m_board_handle,							// handle to device
					IOCTL_CEUSB2_VENDOR_REQUEST,			// operation control code
					&amp;VenRequest,							// input data buffer
					sizeof(CEUSB2_VENDOR_REQUEST_CONTROL),	// size of input data buffer
					&amp;Result,								// output data buffer
					sizeof(unsigned char),					// size of output data buffer
					&amp;BytesReturned,							// byte count
					NULL									// overlapped information
						);

	if ( !Ret )
	{
		return -1;
	}
	else
	{
		if (Result == 0)
			return 1;
		else
			return 0;
	}
}

//function that reads an exo file whose handle is passed with file_stream
//argument. Data read from the file is returned in Buffer argument. Length is
//max. number of bytes requested. It returns actual number of bytes read.
//
int TCapture::ReadExoFileBuffer(FILE *file_stream, unsigned char *Buffer, int Length)
{
	int c;
	int Index;
	int type;
	int i;

	int len;
	int addr;
	int value;
	unsigned char DummyBuffer[64];

	// fill the buffer with data
	if ( (c=getc(file_stream)) != -1) 
	{
		Index = len = 0;
		if (c != 'S') 
		{
			return 0;
		}

		fscanf(file_stream,&quot;%1d&quot;,&amp;type);
		switch (type) 
		{
			case 8:

			case 9:
				break;	//end

			case 1:	//S1 records
				fscanf(file_stream,&quot;%2x%4x&quot;,&amp;len,&amp;addr);
				len-=3;
				for (i=len;i&gt;0;i--)
				{
					fscanf(file_stream,&quot;%2x&quot;,&amp;value);
					Buffer[Index++]=value;
				}
				break;

			case 2:	//S2 records
				fscanf(file_stream,&quot;%2x%6x&quot;,&amp;len,&amp;addr);
				len-=4;
				for (i=0;i&lt;len;i++) 
				{	
					fscanf(file_stream,&quot;%2x&quot;,&amp;value);
					Buffer[Index++]=value;
				}
				break;

			default:
				break;
		}

		// get the rest fo the line 
		fgets((char*)DummyBuffer,Length,file_stream);
	}
	else
		return 0;

	return len;
}

//Configure Fpga
//
int TCapture::ConfigureFpga(int testm)
{
	int BytesRead = 0;
	unsigned char Buffer[64] = {0};
	FILE *m_file_stream;

	//open file-stream for FPGA conf.data
	if( testm == 0)
	{
		if ( (m_file_stream = fopen (ExoFile, &quot;rt&quot; )) == NULL )
		{	
			MessageBox(&quot;The ExoFile must have the same dir as the capture-file!&quot;,&quot;EXO_FILE_OPEN_FAILED&quot;,MB_ICONSTOP);
			return EXO_FILE_OPEN_FAILED;
		}
	}
	else
	{
		if ( (m_file_stream = fopen (ExoFile, &quot;rt&quot; )) == NULL )
		{
			MessageBox(&quot;The ExoFile must have the same dir as the capture-file&quot;,&quot;EXO_FILE_OPEN_FAILED&quot;,MB_ICONSTOP);
			return EXO_FILE_OPEN_FAILED;
		}
	}

	// rewind the file
	rewind(m_file_stream);

//	printf (&quot;Exo-file opened\n&quot;);

	//open handle to CEUSB2 board
	if (!OpenCeUsb2 () )
	{
		m_CaptureError+=&quot;OpenCeUsb2 failed!\r\n&quot;;
		return CEUSB2_OPEN_FAILED;
	}

	m_CaptureError+=&quot;CeUsb2 opened\r\n&quot;;

	// Reset pipe0 for WriteFpga()
	if (!ResetPipe(0))
		return PIPE_RESET_FAILED;

	if (!InitFpga () )
	{
		CloseCeUsb2 (&amp;m_board_handle) ;
		return CEUSB2_FPGA_CONFIGURATION_FAILED;
	}

	m_CaptureError+=&quot;FPGA initialized\r\n&quot;;

	while((BytesRead = ReadExoFileBuffer(m_file_stream, Buffer, 64 )) != 0)
	{
		if ( !WriteFpgaBuffer(Buffer,BytesRead) )
			break;
	}

	fclose( m_file_stream );

	int FpgaStatus = IsFpgaConfiguredCorrectly();

	CloseCeUsb2 (&amp;m_board_handle) ;

	if(FpgaStatus == -1)
		return CEUSB2_IOCTL_OPERATION_FAILED;
	else if(FpgaStatus == 0)
	{
		return CEUSB2_FPGA_CONFIGURATION_FAILED;
	}

	m_CaptureError+=&quot;FPGA configured successfully\r\n&quot;;
	return CAPTURE_NO_ERROR;

}

//Reset FPGA design
//Returns TRUE if succeeds, otherwise returns FALSE
//
bool TCapture::ResetFpga(void)
{
	DWORD BytesReturned;

	CEUSB2_VENDOR_REQUEST_CONTROL VenRequest;	//vendor request control structure

	//set up the FPGA initialization structure to initialize FPGA device for configuration
	VenRequest.Request = CEUSB2_CMD_FPGA_RESET;	
	VenRequest.Direction = 0;
	VenRequest.Value = 0;
	VenRequest.Index = 0;

	//call the driver
	return  DeviceIoControl(
						m_board_handle,							//	handle to device
						IOCTL_CEUSB2_VENDOR_REQUEST,			//	operation control code
						&amp;VenRequest,							//	input data buffer
						sizeof(CEUSB2_VENDOR_REQUEST_CONTROL),	//	size of input data buffer
						NULL,									//	output data buffer
						0,										//	size of output data buffer
						&amp;BytesReturned,							//	byte count
						NULL									//	overlapped information
							)!=0;
}

// Abort the current GPIF waveform transition
//
bool TCapture::GpifAbort(void)
{
	DWORD BytesReturned;

	CEUSB2_VENDOR_REQUEST_CONTROL VenRequest;	//vendor request control structure

	//set up the FPGA initialization structure to initialize FPGA device for configuration
	VenRequest.Request = CEUSB2_CMD_GPIF_ABORT;	
	VenRequest.Direction = 0;
	VenRequest.Value = 0;
	VenRequest.Index = 0;

	//call the driver
	return  DeviceIoControl(
						m_board_handle,							//	handle to device
						IOCTL_CEUSB2_VENDOR_REQUEST,			//	operation control code
						&amp;VenRequest,							//	input data buffer
						sizeof(CEUSB2_VENDOR_REQUEST_CONTROL),	//	size of input data buffer
						NULL,									//	output data buffer
						0,										//	size of output data buffer
						&amp;BytesReturned,							//	byte count
						NULL									//	overlapped information
							)!=0;
}

// Reinitialize Firmware
//
bool TCapture::ReinitFw(void)
{
	DWORD BytesReturned;

	CEUSB2_VENDOR_REQUEST_CONTROL VenRequest;	//vendor request control structure

	//set up the FPGA initialization structure to initialize FPGA device for configuration
	VenRequest.Request = CEUSB2_CMD_FW_REINIT;	
	VenRequest.Direction = 0;
	VenRequest.Value = 0;
	VenRequest.Index = 0;

	//call the driver
	return  DeviceIoControl(
						m_board_handle,							//	handle to device
						IOCTL_CEUSB2_VENDOR_REQUEST,			//	operation control code
						&amp;VenRequest,							//	input data buffer
						sizeof(CEUSB2_VENDOR_REQUEST_CONTROL),	//	size of input data buffer
						NULL,									//	output data buffer
						0,										//	size of output data buffer
						&amp;BytesReturned,							//	byte count
						NULL									//	overlapped information
							)!=0;
}

// Send startsignal to FPGA's internal bitgenerator
//
bool TCapture::StartTracer(void)
{
	DWORD BytesReturned; //dummy
	int startbyte;

	CEUSB2_VENDOR_REQUEST_CONTROL VenRequest;	//vendor request control structure

	//set up the FPGA initialization structure to initialize FPGA device for configuration
	VenRequest.Request = CEUSB2_CMD_GPIF_SINGLE_READ_WRITE;	
	VenRequest.Direction = 1;
	VenRequest.Value = 0;
	VenRequest.Index = 0;

	//call the driver
	return  DeviceIoControl(
						m_board_handle,							//	handle to device
						IOCTL_CEUSB2_VENDOR_REQUEST,			//	operation control code
						&amp;VenRequest,							//	input data buffer
						sizeof(CEUSB2_VENDOR_REQUEST_CONTROL),	//	size of input data buffer
						&amp;startbyte,								//	output data buffer
						sizeof(int),							//	size of output data buffer
						&amp;BytesReturned,							//	byte count
						NULL									//	overlapped information
							)!=0;
}

// Read datastream on pipe1, write it to buffer
//
bool TCapture::ReadData(unsigned char *lBuffer, unsigned long Length)
{
	DWORD BytesReturned; //dummy
	bool Ret;
	int error;

	ULONG pipe = 1;		//pipe 1 means: Type Bulk, Direction In, Endpoint number 6

    Ret = DeviceIoControl(
					m_board_handle,				// handle to device
					IOCTL_CEUSB2_READ_BULK,		// operation control code
					&amp;pipe,						// input data buffer
					sizeof(ULONG),				// size of input data buffer
					lBuffer,				    // output data buffer
					Length,				 		// size of output data buffer
					&amp;BytesReturned,				// byte count
					NULL						// overlapped information
						)!=0;

	error = GetLastError();
	return Ret;
}

// Starts ConfigureFPGA, opens device, creates file, resets FPGA, aborts GPIF Settings,
// resets pipe, starts capturing and finally writes data to file
//

//////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg-Dialogfeld für Anwendungsbefehl &quot;Info&quot;

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

// Dialogfelddaten
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CString	m_Information;
	//}}AFX_DATA

	// Vom Klassenassistenten generierte Überladungen virtueller Funktionen
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV-Unterstützung
	//}}AFX_VIRTUAL

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

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{

}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Text(pDX, IDC_EDIT1, m_Information);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// Keine Nachrichten-Handler
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMchTrDlg Dialogfeld

CMchTrDlg::CMchTrDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMchTrDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMchTrDlg)
	m_CaptureFilePath=_T(&quot;&quot;);
	m_CaptureTime =0;
	m_CaptureChannel = 0;
	m_CaptureError = _T(&quot;&quot;);
	m_Capture64k = FALSE;
	m_Capture128k = FALSE;
	m_CaptureSize = 0;
	m_CaptureStartTrigger = 0;
	m_CaptureEndTrigger = 0;
	m_TriggerCondition = 0;
	//}}AFX_DATA_INIT
	// Beachten Sie, dass LoadIcon unter Win32 keinen nachfolgenden DestroyIcon-Aufruf benötigt
	m_hIcon = AfxGetApp()-&gt;LoadIcon(IDR_MAINFRAME);
}

void CMchTrDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMchTrDlg)
	DDX_Text(pDX, IDC_EDIT1,m_CaptureFilePath);
	DDX_Text(pDX, IDC_EDIT2, m_CaptureTime);
	DDX_Text(pDX, IDC_EDIT, m_CaptureSize);
	DDX_CBIndex(pDX, IDC_COMBO1, m_CaptureChannel);
	DDX_Text(pDX, IDC_EDIT3, m_CaptureError);
	DDX_Check(pDX, IDC_CHECK1, m_Capture64k);
	DDX_Check(pDX, IDC_CHECK2, m_Capture128k);
	DDX_Text(pDX, IDC_EDIT_starttrigger, m_TriggerCondition);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMchTrDlg, CDialog)
	//{{AFX_MSG_MAP(CMchTrDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON2, OnCaptureSaveButton)
	ON_BN_CLICKED(IDC_BUTTON_start, OnCaptureStartButton)
	ON_BN_CLICKED(IDC_RADIO_filter, OnRADIOfilter)
	ON_BN_CLICKED(IDC_RADIO_start, OnRADIOstart)
	ON_BN_CLICKED(IDC_RADIO_stop, OnRADIOstop)
	ON_BN_CLICKED(IDC_BUTTON_start2, OnCaptureStopButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMchTrDlg Nachrichten-Handler

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

	// Hinzufügen des Menübefehls &quot;Info...&quot; zum Systemmenü.

	// IDM_ABOUTBOX muss sich im Bereich der Systembefehle befinden.
	ASSERT((IDM_ABOUTBOX &amp; 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX &lt; 0xF000);

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

	// Symbol für dieses Dialogfeld festlegen. Wird automatisch erledigt
	//  wenn das Hauptfenster der Anwendung kein Dialogfeld ist
	SetIcon(m_hIcon, TRUE);			// Großes Symbol verwenden
	SetIcon(m_hIcon, FALSE);		// Kleines Symbol verwenden

	// ZU ERLEDIGEN: Hier zusätzliche Initialisierung einfügen

	return TRUE;  // Geben Sie TRUE zurück, außer ein Steuerelement soll den Fokus erhalten
}

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

// Wollen Sie Ihrem Dialogfeld eine Schaltfläche &quot;Minimieren&quot; hinzufügen, benötigen Sie 
//  den nachstehenden Code, um das Symbol zu zeichnen. Für MFC-Anwendungen, die das 
//  Dokument/Ansicht-Modell verwenden, wird dies automatisch für Sie erledigt.

void CMchTrDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // Gerätekontext für Zeichnen

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

		// Symbol in Client-Rechteck zentrieren
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&amp;rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Symbol zeichnen
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// Die Systemaufrufe fragen den Cursorform ab, die angezeigt werden soll, während der Benutzer
//  das zum Symbol verkleinerte Fenster mit der Maus zieht.
HCURSOR CMchTrDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMchTrDlg::OnCaptureSaveButton() 
{

	CFileDialog dlg(FALSE,NULL,NULL,NULL,&quot;Textfiles txt|&quot;);

	if ((dlg.DoModal())==IDOK) 
			m_CaptureFilePath=dlg.GetPathName();
	UpdateData(FALSE);

}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//////////////////////CAPTURE START//////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

void CMchTrDlg::OnCaptureStartButton()
{
	UpdateData(TRUE); //Fields ---&gt; Vars

	m_CaptureError=&quot;&quot;;

	UpdateData(FALSE);

	TCapture* capture = new TCapture();	
	char *EXO_TEST_ON = &quot;tracer_testmode_on.exo&quot;;	//FPGA-load has to have the same dir as the saved capture
	char *EXO_TEST_OFF = &quot;tracer_testmode_off.exo&quot;;
	char *CaptureFile = new char[m_CaptureFilePath.GetLength()+1];    // CStrings to ANSI string
	strcpy(CaptureFile,m_CaptureFilePath);
	int c, ok=1, count=0, transfer_rate;
	char buffer[8];

	 char buffer1[_MAX_PATH];
	 HMODULE hEXE;

	 hEXE = GetModuleHandle(&quot;MchTr.exe&quot;);
	 ::GetModuleFileName (hEXE, buffer1, _MAX_PATH);
	 CString Path=buffer;
	 Path=(CString(buffer1).Left(CString(buffer1).ReverseFind('\\')+1));

	 //char d[8];
	 //sprintf(d,&quot;%d&quot;,m_CaptureChannel);
	 //MessageBox(d);	

	if (strcmp(CaptureFile,&quot;&quot;)==0 || !isalpha(CaptureFile[0]) || CaptureFile[1]!=':' || CaptureFile[2]!='\\' || !isgraph(CaptureFile[3]))
	{	m_CaptureError += &quot;Please enter a legal capture-file!\r\n&quot;; ok=0; }
	else
	{
		//combine the exopath
		ExoFile = new char[strlen(Path)+1+strlen(EXO_TEST_ON)];

		strcpy(ExoFile,Path);

		strcat(ExoFile,EXO_TEST_ON);

	}

	//CaptureSize or CaptureTime selected
	if (m_CaptureSize==0 &amp;&amp; m_CaptureTime==0 || m_CaptureSize!=0 &amp;&amp; m_CaptureTime!=0)
	{
			m_CaptureError+=&quot;Please enter Max.FileSize OR CaptureTime!\r\n&quot;;
			ok=0;
	}

	//64k oder 128k -Kanal
	if (m_Capture64k==FALSE &amp;&amp; m_Capture128k==FALSE || m_Capture64k==TRUE &amp;&amp; m_Capture128k==TRUE)
	{ 
		m_CaptureError+=&quot;Please select 64kbit/s OR 128kbit/s\r\n&quot;;
		ok=0;
	}
	else
		if (m_Capture64k==TRUE)
			transfer_rate=64;
		else
		       transfer_rate=128;

	if (ok==1)
	{    
		c = capture-&gt;capture( m_CaptureTime*1024, CaptureFile, m_CaptureChannel, transfer_rate, 0/*TESTMODE ON*/);

		switch (c)
		{
			case CEUSB2_OPEN_FAILED :				m_CaptureError +=&quot;Error 0x01: unable open connection to CeUsb2 board.\r\n&quot;; break;
			case CEUSB2_IOCTL_OPERATION_FAILED :		m_CaptureError +=&quot;Error 0x02: unable to access CeUsb2 board.\r\n&quot;; break;
			case CEUSB2_FPGA_CONFIGURATION_FAILED :		m_CaptureError +=&quot;Error 0x03: unable to configure FPGA on CeUsb2 board.\r\n&quot;; break;
			case CEUSB2_RESET_FAILED :				m_CaptureError +=&quot;Error 0x05: unable to give reset signal.\r\n&quot;; break; 
			case PIPE_RESET_FAILED :				m_CaptureError +=&quot;Error 0x06: unable to reset pipe.\r\n&quot;; break; 
			case PIPE_ABORT_FAILED :				m_CaptureError +=&quot;Error 0x08: unable to abort pipe.\r\n&quot;; break;
			case EXO_FILE_OPEN_FAILED :				m_CaptureError +=&quot;Error 0x04: unable to open FPGA-load.\r\n&quot;; break;
			case TESTMODE_ACTIVATE_FAILED:			m_CaptureError +=&quot;Error 0x09: unable to activate testmode.\r\n&quot;; break;
			case FILE_NOT_CREATED	:			m_CaptureError +=&quot;Error 0x07: unable to create destination file.\r\n&quot;; break;
			case READ_STREAM_FAILED	 :			m_CaptureError +=&quot;Error 0x0A: error reading from stream.\r\n&quot;; break;
			case START_TRACER_FAILED :				m_CaptureError +=&quot;Error 0x0B: error giving start signal to internal bitgenerator.\r\n&quot;; break;
			case WRITE_TO_DISK_FAILED :				m_CaptureError +=&quot;Error 0x0C: unable to write data to disk.\r\n&quot;; break;
			case CAPTURE_NO_ERROR :				break;
			case MANUAL_ABORT:					m_CaptureError +=&quot;Error 0x0D: capture finished by manual abort.\r\n&quot;; break;
			default :						sprintf(buffer,&quot;%x&quot;,c); m_CaptureError+=buffer; m_CaptureError +=&quot;: unknown error occured.\r\n&quot;; break;
		}

		delete capture;
		delete ExoFile;
	}

	m_CaptureError+=&quot;... Capturing finished&quot;;

	UpdateData(FALSE);	//Variablen ---&gt; Felder

	 //exit(0);
}

void CMchTrDlg::OnRADIOfilter() 
{
	filter=true;
	start=false;
	stop=false;
}

void CMchTrDlg::OnRADIOstart() 
{
	filter=false;
	start=true;
	stop=false;
}

void CMchTrDlg::OnRADIOstop() 
{
	filter=false;
	start=false;
	stop=true;
}

//manual abort the capture
void CMchTrDlg::OnCaptureStopButton() 
{
	leave=true;
}
</code></pre>
<p>Hoffe ihr könnt das lesen!<br />
Danke im Voraus für eure Hilfe!</p>
<p>Chiao</p>
]]></description><link>https://www.c-plusplus.net/forum/post/573994</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/573994</guid><dc:creator><![CDATA[curry-king]]></dc:creator><pubDate>Tue, 03 Aug 2004 08:20:54 GMT</pubDate></item></channel></rss>