Serial port



  • Hi guys, I'm apologize for my English, I hope explain well my problem.. I should use C++ to activate the serial port of a microcontrollor which is located on a rover.., and I should be able to communicate with it, sending information and receiving. But I've no idea to start.. Can you help to solve my problem, giving me some advices? Thank you very much



  • Hi JoeMatt! Welcome to DevNet!
    Take a look to a "Qt Serial Port":http://qt-project.org/wiki/QtSerialPort
    I tryed use different libs but this one fully satisfied me.



  • Thank you very much!! It will be very useful!!



  • Create the Class for interfacing with qextserialport for basic open,close,read and write opertaions.

    This will be helpful for you.

    Declare object for QextSerialPort as,

    @QextSerialPort *port;@

    In Cpp,

    @Seriallib::Seriallib()
    {
    QString portName = QLatin1String("ttyS2");// update this to use your port of choice
    baudRateValue=BAUD38400;
    PortListener(portName);
    if (port->open(QIODevice::ReadWrite) == true) {
    if (!(port->lineStatus() & LS_DSR))
    Q_UNUSED(port->lineStatus())
    }
    else {
    qDebug() << "device failed to open:" << port->errorString();
    }
    }
    void Seriallib::PortListener(const QString & portName)
    {
    this->port = new QextSerialPort(portName, QextSerialPort::EventDriven);
    port->setBaudRate(baudRateValue);
    port->setFlowControl(FLOW_OFF);
    port->setParity(PAR_NONE);
    port->setDataBits(DATA_8);
    port->setStopBits(STOP_1);
    port->setTimeout(10);
    }
    unsigned int Seriallib::onReadyRead()
    {
    unsigned int l_nValue=0;short int counterLoop=0;
    while(1){
    counterLoop++;
    if (port->bytesAvailable()) {
    QByteArray l_arrValue = port->readAll();
    l_nValue = l_arrValue.toHex().toUInt(0,16);
    qDebug()<<"onReadyRead :"<<hex<<l_nValue;
    break;
    }
    if(counterLoop==10000){
    //emit Error(true);
    break;
    }
    }
    return l_nValue;
    }
    QByteArray Seriallib::onReadyRead(QByteArray l_arrValue)
    {
    while(1){
    if (port->bytesAvailable()) {
    l_arrValue = port->readAll();
    break;
    }
    }
    return l_arrValue;
    }

    unsigned int Seriallib::onReadyWrite(unsigned int cmd){

        QByteArray xData;
        xData.append(cmd);
        port->write(xData);
        xData.clear();
        return 1;
    

    }
    unsigned int Seriallib::onCloseSerial(){
    port->close();
    return 1;
    }@

    two read functions are given, one directly wait nad read the port content and another will wait for specified loop count and if data not comes it breaks.



  • May be this can help you : http://code.google.com/p/qtterm/


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.