FEUSBDemoDlg.cpp
Go to the documentation of this file.
00001 /*-------------------------------------------------------
00002 |                                                       |
00003 |                  FEUSBDemoDlg.cpp                     |
00004 |                                                       |
00005 ---------------------------------------------------------
00006 
00007 Copyright  2007         FEIG ELECTRONIC GmbH, All Rights Reserved.
00008                                                 Lange Strasse 4
00009                                                 D-35781 Weilburg
00010                                                 Federal Republic of Germany
00011                                                 phone    : +49 6471 31090
00012                                                 fax      : +49 6471 310999
00013                                                 e-mail   : info@feig.de
00014                                                 Internet : http://www.feig.de
00015                                         
00016 Author                  :       Benjamin Stadin
00017 Begin                   :       10.12.2006
00018 
00019 Version                 :       01.00.00 / 24.01.2007 / Benjamin Stadin
00020 
00021 Operation Systems       :       Linux
00022 */
00023 
00024 #include <QMainWindow>
00025 #include <QApplication>
00026 #include "FEUSBDemoDlg.h"
00027 #include "ScanAndOpenDlg.h"
00028 #include "GetScanListParaDlg.h"
00029 #include "SetDeviceParaDlg.h"
00030 #include "GetDeviceParaDlg.h"
00031 #include "DeviceIDDlg.h"
00032 #include "DeviceHandleDlg.h"
00033 #include "TransceiveDlg.h"
00034 #include <usb.h>
00035 
00036 //------------------------------------------------------------------------------
00037 // Name: FEUSBDemoDlg() 
00038 // Desc: constructor
00039 //------------------------------------------------------------------------------
00040 FEUSBDemoDlg::FEUSBDemoDlg()
00041 {
00042         ui.setupUi(this);
00043         setObjectName("FEUSBDemoDlg");
00044         setWindowTitle("FEIG ELECTRONIC GmbH - FEUSB Demo");
00045         
00046         // Scan functions
00047         connect(ui.scanButton, SIGNAL(clicked()), this, SLOT(scanButtonClicked()));
00048         connect(ui.scanAndOpenButton, SIGNAL(clicked()), this, SLOT(scanAndOpenButtonClicked()));
00049         connect(ui.getScanListParaButton, SIGNAL(clicked()), this, SLOT(getScanListParaButtonClicked()));
00050         connect(ui.getScanListSizeButton, SIGNAL(clicked()), this, SLOT(getScanListSizeButtonClicked()));    
00051         connect(ui.clearScanListButton, SIGNAL(clicked()), this, SLOT(clearScanListButtonClicked()));
00052         
00053         // Device functions
00054         connect(ui.openDeviceButton, SIGNAL(clicked()), this, SLOT(openDeviceButtonClicked()));   
00055         connect(ui.closeDeviceButton, SIGNAL(clicked()), this, SLOT(closeDeviceButtonClicked()));
00056         connect(ui.getDeviceParaButton, SIGNAL(clicked()), this, SLOT(getDeviceParaButtonClicked()));
00057         connect(ui.setDeviceParaButton, SIGNAL(clicked()), this, SLOT(setDeviceParaButtonClicked()));
00058         connect(ui.transceiveButton, SIGNAL(clicked()), this, SLOT(transceiveButtonClicked()));
00059         connect(ui.getDeviceListButton, SIGNAL(clicked()), this, SLOT(getDeviceListButtonClicked()));
00060         connect(ui.getDeviceHandleButton, SIGNAL(clicked()), this, SLOT(getDeviceHandleButtonClicked()));
00061         connect(ui.isDevicePresentButton, SIGNAL(clicked()), this, SLOT(isDevicePresentButtonClicked()));
00062         connect(ui.versionButton, SIGNAL(clicked()), this, SLOT(versionButtonClicked()));
00063         connect(ui.exitButton, SIGNAL(clicked()), this, SLOT(close()));
00064         
00065         // static helper function for callbacks to indirect callback fcts to current class instance
00066         setGetClassInstance(this);
00067         // callbacks for device removal / insertion
00068         connect(this, SIGNAL(onConnect(int, unsigned int)), this, SLOT(connectEvent(int, unsigned int)));
00069         connect(this, SIGNAL(onDisconnect(int, unsigned int)), this, SLOT(disconnectEvent(int, unsigned int)));
00070         
00071         FEUSB_EVENT_INIT init;
00072         init.uiFlag = FEUSB_CALLBACK;
00073         init.uiUse = FEUSB_DEV_CONNECT_EVENT; //FEUSB_DEV_DISCONNECT_EVENT
00074         init.cbFct = &connectEventHelper;       
00075         
00076         FEUSB_EVENT_INIT init2;
00077         init2.uiFlag = FEUSB_CALLBACK;
00078         init2.uiUse = FEUSB_DEV_DISCONNECT_EVENT;
00079         init2.cbFct = &disconnectEventHelper;
00080         FEUSB_AddEventHandler(0,&init);
00081         FEUSB_AddEventHandler(0,&init2);
00082         
00083         // hide the standard qt column index on the left of the scanTable
00084         QHeaderView *h = ui.scanTable->verticalHeader();
00085         h->hide();
00086 }
00087 
00088 //------------------------------------------------------------------------------
00089 // Name: FEUSBDemoDlg() 
00090 // Desc: destructor
00091 //------------------------------------------------------------------------------
00092 FEUSBDemoDlg::~FEUSBDemoDlg()
00093 {
00094         //DelEventHandler -> Connect oder DisConnect
00095         FEUSB_EVENT_INIT init3;
00096         init3.uiFlag = FEUSB_CALLBACK;
00097         init3.uiUse = FEUSB_DEV_DISCONNECT_EVENT; 
00098         init3.cbFct = &disconnectEventHelper;
00099 
00100         FEUSB_DelEventHandler(0,&init3);
00101         
00102         FEUSB_EVENT_INIT init4;
00103         init4.uiFlag = FEUSB_CALLBACK;
00104         init4.uiUse = FEUSB_DEV_CONNECT_EVENT; 
00105         init4.cbFct = &connectEventHelper;
00106 
00107         FEUSB_DelEventHandler(0,&init4);
00108 }
00109 
00110 //------------------------------------------------------------------------------
00111 // Name: buildScanList() 
00112 // Desc: builds and displays the scan list table
00113 //------------------------------------------------------------------------------
00114 void FEUSBDemoDlg::buildScanList()
00115 {
00116         char cValue[32];
00117         int iScanCnt = 0;
00118         int iCnt;
00119         int iDevHnd;
00120         unsigned long dwDeviceID;
00121         char tmpStr[256];
00122         QString str;
00123         QString sMsg;
00124         QTableWidgetItem *item;
00125 
00126         ui.scanTable->clearContents();
00127         
00128         iScanCnt = FEUSB_GetScanListSize();
00129         sprintf(tmpStr, "%d", iScanCnt);
00130         
00131         if(iScanCnt > 0)
00132         {
00133                 ui.scanTable->setRowCount(iScanCnt);
00134                 
00135                 for(iCnt=0; iCnt<iScanCnt; iCnt++)
00136                 {
00137                         sprintf(tmpStr, "%d", iCnt);
00138                         str = tmpStr;
00139                         item = ui.scanTable->item(iCnt, 0);
00140                         if (!item)
00141                                 ui.scanTable->setItem(iCnt, 0, new QTableWidgetItem(tmpStr));
00142                         else
00143                                 item->setText(tmpStr);
00144                         // DeviceID 
00145                         int iBack = FEUSB_GetScanListPara(iCnt, "DEVICE-ID", cValue);
00146                         if(iBack)
00147                         {
00148                                 ui.returnStatuslineEdit->setText("Error in FEUSB_GetScanListPara(..DEVICE-ID..)");
00149                         }
00150                         else
00151                         {
00152                                 sscanf((const char*)cValue, "%lx",&dwDeviceID);
00153                                 sprintf(tmpStr, "%lu", dwDeviceID);
00154                                 str = tmpStr;
00155                                 item = ui.scanTable->item(iCnt, 1);
00156                                 ui.scanTable->setItem(iCnt, 1, new QTableWidgetItem(tmpStr));
00157                                 iDevHnd = FEUSB_GetDeviceHnd(dwDeviceID);
00158                                 if(iDevHnd >= 0)
00159                                 {
00160                                         sprintf(tmpStr, "%d", iDevHnd);
00161                                         str = tmpStr;
00162                                         item = ui.scanTable->item(iCnt, 2);
00163                                         ui.scanTable->setItem(iCnt, 2, new QTableWidgetItem(tmpStr));
00164                                         iBack = FEUSB_GetScanListPara(iCnt, "PRESENT", cValue);
00165                                         if(iBack)
00166                                         {
00167                                                 ui.returnStatuslineEdit->setText("Error in FEUSB_GetScanListPara(..PRESENT..)");
00168                                         }
00169                                         else
00170                                         {
00171                                                 int iPresent = atoi(cValue);
00172                                                 if(iPresent)
00173                                                 {
00174                                                         item = ui.scanTable->item(iCnt, 3);
00175                                                         ui.scanTable->setItem(iCnt, 3, new QTableWidgetItem("yes"));
00176                                                 }
00177                                                 else
00178                                                 {
00179                                                         item = ui.scanTable->item(iCnt, 3);
00180                                                                 ui.scanTable->setItem(iCnt, 3, new QTableWidgetItem("no"));
00181                                                 }
00182                                         }
00183                                 }
00184                         }
00185                 }
00186         }       
00187 }
00188 
00189 //------------------------------------------------------------------------------
00190 // Name: scanButtonClicked() 
00191 // Desc: displays found devices
00192 //------------------------------------------------------------------------------
00193 void FEUSBDemoDlg::scanButtonClicked()
00194 {
00195         scan(false);
00196 }
00197 
00198 //------------------------------------------------------------------------------
00199 // Name: scan() 
00200 // Desc: scans the usb bus. If bOpen is true the device(s) will be opened
00201 //------------------------------------------------------------------------------
00202 void FEUSBDemoDlg::scan(bool bOpen)
00203 {
00204         int iBack = 0;
00205         int iScanOpt = 0;
00206         char *sDeviceID;
00207         char *sFamilyID;
00208         char *sProductID;
00209         ScanAndOpenDlg dlg(this);
00210         char tmp[16] = {0};
00211         
00212         if (dlg.exec() != QDialog::Accepted)
00213                 return;
00214         
00215         iScanOpt = dlg.ui.scanOptionComboBox->currentIndex();
00216         sDeviceID = qstrdup(dlg.ui.deviceIDLineEdit->text().toLatin1());
00217         sFamilyID = qstrdup(dlg.ui.familyIDLineEdit->text().toLatin1());
00218         sProductID = qstrdup(dlg.ui.productIDLineEdit->text().toLatin1());
00219 
00220         
00221         if(iScanOpt == 0)
00222                 m_iScanOpt = FEUSB_SCAN_FIRST;
00223                 
00224         if(iScanOpt == 1)
00225                 m_iScanOpt = FEUSB_SCAN_NEXT;
00226 
00227         if(iScanOpt == 2)
00228                 m_iScanOpt = FEUSB_SCAN_NEW;
00229 
00230         if(iScanOpt == 3)
00231                 m_iScanOpt = FEUSB_SCAN_ALL;
00232         
00233         if(dlg.ui.scanPackCheckBox->checkState())
00234                 m_iScanOpt |= FEUSB_SCAN_PACK;
00235         
00236         m_SearchOpt.iMask               = 0;
00237         m_SearchOpt.cFamilyName[0]      = '\0';
00238         m_SearchOpt.cDeviceName[0]      = '\0';
00239         m_SearchOpt.cInterface[0]       = '\0';
00240         m_SearchOpt.cDeviceID[0]        = '\0';
00241 
00242         if(strlen(sFamilyID) > 0)
00243         {
00244                 m_SearchOpt.iMask |= FEUSB_SEARCH_FAMILY;
00245                 strcpy(m_SearchOpt.cFamilyName, sFamilyID);
00246                 m_iScanOpt |= FEUSB_SCAN_SEARCH;
00247         }
00248         if(strlen(sProductID) > 0)
00249         {
00250                 m_SearchOpt.iMask |= FEUSB_SEARCH_PRODUCT;
00251                 strcpy(m_SearchOpt.cDeviceName, sProductID);
00252                 m_iScanOpt |= FEUSB_SCAN_SEARCH;
00253         }
00254         if(strlen(sDeviceID) > 0)
00255         {
00256                 m_SearchOpt.iMask |= FEUSB_SEARCH_DEVICEID;
00257                 unsigned long dwDeviceID = 0;
00258                 sscanf((const char*)sDeviceID, "%lx", &dwDeviceID);
00259                 char sHexDeviceID[64];
00260                 sprintf(sHexDeviceID, "%08X", dwDeviceID);
00261                 strcpy(m_SearchOpt.cDeviceID, sHexDeviceID);
00262                 m_iScanOpt |= FEUSB_SCAN_SEARCH;
00263         }
00264 
00265         if(bOpen)
00266         {
00267                 iBack = FEUSB_ScanAndOpen(m_iScanOpt, &m_SearchOpt);
00268         }
00269         else
00270         {
00271                 iBack = FEUSB_Scan(m_iScanOpt, &m_SearchOpt);
00272         }
00273         
00274         delete sDeviceID;
00275         delete sFamilyID;
00276         delete sProductID;
00277         
00278         if (iBack < 0)
00279         {
00280                 QString errString = "";
00281                 char cErrorText[256] = {0};
00282                 FEUSB_GetErrorText(iBack, cErrorText);
00283                 errString.sprintf("Error: %d : %s", iBack, cErrorText);
00284                 ui.returnStatuslineEdit->setText(errString);
00285         }
00286         else
00287         {
00288                 sprintf(tmp, "%d", iBack);
00289                 ui.returnStatuslineEdit->setText(tmp);
00290         }
00291         buildScanList();
00292 }
00293 
00294 //------------------------------------------------------------------------------
00295 // Name: scanAndOpenButtonClicked() 
00296 // Desc: scans and opens devices and displays them
00297 //------------------------------------------------------------------------------
00298 void FEUSBDemoDlg::scanAndOpenButtonClicked()
00299 {
00300         scan(true);
00301 }
00302 
00303 //------------------------------------------------------------------------------
00304 // Name: getScanListParaButtonClicked() 
00305 // Desc: shows a dialog for scan list parameters
00306 //------------------------------------------------------------------------------
00307 void FEUSBDemoDlg::getScanListParaButtonClicked()
00308 {
00309         int iBack = 0;
00310         int idx;
00311         char *cParaID;
00312         char *sIdx;
00313         char cValue[256] = {0};
00314         QString str;
00315         GetScanListParaDlg dlg(this);
00316         
00317         int row = ui.scanTable->currentRow();
00318         QTableWidgetItem *item; item = ui.scanTable->item(row, 0);
00319         if (item)
00320                 str = item->text();
00321         else
00322                 str = "";
00323                 
00324         dlg.ui.scanlistIndexLineEdit->setText(str);
00325 
00326         if (dlg.exec() != QDialog::Accepted)
00327                 return;
00328                 
00329         sIdx = qstrdup(dlg.ui.scanlistIndexLineEdit->text().toLatin1());        
00330         cParaID = qstrdup(dlg.ui.parameterIDComboBox->currentText().toLatin1());        
00331         idx = atoi(sIdx);
00332         iBack = FEUSB_GetScanListPara(idx, cParaID, cValue);
00333         if (iBack < 0)
00334         {
00335                 QString errString = "";
00336                 char cErrorText[256] = {0};
00337                 FEUSB_GetErrorText(iBack, cErrorText);
00338                 errString.sprintf("Error: %d : %s", iBack, cErrorText);
00339                 ui.returnStatuslineEdit->setText(errString);
00340         }
00341         else
00342         {
00343                 str.sprintf("Search Result for ParameterID = %s, Index=%d ==> %s", cParaID, idx, cValue);
00344                 ui.returnStatuslineEdit->setText(str);
00345         }
00346         delete cParaID;
00347         delete sIdx;
00348 }
00349 
00350 //------------------------------------------------------------------------------
00351 // Name: getScanListSizeButtonClicked() 
00352 // Desc: displays the current number of devices in the usb scan list
00353 //------------------------------------------------------------------------------
00354 void FEUSBDemoDlg::getScanListSizeButtonClicked()
00355 {
00356         QString str;
00357         char tmp[256];
00358                 
00359         str = sprintf(tmp, "ScanListSize = %d", FEUSB_GetScanListSize());
00360         str = tmp;
00361         ui.returnStatuslineEdit->setText(str);
00362 }
00363 
00364 //------------------------------------------------------------------------------
00365 // Name: clearScanListButtonClicked() 
00366 // Desc: clears the internal usb scan list and the display table
00367 //------------------------------------------------------------------------------
00368 void FEUSBDemoDlg::clearScanListButtonClicked()
00369 {
00370         FEUSB_ClearScanList();
00371         ui.returnStatuslineEdit->setText("0");
00372         ui.scanTable->clearContents();
00373         // seems to be a qt bug, doesn't repaint table
00374         /*ui.scanTable->repaint();
00375         ui.scanTable->update();*/
00376         ui.scanTable->setFocus();
00377 }
00378 
00379 //------------------------------------------------------------------------------
00380 // Name: openDeviceButtonClicked() 
00381 // Desc: opens the currently selected device from the table
00382 //------------------------------------------------------------------------------
00383 void FEUSBDemoDlg::openDeviceButtonClicked()
00384 {
00385         QString str;
00386         int row;
00387         QTableWidgetItem *item;
00388         QString sId = "";
00389         DeviceIDDlg dlg(this);
00390         unsigned long dwDeviceID;
00391         char *tmp;
00392         char res[16] = {0};
00393         int cnt;
00394         
00395         cnt = ui.scanTable->selectedItems().count();
00396         row = ui.scanTable->currentRow();
00397         item = ui.scanTable->item(row, 1);
00398         if (item)
00399                 sId = item->text();
00400         else
00401                 sId = "";
00402         
00403         // Initialize the DeviceIDDlg window with the ID of the current selection
00404         dlg.ui.deviceIDLineEdit->setText(sId);
00405         
00406         // Open the device
00407         if (dlg.exec() != QDialog::Accepted)
00408                 return;
00409         
00410         // Try to open the device ID
00411         sId = dlg.ui.deviceIDLineEdit->text();
00412         tmp = qstrdup(sId.toLatin1());
00413         sscanf((const char*)tmp, "%lx",&dwDeviceID);
00414         int iBack = FEUSB_OpenDevice(dwDeviceID);
00415         if (iBack < 0)
00416         {
00417                 QString errString = "";
00418                 char cErrorText[256] = {0};
00419                 FEUSB_GetErrorText(iBack, cErrorText);
00420                 errString.sprintf("Error: %d : %s", iBack, cErrorText);
00421                 ui.returnStatuslineEdit->setText(errString);
00422         }
00423         else
00424         {
00425                 sprintf(res, "%d", iBack); 
00426                 ui.returnStatuslineEdit->setText(res);
00427         }
00428         delete tmp;
00429         
00430         buildScanList();
00431         
00432         // Set the table selection in the main window to the device id
00433         QList<QTableWidgetItem*> items = ui.scanTable->findItems(sId, Qt::MatchExactly);
00434         // Normally just one device with the same id should be there
00435         foreach(QTableWidgetItem *item, items)
00436         {
00437                 ui.scanTable->setItemSelected(item, true);
00438         }
00439         
00440 }
00441 
00442 //------------------------------------------------------------------------------
00443 // Name: closeDeviceButtonClicked() 
00444 // Desc: closes the selected device from the table
00445 //------------------------------------------------------------------------------
00446 void FEUSBDemoDlg::closeDeviceButtonClicked()
00447 {
00448         QString str;
00449         int row;
00450         QTableWidgetItem *item;
00451         QString sHandle = "";
00452         DeviceHandleDlg dlg(this);
00453         int dwDeviceID;
00454         char *tmp;
00455         char res[16] = {0};
00456         int cnt;
00457         
00458         ui.returnStatuslineEdit->setText(res);
00459         
00460         cnt = ui.scanTable->selectedItems().count();
00461         row = ui.scanTable->currentRow();
00462         item = ui.scanTable->item(row, 2);
00463         if (item)
00464                 sHandle = item->text();
00465         else
00466                 sHandle = "";
00467         
00468         // Initialize the DeviceIDDlg window with the ID of the current selection
00469         dlg.ui.deviceHandleLineEdit->setText(sHandle);
00470         
00471         // Open the device
00472         if (dlg.exec() != QDialog::Accepted)
00473                 return;
00474         
00475         // Try to open the device ID
00476         sHandle = dlg.ui.deviceHandleLineEdit->text();
00477         tmp = qstrdup(sHandle.toLatin1());
00478         dwDeviceID = atoi(tmp);
00479         int iBack = FEUSB_CloseDevice(dwDeviceID);
00480         if (iBack < 0)
00481         {
00482                 QString errString = "";
00483                 char cErrorText[256] = {0};
00484                 FEUSB_GetErrorText(iBack, cErrorText);
00485                 errString.sprintf("Error: %d : %s", iBack, cErrorText);
00486                 ui.returnStatuslineEdit->setText(errString);
00487         }
00488         else
00489         {
00490                 sprintf(res, "%d", iBack); 
00491                 ui.returnStatuslineEdit->setText(res);
00492         }
00493         delete tmp;
00494         
00495         buildScanList();
00496 }
00497 
00498 //------------------------------------------------------------------------------
00499 // Name: getDeviceParaButtonClicked() 
00500 // Desc: shows a dialog with options to get device parameters from the selected device
00501 //------------------------------------------------------------------------------
00502 void FEUSBDemoDlg::getDeviceParaButtonClicked()
00503 {
00504         int iBack = 0;
00505         char *sParaID;
00506         char *sDevHnd;
00507         char sValue[256] = {0};
00508         QString str;
00509         GetDeviceParaDlg dlg(this);
00510         int iDevHnd;
00511         
00512         int row = ui.scanTable->currentRow();
00513         QTableWidgetItem *item; item = ui.scanTable->item(row, 2);
00514         if (item)
00515                 str = item->text();
00516         else
00517                 str = "";
00518                 
00519         dlg.ui.deviceHandleLineEdit->setText(str);
00520 
00521         if (dlg.exec() != QDialog::Accepted)
00522                 return;
00523                 
00524         sDevHnd = qstrdup(dlg.ui.deviceHandleLineEdit->text().toLatin1());      
00525         sParaID = qstrdup(dlg.ui.parameterIDComboBox->currentText().toLatin1());        
00526         sscanf((const char*)sDevHnd, "%d", &iDevHnd);
00527         iBack = FEUSB_GetDevicePara(iDevHnd, sParaID, sValue);
00528         if (iBack < 0)
00529         {
00530                 QString errString = "";
00531                 char cErrorText[256] = {0};
00532                 FEUSB_GetErrorText(iBack, cErrorText);
00533                 errString.sprintf("Error: %d : %s", iBack, cErrorText);
00534                 ui.returnStatuslineEdit->setText(errString);
00535         }
00536         else
00537         {
00538                 str.sprintf("Search Result for ParameterID=%s, Handle=%d ==> %s", sParaID, iDevHnd, sValue);
00539                 ui.returnStatuslineEdit->setText(str);
00540         }
00541         delete sParaID;
00542         delete sDevHnd;
00543 }
00544 
00545 //------------------------------------------------------------------------------
00546 // Name: setDeviceParaButtonClicked() 
00547 // Desc: -
00548 //------------------------------------------------------------------------------
00549 void FEUSBDemoDlg::setDeviceParaButtonClicked()
00550 {
00551         int iBack = 0;
00552         char *sParaID;
00553         char *sDevHandle;
00554         char *sValue;
00555         int iDevHnd;
00556         QString str;
00557         SetDeviceParaDlg dlg(this);
00558         
00559         int row = ui.scanTable->currentRow();
00560         QTableWidgetItem *item; item = ui.scanTable->item(row, 2);
00561         if (item)
00562                 str = item->text();
00563         else
00564                 str = "";
00565                 
00566         dlg.ui.deviceHandleLineEdit->setText(str);
00567 
00568         if (dlg.exec() != QDialog::Accepted)
00569                 return;
00570                 
00571         sDevHandle = qstrdup(dlg.ui.deviceHandleLineEdit->text().toLatin1());   
00572         sParaID = qstrdup(dlg.ui.parameterIDComboBox->currentText().toLatin1());
00573         sValue = qstrdup(dlg.ui.parameterValueLineEdit->text().toLatin1());
00574         sscanf((const char*)sDevHandle, "%d", &iDevHnd);
00575         iBack = FEUSB_SetDevicePara(iDevHnd, sParaID, sValue);
00576         if (iBack < 0)
00577         {
00578                 QString errString = "";
00579                 char cErrorText[256] = {0};
00580                 FEUSB_GetErrorText(iBack, cErrorText);
00581                 errString.sprintf("Error: %d : %s", iBack, cErrorText);
00582                 ui.returnStatuslineEdit->setText(errString);
00583         }
00584         else
00585         {
00586                 str.sprintf("Result for ParameterID=%s, Handle=%d ==> %s", sParaID, iDevHnd, sValue);
00587                 ui.returnStatuslineEdit->setText(str);
00588         }
00589         delete sParaID;
00590         delete sDevHandle;
00591 }
00592 
00593 //------------------------------------------------------------------------------
00594 // Name: transceiveButtonClicked() 
00595 // Desc: transmits data to the selected device and displays the received data
00596 //------------------------------------------------------------------------------
00597 void FEUSBDemoDlg::transceiveButtonClicked()
00598 {
00599         int iBack = 0;
00600         unsigned char ucRecData[4096] = {0}; // max prot length
00601         QString str;
00602         TransceiveDlg dlg(this);
00603         int deviceHandle;
00604         
00605         // Set the device handle in the transceive dialog from the current table selection
00606         int row = ui.scanTable->currentRow();
00607         QTableWidgetItem *item; 
00608         item = ui.scanTable->item(row, 2);
00609         if (item)
00610                 str = item->text();
00611         else
00612                 str = "";
00613                 
00614         dlg.ui.deviceHandleLineEdit->setText(str);
00615 
00616         if (dlg.exec() != QDialog::Accepted)
00617                 return;
00618 
00619         // sw version: 05FF65E5CB
00620         // Copy values of Transceive dialog and Transceive
00621         char *cData;
00622         char *cDevHandle;
00623         cDevHandle = qstrdup(dlg.ui.deviceHandleLineEdit->text().toLatin1());   
00624         cData = qstrdup(dlg.ui.dataLineEdit->text().toLatin1());        
00625         deviceHandle = atoi (cDevHandle);
00626         
00627         // Create a hex record twice the size of the input string
00628         int hexLen = 2 * strlen(cData) + 1;
00629         unsigned char *ucHexData = (unsigned char*)malloc(hexLen);
00630         //memset(ucHexData, '\0', hexLen-1);
00631         int iSendLen = FEDM_ConvHexCharToHexUChar(cData, strlen(cData), ucHexData, hexLen); // <<- böös
00632                 
00633         char p[32];
00634         sprintf(p, "FEDM_ConvHexCharToHexUChar() = %d", iSendLen);
00635         ui.returnStatuslineEdit->setText(p);
00636         if (iSendLen <0)
00637                 return;
00638 
00639         iBack = FEUSB_Transceive(deviceHandle, FEUSB_INTERFACE_OBID_RCI_V2, 0,
00640                                  ucHexData, iSendLen, ucRecData, sizeof(ucRecData));
00641         
00642         if (cData)
00643                 delete cData;
00644         if (cDevHandle)
00645                 delete cDevHandle;
00646         if (ucHexData)
00647                 free(ucHexData);
00648 
00649         if(iBack == 0) // No data returned
00650         {
00651                 char tmp[64];
00652                 sprintf(tmp, "FEUSB_Transceive() = %d", iBack);
00653                 ui.returnStatuslineEdit->setText(tmp);
00654                 return;
00655         }
00656         else if (iBack < 0) // Error
00657         {
00658                 QString errString = "";
00659                 char cErrorText[256] = {0};
00660                 FEUSB_GetErrorText(iBack, cErrorText);
00661                 errString.sprintf("Error: %d : %s", iBack, cErrorText);
00662                 ui.returnStatuslineEdit->setText(errString);
00663                 return;
00664         }
00665         
00666         // Create a hex record twice the size of the returned data and 
00667         // convert the return value into a hex string
00668         int hexRecLen = 2 * iBack + 1;
00669         char *cHexRecData;  
00670         cHexRecData = (char*)malloc(hexRecLen);
00671         memset(cHexRecData, 0, hexRecLen);      
00672         int iHexStrLen;
00673         if (FEDM_ConvHexUCharToHexChar(ucRecData, iBack, cHexRecData, hexRecLen) == 0)
00674                 iHexStrLen = strlen(cHexRecData);
00675         else
00676                 iHexStrLen = 0;
00677         ui.returnStatuslineEdit->setText((char*)cHexRecData);
00678         str = "";
00679         for (int iCnt=0; iCnt < iHexStrLen-1; iCnt +=2)
00680         {
00681                 if (iHexStrLen %2 >= 0) // For convenience make sure the string is large enough
00682                 {
00683                         str += cHexRecData[iCnt];
00684                         str += cHexRecData[iCnt+1];
00685                 }
00686                 if ((iHexStrLen-1) - iCnt > 2)
00687                         str += " ";
00688         }
00689         ui.returnStatuslineEdit->setText(str);
00690         
00691         if (cHexRecData)
00692                 free(cHexRecData);
00693 }
00694 
00695 //------------------------------------------------------------------------------
00696 // Name: getDeviceListButtonClicked() 
00697 // Desc: gets the device list
00698 //------------------------------------------------------------------------------
00699 void FEUSBDemoDlg::getDeviceListButtonClicked()
00700 {
00701         QString str;
00702         int row;
00703         QTableWidgetItem *item;
00704         QString sHandle = "";
00705         DeviceHandleDlg dlg(this);
00706         int deviceHandle;
00707         char *cHandle;
00708         char res[64] = {0};
00709         int cnt;
00710         
00711         cnt = ui.scanTable->selectedItems().count();
00712         row = ui.scanTable->currentRow();
00713         item = ui.scanTable->item(row, 2);
00714         if (item)
00715                 sHandle = item->text();
00716         else
00717                 sHandle = "";
00718         
00719         // Initialize the DeviceHandleDlg window with the ID of the current selection
00720         dlg.ui.deviceHandleLineEdit->setText(sHandle);
00721         
00722         // Open the device
00723         if (dlg.exec() != QDialog::Accepted)
00724                 return;
00725         
00726         // Try to open the device ID
00727         sHandle = dlg.ui.deviceHandleLineEdit->text();
00728         cHandle = qstrdup(sHandle.toLatin1());
00729         deviceHandle = atoi(cHandle);
00730         int iBack = FEUSB_GetDeviceList(deviceHandle);
00731         if (iBack < 0)
00732         {
00733                 QString errString = "";
00734                 char cErrorText[256] = {0};
00735                 FEUSB_GetErrorText(iBack, cErrorText);
00736                 errString.sprintf("Error: %d : %s", iBack, cErrorText);
00737                 ui.returnStatuslineEdit->setText(errString);
00738         }
00739         else
00740         {
00741                 sprintf(res, "Next Device Handle after %d is %d", deviceHandle, iBack); 
00742                 ui.returnStatuslineEdit->setText(res);
00743         }
00744         delete cHandle;
00745         
00746         buildScanList();
00747 }
00748 
00749 //------------------------------------------------------------------------------
00750 // Name: getDeviceHandleButtonClicked() 
00751 // Desc: gets the device handle of the selected device
00752 //------------------------------------------------------------------------------
00753 void FEUSBDemoDlg::getDeviceHandleButtonClicked()
00754 {
00755         QString str;
00756         int row;
00757         QTableWidgetItem *item;
00758         QString sID = "";
00759         DeviceIDDlg dlg(this);
00760         unsigned long dwDeviceID;
00761         char *tmp;
00762         char res[64] = {0};
00763         
00764         row = ui.scanTable->currentRow();
00765         item = ui.scanTable->item(row, 1);
00766         if (item)
00767                 sID = item->text();
00768         else
00769                 sID = "";
00770         
00771         // Initialize the DeviceIDDlg window with the ID of the current selection
00772         dlg.ui.deviceIDLineEdit->setText(sID);
00773         
00774         // Open the device
00775         if (dlg.exec() != QDialog::Accepted)
00776                 return;
00777         
00778         // Try to open the device ID
00779         sID = dlg.ui.deviceIDLineEdit->text();
00780         tmp = qstrdup(sID.toLatin1());
00781         sscanf((const char*)tmp, "%lu", &dwDeviceID);
00782         delete tmp;
00783         int iBack = FEUSB_GetDeviceHnd(dwDeviceID);
00784         if (iBack < 0)
00785         {
00786                 QString errString = "";
00787                 char cErrorText[256] = {0};
00788                 FEUSB_GetErrorText(iBack, cErrorText);
00789                 errString.sprintf("Error: %d : %s", iBack, cErrorText);
00790                 ui.returnStatuslineEdit->setText(errString);
00791         }
00792         else
00793         {
00794                 sprintf(res, "Device Handle for DeviceID %lu is %d", dwDeviceID, iBack); 
00795                 ui.returnStatuslineEdit->setText(res);
00796         }
00797         
00798         buildScanList();
00799 }
00800 
00801 //------------------------------------------------------------------------------
00802 // Name: static setGetClassInstance(FEUSBDemoDlg* classInstance)
00803 // Desc: set / get function to share a pointer of the class instance of the UI with the static callback.
00804 // Return: Null if new pointer is set, or a pointer to the instance if instance == NULL 
00805 //------------------------------------------------------------------------------
00806 FEUSBDemoDlg* FEUSBDemoDlg::setGetClassInstance(FEUSBDemoDlg* classInstance)
00807 {
00808         static FEUSBDemoDlg* statInstance = NULL;
00809         
00810         if (classInstance != NULL)
00811         {
00812                 statInstance = classInstance;
00813                 return NULL;
00814         }
00815         else
00816         {
00817                 return statInstance;
00818         }
00819 }
00820 
00821 //------------------------------------------------------------------------------
00822 // Name: connectEventHelper(int i, unsigned int k)
00823 // Desc: static helper function to emit a signal for connect event callback
00824 //------------------------------------------------------------------------------
00825 void FEUSBDemoDlg::connectEventHelper(int i, unsigned int k)
00826 {
00827         FEUSBDemoDlg* classInstance = setGetClassInstance(NULL);
00828         
00829         emit(classInstance->onConnect(i, k));
00830 }
00831 
00832 //------------------------------------------------------------------------------
00833 // Name: disconnectEventHelper(int i, unsigned int k)
00834 // Desc: static helper function to emit a signal for disconnect event callback
00835 //------------------------------------------------------------------------------
00836 void FEUSBDemoDlg::disconnectEventHelper(int i, unsigned int k)
00837 {
00838         FEUSBDemoDlg* classInstance = setGetClassInstance(NULL);
00839         
00840         emit(classInstance->onDisconnect(i, k));
00841 }
00842 
00843 //------------------------------------------------------------------------------
00844 // Name: connectEvent(int i, unsigned int k)
00845 // Desc: callback is triggered when a usb device is plugged in
00846 //------------------------------------------------------------------------------
00847 void FEUSBDemoDlg::connectEvent(int i, unsigned int k)
00848 {
00849         // You may wish to automatically open or close devices. Ths is the place to do it
00850         //FEUSB_ScanAndOpen(FEUSB_SCAN_ALL, NULL);
00851         buildScanList();
00852         char keks[256] = {0};
00853         sprintf(keks, "New device with serial number %ld (device handle %d) is connected", k, i);
00854         ui.returnStatuslineEdit->setText(keks);
00855         
00856 }
00857 
00858 //------------------------------------------------------------------------------
00859 // Name: disconnectEvent(int i, unsigned int k)
00860 // Desc: callback is triggered when a usb device is disconnected
00861 //------------------------------------------------------------------------------
00862 void FEUSBDemoDlg::disconnectEvent(int i, unsigned int k)
00863 {
00864         /*if(i > 0)
00865                 FEUSB_CloseDevice(i);
00866         else
00867                 ui.returnStatuslineEdit->setText("disconnect event: device handle = 0");
00868         */
00869         buildScanList();
00870         char keks[256] = {0};
00871         sprintf(keks, "Device with serial number %ld (device handle %d) was removed", k, i);
00872         ui.returnStatuslineEdit->setText(keks);
00873 }
00874 
00875 //------------------------------------------------------------------------------
00876 // Name: isDevicePresentButtonClicked() 
00877 // Desc: checks wether a device is present
00878 //------------------------------------------------------------------------------
00879 void FEUSBDemoDlg::isDevicePresentButtonClicked()
00880 {
00881         QString str;
00882         int row;
00883         QTableWidgetItem *item;
00884         QString sHandle = "";
00885         DeviceHandleDlg dlg(this);
00886         unsigned int deviceHandle;
00887         char *cHandle;
00888         int cnt;
00889         
00890         cnt = ui.scanTable->selectedItems().count();
00891         row = ui.scanTable->currentRow();
00892         item = ui.scanTable->item(row, 2);
00893         if (item)
00894                 sHandle = item->text();
00895         else
00896                 sHandle = "";
00897         
00898         // Initialize the DeviceHandleDlg window with the ID of the current selection
00899         dlg.ui.deviceHandleLineEdit->setText(sHandle);
00900         
00901         // Open the device
00902         if (dlg.exec() != QDialog::Accepted)
00903                 return;
00904         
00905         // Try to open the device ID
00906         sHandle = dlg.ui.deviceHandleLineEdit->text();
00907         cHandle = qstrdup(sHandle.toLatin1());
00908         deviceHandle = atoi(cHandle);
00909         int iBack = FEUSB_IsDevicePresent(deviceHandle);
00910         if (iBack > 0)
00911                 ui.returnStatuslineEdit->setText("Device is present");
00912         else if (iBack == 0)
00913                 ui.returnStatuslineEdit->setText("Device is not present");
00914         else
00915         {
00916                 QString errString = "";
00917                 char cErrorText[256] = {0};
00918                 FEUSB_GetErrorText(iBack, cErrorText);
00919                 errString.sprintf("Error: %d : %s", iBack, cErrorText);
00920                 ui.returnStatuslineEdit->setText(errString);
00921         }
00922         delete cHandle;
00923         
00924         buildScanList();
00925 }
00926 
00927 //------------------------------------------------------------------------------
00928 // Name: versionButtonClicked() 
00929 // Desc: displays the feusb version
00930 //------------------------------------------------------------------------------
00931 void FEUSBDemoDlg::versionButtonClicked()
00932 {
00933         char cVersion[100] = "";
00934         FEUSB_GetDLLVersion(cVersion);
00935         ui.returnStatuslineEdit->setText((char*)cVersion);
00936 }
00937 


maggie_rfid_drivers
Author(s): Raul Perula-Martinez
autogenerated on Mon Sep 14 2015 03:05:31