I am trying to write to serial port and read as well. Below is my code for it.
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qextserialport.h>
#include <QDebug>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
PortSettings portSettings;
portSettings.BaudRate = BAUD9600;
portSettings.DataBits = DATA_8;
portSettings.Parity = PAR_EVEN;
portSettings.StopBits = STOP_1;
portSettings.FlowControl = FLOW_OFF;
//portSettings.Timeout_Millisec=1000;
port= new QextSerialPort("COM3",portSettings,QextSerialPort::EventDriven);
QObject::connect(port,
SIGNAL(readyRead
()),
this,
SLOT(readData
()));
if(port->isOpen())
{
port->flush();
port->close();
}
port->open(QextSerialPort::ReadWrite);
if (port->isOpen()) {
qDebug("connected!!");
//delay->setInterval(2000);
}
else qDebug("not connected");
writeData(":0B1000408000102FA00DC\r\n");//this is the data that i need to send
}
void MainWindow::readData()
{
port->flush();
ui->textBrowser->append(response);
}
void MainWindow::writeData(char* msg)
{
port->write(msg);
ui->textBrowser->append("writing data");
ui->textBrowser->append(msg);
port->flush();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qextserialport.h>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
PortSettings portSettings;
portSettings.BaudRate = BAUD9600;
portSettings.DataBits = DATA_8;
portSettings.Parity = PAR_EVEN;
portSettings.StopBits = STOP_1;
portSettings.FlowControl = FLOW_OFF;
//portSettings.Timeout_Millisec=1000;
port= new QextSerialPort("COM3",portSettings,QextSerialPort::EventDriven);
QObject::connect(port,SIGNAL(readyRead()),this,SLOT(readData()));
if(port->isOpen())
{
port->flush();
port->close();
}
port->open(QextSerialPort::ReadWrite);
if (port->isOpen()) {
qDebug("connected!!");
//delay->setInterval(2000);
}
else qDebug("not connected");
writeData(":0B1000408000102FA00DC\r\n");//this is the data that i need to send
}
void MainWindow::readData()
{
port->flush();
QByteArray response=port->readAll();
ui->textBrowser->append(response);
}
void MainWindow::writeData(char* msg)
{
port->write(msg);
ui->textBrowser->append("writing data");
ui->textBrowser->append(msg);
port->flush();
}
To copy to clipboard, switch view to plain text mode
this code writes to virtual serial(realterm) and even reads the response. But when i use hardware to write serial data to an atmega, no response is obtained. But i received response when i sent this data to atmega using python. Is there any fault in here?
Bookmarks