You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

How to write C++ programs using the new DLL starting from the old DLL.

Introduction

There are some major differences between the two DLLs.

feature

Dewesoft C++ DLL

Trenz Electronic C++ DLL

programming language

C++

C++

architecture

standard
(TE0300DLL.dll)

stacked
(TE_USB_FX2_CyAPI.dll requires Cypress CyAPI.dll);

Handles

present

absent

structures

embedded

defined in Cypress CyAPI.h

parameters*

less

more

freedom*

less

more

Example: in TE0300DLL.dll, the buffer size is fixed to 2 kbyte, while in TE_USB_FX2_CyAPI.dll you are free to choose 4 kbyte or more.

Function translation

Dewesoft C++ DLLTrenz Electronic C++ DLL
HANDLE m_handle = 0;CCyUSBDevice *USBdevList = new CCyUSBDevice((HANDLE)0,CYUSBDRV_GUID,true);
cout << endl << TE0300_ScanCards() << endl;cout << endl << TE_USB_FX2_ScanCards(USBdevList) << endl;
TE0300_Open(&m_handle, 0) !=0;TE_USB_FX2_Open(USBdevList, 0)!=0;
TE0300_Open(&m_handle, 1)!=0;TE_USB_FX2_Open(USBdevList, 1)!=0
TE0300_Close(&m_handle);TE_USB_FX2_Close(USBdevList);
TE0300_SendCommand(handle, cmd, cmd_length, reply, &reply_length, timeout)TE_USB_FX2_SendCommand(USBdevList, cmd, cmd_length, reply, reply_length, timeout)

Note 1

TE_USB_FX2_Open(USBdevList, x) acts more as a SelectCard() function because the list (handles) of USB devices is already created in USBdevList.

 

The TE_USB_FX2_Close() function takes an already initialized USB device list (USBDeviceList) and closes the handle to the Trenz Electronic device driver, if one is open. This function closes all internal handles of USBDeviceList.

 

Warning about derived variables

 

If TE_USB_FX2_Close() is called, then dynamically allocated members of the CCyUSBDevice class are de-allocated. And, all "shortcut" pointers to elements of the EndPoints array (ControlEndPt, IsocIn/OutEndPt, BulkIn/OutEndPt, InterruptIn/OutEndPt) are reset to NULL.

 

This function does not differ much from from its homonym of the previous TE0300DLL.dll; the only difference is that this function closes a handle (like TE0300DLL.dll) to the driver but the handle is not exposed to user because it is not exposed by USBDeviceList (unlike TE0300DLL.dll).

 

In the code, it is possible to call TE_USB_FX2_Open() where TE0300_Open() is used.

 

In the code, it is possible to call TE_USB_FX2_Close() where TE0300_Close() is used, but

 

    it is rare that you would ever need to call TE_USB_FX2_Close() explicitly (though doing so would not cause any problems).

    TE_USB_FX2_Open() realize automatically much of the TE_USB_FX2_Close() work.

 

 

Other internal handles (inside USBDeviceList) are automatically closed by TE_USB_FX2_Open() function when another handle to the device driver is already open (i.e. a TE_USB_FX2_Open() has been successfully used before). It is a behavior inherithed by CyAPI.dll Open() function.

Note 2:
In the code, it is possible to call TE_USB_FX2_Open() where TE0300_Open() is used.In the code, it is possible to call TE_USB_FX2_Close() where TE0300_Close() is used, but it is rare that you would ever need to call TE_USB_FX2_Close() explicitly (though doing so would not cause any problems). The TE_USB_FX2_Close() function closes the handle to the Trenz Electronic device driver, if one is open.
If TE_USB_FX2_Close() is called, then dynamically allocated members of the CCyUSBDevice class are de-allocated. And, all "shortcut" pointers to elements of the EndPoints array (ControlEndPt, IsocIn/OutEndPt, BulkIn/OutEndPt, InterruptIn/OutEndPt) are reset to NULL.
Close is automatically carried out by the TE_USB_FX2_Open() function, if a handle to the device driver is already open (i.e. a TE_USB_FX2_Open() has been successfully used before).

