Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QextSerialPort, dTxfrUSB and microcontroller Arduino - read from device



  • Hi all,
    I would like to design GUI for microcontroller Arduino in Qt Creator. So I have looked at internet and found aQtLow (https://code.google.com/p/aqtlow/)
    which has its own communication library dTxfrUSB for Arduino.
    I wanted to use this for my project but I don't understand how to use the dTxfrUSB in Qt GUI. I have implemented the library into code for microcontroller (Arduino) and now - how to read the data microcontroller sends to me per USB in my Qt GUI?
    This is the dTxfrUSB code:
    @/*
    dTxfrUSB - an Arduino library for a communicating with Qt.
    Copyright (C) 2011 Dee Wykoff

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    

    */

    #include "dTxfrUSB.h"

    dTxfrUSB::dTxfrUSB()
    {
    }

    void dTxfrUSB::Run()
    {
    /*
    Incoming messages from PC
    Byte Meaning
    0 Hi byte of function code
    1 Low byte
    2 Hi byte of destination (if a write command)
    3 Low byte
    4 Hi byte of data (if a write command)
    5 Low byte
    6 Check byte
    */
    uint8_t ByteArray[255];
    int16_t ArrayPosition = 0;
    uint8_t Check = 0x55;
    if(Serial.available()>=7)
    {
    while(Serial.available())
    {
    ByteArray[ArrayPosition] = Serial.read();
    ArrayPosition++;
    }
    for(int i = 0; i < 6; i++)
    {
    Check = Check ^ ByteArray[i];
    }
    if(Check == ByteArray[6])
    {
    SetFC(word(ByteArray[0],ByteArray[1]));
    if(!Active)
    {
    Active = true;
    }
    PreviousActivityTime = millis();
    }
    }
    if(millis() > (PreviousActivityTime + 60000))
    {
    if(Active)
    {
    Active = false;
    }
    }

    //****************** Send Data ***********************
    if(FC == DT_FC_REQUEST_DATA)
    {
    Send();
    FC = DT_FC_NONE;
    }

    //****************** Write Coil **********************
    if(FC == DT_FC_WRITE_COIL)
    {
    C[word(ByteArray[2],ByteArray[3])] = word(ByteArray[4],ByteArray[5]) > 0;
    FC = DT_FC_NONE;
    }

    //****************** Write Register ******************
    if(FC == DT_FC_WRITE_REGISTER)
    {
    R[word(ByteArray[2],ByteArray[3])] = word(ByteArray[4],ByteArray[5]);
    FC = DT_FC_NONE;
    }
    }

    void dTxfrUSB::Send()
    {
    /*
    Outgoing message to PC
    Byte Meaning
    0 Hi byte of R[0]
    1 Low byte
    ...
    190 Hi byte of R[95]
    191 Low byte
    192 C[0-7]
    203 C[88-95]
    204 Check byte
    */
    uint8_t ByteArray[255];
    uint8_t Check = 0x55;
    int16_t ArrayPosition = 0;
    for(int i = 0; i < DT_N_R; i++)
    {
    ByteArray[ArrayPosition] = highByte(R[i]);
    Check = Check ^ ByteArray[ArrayPosition];
    ArrayPosition++;
    ByteArray[ArrayPosition] = lowByte(R[i]);
    Check = Check ^ ByteArray[ArrayPosition];
    ArrayPosition++;
    }
    uint8_t Byte;
    for(int i = 0; i < (DT_N_C/8); i++)
    {
    Byte = 0;
    for(int j = 0; j < 8; j++)
    {

      Byte |= (C[i*8+j] << j);
    }
    ByteArray[ArrayPosition] = Byte;
    Check = Check ^ ByteArray[ArrayPosition];
    ArrayPosition++;
    

    }
    ByteArray[ArrayPosition] = Check;
    ArrayPosition++;
    Serial.write(ByteArray, ArrayPosition);
    }

    void dTxfrUSB::SetFC(int fc)
    {
    /*
    Function codes
    Code Meaning
    1 Gimme data
    5 Write coil C
    6 Write register R
    */
    switch(fc)
    {
    case 1:
    FC = DT_FC_REQUEST_DATA;
    break;
    case 5:
    FC = DT_FC_WRITE_COIL;
    break;
    case 6:
    FC = DT_FC_WRITE_REGISTER;
    break;
    default:
    FC = DT_FC_NONE;
    break;
    }
    }@

    I am using QextSerialPort, so far I have:

    @port = new QextSerialPort(schnittstelle);

    port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
    if(!port->isOpen())
    {
        QMessageBox::warning(this, "port error", "Schnittstelle "+schnittstelle+" reagiert nicht.");
    }
    
    port->setBaudRate(BAUD9600);
    port->setFlowControl(FLOW_OFF);
    port->setParity(PAR_NONE);
    port->setDataBits(DATA_8);
    port->setStopBits(STOP_1);@
    

    and now I would like to read the coming information from Arduino. In Arduino code, I have variable dT.R[ 0 ] - dT.R[ 6 ] and dT.C[ 0 ] - dT.C[ 6 ] and I would like to use them in my Qt GUI as actual values (e.g. voltage on the device, it must be actual).
    The dT.Run(); function in Arduino is called every loop so the values of dT.R[i] and dT.C[i] should be sent to me in Mhz takt. Will they appear in console as well? It is a pity I have forgotten my Arduino at work so I can't experiment with it until Monday ;) Only to write the code.
    How can I read the dT.R and dT.C please in Qt Creator? I will create for them widgets where they appear then.
    Many thanks.


Log in to reply