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


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