Client PC is not sending TCP data. (solved)
-
My server is not receiving data from my client.
I used 2 object out (QDatastream) & block (QByteArray) to send TCP data.
Below is my code
Please highlight my mistake.@
socket.connectToHost(ServerIP,10010); qint16 CRC; unsigned char TCPdata[5]; TCPdata[0]=0x20; TCPdata[1]=DeviceNum.toInt(); TCPdata[2]=0x00; CRC =ModbusCRC((unsigned char*)TCPdata,3); QByteArray block; QDataStream out(&block,QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_5_0); out.device()->seek(0); // serialize array input. out<<0x20<<DeviceNum.toInt()<<0x00<<CRC; // i presumed this is sending data out via TCP socket.writeData(block.data(),block.size());
@
-
Hi,
You don't check anywhere that the connection was successful, you should at least check that with waitForConnected or use the connected/error signals
-
I did use connected signals, but it never go into my slots.
I am frustrated and insert all the slots code back into function. -
Do you also check the error signal ?
-
can you explain why out and block?
is there any link to sample code? i tried but i never really succeed sending the data -
Take a look at the size() at line 18. I guess it will not be the 5 bytes you are expecting but probably something like 14 bytes:
0x20 => 4 byte integer
DeviceNum.toInt() => 4 bytes
0x00 => 4 bytes
CRC => 2 bytes -
You're not answering the important question: are you checking whether you had an error when trying to connect to your server ?
-
Thanks, this is solved
@psocket = new QTcpSocket(this);
psocket->connectToHost(ServerIP,10010);
if (psocket->waitForConnected(3000)) {
psocket->write(data);
psocket->waitForBytesWritten(1000);
psocket->disconnectFromHost();
}
else
{
qDebug()<<"Not Connected";
}@