00001 /*------------------------------------------------------- 00002 | | 00003 | FEReadTagsThread.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 / 13.12.2006 / Benjamin Stadin 00020 00021 Operation Systems : Linux 00022 */ 00023 00024 #include "FEReadTagsThread.h" 00025 #include "../../../../fedm-classlib/src/FedmIscCore.h" 00026 #include "../../../../feisc-lib/include/feisc.h" 00027 00028 //------------------------------------------------------------------------------ 00029 // Name: readTags() 00030 // Desc: starts a thread or triggers a signal to wake the thread up again 00031 //------------------------------------------------------------------------------ 00032 void FEReadTagsThread::readTags() 00033 { 00034 QMutexLocker locker(&m_mutex); 00035 00036 if (!isRunning()) 00037 { 00038 m_bWait = false; 00039 start(LowPriority); 00040 } 00041 else 00042 { 00043 m_condition.wakeOne(); 00044 } 00045 } 00046 00047 //------------------------------------------------------------------------------ 00048 // Name: FEReadTagsThread() 00049 // Desc: constructor 00050 //------------------------------------------------------------------------------ 00051 FEReadTagsThread::FEReadTagsThread(FEDM_ISCReader* reader, QObject *parent) 00052 : QThread(parent) 00053 { 00054 m_bFinish = false; 00055 m_iscReader = reader; 00056 m_bWait = m_bWait = true;; 00057 } 00058 00059 //------------------------------------------------------------------------------ 00060 // Name: ~FEReadTagsThread() 00061 // Desc: destructor 00062 //------------------------------------------------------------------------------ 00063 FEReadTagsThread::~FEReadTagsThread() 00064 { 00065 m_mutex.lock(); 00066 m_bWait = false; 00067 m_bFinish = true; 00068 m_condition.wakeOne(); 00069 m_mutex.unlock(); 00070 00071 wait(); 00072 } 00073 00074 //------------------------------------------------------------------------------ 00075 // Name: pauseThread() 00076 // Desc: sets a signal to pause the loop in run() 00077 //------------------------------------------------------------------------------ 00078 void FEReadTagsThread::pauseThread() 00079 { 00080 m_mutex.lock(); 00081 m_bWait = true; 00082 m_mutex.unlock(); 00083 } 00084 00085 //------------------------------------------------------------------------------ 00086 // Name: running() 00087 // Desc: returns false if the loop in run() is waiting for a signal to continue, 00088 // or if the thread isnt started yet 00089 //------------------------------------------------------------------------------ 00090 bool FEReadTagsThread::running() 00091 { 00092 return (!m_bWait && isRunning()); 00093 } 00094 00095 //------------------------------------------------------------------------------ 00096 // Name: run() 00097 // Desc: thread function to read tags. Triggers signals (emit xy) that can be catched 00098 // by the main application 00099 //------------------------------------------------------------------------------ 00100 void FEReadTagsThread::run() 00101 { 00102 int iBack; 00103 int ret = 1; 00104 00105 iBack = m_iscReader->SendProtocol(0x65); 00106 if (iBack < 0) 00107 { 00108 m_mutex.lock(); 00109 emit onError(ret); 00110 m_condition.wait(&m_mutex); 00111 m_mutex.unlock(); 00112 } 00113 00114 while(true) 00115 { 00116 if (m_bFinish) 00117 return; 00118 00119 switch(unsigned char t = m_iscReader->GetReaderType()) 00120 { 00121 case FEDM_ISC_TYPE_ISCLR200: 00122 m_iscReader->SetData(FEDM_ISCLR_TMP_BRM_SETS, (unsigned char)255); 00123 ret = m_iscReader->SendProtocol(0x21); 00124 if (ret < 0) 00125 { 00126 m_mutex.lock(); 00127 emit onError(ret); 00128 m_condition.wait(&m_mutex); 00129 m_mutex.unlock(); 00130 } 00131 break; 00132 default: 00133 m_iscReader->SetData(FEDM_ISC_TMP_ADV_BRM_SETS, (UINT)255); 00134 ret = m_iscReader->SendProtocol(0x22); 00135 if (ret < 0) 00136 { 00137 m_mutex.lock(); 00138 emit onError(ret); 00139 m_condition.wait(&m_mutex); 00140 m_mutex.unlock(); 00141 } 00142 break; 00143 } 00144 00145 if (m_iscReader->GetTableLength(FEDM_ISC_BRM_TABLE) > 0) 00146 { 00147 m_mutex.lock(); 00148 emit onNewData(ret); 00149 m_condition.wait(&m_mutex); 00150 m_mutex.unlock(); 00151 00152 // clear data buffer 00153 m_iscReader->SendProtocol(0x32); 00154 } 00155 00156 // pause 00157 m_mutex.lock(); 00158 if (m_bWait) 00159 m_condition.wait(&m_mutex); 00160 m_bWait = false; 00161 m_mutex.unlock(); 00162 00163 msleep(50); 00164 00165 } 00166 } 00167