Hello,
I have a serial hardware device that is sending a certain (hardware-controlled) numeric value in the range [0 .. 255] over and over again.
I am sure the device is sending the correct value, but when I am trying to read it through an instance of QextSerialPort, I am getting wrong values.
And even when changing that (hardware-controlled) value, I am reading the same old wrong value 
Here is the code used (a modified version of the example shipped with QextSerialPort):
/* qesptest.h
**************************************/
#ifndef _QESPTEST_H_
#define _QESPTEST_H_
#include <QWidget>
class QextSerialPort;
{
Q_OBJECT
public:
virtual ~QespTest();
private:
QextSerialPort *port;
private slots:
void receiveByte();
};
#endif
/* qesptest.h
**************************************/
#ifndef _QESPTEST_H_
#define _QESPTEST_H_
#include <QWidget>
class QTextEdit;
class QextSerialPort;
class QespTest : public QWidget
{
Q_OBJECT
public:
QespTest(QWidget *parent=0);
virtual ~QespTest();
private:
QTextEdit *received_msg;
QextSerialPort *port;
private slots:
void receiveByte();
};
#endif
To copy to clipboard, switch view to plain text mode
charToBinaryStdString() is created so that I can display the read char as 1s and 0s...
/* qesptest.cpp
**************************************/
#include "qesptest.h"
#include <qextserialport.h>
#include <QLayout>
#include <QTextEdit>
#include <QPushButton>
#include <string>
QespTest
::QespTest(QWidget* parent
)
{
//port settings
port = new QextSerialPort("COM1");
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_XONXOFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
connect(receiveButton, SIGNAL(clicked()), SLOT(receiveByte()));
myVBox->addWidget(receiveButton);
myVBox->addWidget(received_msg);
setLayout(myVBox);
}
QespTest::~QespTest()
{
port->close();
delete port;
port = NULL;
}
std::string charToBinaryStdString(char ch)
{
std::string result;
int i = 8;
while (i >0)
{
--i;
result += (ch&(1 << i) ? '1' : '0');
}
return result;
}
void QespTest::receiveByte()
{
char ch;
if(port->bytesAvailable() > 0)
{
port->read(&ch,1);
received_msg
->append
(QString::fromStdString(charToBinaryStdString
(ch
)));
}
}
/* qesptest.cpp
**************************************/
#include "qesptest.h"
#include <qextserialport.h>
#include <QLayout>
#include <QTextEdit>
#include <QPushButton>
#include <string>
QespTest::QespTest(QWidget* parent)
: QWidget(parent)
{
//port settings
port = new QextSerialPort("COM1");
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_XONXOFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->open(QIODevice::ReadOnly);
QPushButton *receiveButton = new QPushButton("Receive");
connect(receiveButton, SIGNAL(clicked()), SLOT(receiveByte()));
received_msg = new QTextEdit();
QVBoxLayout *myVBox = new QVBoxLayout;
myVBox->addWidget(receiveButton);
myVBox->addWidget(received_msg);
setLayout(myVBox);
}
QespTest::~QespTest()
{
port->close();
delete port;
port = NULL;
}
std::string charToBinaryStdString(char ch)
{
std::string result;
int i = 8;
while (i >0)
{
--i;
result += (ch&(1 << i) ? '1' : '0');
}
return result;
}
void QespTest::receiveByte()
{
port->open(QIODevice::ReadOnly);
char ch;
if(port->bytesAvailable() > 0)
{
port->read(&ch,1);
received_msg->append(QString::fromStdString(charToBinaryStdString(ch)));
}
}
To copy to clipboard, switch view to plain text mode
main() just includes an instance of QespTest and QApplication...
--------
What's wrong?
Bookmarks