Need advice on sending QString command over TCP
-
Hi all,
i am building an app to send QString command to a video projector over TCP
I use QTcpSocket and QTcpAbstractSocket.
In these classes there is the write() method to send messages over ethernet. My commands are QString but the write() method uses QByteArray or char*.
I found a solution, but more after trying a lot of things than with my skills in C++ ! This seems to work but i still have somme communication problems and maybe it is a command problem which may work in some situations but not for others....
So i would like some advice and would like you to tell me if anything seems wrong. For instance, maybe some character are hidden (like \n ou \0) somewhere i can't see it. If so, should i keep them or not ? and how ?
For now i have QString commands, sometimes i "build" command by assembling multiple parts like
[code]
QString input = "1";
QString layer = "4";
QString command = input + "sW" + layer + "!\r";
[/code]I couldn't send "command" as is so i found this solution :
[code]
socket->write(command.toStdString().data());
[/code]Is that correct ?
Thanks a lot.
-
Yes, or QString::toUtf8(). There will be no "hidden" nul characters in the resulting QByteArray unless they were there in the original QString. As it stands your example could be built directly using QByteArray without the QString involvement.
I assume the string is some sort of command that you are expecting the projector to execute, and the projector is not responding. First check that the projector is expecting the command to be terminated with a lone CR character '\r' and not the far more usual CR LF pair "\r\n" or lone LF character '\n'.
-
thanks for the reply,
yes the QString is a command for the projector to execute (like power on/off) or requests about its states (lamps hours, is on or off etc...)
Here is the problem, some commands work fine, some don't. I sometimes get unexpected values from requests. Or even sometimes commands work fine... and then stops working.
my question is part of a larger issue which I am unable to fix so far... probably something dealing with the way i handle writing and reading values thru the socket...
-
Also, i tried to use QByteArray instead of QString for my commands but this kind of thing doesn't work anymore :
@QByteArray command = input + "sW" + layer + "!\r";
@i assume that the operator "+" can't be used with QByteArray ? too bad this is really convenient feature
I was also suggected to use qPrintable() or QTextStream().
What do you think ?
-
[quote author="Marty" date="1381565545"]
i assume that the operator "+" can't be used with QByteArray ? too bad this is really convenient feature[/quote]
I think you need to read the QByteArray docs and error message your compiler is giving you. There are, indeed, several operator+() overloads for QByteArray.If some commands are working then clearly the communication is working and it is the command you are sending that is at fault. You have not addressed my query about the CR character. Also be sure whether the device is expecting the "input" and "layer" values as ASCII digits (as your example sends) and not byte values, i.e. the difference between '4' and '\x04'
-
Thanks for your answer.
Concerning CR, the protocol specifies commands terminates with a single CR not CR LF and all commands are plain strings.i think commands are ok. i think the issue is when reading a response from the device like i'm not reading the entire response or not at the good time (some part of other response sill exists in the buffer) or something like that...
-
Well, perhaps you had better show us how you are reading the response. The usual mistake is to assume that an entire response is available the when the first readyRead() signal is emitted.