PDA

View Full Version : write data using QextSerialPort is writing 0 bytes always



prasad_N
24th April 2016, 11:23
Hi, I am trying to send 1/0 to my ARDUINO Board & trying to receive some data from board as a response , I am using QextSerialPort for this But I am not able to write any data to the board & not able to receive any data also.

http://docs.qextserialport.googlecode.com/git/1.2/qextserialport.html


qDebug() << "send.size() : " << send.size() << " data = " << send.data() <<" Written = " << port->write(send, send.size());
This printing : send.size() : 1 data = 1 Written = 0 //Means I am writing 0 bytes every time

Is there a problem in my code ??



void MainWindow::ledOnOff(bool on)
{
if(port == 0)
{
port = new QextSerialPort("COM6", QextSerialPort::EventDriven); //QextSerialPort* port is class member
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_2);

connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));

if(port->open(QIODevice::ReadWrite) == true)
{
qDebug() << "Port open success";
}
else
{
qDebug() << "Port open success";
}
}
else
{
port->close();
}

quint8 writeByte = 0;

if(on)
{
writeByte = 1;
}

if(port->isOpen() || port->open(QIODevice::ReadWrite) == true)
{
QByteArray send;
send.resize(writeByte );
send = QByteArray::number(writeByte);

port->flush();
qDebug() << "send.size() : " << send.size() << " data = " << send.data()
<<" Writtend = " << port->write(send, send.size());
}
else
{
qDebug() << "device failed to open:" << port->errorString();
}
}


void MainWindow::onReadyRead()
{
QByteArray bytes;
quint8 a = port->bytesAvailable();
bytes.resize(a);
port->read(bytes.data(), bytes.size());

qDebug() << bytes.constData();
}



Thanks In Advance.

prasad_N
24th April 2016, 19:40
Moved to QSerialPort. Working now



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow), Serial(NULL)
{
ui->setupUi(this);
connect(ui->ledBut, SIGNAL(clicked(bool)), this, SLOT(ledOnOff(bool)));
setup();
}

MainWindow::~MainWindow()
{
delete ui;
Serial->close();
delete Serial;
Serial = NULL;
}

void MainWindow::ledOnOff(bool on)
{
int write = -1;

if(on)
{
write = 1;
ui->ledBut->setText("OFF");
}
else
{
write = 0;
ui->ledBut->setText("OFF");
}

if(Serial->isOpen() || this->Serial->open(QIODevice::ReadWrite))
{
this->Serial->setBaudRate(QSerialPort::Baud9600);
this->Serial->setParity(QSerialPort::NoParity);
this->Serial->setStopBits(QSerialPort::OneStop);
this->Serial->setFlowControl(QSerialPort::NoFlowControl);
this->Serial->setDataBits(QSerialPort::Data8);

QByteArray dayArray;
dayArray[0]= write;
qDebug() << "Written Bytes : " << this->Serial->write(dayArray);
this->Serial->waitForBytesWritten(-1);
}
else
{
qDebug() << "Failled open com port";
}
}

void MainWindow::onReadyRead()
{
QByteArray bytes;
int a = Serial->bytesAvailable();
bytes.resize(a);
Serial->read(bytes.data(), bytes.size());
qDebug() << "bytes read:" << bytes.size() << " Written data = " << bytes.data();
}

void MainWindow::errorReport(QSerialPort::SerialPortErr or error)
{
if(error!=0)
qDebug()<<"ERROR:"<<endl<<error;
}

void MainWindow::setup()
{
if(!Serial)
{
this->Serial=new QSerialPort(this);
this->Serial->setPortName("COM6");
this->Serial->setBaudRate(QSerialPort::Baud9600);
connect(this->Serial,SIGNAL(error(QSerialPort::SerialPortError)) ,this,SLOT(errorReport(QSerialPort::SerialPortErro r)));
connect(Serial, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
}
}