PDA

View Full Version : Cutting information



cairos
7th November 2011, 09:22
Hi all,

I'm new in qt and need some help...

1. I do success to read data from port USB, but it is not continuous reading automatically and i must click the read button many times to get the information from GPS in realtime mode. My question is: How can i get this information automatic with single click(read button)?
i have read this topic http://www.qtcentre.org/threads/6824-First-attempt-to-display-serial-port-data-on-GUI? and still confuse.

2. If the information i got from GPS: D=1, B=10, Longitude=102, Latitude=3, LS-A=1 LS=1 LS-L-U=1 LS-EL-D=1
And all i need is "Longitude and latitude" information only, how can i cut this information and display in my QTextEdit only the Long and Lat information?
example: 102, 3

Cloud you please help me to get some solution?

ChrisW67
7th November 2011, 23:00
1. Since there is no USB functionality in Qt there is not much we can say to help with reading your USB port. If you can show how your code is currently structured then perhaps there are some pointers we could give. What have you tried to do to get "continuous" input?

2. Assuming that data comes as a string then you want to read the friendly documentation for QString and QRegexp. You will definitely need the first, and could possibly do it with the second.

cairos
8th November 2011, 15:44
1. Since there is no USB functionality in Qt there is not much we can say to help with reading your USB port. If you can show how your code is currently structured then perhaps there are some pointers we could give. What have you tried to do to get "continuous" input?

2. Assuming that data comes as a string then you want to read the friendly documentation for QString and QRegexp. You will definitely need the first, and could possibly do it with the second.

Hi Chris,

Many thanks for your answer.
1. You are right there's no USB functionality in Qt, I'm sorry i mean Serial port, not USB Port since i used serial to USB converter.
I used QextSerialPort for open and configure the serial and success get information from GPS. My GPS always send information continuously without any comment sent to GPS device. I want to receive all this information that given but right now i only get information when i clicked receive button in my GUI. My mission is display this continuous information in one click :D
(sorry i couldn't give the code right now since i used my mobile phone to reply)

2. It solved after i read many references. Only QString function in my mind.
for example:
QString a = "D=1, B=10, Longitude=102.00, Latitude=003.00, LS-A=1 LS=1 LS-L-U=1 LS-EL-D=1";
QString b = a.section(',' , 2, 4); //the result: " Longitude=102.00, Latitude=003.00"
QString c = b.mid(11, 6); //the result: "102.00"
QString d = b.mid(28, 6); //the result: "003.00"

Please correct me if i do wrong, and tell me if there's any better idea.

Thanks..

ChrisW67
8th November 2011, 22:50
1. Create a persistent QByteArray to act as a buffer, connect the readyRead() signal from the QExtSerialPort QIODevice to a slot that puts all available data in the buffer. Still in the slot code, after adding the new data to the buffer check if you have a whole line then process that line and remove it from the buffer, repeat until no complete lines remain. You exit the slot with nothing, or a partial line, in the buffer waiting for the next time data is ready to read. The code is identical to how you might handle a simple network connection (also a QIODevice) and you will find examples in the forums.

You may have people suggest using a multi-threaded solution... my advice is don't use threads unless you must for other reasons.

2. If it works, then it works! There are numerous ways to tackle it.

cairos
9th November 2011, 15:10
1. Create a persistent QByteArray to act as a buffer, connect the readyRead() signal from the QExtSerialPort QIODevice to a slot that puts all available data in the buffer. Still in the slot code, after adding the new data to the buffer check if you have a whole line then process that line and remove it from the buffer, repeat until no complete lines remain. You exit the slot with nothing, or a partial line, in the buffer waiting for the next time data is ready to read. The code is identical to how you might handle a simple network connection (also a QIODevice) and you will find examples in the forums.

You may have people suggest using a multi-threaded solution... my advice is don't use threads unless you must for other reasons.

2. If it works, then it works! There are numerous ways to tackle it.


Hi Chris,
Okay, Thanks before for your answer. I will figure it out first and come back with another problem for sure :D:D:D.
i will update soon..

regards,

cairos
21st November 2011, 06:14
Hi Chris and all,

Still have problems:
1. How to do repeat function? When i put "while" in my function my program in GUI became hung. If i may analyze it, port Open and function read repeatly but it never display in QTextEdit. When i tried to make the same function (using "while" or "do while") in console application, it normal (not in GUI mode)

2. Another question : What if i 2 serial connected and i want to read all information from this both serials in the same time? Please advice.

Thanks alot.

ChrisW67
21st November 2011, 06:51
1. How to do repeat function? When i put "while" in my function my program in GUI became hung. If i may analyze it, port Open and function read repeatly but it never display in QTextEdit. When i tried to make the same function (using "while" or "do while") in console application, it normal (not in GUI mode)

Repeat what? The program responds when more data is received, processes all the complete data it has, leaves what remains, and then waits for more data. You do not execute any sort of busy-wait loop, which is what it sounds like you are doing, because it locks the entire program. You could save us the effort of guessing by posting your actual code.


2. Another question : What if i 2 serial connected and i want to read all information from this both serials in the same time? Please advice.

You do the same thing twice (or three, four...).

cairos
21st November 2011, 09:02
Repeat what? The program responds when more data is received, processes all the complete data it has, leaves what remains, and then waits for more data. You do not execute any sort of busy-wait loop, which is what it sounds like you are doing, because it locks the entire program. You could save us the effort of guessing by posting your actual code.

here is my simple program (newbie's program):


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qextserialport.h"


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
port = new QextSerialPort("/dev/ttyUSB0");
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->setRts(true);
port->setDtr(true);

connect(ui->btnExit, SIGNAL(clicked()), SLOT(exit()));
connect(ui->btnConnect, SIGNAL(clicked()), SLOT(openPort()));
connect(ui->btnRead, SIGNAL(clicked()), SLOT(readSerial()));
connect(ui->btnDisconnect, SIGNAL(clicked()), SLOT(closePort()));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::openPort()
{
port->open(QIODevice::ReadOnly | QIODevice::Unbuffered);

if (port->bytesAvailable())
{
ui->textEdit->setText("Connected");
}
else
ui->textEdit->append("Please Check Your Serial");
}

void MainWindow::readSerial()
{
char buff[64];
qint64 i=port->readLine(buff,63);
while(1)
{
if (port->bytesAvailable())
{
buff[i]='\0';
QString msg = buff;

//Dispaly
ui->textEdit->append(msg);
}
}

void MainWindow::closePort()
{
port->flush();
port->close();
}

void MainWindow::exit()
{
close();
}

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}




You do the same thing twice (or three, four...).
I'm sorry i don't really understand which step should i do to make serial works in the same time.
You mean that i should open first port, second port?
Is it possible if we open port in the same application?

I really appreciate for your kindness to help me, Thank you..

ChrisW67
21st November 2011, 09:26
Line 47 is a busy-wait loop. Since you never exit this loop your program can never respond to user interaction or update the display.

You don't appear the have tried to follow any of the advice. Here are the steps again:

Create a persistent QByteArray to act as a buffer (member variable)
Connect the readyRead() signal from the QExtSerialPort QIODevice to a slot
In the slot code:

Put all available data on the end of the buffer
Check if you have a whole line. If so, process that line and remove it from the buffer.
Repeat until no complete lines remain.
Exit the slot with nothing, or a partial line, in the buffer waiting for the next time data is ready to read.


It is important that you do not stay in the slot.