Ejemplo n.º 1
0
MainWindow::MainWindow(QIODevice *ioDev, QWidget *parent) :
        QMainWindow(parent),
        ioDev(ioDev),
        ui(new Ui::MainWindow)
{
    this->setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
    ui->setupUi(this);
    connect(ioDev, SIGNAL(readyRead()), this, SLOT(onReadyRead()));

    //edited
    AbstractSerial *tempDev = (AbstractSerial *) ioDev;
    tempDev->setDtr(false);
    tempDev->setRts(true);

    save="";
    readFormatedTimer = new QTimer(this);
    readFormatedTimer->start(1000);
    connect(readFormatedTimer,SIGNAL(timeout()),this,SLOT(checkData()));
    ///

    readWaitTimer = new QTimer(this);
    connect(readWaitTimer, SIGNAL(timeout()), this, SLOT(onReadyRead()));

    onReadyRead();
}
Ejemplo n.º 2
0
//edited
void MainWindow::sendData(QString data) {
    AbstractSerial *tempDev = (AbstractSerial *) ioDev;
    tempDev->setDtr(true);
    tempDev->setRts(false);
    if(ioDev->write(data.toAscii()) == -1) {
         statusBar()->showMessage(tr("Can't send data!"));
         printChat(System, "Can't send data :o");
     } else {
         printChat(Send, data);
     }
    tempDev->setDtr(false);
    tempDev->setRts(true);
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    /* 1. First - create an instance of an object.
    */
    AbstractSerial *port = new AbstractSerial();

    char dn[50]; //device name
    cout << "Please enter serial device name, specific by OS, \n example: in Windows -> COMn, in GNU/Linux -> /dev/ttyXYZn: ";
    cin >> dn;

    /* 2. Second - set the device name.
    */
    port->setDeviceName(dn);

    /* 3. Third - open the device.
    
        Here using the open flag "Unbuffered". 
        This flag disables the internal buffer class, 
        and also disables the automatic data acquisition (disables asynchronous mode).
        In this case, we have disabled the asynchronous mode to read from port data using timeouts on the packet.
        Ie if we call, for example, read(5) and in buffer UART 
        not yet available - then the method will wait for a time (total read timeout and/or char interval timeout) until the data arrive.
        
        Note: Behavior would be different if you open a port without a flag "Unbuffered". 
              I will not describe it - test/check it yourself. ;)
    */
    if (port->open(AbstractSerial::ReadOnly | AbstractSerial::Unbuffered)) {
        qDebug() << "Serial device " << port->deviceName() << " open in " << port->openMode();

        //Here, the default current parameters (for example)
        qDebug() << "= Default parameters =";
        qDebug() << "Device name            : " << port->deviceName();
        qDebug() << "Baud rate              : " << port->baudRate();
        qDebug() << "Data bits              : " << port->dataBits();
        qDebug() << "Parity                 : " << port->parity();
        qDebug() << "Stop bits              : " << port->stopBits();
        qDebug() << "Flow                   : " << port->flowControl();
        qDebug() << "Total read timeout constant, msec : " << port->totalReadConstantTimeout();
        qDebug() << "Char interval timeout, usec       : " << port->charIntervalTimeout();

        /* 4. Fourth - now you can set the parameters. (after successfully opened port)
        */

        //Here example set baud rate 115200 bit/sec (baud)
        if (!port->setBaudRate(AbstractSerial::BaudRate115200)) {
            qDebug() << "Set baud rate " <<  AbstractSerial::BaudRate115200 << " error.";
            goto label;
        };

        if (!port->setDataBits(AbstractSerial::DataBits8)) {
            qDebug() << "Set data bits " <<  AbstractSerial::DataBits8 << " error.";
            goto label;
        }

        if (!port->setParity(AbstractSerial::ParityNone)) {
            qDebug() << "Set parity " <<  AbstractSerial::ParityNone << " error.";
            goto label;
        }

        if (!port->setStopBits(AbstractSerial::StopBits1)) {
            qDebug() << "Set stop bits " <<  AbstractSerial::StopBits1 << " error.";
            goto label;
        }

        if (!port->setFlowControl(AbstractSerial::FlowControlOff)) {
            qDebug() << "Set flow " <<  AbstractSerial::FlowControlOff << " error.";
            goto label;
        }

        /*
            ...
            here you can set other parameters.
            ...
        */

        /*
        Important Note:

        1. For All OS:
        If you use buffered mode (ie, at the opening did not put the flag AbstractSerial::Unbuffered),
        there is no need to set timeouts reading (Ie they are to remain the default = 0)/
        Any value other than 0 will only slow down data acquisition.

        2. For Windows:
        If you are using unbuffered mode, the timeouts have the effect of reading!
        Necessary for the total timeout to set the value of reading At least 1 ms,
        or (at 0) will not be read.

        PS: I have not figured out yet what the reason.

        3. For *.nix:
        If you are using unbuffered mode, the timeouts have the effect of reading!
        Necessary for the total timeout to set the value of reading At least 1 ms,
        as if the value is 0 read will return immediately,
        so you can not read the requested number of bytes (ie, reading function can return fewer bytes).

        In any case, experiment with options for treatment with buffered/unbuffered,
        as well as the timeout values from 0 to N and find the differences. :)
        */

        // Here set total timeout for read 1 ms, if open mode is Unbuffered only!
#if defined (Q_OS_UNIX)
        // Method setTotalReadConstantTimeout() not supported in *.nix.
        if (port->openMode() & AbstractSerial::Unbuffered)
            port->setCharIntervalTimeout(5000);//5 msec
#elif defined (Q_OS_WIN)
        if (port->openMode() & AbstractSerial::Unbuffered)
            port->setTotalReadConstantTimeout(100);
#endif

        //Here, the new set parameters (for example)
        qDebug() << "= New parameters =";
        qDebug() << "Device name            : " << port->deviceName();
        qDebug() << "Baud rate              : " << port->baudRate();
        qDebug() << "Data bits              : " << port->dataBits();
        qDebug() << "Parity                 : " << port->parity();
        qDebug() << "Stop bits              : " << port->stopBits();
        qDebug() << "Flow                   : " << port->flowControl();
        qDebug() << "Total read timeout constant, msec : " << port->totalReadConstantTimeout();
        qDebug() << "Char interval timeout, usec       : " << port->charIntervalTimeout();

        int rrto = 0; //timeout for ready read
        cout << "Please enter wait timeout for ready read, msec : ";
        cin >> rrto;

        int len = 0; //len data for read
        cout << "Please enter len data for read, bytes : ";
        cin >> len;

        qDebug() << "Enter is " << rrto << " msecs, " << len << " bytes";
        qDebug() << "Starting waiting ready read in time : " << QTime::currentTime();

        QByteArray ba; //received data

        /* 5. Fifth - you can now read / write device, or further modify its settings, etc.
        */
        while (1) {
            if ((port->bytesAvailable() > 0) ||  port->waitForReadyRead(rrto)) {
                ba.clear();
                ba = port->read(len);
                qDebug() << "Readed is : " << ba.size() << " bytes";
            }
            else {
                qDebug() << "Timeout read data in time : " << QTime::currentTime();
            }
        }//while
    }
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    AbstractSerial *MyDevice = new AbstractSerial();

    if (!MyDevice) {
        qDebug() << "Create object is fail!";
        return 0;
    }

    char dn[50]; //device name
    cout << "Please enter serial device name, specific by OS, \n example: in Windows -> COMn, in GNU/Linux -> /dev/ttyXYZn: ";
    cin >> dn;

    MyDevice->setDeviceName(dn);

    /*!
        \warning
        \~english To annex "correctly" to work - need to open the device with the flag Unbuffered!
        \~russian Чтобы приложение "корректно" работало - необходимо открывать устройство с флагом Unbuffered!
    */
    if (MyDevice->open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
        qDebug() << "Serial device " << MyDevice->deviceName() << " open in " << MyDevice->openMode();

        qDebug() << "= Defaults parameters =";
        qDebug() << "Device name            : " << MyDevice->deviceName();
        qDebug() << "Baud rate              : " << MyDevice->baudRate();
        qDebug() << "Data bits              : " << MyDevice->dataBits();
        qDebug() << "Parity                 : " << MyDevice->parity();
        qDebug() << "Stop bits              : " << MyDevice->stopBits();
        qDebug() << "Flow                   : " << MyDevice->flowControl();
        qDebug() << "Char timeout, msec     : " << MyDevice->charIntervalTimeout();

        int rrto = 0; //timeout for ready read
        cout << "Please enter wait timeout for ready read, msec : ";
        cin >> rrto;

        int len = 0; //len data for read
        cout << "Please enter len data for read, bytes : ";
        cin >> len;

        qDebug() << "Enter is " << rrto << " msecs, " << len << " bytes";

        qDebug() << "Starting waiting ready read in time : " << QTime::currentTime();

        QByteArray ba; //received data

        while (1) {
            if (MyDevice->waitForReadyRead(rrto)) {
                ba.clear();
                ba = MyDevice->read(len);

                cout << "Rx : \n";
                printDataToHex(ba);

                qDebug() << " \n Readed is : " << ba.size() << " bytes";
            }
            else {
                qDebug() << "Timeout read data in time : " << QTime::currentTime();
            }
        }//while
    }