Unmanaged C++ TE_USB_FX2_Open()
of TE_USB_FX2_CyAPI.dll

You can use this function to select the desired module without the need to call TE_USB_FX2_Close() before. Though doing so would not cause any problems.
Close is automatically realized by the TE_USB_FX2_Open() function, if a handle to the driver is already open (i.e. TE_USB_FX2_Open() has been successfully called before).

Managed C# TE_USB_FX2_Open()
of TE_USB_FX2_CyUSB.dll

You can use this function to select the desired module without the need to call TE_USB_FX2_Close() before. This function is not necessary.

Unmanaged C++ TE_USB_FX2_Close()
of TE_USB_FX2_CyAPI.dll

It can be freely called but it is rare that you would ever need to call TE_USB_FX2_Close() explicitly (though doing so would not cause any problems).

Managed C# TE_USB_FX2_Close()
of TE_USB_FX2_CyUSB.dll

It cannot be freely called and it is rare that you would ever need to call TE_USB_FX2_Close() explicitly. Furthermore, if it is used in the wrong way, the program can raise exceptions.

Dewesoft C++ DLL

Trenz Electronic C++ DLL

//test code, not production code

int packetlen = 512;
byte data[512];






for (int i = 0; i < 10; i++)
{
packetlen = 512;
for (int j = 0; j < packetlen; j++)
data[j] = j;
if (TE0300_SetData(handle, data, packetlen, PI_EP8))
{
cout << "ERROR" << endl;
return;
}
}

//test code, not production code

int packetlen = 512;
byte data[512];

CCyBulkEndPoint *BulkOutEP = NULL;

TE_USB_FX2_SetData_InstanceDriverBuffer ( USBdevList, &BulkOutEP, PI_EP8, timeout, DeviceDriverBufferSize);

for (int i = 0; i < 10; i++)
{
packetlen = 512;
for (int j = 0; j < packetlen; j++)
data[j] = j;
if (TE_USB_FX2_SetData(&BulkOutEP, data, packetlen))
{
cout << "ERROR" << endl;
return;
}
}

Dewesoft C++ DLL

Trenz Electronic C++ DLL

int packetlen = 512;
byte data[512];







for (int i = 0; i < 10; i++)
{
packetlen = 512;
if (TE0300_GetData(handle, data, &packetlen, PI_EP6, 1000))
{
cout << "ERROR" << endl;
return;
}
for (int j = 0; j < packetlen; j++)
cout << data[j];
cout << endl;
}

int packetlen = 512;byte data[512];

CCyBulkEndPoint *BulkInEP = NULL;

TE_USB_FX2_GetData_InstanceDriverBuffer ( USBdevList, &BulkInEP, PI_EP6, timeout, DeviceDriverBufferSize);



for (int i = 0; i < 10; i++)
{
packetlen = 512;
if (TE_USB_FX2_GetData(&BulkInEP, data, packetlen))
{
cout << "ERROR" << endl;
return;
}
for (int j = 0; j < packetlen; j++)
cout << data[j];
cout << endl;
}

Dewesoft C++ DLL

Trenz Electronic C++ DLL

void ReadData(unsigned int handle)
{


int packetlen = RX_PACKET_LEN;
unsigned int packets = 1200;
byte * data;

unsigned int total_cnt = 0;
unsigned int errors = 0;
data = new byte [RX_PACKET_LEN*packets];
//allocate memory

ResetFX2FifoStatus(handle);
SendFPGAcommand(handle,FX22MB_REG0_START_TX);
//starts test




ElapsedTime.Start(); //StopWatch start
for (unsigned int i = 0; i < packets; i++)
{
packetlen = RX_PACKET_LEN;
if (TE0300_GetData(handle, data+total_cnt, &packetlen, PI_EP6,TIMEOUT_MS))
{
cout << "ERROR" << endl;
errors++;
break;
}
total_cnt += packetlen;
}
TheElapsedTime = ElapsedTime.Stop(false);
//DEBUG StopWatch timer
SendFPGAcommand(handle,FX22MB_REG0_STOP);
//stops test
delete data;
}

void ReadData(CCyUSBDevice *USBdevList, unsigned int DeviceDriverBufferSize, int RX_PACKET_LEN, unsigned long TIMEOUT)
{
long packetlen = RX_PACKET_LEN;
unsigned int packets = 1200;
byte * data;
byte * data_temp = NULL;
unsigned int total_cnt = 0;
unsigned int errors = 0;
data = new byte [RX_PACKET_LEN*packets];
//allocate memory

ResetFX2FifoStatus(USBdevList);
SendFPGAcommand(USBdevList,FX22MB_REG0_START_TX);
//starts test
CCyBulkEndPoint *BulkInEP = NULL;
TE_USB_FX2_GetData_InstanceDriverBuffer ( USBdevList, CardNo, &BulkInEP, PI_EP6, TIMEOUT, DeviceDriverBufferSize);
ElapsedTime.Start(); //StopWatch start
for (unsigned int i = 0; i < packets; i++)
{
packetlen = RX_PACKET_LEN;
data_temp = &data[total_cnt];
if (TE_USB_FX2_GetData(&BulkInEP,data_temp,packetlen))
{
cout << "ERROR read" << endl;
errors++;
break;
}
total_cnt += (packetlen);
}
TheElapsedTime = ElapsedTime.Stop(false);
//DEBUG StopWatch timer
SendFPGAcommand(USBDevice,FX22MB_REG0_STOP);
//stops test
delete data;
}

Dewesoft C++ DLL

Trenz Electronic C++ DLL

void WriteData(unsigned int handle)
{


int packetlen = TX_PACKET_LEN;
unsigned int packets = 1200;
byte * data;

unsigned int total_cnt = 0;
unsigned int errors = 0;
data = new byte [TX_PACKET_LEN*packets];
//allocate memory
SetData (data);
ResetFX2FifoStatus(handle);
SendFPGAcommand(handle,FX22MB_REG0_START_RX);
//starts test



ElapsedTime.Start();
//StopWatch start
for (unsigned int i = 0; i < packets; i++)
{
packetlen = TX_PACKET_LEN;
if (TE0300_GetData(handle, data+total_cnt, &packetlen, PI_EP8,TIMEOUT_MS))
{
cout << "ERROR" << endl;
errors++;
break;
}
total_cnt += packetlen;
}
TheElapsedTime = ElapsedTime.Stop(false);
//DEBUG StopWatch timer
SendFPGAcommand(handle,FX22MB_REG0_STOP);
//stops test
delete data;
}

void WriteData(CCyUSBDevice *USBdevList, unsigned int DeviceDriverBufferSize, int TX_PACKET_LEN, unsigned long TIMEOUT)
{
long packetlen = TX_PACKET_LEN;
unsigned int packets = 1200;
byte * data;
byte * data_temp = NULL;
unsigned int total_cnt = 0;
unsigned int errors = 0;
data = new byte [TX_PACKET_LEN*packets];
//allocate memory
SetData (data);
ResetFX2FifoStatus(USBdevList);
SendFPGAcommand(USBdevList,FX22MB_REG0_START_RX);
//starts test
CCyBulkEndPoint *BulkOutEP = NULL;
TE_USB_FX2_SetData_InstanceDriverBuffer ( USBdevList, CardNo, &BulkOutEP, PI_EP8,TIMEOUT,DeviceDriverBufferSize);
ElapsedTime.Start();
//StopWatch start
for (unsigned int i = 0; i < packets; i++)
{
packetlen = TX_PACKET_LEN;
data_temp = &data[total_cnt];
if (TE_USB_FX2_GetData(&BulkInEP,data_temp,packetlen))
{
cout << "ERROR read" << endl;
errors++;
break;
}
total_cnt += (packetlen);
}
TheElapsedTime = ElapsedTime.Stop(false);
//DEBUG StopWatch timer
SendFPGAcommand(USBDevice,FX22MB_REG0_STOP);
//stops test
delete data;
}

 

Document Change History

version

date

author

description

0.9

2012-06-01

SP, FDR

Release Preview.

1.0

 

 

Initial release.

1.1

2013-04-23

SP

Improved description of TE_USB_FX2_Open() and TE_USB_FX2_Close() functions

  • No labels