FEDetectReaderDlg.cpp
Go to the documentation of this file.
00001 /*-------------------------------------------------------
00002 |                                                       |
00003 |                  FEDetectReaderDlg.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                   :       11.12.2006
00018 
00019 Version                 :       01.00.00 / 11.01.2007 / Benjamin Stadin
00020 
00021 Operation Systems       :       Linux
00022 */
00023 
00024 #include "FEDetectReaderDlg.h"
00025 #include "InterfaceConfig.h"
00026 
00027 #include "../../../../feusb-lib/include/feusb.h"
00028 
00029 #include <stdio.h>
00030 
00031 
00032 //------------------------------------------------------------------------------
00033 // Name: FEDetectReaderDlg(QWidget *parent)
00034 // Desc: constructor
00035 //------------------------------------------------------------------------------
00036 FEDetectReaderDlg::FEDetectReaderDlg(QWidget *parent)
00037     : QDialog(parent)
00038 {
00039         ui.setupUi(this);
00040         
00041         // read configuration. The configuration is stored in file feconfig.dat
00042         config.readConfigFromFile();
00043       
00044         connect(ui.detectPushButton, SIGNAL(clicked()), this, SLOT(onDetectButton()));
00045         connect(ui.morePushButton, SIGNAL(clicked()), this, SLOT(onMoreButton()));
00046         connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(onOkButton()));
00047 
00048         // list available com ports
00049         QStringList comPorts;
00050         int count = 0;
00051         int iPorts[256];
00052         count = config.getComPorts(iPorts);     
00053         comPorts += "-";
00054         for (int i = 0; i < count; i++) {
00055                 char cPortNr[30] = {0};
00056                 sprintf(cPortNr, "%i", iPorts[i]);
00057                 comPorts += cPortNr;
00058         }  
00059         
00060         m_bReaderDetected = false;
00061         m_reader = NULL;
00062     
00063         ui.comPortComboBox->addItems(comPorts);
00064         
00065         // set values from config to ui
00066         if (config.getCommMode() == COM_DEVICE)
00067                 ui.comPortRadio->setChecked(true);
00068         else if (config.getCommMode() == TCP_DEVICE)
00069                 ui.tcpRadio->setChecked(true);
00070         else if (config.getCommMode() == USB_DEVICE)
00071                 ui.usbRadio->setChecked(true);
00072                 
00073         char sHost[32] = {0};
00074         strcpy(sHost, config.getHost());
00075         ui.ipAddressLineEdit->setText(sHost);
00076         
00077         int iIpPort = config.getIPPort();
00078         char sIpPort[16] = {0};
00079         sprintf(sIpPort, "%d", iIpPort);
00080         ui.ipPortLineEdit->setText(sIpPort);
00081         
00082         char sComPort[8] = {0};
00083         sprintf(sComPort, "%d", config.getComPortNumber());
00084         for (int i=0; i<ui.comPortComboBox->count(); i++)
00085         {
00086                 if (ui.comPortComboBox->itemText(i) == sComPort)
00087                 {
00088                         ui.comPortComboBox->setCurrentIndex(i);
00089                         break;
00090                 }
00091         }
00092 }
00093 
00094 //------------------------------------------------------------------------------
00095 // Name: ~FEDetectReaderDlg()
00096 // Desc: destructor
00097 //------------------------------------------------------------------------------
00098 FEDetectReaderDlg::~FEDetectReaderDlg()
00099 {
00100         //
00101 }
00102 
00103 //------------------------------------------------------------------------------
00104 // Name: setReader(FEDM_ISCReaderModule *reader) 
00105 // Desc: sets a pointer to a reader (set by main application to keep the detect class independent
00106 // from the main application)
00107 //------------------------------------------------------------------------------
00108 void FEDetectReaderDlg::setReader(FEDM_ISCReaderModule *reader) 
00109 {
00110         this->m_reader = reader;
00111 }
00112 
00113 //------------------------------------------------------------------------------
00114 // Name: getReader() 
00115 // Desc: convenience function to check if a reader has already been set
00116 //------------------------------------------------------------------------------
00117 FEDM_ISCReader *FEDetectReaderDlg::getReader() 
00118 {
00119         return m_reader;
00120 }
00121 
00122 //------------------------------------------------------------------------------
00123 // Name: onDetectButton() 
00124 // Desc: detect a reader when detect button is clicked
00125 //------------------------------------------------------------------------------
00126 void FEDetectReaderDlg::onDetectButton() 
00127 {
00128         int tmpCharTimeoutMpy = 8;
00129         int status;
00130         int baud;
00131         int error = 0;
00132         int iBack;
00133         char sErrorText[256];
00134         QString sMsg;
00135         char* sPort;
00136 
00137         m_bReaderDetected = false;
00138 
00139         ui.readerListWidget->clear();
00140         
00141         if (m_reader == NULL)
00142         {
00143                 QMessageBox::critical(this, tr("Error"), "No reader set", QMessageBox::Abort);  
00144                 return;
00145         }
00146 
00147         // close the previously used communication port
00148         if(m_reader->GetPortHnd() > 0) 
00149         {
00150                 m_reader->DisConnect();
00151         }
00152         
00153         // When switching between readers the current protocol might be wrong, so set standard protocol
00154         // and switch to advanced later if supported
00155         m_reader->SetProtocolFrameSupport(FEDM_PRT_FRAME_STANDARD);
00156 
00157         // now open the new selected port and try to detect the reader
00158         if (ui.comPortRadio->isChecked())
00159         {
00160                 config.setCommMode(COM_DEVICE);
00161                 // Set CharTimeoutMultiplier temporary to 8 to be sure to detect slow usb-serial devices
00162                 tmpCharTimeoutMpy = config.getCharTimeoutMpy();
00163                 config.setCharTimeoutMpy(8);
00164 
00165                 if (ui.comPortComboBox->currentText() == "-")
00166                         return;
00167                 
00168                 sPort = qstrdup(ui.comPortComboBox->currentText().toLatin1());
00169                 int iComPort;
00170                 sscanf(sPort, "%d", &iComPort); 
00171 
00172                 iBack = m_reader->ConnectCOMM(iComPort);
00173                 
00174                 delete [] sPort;
00175                 
00176                 if (iBack == FEDM_OK) 
00177                 {
00178                         char val[256] = {0};
00179 
00180                         config.setComPortConfiguration(m_reader->GetPortHnd());
00181                         
00182                         /* 
00183                                 NOTE: The Baud rate setting on the serial port settings form is overwritten 
00184                                 by FindBaudRate(). 
00185                                 On some Linux systems the baud rate detection doesn't work properly. 
00186                                 If FindBaudRate() doesn't work for you, comment out the block below. The 
00187                                 effective baud rate will then be the value you set on the serial port settings form.
00188                         */
00189                         
00190                         // BLOCK BEGIN
00191                         m_reader->FindBaudRate(); 
00192                         status = m_reader->GetPortPara("Baud", val);
00193                         if (status == 0) 
00194                         {
00195                                 baud = atoi(val);
00196                                 config.setBaud(baud);
00197                         }
00198 
00199                         status = m_reader->GetPortPara("Frame", val);
00200                         if (status == 0) 
00201                         {
00202                                 char frm[8] = {0};
00203                                 strcpy(frm, val);
00204                                 config.setFrame(frm);
00205                         }
00206                         // BLOCK END
00207                         
00208                 }
00209                 else 
00210                 {
00211                         return;
00212                 }
00213 
00214         }
00215         else if (ui.usbRadio->isChecked())
00216         {       
00217                 config.setCommMode(USB_DEVICE);
00218                 FEUSB_Scan(FEUSB_SCAN_ALL, NULL);
00219                 if (FEUSB_GetScanListSize() > 1) 
00220                 {
00221                         QMessageBox::critical(this, tr("Error"),
00222                                         "More than one USB-Reader detected.\nPlease connect only 1 USB-Reader!",
00223                                         QMessageBox::Abort);
00224                         return;
00225                 }
00226 
00227                 if (FEUSB_GetScanListSize() == 1) 
00228                 {
00229                         iBack = m_reader->ConnectUSB(0);
00230                         if (iBack != FEDM_OK) 
00231                         {
00232                                 m_reader->GetErrorText(sErrorText, iBack);
00233                                 QMessageBox::critical(this, tr("Error"), sErrorText,
00234                                                                 QMessageBox::Abort);
00235                                 return;
00236                         }
00237                 }
00238                 else
00239                 {
00240                         return;
00241                 }
00242 
00243         }
00244         else if (ui.tcpRadio->isChecked())
00245         {       
00246                 char* sHost;
00247                 char* sIpPort;
00248                 
00249                 config.setCommMode(TCP_DEVICE);
00250                 
00251                 sHost = qstrdup(ui.ipAddressLineEdit->text().toLatin1());
00252                 sIpPort = qstrdup(ui.ipPortLineEdit->text().toLatin1());
00253                 int iIpPort = atoi(sIpPort);
00254                 
00255                 int iBack = m_reader->ConnectTCP(sHost, iIpPort);
00256                 
00257                 delete [] sHost;
00258                 delete [] sIpPort;
00259                 
00260                 if(iBack != FEDM_OK)
00261                 {
00262                         m_reader->GetErrorText(sErrorText, iBack);
00263                         //m_reader->SetPortHnd(0);
00264                         sMsg.sprintf("%d: %s", iBack, sErrorText);
00265                         QMessageBox::critical(this, tr("QMessageBox::critical()"), sMsg, QMessageBox::Abort);
00266                 }
00267         }
00268                 
00269         // try to get the readers type info
00270         if (m_reader->GetPortHnd() > 0)
00271         {
00272                 error = m_reader->SendProtocol(0x65);
00273                 if (error == FEDM_OK)
00274                 {                       
00275                         QString sReaderName = m_reader->GetReaderName();
00276                         ui.readerListWidget->addItem(sReaderName);
00277                         switch(m_reader->GetReaderType())
00278                         {
00279                                 case FEDM_ISC_TYPE_ISCLRU1000:
00280                                 case FEDM_ISC_TYPE_ISCLRU2000:
00281                                         m_reader->SetProtocolFrameSupport(FEDM_PRT_FRAME_ADVANCED);
00282                                         break;
00283                                 default:
00284                                         m_reader->SetProtocolFrameSupport(FEDM_PRT_FRAME_STANDARD);
00285                                         break;
00286                         }
00287                         m_bReaderDetected = true;
00288                 }
00289                 else
00290                 {
00291                         m_reader->GetErrorText(sErrorText, error);
00292                         QMessageBox::critical(this, tr("Error"), sErrorText, QMessageBox::Abort);
00293                 }
00294         }
00295 
00296         // A char timeout multiplier of 8 is used to detect all devices. 
00297         // Set it back to config value again
00298         if (config.getCommMode() == COM_DEVICE && m_reader->GetPortHnd() > 0)
00299         {
00300                 config.setCharTimeoutMpy(tmpCharTimeoutMpy);
00301                 config.setComPortConfiguration(m_reader->GetPortHnd());
00302         }
00303 }
00304 
00305 //------------------------------------------------------------------------------
00306 // Name: isReaderDetected() 
00307 // Desc: returns true if a reader was detected by the detect function
00308 //------------------------------------------------------------------------------
00309 bool FEDetectReaderDlg::isReaderDetected()
00310 {
00311         return m_bReaderDetected;
00312 }
00313 
00314 //------------------------------------------------------------------------------
00315 // Name: FEDetectReaderDlg() 
00316 // Desc: further configuration parameters for serial port
00317 //------------------------------------------------------------------------------
00318 void FEDetectReaderDlg::onMoreButton() 
00319 {
00320         // set com port settings in extended settings dialog (more button)
00321         char cTimeout[16] = {0};
00322         sprintf(cTimeout, "%d", config.getTimeout());
00323         char cDelayTime[16] = {0};
00324         sprintf(cDelayTime, "%d", config.getTxDelayTime());
00325         char cCharTimeout[16] = {0};
00326         sprintf(cCharTimeout, "%d", config.getCharTimeoutMpy());
00327         serialPortSettingsDlg.ui.timeoutLineEdit->setText(cTimeout);
00328         serialPortSettingsDlg.ui.delayTimeLineEdit->setText(cDelayTime);
00329         serialPortSettingsDlg.ui.charTimeoutLineEdit->setText(cCharTimeout);
00330         serialPortSettingsDlg.ui.txTimeCheckBox->setChecked(config.getTxTimeControl());
00331         serialPortSettingsDlg.ui.rtsCheckBox->setChecked(config.getRTS());
00332         serialPortSettingsDlg.ui.dtrCheckBox->setChecked(config.getDTR());
00333         // set baud rate combo bx to selected 
00334         char cBaud[16] = {0};
00335         bool found = false;
00336         sprintf(cBaud, "%d", config.getBaud());
00337         for (int i=0; i<serialPortSettingsDlg.ui.baudRateComboBox->count(); i++)
00338         {
00339                 if (ui.comPortComboBox->itemText(i) == cBaud)
00340                 {
00341                         serialPortSettingsDlg.ui.baudRateComboBox->setCurrentIndex(i);
00342                         found = true;
00343                         break;
00344                 }
00345         }
00346         if (!found)
00347         {
00348                 serialPortSettingsDlg.ui.baudRateComboBox->setCurrentIndex(0);
00349                 serialPortSettingsDlg.ui.baudRateComboBox->setItemText(0, cBaud);
00350         }
00351 
00352         // show serial port settings window
00353         if (serialPortSettingsDlg.exec() != QDialog::Accepted)
00354                 return;
00355                 
00356         char* sTimeout = qstrdup(serialPortSettingsDlg.ui.timeoutLineEdit->text().toLatin1());
00357         char* sTxDelayTime = qstrdup(serialPortSettingsDlg.ui.delayTimeLineEdit->text().toLatin1());
00358         char* sCharTimeout = qstrdup(serialPortSettingsDlg.ui.charTimeoutLineEdit->text().toLatin1());
00359         int iIndex = serialPortSettingsDlg.ui.baudRateComboBox->currentIndex();
00360         char* sBaud = qstrdup(serialPortSettingsDlg.ui.baudRateComboBox->itemText(iIndex).toLatin1());
00361         bool bTxTimeCtl = serialPortSettingsDlg.ui.txTimeCheckBox->isChecked();
00362         bool bRts = serialPortSettingsDlg.ui.rtsCheckBox->isChecked();
00363         bool bDtr = serialPortSettingsDlg.ui.dtrCheckBox->isChecked();
00364         
00365         config.setTimeout(atoi(sTimeout));
00366         config.setTxDelayTime(atoi(sTxDelayTime));
00367         config.setCharTimeoutMpy(atoi(sCharTimeout));
00368         config.setBaud(atoi(sBaud));
00369         config.setTxTimeControl(bTxTimeCtl);
00370         config.setRTS(bRts);
00371         config.setDTR(bDtr);
00372         
00373         if (m_reader->GetPortHnd() > 0)
00374                 config.setComPortConfiguration(m_reader->GetPortHnd());
00375         
00376         delete [] sTimeout;
00377         delete [] sTxDelayTime;
00378         delete [] sCharTimeout;
00379         delete [] sBaud;
00380 }
00381 
00382 //------------------------------------------------------------------------------
00383 // Name: onOkButton() 
00384 // Desc: saves the configuration when ok button is clicked
00385 //------------------------------------------------------------------------------
00386 void FEDetectReaderDlg::onOkButton() 
00387 {
00388         if (ui.comPortRadio->isChecked())
00389                 config.setCommMode(COM_DEVICE);
00390         else if (ui.usbRadio->isChecked())
00391                 config.setCommMode(USB_DEVICE);
00392         else if (ui.tcpRadio->isChecked())
00393                 config.setCommMode(TCP_DEVICE);
00394                 
00395         char* tmp = qstrdup(ui.ipAddressLineEdit->text().toLatin1());
00396         config.setHost(tmp);
00397         delete [] tmp;
00398         
00399         tmp = qstrdup(ui.ipPortLineEdit->text().toLatin1());
00400         int ipPort = atoi(tmp);
00401         config.setIPPort(ipPort);
00402         delete [] tmp;
00403         
00404         QString qsComPort = ui.comPortComboBox->currentText();
00405         if (qsComPort != "-")
00406         {
00407                 tmp = qstrdup(qsComPort.toLatin1());
00408                 int iComPort = atoi(tmp);
00409                 config.setComPortNumber(iComPort);
00410                 delete [] tmp;
00411         }
00412         
00413         config.writeConfigToFile();
00414 }


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