Hi all,

I am trying to read back the udp data , sent from my stepper motor. Reply varies from 3-12 bytes.

I am currently doing this to read my udp data:

Qt Code:
  1. qint64 size = udpSocket->pendingDatagramSize();
  2. QHostAddress sender;
  3. quint16 senderPort;
  4. char* reply = new char[size]
  5. qint64 res = udpSocket->readDatagram( reply, size, &sender, &senderPort );
  6.  
  7. char buf[12];
  8. for ( int i = 2; i <= 11; ++i )
  9. buf[ i -2 ] = reply[ i ];
  10.  
  11. ui->Position->setText(buf);
To copy to clipboard, switch view to plain text mode 

The problem I am facing is that sometime there are only 4 bytes sometime there are 11 bytes and when there are less bytes suppose 4-5 my text box will be filled with weird characters of unreadable format.
how can I change my current code to just read proper bytes received.