JSON with multi tasking
-
Hi Guys,
I am new for QT C++ development. I have an processor which will control the Hardware. From server (Java) I will sent the instructions to that Hardware. According to that particular instruction it is going to perform. This is general idea of my project.
Basic control instructions written on board. Now What I want and What is the problem I am going to describe that ? Sorry for my poor english.
I am going to send the JSON string from the server. For example
working|pq|0|1
From websocket I will receive the string
else if(message.contains("working")) { emit OnMsgRecievedConnect("connect"); workingpq = message; qDebug() << workingpq; }
Now I have received a string from server. I have taken that string into another thread for giving instruction to Hardware.
workingvaluepq = EchoClient::workingpq ; qDebug() << "End pq value Received from server :" << workingvaluepq ; QStringList pq = workingvaluepq.split("|"); int pqSize = pq.size(); // get the pq list size; qDebug() << "End pq size :" << pqSize; if(pqSize == 5){ p = pq[3]; q = pq[4]; } int pInt = p.toInt(&ok); int qInt = q.toInt(&ok); if(!ok && pqSize == 5){ pend = pInt; } if(!ok && pqSize == 5){ qend = qInt; } pend = pInt; qend = qInt; if(pqSize == 1 ){ pend = jread->endvaluep; qend = jread->endvalueq; } while(true) { (All other stuff here) if(pqSize == 5){ if (pend == pstart && qend == qstart) { Jsonendflag = 1; } } if(pqSize == 1){ if (pend == pstart && qend == qstart) { Rightstopflag = 1; } }
So when this point (whatever we fixed) reached the particular task, finally it will get stop. May be above code have some experienced issue. So person can help me to improve the code. One more question, This is only one task, I have tried.
Note : pstart, qstart, pend, qend these things defined in on board Json file. Just I will read and execute so reading executing is not a big deal
If I receive the multiple string (task) from server machine have to complete the task one by one. For example will receive the string like given below
working|xy|2|3*working1|xy|8|7*working3|xy|12|15*working4|xy|17|20
Above string have a four different task, I will receive in same time, now I want to split and complete the task one by one continuously. I hope here so many experienced person can help and solve this issue.
-
Hi @Geeva,
It's not clear: What is your question? Do you want to know how to split the long string into individual tasks?
working|xy|2|3*working1|xy|8|7*working3|xy|12|15*working4|xy|17|20
-
Split this string using the
*
character first, to get 4 task strings:- working|xy|2|3
- working1|xy|8|7
- working3|xy|12|15
- working4|xy|17|20
-
Take the first task and split using the
|
character. -
Perform the task
-
Repeat #2 and #3 with the 2nd task
-
Repeat #2 and #3 with the 3rd task
-
Repeat #2 and #3 with the 4nd task
Please note: Your strings are not JSON!
-
-
@JKSH Sorry for confusion. I know how to split. After splitting will get 4 different task. Here, the question is How to perform the task one by one.
Once we received the string we can split it using * and |. Now the task going to start. First working|xy|2|3 this task should complete the it should go to next task automatically, similar should happen each task.
After completed all task it should stop & sent the message to server. This is what I want !!! I hope you can understand now.
-
@Geeva said in JSON with multi tasking:
How to perform the task one by one.
After you split using
*
, use a loop to process your tasks one by one.const QStringList taskList = QString("working|xy|2|3*working1|xy|8|7*working3|xy|12|15*working4|xy|17|20").split('*'); for (const QString& task : taskList) { auto params = task.split('|'); /* Perform 1 task */ // ... } /* Report back to server */ // ...
The code above uses a C++ ranged-for-loop.
You can do the same using an older for-loop:
for (int i = 0; i < taskList.count(); ++i) { auto params = taskList[i].split('|'); /* Perform 1 task */ // ... }
-
@JKSH Thanks a lot. It is very useful. Just I want to take values. Consider p and q values are 2|3 8|7 12|15 17|20. If I start the machine, pq(Consider as x and y axis) first it will start and reached to 2,3 after this process it will reach to 8,7 etc.
So similar condition (given by you), is it applicable for this? or else any different approach I have to do. These condition will come inside while loop or outside while loop ? Important thing task one by one only it should complete.
-
@Geeva said in JSON with multi tasking:
These condition will come inside while loop or outside while loop ?
Sorry, I don't understand what you mean by "condition".
Important thing task one by one only it should complete.
The loop runs "one by one". It will read 2|3 first. After it finishes reading 2|3, it will read 8|7. After it finishes reading 8|7, it will read 12|15.
Loops are very basic but very important in most programming languages. If you have uncertainties about how loops work, I recommend you take some C++ lessons. This will give you important skills and knowledge.
-
@JKSH Thanks for solving basic confusion. I have one more doubt also. Here, I have attached program for verification. I hope you can provide a proper solution
void Thread::run() { workingvaluepq = " working|xy|1|0 "; qDebug() << "End pq value Received from server :" << workingvaluepq ; QStringList pq = workingvaluepq.split("|"); int pqSize = pq.size(); // get the pq list size; qDebug() << "End pq size :" << pqSize; if(pqSize == 5){ p = pq[3]; q = pq[4]; } int pInt = p.toInt(&ok); int qInt = q.toInt(&ok); if(!ok && pqSize == 5){ pend = pInt; } if(!ok && pqSize == 5){ qend = qInt; } pend = pInt; qend = qInt; for (int i = 0; i < taskList.count(); ++i) { auto params = taskList[i].split('|'); /* Perform 1 task */ } while(true) { All the stuff here }
@JKSH condition (Program instruction)
-
Hi @Geeva,
@Geeva said in JSON with multi tasking:
@JKSH Thanks for solving basic confusion. I have one more doubt also. Here, I have attached program for verification. I hope you can provide a proper solution
You're welcome. I'm happy to teach you basics. However, verifying your program is too difficult to do on a forum.
You need to do 2 things:
- Take C++ lessons
- Practice writing and debugging simple programs for your hardware