I am currently working on a project that involves serial communication between a Arduino and a laptop. I know the Arduino is indeed sending the data that I need, see this picture: http://s1.postimg.org/w5wisaetr/Help.png
Now on the other end my laptop is connected to the Arduino and running a program that I made using QT Creator. However, when reading data from the serial Port I can't get the program to display this information.
I connected my readData() function to be executed when data is received like this:
connect(m_serialPort, SIGNAL(readyRead()), m_dataGathering, SLOT(newData()));
This works and the newData() function is called whenever something in transmitted from the Arduino. However the function newData() does not display the data that I need.
newData():
void DataGathering::newData()
{
QByteArray rMsg = m_serial->readAll();
qDebug() << rMsg.constData();
}
This only sends empty message to the display. Like this: http://s2.postimg.org/dkcyip2u1/empty.png
The following code however works:
void DataGathering::newData()
{
QByteArray rMsg("\nTest...");// = m_serial->readAll();
qDebug() << rMsg.constData();
}
This code display the message like it should.
However, another difference in the output display is that when the working code is executed my console also displays a lot of Framing errors, I assumed this is because the baudrate of the unwanted characters differs from the data that I need.
That is why i started questioning the readAll() function.
It is also obvious that the Arduino is not only sending the data that I need but also some unwanted characters (see image in first link), but I don't see this as a problem since I will filter this out later.
All help is very much appreciated.
Update: I found out that the readAll() function is returning QByteArrays with size() equals to 0.
Looks like the serial port QIODevice does not implement bytesAvailable, if it returns 0. This may also be why readAll() fails, depending on how it is implemented. But at least readAll() has the problem of not being able to report error.
Try using read method instead for better diagnostics, like this (untested):
void DataGathering::newData()
{
QByteArray rMsg;
for(;;) {
char buf[256]; // read data in this size chunks
qint64 len = m_serial->read(buf, sizeof buf);
if (len <= 0) {
if (len < 0) {
qDebug() << "newData() read error" << m_serial->errorString();
}
break; // for(;;)
}
rMsg.append(buf, len);
}
qDebug() << "newData() got byte array" << rMsg.size() << ":" << rMsg;
}
It may not solve your problem, but with luck it will give you error message.
Related
I would like to know how to efficiently extract data from a QAudioBuffer object. I have a wav audio file that I am decoding with a QAudioDecoder object. I want to extract the results of the decoding contained in the QAudioBuffer object to apply filtering operations on it and finally send the filtered data to a subclass of QIODevice to listen to the result in real time.
At first, I just do a test to make sure that the extraction works well. To do this, I save the data contained in the QAudioBuffer object in a txt file. But I encounter 2 problems.
the resulting TXT file contains only characters, no numbers.
With MATLAB, when I plot the signal represented by the data contained in the TXT file, I get the shape of the original audio signal (the one in the WAV file) but the amplitudes are too big and should be between -1 and 1.
Can you please tell me how to extract the data so that I get a result on which I can apply a filter and how to have data between -1 and 1?
I use Qt6.4
thanks in advance
the code and the slot
QAudioFormat *format_decoder;
format_decoder = new QAudioFormat;
format_decoder->setSampleRate(44100);
format_decoder->setChannelCount(1);
format_decoder->setSampleFormat(QAudioFormat::Int16);
QAudioDecoder decoder;
decoder.setSource(filenameSource);
decoder.setAudioFormat(*format_decoder);
decoder.start();
QObject::connect(&decoder, &QAudioDecoder::bufferReady, this, &MainWindow::test_copy_to_txt)
the slot
void MainWindow::test_copy_to_txt(){
QAudioBuffer audioBuffer = decoder.read();
const qint16* samples = audioBuffer.constData<qint16>(); // Signal shape ok, but not the amplitudes
QFile outputFile(filenameTest1);
if(!outputFile.open(QIODevice::WriteOnly|QIODevice::Append)){
qDebug() << "ERROR";}
QTextStream out(&outputFile);
for (int i = 0; i < audioBuffer.sampleCount(); ++i) {
out << samples[i] << "\n"; // only characters, no numbers.
}
outputFile.close();
}
another question: Can you recommend a documentation other than the one on the Qt site to have more details on audio processing with Qt? How do the classes react to each other? An example so that you understand why I am looking for such documentation is the pure virtual function quint64 readData(char *data, quint64 Len) from QIODevice. For my project, I will have to reimplement it, but I would like to know what function calls it and how to determine the Len parameter.
Thank you for your answers. I followed your recommendations and everything is ok now.
Here is the corrected code
out << static_cast< float >(samples[i]) / std::numeric_limits<qint16>::max() << "\r\n";
I've just noticed something when using QNetworkReply that I was unable to find the slightest hint in the Qt documentation for QIODevice::readAll() (which the QNetworkReply inherits this method from).
Here is what the documentation states:
Reads all remaining data from the device, and returns it as a byte
array.
This function has no way of reporting errors; returning an empty
QByteArray can mean either that no data was currently available for
reading, or that an error occurred.
Let's say I have the following connection:
connect(this->reply, &QIODevice::readyRead, this, &MyApp::readyReadRequest);
Ths readyReadRequest() slot looks like this:
void MyApp::readyReadRequest()
{
LOG(INFO) << "Received data from \"" << this->url.toString() << "\"";
LOG(INFO) << "Data contents:\n" << QString(this->reply->readAll());
this->bufferReply = this->reply->readAll();
}
The surprise came after I called this->bufferReply (which a QByteArray class member of MyApp). I passed it to a QXmlStreamReader and did:
while (!reader.atEnd())
{
LOG(DEBUG) << "Reading next XML element";
reader.readNext();
LOG(DEBUG) << reader.tokenString();
}
if (reader.hasError())
{
LOG(ERROR) << "Encountered error while parsing XML data:" << reader.errorString();
}
Imagine my surprise when I got the following output:
2017-10-17 16:12:18,591 DEBUG [default] [void MyApp::processReply()][...] Reading next XML element
2017-10-17 16:12:18,591 DEBUG [default] [void MyApp::processReply()] [...] Invalid
2017-10-17 16:12:18,591 ERROR [default] Encountered error while parsing XML data: Premature end of document
Through debugging I got that my bufferReply at this point is empty. I looked in the docs again but couldn't find anything that hints removing the data from the device (in my case the network reply) after reading it all.
Removing the line where I print the byte array or simply moving it after this->bufferReply = this->reply->readAll(); and then printing the contents of the class member fixed the issue:
void MyApp::readyReadRequest()
{
LOG(INFO) << "Received data from \"" << this->url.toString() << "\"";
this->bufferReply = this->reply->readAll();
LOG(INFO) << "Data contents:\n" << QString(this->bufferReply);
}
However I would like to know if I'm doing something wrong or is the documentation indeed incomplete.
Since readAll() doesn't report errors or that data is not available at the given point in time returning an empty byte array is the only thing that hints towards the fact that something didn't work as intended.
Yes. When you call QIODevice::readAll() 2 times, it is normal that the 2nd time you get nothing. Everything has been read, there is nothing more to be read.
This behavior is standard in IO read functions: each call to a read() function returns the next piece of data. Since readAll() reads to the end, further calls return nothing.
However, this does not necessarily means that the data has been flushed. For instance when you read a file, it just moves a "cursor" around and you can go back to the start of the file with QIODevice::seek(0). For QNetworkReply, I'd guess that the data is just discarded.
I use Qt 5.4 on Ubuntu Gnome 14.04 LTS to reads a string line from serial port. Everything was OK, but when I re-installed Ubuntu Gnome 14.04 and Qt 5.4, my code of serial port does not work well. When the Arduino send "0" the code of Qt reads it like this "�" and the other numbers that sends over serial the Qt reads it as a letters and symbols. I think the problem with unicode of my Qt. The unicode of my ubuntu is en.US.UTF-8 and the QT unicode is setted to "system". Please help me :(
This is my code that read the data from serial port:
QByteArray input;
if (serial->canReadLine()) //chect if data line is ready
input.clear();
input = serial->readLine(); //read data line from sreial port
ui->label->setText(input);
qDebug ()<<input<<endl;
this code of Arduino it is working fine with CuteCom and Arduino serial monitor
const int analogInPin = A0;
unsigned int sensorValue = 0; // value read from the pot
void setup() {
Serial.begin(19200);
}
void loop() {
Serial.print("\n");
Serial.print("#");
for (int i=0; i < 5; i++) {
// read the analog in value:
sensorValue = analogRead(analogInPin);
Serial.print(sensorValue);
Serial.print("#");
};
sensorValue = analogRead(analogInPin);
Serial.print(sensorValue);
Serial.print("# \n");
}
Sorry for my English
If the parity or data/stop bit parameters are different you can still write and read, but you get "funny" output similar to the one you showed us above, and this is not a problem of the unicode setting (especially not with '0', which is a character of the ASCII set).
Try setting the same port parameters explicitly on both ends just before starting the communication.
There are several problems:
You didn't post enough code to know the context of how you use it. I'm assuming that you handle the data in a method attached to the readyRead signal.
You are only reading one line, where you should be reading lines until no more are available. The readyRead signal can be emitted with any number of bytes available for reading: these may make up no complete lines, or several complete lines! If you don't keep on reading lines until no more are available, you'll be severely lagging behind incoming data.
You are using implicit QByteArray to QString conversions. These are a bad code smell. Be explicit about it.
You have fairly verbose code. You don't need to clear a QByteArray before setting its value. You also should declare it at the point of use. Better yet, use type inference that C++11 brought.
Thus:
class MyWindow : public QDialog {
QSerialPort m_port;
void onData() {
while (m_port->canReadLine())
auto input = QString::fromLatin1(m_port->readLine());
ui->label->setText(input);
qDebug() << input;
}
}
...
public:
MyWindow(QWidget * parent = 0) : QDialog(parent) {
...
connect(&m_port, &QIODevice::readyRead, this, &MyWindow::onData);
}
};
Recently I've been messing around with some sockets by trying to make a client/server program. So far I have been successful, but it seems I hit a roadblock. For some quick background information, I made a server that can accept a connection, and once everything is set up and a connection to a client is made, this block of code begins to exectue:
while(1){
read(newsockfd, &inbuffer, 256);
std::cout << "Message from client " << inet_ntoa(cli_addr.sin_addr) << " : ";
for(int i = 0; i < sizeof(inbuffer); i++){
std::cout << inbuffer[i];
}
std::cout << std::endl;
}
Now the client simply, when executed, connects to the server and writes to the socket, and then exits. So since one message was sent, this loop should only run once, and then wait for another message if what I read was correct.
But what ends up happenning is that this loop continues over and over, printing the same message over and over. From what I read (on this site and others) about the read() function is that after it is called once, it waits for another message to be recieved. I may be making a stupid mistake here, but is there any way I can have this read() function wait for a new message, instead of using the same old message over and over? Or is there another function that could replace read() to do what I want it to?
Thanks for any help.
You don't check the return value of read. So if the other end closes the connection or there's an error, you'll just loop forever outputting whatever happened to be in the buffer. You probably want:
while(1){
int msglen = read(newsockfd, &inbuffer, 256);
if (msglen <= 0) break;
std::cout << "Data from client " << inet_ntoa(cli_addr.sin_addr) << " : ";
for(int i = 0; i < msglen; i++){
std::cout << inbuffer[i];
}
std::cout << std::endl;
}
Notice that I changed the word "message" to "data". Here's why:
So since one message was sent, this loop should only run once, and then wait for another message if what I read was correct.
This is incorrect. The code above does not have any concept of a "message", and TCP does not preserve application message boundaries. So not only is this wrong, there's no way it could be correct because the word "message" has no meaning that could possibly apply in this context. TCP does not "glue together" the bytes that happend to be passed in a single call to a sending function.
I'm working on creating a maze game, where two players connect (one acts as host, the other the player). In this, I'm sending XML data as a string using the send() function. (I'm also using a pre-made Socket class, keeping in mind this is for non-profit activities, meaning it doesn't break the copyright.) Keep in mind the client & server are running on Windows 7 using the WinSock2.h package.
The problem I'm encountering is fairly straightforward. I first send the Maze XML file, this reads properly and is able to save the maze in a series of tiles. After this, another XML file is sent, updating the position of the player (and enemy) of the other user's game. However, when I attempt to READ this line, it starts reading from the beginning of the buffer, and it seems as if the buffer isn't being cleared because it starts reading the Maze XML file again.
Is there a way to clear the buffer that recv() uses? I can't think of any other reason why the Maze XML is being read twice, when it isn't being sent twice.
Below is the code that receives the XML, character by character. This is the server's code, the client code just reverses the order of sending/receiving the data. Not sure if that's necessary or relevant.
[code]
while (1) { char r;
switch(recv(s_, &r, 1, 0)) {
case 0: // not connected anymore;
// ... but last line sent
// might not end in \n,
// so return ret anyway.
return ret;
case -1:
return "";
// if (errno == EAGAIN) {
// return ret;
// } else {
// // not connected anymore
// return "";
// }
}
ret += r;
if (r == '<') {
counter = 0;
check = "";
}
check += r;
if (counter == 6 && check.compare(ender) == 0)
{
return ret;
}
//if (r == '\n') return ret;
counter++;
}
[/code]
And this is the code that sends/receives the different XML files.
[code]
Socket* s=in.Accept();
cout << "Accepted a Call from a Client." << endl;
// Here is where we receive the first (Maze) XML File, and
// send our maze as XML
string mazeS = s->ReceiveLineMaze();
TiXmlDocument testDoc;
testDoc.Parse(mazeS.c_str(), 0, TIXML_ENCODING_UTF8);
testDoc.SaveFile("ServerTestDoc.xml");
//testDoc.SaveFile("testXMLFromString.xml");
Tile** theirMaze = readXML(testDoc);
TiXmlDocument theMaze = maze->mazeToXML();
//theMaze.SaveFile("ClientTestWrite.XML");
TiXmlPrinter printer;
theMaze.Accept(&printer);
string toSend = printer.CStr();
cout << toSend << endl;
s->SendLine(toSend);
//RENDER STUFF IN THIS LOOP
bool inOurMaze = false;
while(boolValues->running) {
// This next line is where I want to receive the update on position
// but instead it reads the Maze XML file again, the one I read up
// above
string posReceive = s->ReceiveLineUpdate();
TiXmlDocument theirPos;
theirPos.Parse(posReceive.c_str(), 0, TIXML_ENCODING_UTF8);
... This is where I process the update XML ...
TiXmlDocument updatePos = maze->updatePositionXML();
TiXmlPrinter printerPos;
updatePos.Accept(&printerPos);
string posSend = printer.CStr();
s->SendLine(posSend);
[/code]
Any help is appreciated. If it isn't clear up top, let me summarize.
I first swap an XML file that details the Maze itself. This works fine. Then I attempt to swap the update XML files, that update the player/enemy positions for the other user. But when I attempt to use recv(...), it starts to read the Maze file again, NOT the update file. It's...perplexing.
Oh, and here's the send code (very simple):
[code]
s += '\n';
send(s_,s.c_str(),s.length(),0);
[/code]
where s_ is the socket and s.c_str is the string that needs to be sent (in this case the different XML files).
As #Martin points out, the issue is definitely with the code. Few things to check, looks like the data is read into the buffer "ret" and that's under your control. Is that getting flushed / cleared each time (the code does not make that clear). If this is fine, check the client code to ensure that the data is sent correctly.
Best option is to debug thru your send and receive functions in the IDE, you should be able to spot whats wrong.