Hello all,
i want to process output from a serial device , which sending 5 bytes of data serially with 4800 BR and odd parity. i read data serially and displayed in text edit. Now i want to check the received data package is 5 Byte and 7th bit of 1st byte is high and store second byte in QByte array .
Qt code :
ui->setupUi(this);
QVector<double> data_x(101), data_y(101);
timer.start();
init_port();
init_line_plot();
x_position = 0;
connect(serial, SIGNAL(readyRead()), this, SLOT(receive()));
}
MainWindow::~MainWindow()
{
delete ui;
serial->close();
}
void MainWindow::init_port()
{
serial = new QSerialPort(portname);
{
QMessageBox::warning(this,
"port error",
"cannot open port");
}
serial->setBaudRate(QSerialPort::Baud4800);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setParity(QSerialPort::OddParity);
serial->setDataBits(QSerialPort::Data8);
serial->setStopBits(QSerialPort::OneStop);
out <<"port opened"<<endl<<endl;
}
void MainWindow::init_line_plot()
{
ui->customPlot->addGraph();
ui->customPlot->xAxis->setLabel("t");
ui->customPlot->yAxis->setLabel("V");
}
void MainWindow::receive()
{
// recieves data as ASCII string
int datalength = 1000;
char data [1000];
int bytesRead = serial->readLine(data, datalength);
data[bytesRead]='\0';
ui
->textEdit
->append
(QString(data
));
out << data << endl;
addDataPoint(atof(data));
}
void MainWindow::addDataPoint(double datapoint)
{
if (x_position>250)data_x.pop_front();
double ms = timer.elapsed();
data_x.push_back((double)ms/1000);
x_position++;
if (x_position>250) data_y.pop_front();
data_y.push_back(datapoint);
ui->customPlot->graph(0)->setData(data_x,data_y);
ui->customPlot->setBackground(Qt::black);
ui->customPlot->graph(0)->rescaleAxes();
ui->customPlot->replot();
}
ui->setupUi(this);
QVector<double> data_x(101), data_y(101);
timer.start();
init_port();
init_line_plot();
x_position = 0;
connect(serial, SIGNAL(readyRead()), this, SLOT(receive()));
}
MainWindow::~MainWindow()
{
delete ui;
serial->close();
}
void MainWindow::init_port()
{
QTextStream out(stdout);
QString portname = "/dev/ttyUSB0";
serial = new QSerialPort(portname);
if (!serial->open(QIODevice::ReadWrite))
{
QMessageBox::warning(this, "port error", "cannot open port");
}
serial->setBaudRate(QSerialPort::Baud4800);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setParity(QSerialPort::OddParity);
serial->setDataBits(QSerialPort::Data8);
serial->setStopBits(QSerialPort::OneStop);
out <<"port opened"<<endl<<endl;
}
void MainWindow::init_line_plot()
{
ui->customPlot->addGraph();
ui->customPlot->xAxis->setLabel("t");
ui->customPlot->yAxis->setLabel("V");
}
void MainWindow::receive()
{
// recieves data as ASCII string
int datalength = 1000;
char data [1000];
int bytesRead = serial->readLine(data, datalength);
data[bytesRead]='\0';
ui->textEdit->append(QString(data));
QTextStream out(stdout);
out << data << endl;
addDataPoint(atof(data));
}
void MainWindow::addDataPoint(double datapoint)
{
if (x_position>250)data_x.pop_front();
double ms = timer.elapsed();
data_x.push_back((double)ms/1000);
x_position++;
if (x_position>250) data_y.pop_front();
data_y.push_back(datapoint);
ui->customPlot->graph(0)->setData(data_x,data_y);
ui->customPlot->setBackground(Qt::black);
ui->customPlot->graph(0)->rescaleAxes();
ui->customPlot->replot();
}
To copy to clipboard, switch view to plain text mode
Anyone please help me ....
Bookmarks