# xbinterface.cpp

Go to the documentation of this file.

    00001 /*
    00002  * SmartHome server: Plug-in xbee
    00003  *
    00004  * Copyright (C) 2010 Daniel Pisi <dan.pisi@gmail.com>
    00005  *
    00006  * This program is free software; you can redistribute it and/or modify it
    00007  * under the terms of version 2 of the GNU General Public License
    00008  * as published by the Free Software Foundation.
    00009  *
    00010  */
    00011
    00018 #include "xbinterface.h"
    00019
    00020 // -----------------------------------------------------------------------------
    00021
    00022 XbInterface::XbInterface(QString param, QObject *parent) : QObject(parent)
    00023 {
    00024     mState = 0;
    00025     mSize  = 0;
    00026     mIndex = 0;
    00027
    00028     mXbeePort = new QextSerialPort(param, QextSerialPort::EventDriven);
    00029     mXbeePort->setBaudRate(BAUD9600);
    00030     mXbeePort->setFlowControl(FLOW_OFF);
    00031     mXbeePort->setDataBits(DATA_8);
    00032     mXbeePort->setParity(PAR_NONE);
    00033     mXbeePort->setStopBits(STOP_1);
    00034
    00035     if (mXbeePort->open(QIODevice::ReadWrite) == true)
    00036     {
    00037         connect(mXbeePort, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    00038     }
    00039     else
    00040         qWarning(QString("Opening the XBee device failed: %1").arg(mXbeePort->errorString()).toLatin1());
    00041 }
    00042 // -----------------------------------------------------------------------------
    00043
    00044 void XbInterface::onReadyRead()
    00045 {
    00046     char b;
    00047
    00048     while (mXbeePort->bytesAvailable())
    00049     {
    00050         mXbeePort->read(&b, 1);
    00051
    00052         switch (mState)
    00053         {
    00054         case 0:
    00055             // start byte
    00056             if (b == 0x7E)
    00057             {
    00058                 mBuffer[mIndex++] = b;
    00059                 mState = 1;
    00060             }
    00061             break;
    00062         case 1:
    00063             // packet size MSB
    00064             mBuffer[mIndex++] = b;
    00065             mSize = b * 256;
    00066             mState = 2;
    00067             break;
    00068         case 2:
    00069             // packet size LSB
    00070             mBuffer[mIndex++] = b;
    00071             mSize += b;
    00072             mState = 3;
    00073             break;
    00074         case 3:
    00075             // packet data
    00076             mBuffer[mIndex++] = b;
    00077             mSize--;
    00078
    00079             if (mSize == 0)
    00080                 mState = 4;
    00081             break;
    00082         case 4:
    00083             // checksum
    00084             mBuffer[mIndex++] = b;
    00085             QByteArray data(mBuffer, mIndex);
    00086
    00087             if (XbPacket::valid(data))
    00088                 emit newPacketReceived(data);
    00089
    00090             mState = 0;
    00091             mIndex = 0;
    00092             break;
    00093         }
    00094     }
    00095 }
    00096 // -----------------------------------------------------------------------------
    00097
    00098 void XbInterface::sendPacket(XbPacket &packet)
    00099 {
    00100     mXbeePort->write(packet.frameData());
    00101 }
    00102 // -----------------------------------------------------------------------------

All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends