PDA

View Full Version : how to check data package received in serial port is 5 byte and check each byte ?



suhairkp
14th February 2017, 13:50
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()
{
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();

}


Anyone please help me ....

high_flyer
14th February 2017, 23:22
Anyone please help me ....
Gladly, just tell us what the problem or question is, then we might help you...

ado130
15th February 2017, 09:24
Not sure if I understand, but you can use static_cast the first byte to uint, and then mask 7th bit.

Lesiok
15th February 2017, 09:57
First of all : in line 47 (serial->readLine()...) You can get any number of bytes in the range of <1..1000>.
Before atof() you need to check whether the buffer contains a complete package.