Friday, December 23, 2011

Codes for Serial Port Communication in QT

At first you need to include the header files at windows.cpp section and start coding
#include <qextserialport.h>
QextSerialPort *port;//create new object of class QextSerialPort.
port=new QextSerialPort(COM3);//use 3 or 4 according to your PC
if(port->isOpen())
        {
        port->flush();
        po 
 if(port->isOpen())
{
port->flush();
port->close(); 
        port->close();
 //serial port initialization before openinng the port for reading and writing purpose
port->open(QIODevice::ReadWrite | QIODevice::Unbuffered); 
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF); 
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->setQueryMode(QextSerialPort::EventDriven);

//writing data from serial port
QByteArray b;
unsigned char c = somevalue;
QString sdata = QString(c);
b = sdata.toAscii();
ui->lineEdit_2->setText(QString::number(somevalue));
port->write(b);
    b.clear();
port->write(b);
b.clear(); 
port->flush(); 

//for reading purpose
//for reading purpose
char buff[1024];
if(port->bytesAvailable())
{
int i = port->read(buff, 1);//read 1 byte at a time
buff[i] = '\0';
if(i != -1)
{
QString str(buff);
ui->textBrowser->append(QString::number(buff[i-1]));//append the data in textbrowser to see whether the data written is correct or not
}
else
{
qDebug("error in port"); 

4 comments:

  1. Thanks for your help but i get an error... I create a Qt Gui application, i put this code to mainwindow.cpp but i get an error error: 'COM4' was not declared in this scope... The same for COM3... Can you please help?

    ReplyDelete
  2. //this is my code

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include
    #include
    #include


    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QextSerialPort *port;//create new object of class QextSerialPort.
    port=new QextSerialPort(COM4);//use 3 or 4 according to your PC

    if(port->isOpen())
    {
    port->flush();
    port->close();
    port->close();
    }


    port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
    port->setBaudRate(BAUD9600);
    port->setFlowControl(FLOW_OFF);
    port->setParity(PAR_NONE);
    port->setDataBits(DATA_8);
    port->setStopBits(STOP_1);
    port->setQueryMode(QextSerialPort::EventDriven);




    char buff[1024];
    if(port->bytesAvailable())
    {
    int i = port->read(buff, 1);//read 1 byte at a time
    buff[i] = '\0';
    if(i != -1)
    {
    QString str(buff);
    //QTextBrowser *textBrowser=new QTextBrowser(this);
    //setCentralWidget(textBrowser);
    ui->textBrowser->append(QString::number(buff[i-1]));//append the data in textbrowser to see whether the data written is correct or not
    }
    else
    {
    qDebug("error in port");
    }
    }

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    ReplyDelete
  3. it would be better if you put the intialization code in separate function
    for example initialization with single push button click might be better
    code:
    port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
    port->setBaudRate(BAUD9600);
    port->setFlowControl(FLOW_OFF);
    port->setParity(PAR_NONE);
    port->setDataBits(DATA_8);
    port->setStopBits(STOP_1);
    port->setQueryMode(QextSerialPort::EventDriven);

    you must also put this reading section code in seperate function other than your constructor
    code:
    char buff[1024];
    if(port->bytesAvailable())
    {
    int i = port->read(buff, 1);//read 1 byte at a time
    buff[i] = '\0';
    if(i != -1)
    {
    QString str(buff);
    //QTextBrowser *textBrowser=new QTextBrowser(this);
    //setCentralWidget(textBrowser);
    ui->textBrowser->append(QString::number(buff[i-1]));//append the data in textbrowser to see whether the data written is correct or not
    }
    else
    {
    qDebug("error in port");
    }
    }

    }

    ReplyDelete
  4. I know how to fix it
    QString strPort ="COM4";
    port=new QextSerialPort(strPort);
    rest of it is the same ..

    ReplyDelete