QlightRead package | ESS final project 1.22-beta
|
00001 00042 #include <qextserialport.h> 00043 #include <QDebug> 00044 00045 #include "serial.h" 00046 00047 00059 SerialPortReader::SerialPortReader( QObject * parent ) 00060 : QThread( parent ) 00061 { 00062 // Use of QextSerialPort proprietary library 00063 COM_Port = new QextSerialPort( "COM3" ); 00064 00065 if ( COM_Port->open( QIODevice::ReadOnly | QIODevice::Unbuffered ) ) 00066 { 00067 COM_Port->setBaudRate(BAUD9600); 00068 COM_Port->setFlowControl(FLOW_OFF); 00069 COM_Port->setParity(PAR_NONE); 00070 COM_Port->setDataBits(DATA_8); 00071 COM_Port->setStopBits(STOP_1); 00072 qDebug() << QString("Connected."); 00073 } else { 00074 qDebug() << QString("Connection failed!"); 00075 } 00076 } 00077 00088 SerialPortReader::~SerialPortReader() 00089 { 00090 COM_Port->close(); 00091 delete COM_Port; 00092 } 00093 00104 bool SerialPortReader::isOpen() 00105 { 00106 return COM_Port->isOpen(); 00107 } 00108 00109 // Run function is launched automaticaly with 00110 // ->start() method of QThread 00121 void SerialPortReader::run() 00122 { 00123 char data[6]; 00124 QString* str = new QString; 00125 int bytesReaded; 00126 int sgnVal; 00127 00128 while( 1 ) 00129 { 00130 // Read serial buffer by 5 bytes 00131 // (3 numbers + '\r' + '\n') 00132 bytesReaded = COM_Port->read( &data[0], 5 ); 00133 data[5] = '\0'; 00134 *str = data; 00135 sgnVal = str->toInt(); 00136 emit serialSignal( &sgnVal ); 00137 } 00138 }