I'm sending string from Arduino to PC using serial communication. Format of message includes char, value and space (separating data). Example message: "H123 V2 L63 V2413 I23 CRC2a".
I have problem with decoding this message in Qt because when I use for example Utf-8 decoding it casting integers to chars (in a simplified way) and I receiving something like that: "H&?? j\u0002I&\u001AICL?H". Message length is not constant (different size for ex. H12 and H123) so I can't use predetermined position to casting. Do you have any idea how to decode message correctly?
Arduino code:
uint8_t H = 0, V = 0, L = 0, I = 0, CRC = 0;
String data;
void loop() {
++H; ++V; ++L; ++I;
data = String("H");
data += String(H, DEC);
data += String(" V");
data += String(V, DEC);
data += String(" L");
data += String(L, DEC);
data += String(" I");
data += String(I, DEC);
CRC = CRC8(data.c_str(), strlen(data.c_str()));
data += String(" CRC");
data += String(CRC, HEX);
Serial.println(data);
delay(1000);
}
Qt code:
while(serial.isOpen())
{
QByteArray data;
if (serial.waitForReadyRead(1000))
{
data = serial.readLine();
while (serial.waitForReadyRead(100))
{
data += serial.readAll();
}
QString response = QString::fromUtf8(data);
qDebug() << "Data " << response << endl;
}
else
qDebug() << "Timeout";
}
The problem is that you use UTF-8 decoding, while Arduino send just 1-byte ASCII chars. So use fromLocal8Bit as said albert828
Like this:
while(serial.isOpen())
{
QByteArray data;
if (serial.waitForReadyRead(1000))
{
data = serial.readLine();
while (serial.waitForReadyRead(100))
{
data += serial.readAll();
}
QString response = QString::fromLocal8Bit(data);
qDebug() << "Data " << response << endl;
}
else
qDebug() << "Timeout";
}
Related
I have created a ClientSocket and a ServerSocket class for simplifying functions. while sending a data, at first I am sending a 16 bytes header containing the message length followed by the message. But I am having trouble while sending data from client to server on the 2nd time. At first it is sending the header and the message properly but after that I am getting 0 bytes output from read() in ServerSocket::get_message while reading the header from the client. Please help me out here.
Sending and receiving part in Server.cpp
string ServerSocket::get_message(int client_socket_fd) {
//char *header = client_buffers[client_socket_fd].read_header;
char *read_buffer = client_buffers[client_socket_fd].read_buffer;
char header[16];
memset(header, 0, sizeof(header));
int read_result = -1;
read_result = read(client_socket_fd, header, 16);
cout << read_result << endl;
if (read_result > 0){
int read_size = stoi(string(header));
cout << read_size << endl;
memset(read_buffer, 0, sizeof(read_buffer));
read_result = read(client_socket_fd, read_buffer,read_size);
if (read_result > 0) return string(read_buffer);
}
cerr << "Unable to recieve message from client socket " << client_socket_fd << endl;
return "";
}
int ServerSocket::_send(int client_socket_fd, string message) {
//char *header = client_buffers[client_socket_fd].write_header;
char *write_buffer = client_buffers[client_socket_fd].write_buffer;
char header[16];
memset(header, 0, sizeof(header));
string write_size = to_string(message.length());
copy(write_size.begin(), write_size.end(), header);
int write_result = write(client_socket_fd, header, 16); // sending size of message
if (write_result > 0) {
write_result = write(client_socket_fd, message.c_str(), message.length());
}
if (write_result <= 0)
cerr << "Unable to send to client socket fd : " << client_socket_fd << endl;
return write_result;
}
Sending and receiving part in Client.cpp
string ClientSocket::_recieve(){
char read_header[16];
memset(read_header, 0, sizeof(read_header));
int read_result = read(socket_fd, read_header, 16);
if (read_result >0) {
int read_size = stoi(string(read_header));
memset(recieve_buffer, 0, sizeof(recieve_buffer));
read_result = read(socket_fd, recieve_buffer, read_size);
}
if ( read_result > 0) return string(recieve_buffer);
cerr << "Unable to read from server." << endl;
return "";
}
int ClientSocket::_send(string message) {
char write_header[16];
memset(write_header, 0, sizeof(write_header));
cout << message.length() << endl;
string s = to_string(message.length());
copy(s.begin(),s.end(), write_header);
int write_result = write(socket_fd, write_header, 16);
if (write_result > 0)
write_result = write(socket_fd, message.c_str(), message.length());
if (write_result <=0) cerr << "Unable to send message : "<< message << endl;
return write_result;
}
The code exhibits the two most frequent errors when using sockets:
Socket send/write and recv/read may not send/receive the number of bytes requested. The code must handle partial reads/writes in order to work correctly.
The received socket data is not zero-terminated. You need to zero-terminate the received data before passing it to functions that expect zero-terminated stings (std::string and stoi here). memset doesn't help when recv fills the entire buffer, you need to reserve one extra byte for the null terminator that recv doesn't overwrite.
Recently, i've been assigned a client/server project, which is basically a chat room, where files can be sent and recieved, and we can use webcams.
I'm currently working on the file transfer part, and after looking at some online tutorials, i've noticed most of them use offsets to write into their buffers, then they write the whole buffer into their new file.
To replicate that kind of code, i've set up 2 buffers, one on the client side, the other on the server side. On the server side, i read 8192 bytes from my file, into the buffer, then i send it into the client side, which recieves it, and adds it to my buffer. Problem is, after the second file transfer, every single transfer it does, it's a SOCKET_ERROR, which probably means something's not quite right.
server:
std::ifstream readFile;
readFile.open(FileName, std::ios::binary | std::ios::ate);
if (!readFile)
{
std::cout << "unable to open file" << std::endl;
}
int FileSize = readFile.tellg();
readFile.seekg(0);
int remainingBytes = 0;
uint32_t FileSize32t = (uint32_t)FileSize;
FileSize32t = htonl(FileSize32t);
send(connections[ID], (char*)&FileSize32t, sizeof(uint32_t), 0);
int sent_bytes = 0;
int offset = 0;
char data[8192];
remainingBytes = FileSize;
int i = 0;
while (i<6)
{
readFile.read(data, 8192);
if (remainingBytes < 8192)
{
sent_bytes = send(connections[ID], data+offset, remainingBytes, 0);
remainingBytes -= sent_bytes;
offset += sent_bytes;
}
else
{
sent_bytes = send(connections[ID], data+offset, 8192, 0);
if (sent_bytes == SOCKET_ERROR)
{
std::cout << "erro" << std::endl;
}
remainingBytes -= sent_bytes;
offset += sent_bytes;
std::cout <<"offset: "<< offset << std::endl;
std::cout << "Sent bytes: " << sent_bytes << std::endl;
std::cout << "remaining bytes: " << remainingBytes << std::endl;
}
i++;
}
Client:
char data[8192];
std::ofstream writeFile;
writeFile.open("Putin.jpg", std::ios::binary);
int bytesReceieved = 0;
int totalBytesReceieved = 0;
int i = 0;
while (i<6)
{
if (recvFileSize - totalBytesReceieved < 8192)
{
bytesReceieved = recv(connection, data+totalBytesReceieved, recvFileSize - totalBytesReceieved, 0);
totalBytesReceieved += bytesReceieved;
}
else
{
bytesReceieved = recv(connection, data + totalBytesReceieved, 8192, 0);
totalBytesReceieved += bytesReceieved;
std::cout << totalBytesReceieved << std::endl;
}
i++;
}
writeFile.write(data, totalBytesReceieved);
std::cout << "transferĂȘncia terminada, bytes recebidos: " << totalBytesReceieved << std::endl;
writeFile.close();
Do note that this is just a test program, and it's preety much one of my first interactions with C++. I've been told this probably isn't the best way to start off with C++, but i need this assignment done until the 15th of september, so i need to finish it regardless. If you find any errors or problems besides my original issue do feel free to point them out and if you can, explain me why it's wrong.
Thank you very much for your help.
I tried to modify the Qt network tutorial, and implemented it like:
quint16 blockSize;
void Client::readData()
{
qDebug() << "Received Data!";
QByteArray data;
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);
if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
qDebug() << "Received DATA II with blocksize " << blockSize;
if (tcpSocket->bytesAvailable() < blockSize)
{
qDebug() << tcpSocket->bytesAvailable() << ' ' << blockSize;
return;
}
qDebug() << "Received DATA III";
in >> data;
qDebug() << data;
QByteArray dbg = data; // create a copy to not alter the buffer itself
dbg.replace('\\', "\\\\"); // escape the backslash itself
dbg.replace('\0', "\\0"); // get rid of 0 characters
dbg.replace('\n', "\\n");
//dbg.replace('"', "\\\""); // more special characters as you like
qDebug() << dbg;
QString data_string(data);
qDebug() << "Emitting Signal";
emit Client::gotData(data_string);
}
void Server::sendData(void)
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << "Hello World, this is a very long text!";
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
qDebug() << "First number is: " << (quint16)(block.size() - sizeof(quint16));
qDebug() << "Blocksize is: " << block.size() << "with quint size: " << sizeof(quint16);
qDebug() << "Sending data!";
//Debug
QByteArray dbg = block; // create a copy to not alter the buffer itself
dbg.replace('\\', "\\\\"); // escape the backslash itself
dbg.replace('\0', "\\0"); // get rid of 0 characters
dbg.replace('\n', "\\n");
//dbg.replace('"', "\\\""); // more special characters as you like
qDebug() << dbg;
connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
clientConnection->write(block);
//clientConnection->disconnectFromHost();
}
Connecting works fine, but when I call sendData(), I get (from the same function) the transmitted block:
First number is: 43
Blocksize is: 45 with quint size: 2
Sending data!
"\0+\0\0\0'Hello World, this is a very long text!\0"
My first problem is: Where do all the \0 come from? As far as I understand the code, first I'm creating a 0, then I write the text, and them I am going back to the front and write the full size of the block. Is it because of the size of the (quint16)? When entering nothing, I get \0\0 and a size of 0, which is correct. The size of the example above is 43, which corresponds to + in ascii (and this sign is in the block above, too).
My second problem is: My readData()-function does not recognize the block size, (it always returns 450 as block size, which is clearly wrong). Why? Did I miss something?
UPDATE: After changing QByteArray data; to QString data; my problems are gone, no more strange \0 in my code -> should have used the right data type -> Head->Desk
I'm writing a simple network application. Client sending to server message server printing it in QTextEdit and responding to client .
I'm using QTcpServer and QTcpSocket.
There is a problem I can't solve. Receiving data is quint16 + QTime + QString which is sending as QByteArrey.
I use quint16 for receiving size of data blocks. And for some reason when client send to server
next block size: 16 (quint16 value)
block size: 18
Server get:
next block size: 30073 (quint16 value)
block size: 18
As you can see, for some reason server getting from QDataStrem wrong variable value and it is always 30073. I don't understand why?
void Widget::slotSendToServer()
{
logTextEdit->append("slotSendToServer()");
QByteArray arrBlock;
QDataStream serverSendStream(&arrBlock, QIODevice::ReadWrite);
QString messageStr = messageLineEdit->text();
serverSendStream << quint16(0) << QTime::currentTime()
<< messageStr;
serverSendStream.device()->seek(0);
serverSendStream << (quint16)(arrBlock.size() - sizeof(quint16));
qDebug() << "next_block_size:"
<<(quint16)(arrBlock.size() - sizeof(quint16))
<< endl
<< "full size of Byte arrey:" << arrBlock.size();
tcpSocket->write(arrBlock);
messageLineEdit->clear();
}
void Widget::slotReadClient()
{
logTextEdit->append("slotReadClient()");
QTcpSocket *tcpSocket = (QTcpSocket*)sender();
QDataStream clientReadStream(tcpSocket);
while(true)
{
if (!next_block_size)
{
if (tcpSocket->bytesAvailable() < sizeof(quint16))
{
break;
}
clientReadStream >> next_block_size;
}
if (tcpSocket->bytesAvailable() < next_block_size)
{
break;
}
QTime time;
QString messageTextStr;
clientReadStream >> time >> messageTextStr;
QString messageCompleteStr =
time.toString() + " " + "Client has sent - "
+ messageTextStr;
logTextEdit->append("Message received: ");
logTextEdit->append(messageCompleteStr);
next_block_size = 0;
sendToClient(tcpSocket,
"Server Response: Received \""
+ messageTextStr + "\"");
}
}
You should ensure that the variable next_block_size is initialized to 0 each time the socket is connected.
If you don't reuse the same QTcpSocket object, this can be done in your Widget class constructor, or if you do, in a slot connected to signal connected().
I have no idea why you did it it so complicated.
This should work:
void Widget::slotSendToServer()
{
logTextEdit->append("slotSendToServer()");
QByteArray arrBlock;
QDataStream serverSendStream(tcpSocket);
serverSendStream << QTime::currentTime()
<< messageLineEdit->text();
messageLineEdit->clear();
}
void Widget::slotReadClient()
{
logTextEdit->append("slotReadClient()");
QTcpSocket *tcpSocket = (QTcpSocket*)sender();
QDataStream clientReadStream(tcpSocket);
QTime time;
QString message;
clientReadStream >> time >> message;
emit YourSignal(time, message);
}
You don't have to worry about sizes, QDataStream keep track of it, you only have to maintain same sequence of read as it was done on write, so your buffer arrBlock is waste of code.
According to documentation my code should block when not all data are available at the moment of reading and this is only disadvantage. For small data like date and some string it will never happen.
About your code, you have messed up your reader with while loop for sure, so here is correction without this loop.
void Widget::slotReadClient()
{
logTextEdit->append("slotReadClient()");
QTcpSocket *tcpSocket = (QTcpSocket*)sender();
QDataStream clientReadStream(tcpSocket);
if (next_block_size==0) {
// need to read data size
if (tcpSocket->bytesAvailable() < sizeof(quint16))
return; // wait for next signal
// next_block_size must be type of qint16
clientReadStream >> next_block_size;
}
if (tcpSocket->bytesAvailable() < next_block_size) {
// not enought data to complete read immediately
return; // wait for next signal
}
QTime time;
QString messageTextStr;
clientReadStream >> time >> messageTextStr;
QString messageCompleteStr =
time.toString() + " " + "Client has sent - "
+ messageTextStr;
logTextEdit->append("Message received: ");
logTextEdit->append(messageCompleteStr);
// mark that data read has been finished
next_block_size = 0;
}
hopefully this will be my last C++ question related to sockets.
I have a .SWF file and it sends a policy file request.
I check if my incoming data char 0 is <, so like this:
if (raw[0] == '<')
Then I send my policy shit:
send(this->s, Env::Policy().c_str(), sizeof(Env::Policy()), 0);
std::cout << "Sent " << Env::Policy().c_str() << std::endl;
running = false;
closesocket(this->s);
break;
break; will stop the while (this->running) loop.
My policy string:
std::string Env::Policy()
{
char c = 0;
return "<?xml version=\"1.0\"?>\r\n<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">\r\n<cross-domain-policy>\r\n<allow-access-from domain=\"*\" to-ports=\"1-31111\" />\r\n</cross-domain-policy>" + c;
}
But every time I send it, nothing happens. The socket won't receive a new connection (like in C# socket server). But when I reconnect on the .swf, it will accept a new connection.
What's going on?
My full while(this->running) loop:
while (running)
{
char c[256];
int bits = recv(s, c, sizeof(c), 0);
if (bits > 0)
{
std::string data = c;
std::string raw = data.substr(0, bits);
std::cout << "First char: " << raw[0] << std::endl;
if (raw[0] == '<')
{
send(this->s, Env::Policy().c_str(), sizeof(Env::Policy()), 0);
std::cout << "Sent " << Env::Policy().c_str() << std::endl;
running = false;
closesocket(this->s);
break;
}
int header = Env::B64Decode(raw.substr(3, 2));
switch (header)
{
case 202:
this->msg = new ServerMessage("DA");
this->msg->AddInt32(6);
this->msg->AddInt32(0);
this->msg->AddInt32(1);
this->msg->AddInt32(1);
this->msg->AddInt32(1);
this->msg->AddInt32(3);
this->msg->AddInt32(0);
this->msg->AddInt32(2);
this->msg->AddInt32(1);
this->msg->AddInt32(4);
this->msg->AddInt32(1);
this->msg->AddString("dd-MM-yyyy");
this->msg->AddChar(2);
this->sendData(this->msg->toString());
this->msg = new ServerMessage("#H");
this->msg->AddString("[100,105,110,115,120,125,130,135,140,145,150,155,160,165,170,175,176,177,178,180,185,190,195,200,205,206,207,210,215,220,225,230,235,240,245,250,255,260,265,266,267,270,275,280,281,285,290,295,300,305,500,505,510,515,520,525,530,535,540,545,550,555,565,570,575,580,585,590,595,596,600,605,610,615,620,625,626,627,630,635,640,645,650,655,660,665,667,669,670,675,680,685,690,695,696,700,705,710,715,720,725,730,735,740]");
this->msg->AddChar(2);
this->sendData(this->msg->toString());
break;
default:
std::cout << "Unregistered header " << header << std::endl;
break;
}
}
else
{
break;
}
}
std::string data = c;
is only good if the string is surely 0-terminated
std::string raw = data.substr(0, bits);
you could do that simpler
const std::string raw(c, c+bits);
in your policy function there's a char c for no reason, but if it had value >0, would likely cause problems.
And most importantly, sending sizeof(Env::Policy()) bytes makes no sense at all, you shall send the whole string!
const auto& policy = Env::Policy();
send(this->s, policy, policy.size() + 1, 0);
maybe without +1, depending if you want the 0.