I am developing an application in C++, winApi and Qt. What this application does is that it communicates through serial COM ports.
A user can open multiple pairs of ports and an opened port listens other port constantly.
To implement this functionality I use windows threads.There is a method named startRead() that reads from other port constantly and changes the text area.
void SendAndReceive::startRead(){
DWORD numRead = 0;
std::string hex;
while (1)
{
char *buffer = (char *)malloc(sizeof(char) * 500);
BOOL ret = ReadFile(portHandler, buffer, 500, &numRead, 0);
if (!ret)
{
std::string errorMessage = "";
}
buffer[numRead] = '\0';
std::string receivedData(buffer);
QString QData(receivedData.c_str());
if (ui->checkBox->isChecked())
{
std::string receivedData(buffer);
hex= stringToHex(receivedData);
QString QData1(hex.c_str());
emit asHex(QData1);
}
QString QData2(receivedData.c_str()) ;
emit finished(QData2);
free(buffer);
}
}
And there is another thread that writes data periodically to other port. For example , if you enter 2 seconds in a text line , the program writes data to other port at every 2 seconds and this method is writePeriodic().
void SendAndReceive::writePeriodic(){
DWORD numWritten;
while (1 && checkWrite == true )
{
Concurrency::wait(timePeriod*1000);
QString QData = ui->textEdit->toPlainText();
std::string data = QData.toStdString();
WriteFile(portHandler, data.c_str(), strlen(data.c_str()), &numWritten, NULL);
}
}
So when I run this program , program runs smoothly for 2 or 3 minutes and then it crashes and I get errors like "Program has stopped working" , "program closed unexpectedly".When I debug program , it says something like "Qt5Core not loaded" or "Qt5Gui not loaded".
Before I ask here , I did some search on the web . First I did not use emit finished(QString) signal , instead I directly manipulated GUI objects inside startRead() method.(I did something like ui->text_edit->setText(some QString)).But after some search I learn that I can not change GUI objects from another thread so I decided to use signal and slot mechanism and it has not solved my problem so far.I get same error again and again.Please tell me what I am doing wrong . If you need further explanation, I will happily give more details.
Related
I need to read ID data through Serial Port. When I send the ID read command with the leather port, the ID data comes to me a little late. Therefore, when I try to split the data and display it on the screen, it comes up blank and the application closes itself. How can I wait until the data comes in?
void productDetail::on_pushButton_clicked()
{
QSerialPortInfo info;
QList<QSerialPortInfo> infoList = QSerialPortInfo::availablePorts();
foreach(info, infoList) QTextStream(stdout) << info.portName();
QString curport = info.portName();
serial.begin(curport, 9600, 8, 0, 1, 0, false);
if(serial.isOpen()){
qDebug()<<"serial open";
QString sendWC= "WR+1121"; //Read ID command
serial.send(sendWC);
QString serialID = serial.getString();
serialID = serialID.trimmed();
QStringList buffer_split = serialID.split(",");
ui->IDlabel->setText(buffer_split[2]; //when I write this program closes
}
}
}
error: ASSERT failure in QList::operator[]: "index out of range",
Qt's QIODevice provides a signal readyRead (https://doc.qt.io/qt-5/qiodevice.html#readyRead) which is emitted once every time new data is available for reading from the device.
In the productDetail class constructor, connect serial port readyRead signal to a slot.
connect(serial, &QSerialPort::readyRead, this, &productDetail::readData);
Define readData method as follows:
void productDetail::readData()
{
const QByteArray data = m_serial->readAll();
}
For serial communication there is no guarantee that you will receive entire message data in a single read. I prefer to store the received data in a circular buffer and process the circular buffer in a separate slot.
I need the simplest most reliable IPC method from one C++ app running on the RPi to another app.
All I'm trying to do is send a string message of 40 characters from one app to another
The first app is running as a service on boot, the other app is started at a later time and is frequently exited and restarted for debugging
The frequent debugging for the second app is whats causing problems with the IPCs I've tried so far
I've tried about 3 different methods and here is where they failed:
File FIFO, the problem is one program hangs while the other program is writing to the file
Shared memory: cannot initialize on one thread and read from another thread. Also frequent exiting while debugging causing GDB crashes with the following GDB command is taking too long to complete -stack-list-frames --thread 1
UDP socket with localhost - same issue as above, plus improper exits block the socket, forcing me to reboot device
Non blocking pipe - not getting any messages on the receiving process
What else can I try? I dont want to get the DBus library, seems too complex for this application.
Any simple server and client code or a link to it would be helpful
Here is my non-blockign pipe code, that doesnt work for me,
I assume its because I dont have a reference to the pipe from one app to the other
Code sourced from here: https://www.geeksforgeeks.org/non-blocking-io-with-pipes-in-c/
char* msg1 = "hello";
char* msg2 = "bye !!";
int p[2], i;
bool InitClient()
{
// error checking for pipe
if(pipe(p) < 0)
exit(1);
// error checking for fcntl
if(fcntl(p[0], F_SETFL, O_NONBLOCK) < 0)
exit(2);
//Read
int nread;
char buf[MSGSIZE];
// write link
close(p[1]);
while (1) {
// read call if return -1 then pipe is
// empty because of fcntl
nread = read(p[0], buf, MSGSIZE);
switch (nread) {
case -1:
// case -1 means pipe is empty and errono
// set EAGAIN
if(errno == EAGAIN) {
printf("(pipe empty)\n");
sleep(1);
break;
}
default:
// text read
// by default return no. of bytes
// which read call read at that time
printf("MSG = % s\n", buf);
}
}
return true;
}
bool InitServer()
{
// error checking for pipe
if(pipe(p) < 0)
exit(1);
// error checking for fcntl
if(fcntl(p[0], F_SETFL, O_NONBLOCK) < 0)
exit(2);
//Write
// read link
close(p[0]);
// write 3 times "hello" in 3 second interval
for(i = 0 ; i < 3000000000 ; i++) {
write(p[0], msg1, MSGSIZE);
sleep(3);
}
// write "bye" one times
write(p[0], msg2, MSGSIZE);
return true;
}
Please consider ZeroMQ
https://zeromq.org/
It is lightweight and has wrapper for all major programming languages.
void CSerialController::sl_executeCommand(const QString s_command,QString&s_result){
QMutexLocker locker(mpo_mutex);
KT_Error t_error = ERROR_NONE;
QByteArray o_serialOutput;
QString s_serialAnswer = "";
char c_serialLetter = ' ';
// Port is already open
mpo_serial->clearBuffer(); // Flush and ReadAll
t_error = mpo_serial->sendStr(s_command); //
NO_SUCCESS(t_error)
{
Q_EMIT sg_throwError(t_error);
}
SUCCESS(t_error)
{
while ((t_error == ERROR_NONE) && (c_serialLetter != '\n')) // Reads Serial till newline is found
{
t_error = mpo_serial->getChar(c_serialLetter); // -> checks BytesAvailable -> if no bytes available -> waitForReadyRead(1) <- This is in a Loop while given time is over(Timeouttime)
o_serialOutput.append(c_serialLetter);
}
}
NO_SUCCESS(t_error)
{
Q_EMIT sg_throwError(t_error);
}
SUCCESS(t_error)
{
s_serialAnswer = o_serialOutput;
s_serialAnswer.remove("\r\n");
}
s_result = s_serialAnswer; }
Im working with C++ and Qt 5.5 and i cant get the serial connection to work correctly. Im using signals to connect a QPushButton with a slot in the CSerialController class which calls this function in the same class. I tried this function with a endless while-loop and it works correctly all the time. Its just not working when i use the buttons. First time i use a button it works correctly but the second time i press a button which calls this slot, the serialport only sometimes returns a value. (Writing works just not reading) I even tried changing the Thread of this class but that didnt help aswell. If you want i can post the sendStr and getChar functions aswell but like i said they work without a problem(allready used in a lot of projects)
UPDATE: I tried using the Windows API for the serial connection = problem is gone. Seems like a problem with QSerialPort usage on an old CPU (I7-870)
I am writing a Qt application to communicate with another computer over a serial port. I have 2 real issues.
1.
I can send and receive data fine, but sometimes the serial port "eats" part of my input.
For example if I send:
cd /application/bin
sometimes (not always) it will only receive:
cd /applica
(Since it's a terminal it echoes the input back. Also my prompt tells me I'm clearly in the wrong spot.)
2.
Also, sometimes the Qt slot which fires when there is data available doesn't fire even though I know that there's data I can receive. If I send another \r\n down the port the slot will fire.
For example sometimes I'll ls something, and the command name will be read back from the port, but the contents of the folder sit there in limbo until I hit return again. Then I get the listing of the directory and two prompts.
Here's my code:
void Logic::onReadyRead(){
QByteArray incomingData;
incomingData = port->readAll();
QString s(incomingData);
emit dataAvailable(s);// this is a Qt slot if you don't know what it is.
qDebug() << "in:"<< s.toLatin1();
}
void Logic::writeToTerminal(QString string )
{
string.append( "\r\n");
port->write((char*)string.data(), string.length());
if ( port->bytesToWrite() > 0){
port->flush();
}
qDebug() << "out:" << string.toLatin1();
}
I found the solution, and I suspect it was an encoding error, but I'm not sure. Instead of sending a QString down the serial port, sending a QByteArray fixed both problems. I changed the writeToTerminal() method:
void Logic::writeToTerminal(QString string )
{
string.append( "\r");
QByteArray ba = string.toAscii();
port->write(ba);
}
From this forum, it appears that sometimes not all the data gets sent, and whatever does gets sent has a '\0' appended to it. So if
cd /applica'\0'
got sent, then the port->readAll() would stop there, because it thinks it has read everything.
One suggested answer on that forum was to read line by line, which your code almost does. So I think in your case, you can change your code to:
void Logic::onReadyRead(){
QByteArray incomingData;
if(port->canReadLine()) {
incomingData = port->readLine();
QString s(incomingData);
emit dataAvailable(s);// this is a Qt slot if you don't know what it is.
qDebug() << "in:"<< s.toLatin1();
}
}
void Logic::writeToTerminal(QString string )
{
string.append( "\r\n");
port->write((char*)string.data(), string.length());
if ( port->bytesToWrite() > 0){
port->flush();
}
qDebug() << "out:" << string.toLatin1();
}
Using Qt, I'm attempting to read the contents of the stdin stream in a non-blocking fashion. I'm using the QSocketNotifier to alert me when the socket has recieved some new data. The setup for the notifier looks like this:
QSocketNotifier *pNot = new QSocketNotifier(STDIN_FILENO, QSocketNotifier::Read, this);
connect(pNot, SIGNAL(activated(int)), this, SLOT(onData()));
pNot->setEnabled(true);
The onData() slot looks like this:
void CIPCListener::onData()
{
qDebug() << Q_FUNC_INFO;
QTextStream stream(stdin, QIODevice::ReadOnly);
QString str;
forever
{
fd_set stdinfd;
FD_ZERO( &stdinfd );
FD_SET( STDIN_FILENO, &stdinfd );
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
int ready = select( 1, &stdinfd, NULL, NULL, &tv );
if( ready > 0 )
{
str += stream.readLine();
}
else
{
break;
}
}
qDebug() << "Recieved data:" << str;
}
As you can see I'm attempting to use the select() system call to tell me when I've run out of data to read. However, in practise what is happening is the select() call returns 0 after I've read the first line of text. So, for example, if I write 5 lines of text to the process's stdin stream, I only ever read the first line.
What could be the problem?
Line buffering.
Default is flushing after a "\n". If you write 5 lines to your process, your slot gets called 5 times. If you want to avoid that, you have to call setbuf(stdin, _IOFBF). But even then it is not guaranteed you can read arbitrarily large amounts of data in one chunk.
Edit: It would probably better to use QTextStream::atEnd() instead of select, since QTextStream has its own internal buffers.
I've found and example in other answer that fits almost to this question and with complete and simple code:
https://stackoverflow.com/a/7389622/721929
I've used it to implement a QT console based app with a textual menu to choose on user selection.