qCNC 0.82-alpha
/home/trilog/Desktop/project/gui/Final/src/serial_tools.cpp
Go to the documentation of this file.
00001 
00042 #include "serial_tools.h"
00043 #include "global_includes.h"
00044 
00045 
00046 
00058 SerialPortReader::SerialPortReader( QObject * parent, QString *port ) : QThread( parent ) {
00059    // Use of QextSerialPort proprietary library
00060    COM_Port = new QextSerialPort(*port);
00061 
00062    if ( COM_Port->open( QIODevice::ReadOnly | QIODevice::Unbuffered ) ) {
00063       COM_Port->setBaudRate(BAUD9600);
00064       COM_Port->setFlowControl(FLOW_OFF);
00065       COM_Port->setParity(PAR_NONE);
00066       COM_Port->setDataBits(DATA_8);
00067       COM_Port->setStopBits(STOP_1);
00068       qDebug() << QString("Connected.");
00069    }
00070    else {
00071       qDebug() << QString("Connection failed!");
00072    }
00073 }
00074 
00085 SerialPortReader::~SerialPortReader()
00086 {
00087    COM_Port->close();
00088    delete COM_Port;
00089 }
00090 
00101 bool SerialPortReader::isOpen()
00102 {
00103    return COM_Port->isOpen();
00104 }
00105 
00106 // Run function is launched automaticaly with
00107 // ->start() method of QThread
00118 void SerialPortReader::run() {
00119    char data[256];
00120    str = new QString;
00121    int bytesReaded;
00122    int sgnVal;
00123 
00124    while( 1 )
00125    {
00126       // Read serial buffer by 5 bytes
00127       // (3 numbers + '\r' + '\n')
00128       bytesReaded = COM_Port->read( &data[0], 255 );
00129       data[256] = '\0';
00130       *str = data;
00131       sgnVal = str->toInt();
00132       emit serialSignal( &sgnVal );
00133    }
00134 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines