Results 1 to 8 of 8

Thread: Moving stage using udp command in qt gui

  1. #1
    Join Date
    Jan 2016
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Moving stage using udp command in qt gui

    Hi all,

    I am new to qt, I am trying to make a gui to control my stage(based on stepper motor). it uses udp protocol to send commands. command structure is as:
    header =07 ( 2 bytes)
    command to move = FL10000 ( to move 1 cm)
    termination <CR> = 13

    I made a push button and created slot for the same shown below:

    Qt Code:
    1. void MainWindow::on_move_1cm_clicked()
    2. {
    3. QByteArray datagram;
    4. datagram[0] = 0;
    5. datagram[1] = 7;
    6. datagram[2] = 70;
    7. datagram[3] = 76;
    8. datagram[4] = 45;
    9. datagram[5] = 49;
    10. datagram[6] = 48;
    11. datagram[7] = 48;
    12. datagram[8] = 48;
    13. datagram[9] = 48;
    14. datagram[10] = 13;
    15.  
    16. QUdpSocket udpSocket;
    17. udpSocket.writeDatagram(datagram, QHostAddress("10.10.10.10"),7775);
    To copy to clipboard, switch view to plain text mode 

    The problem I am facing is on clicking the button at the first time the stage move, but doesnot when I click it again.
    to make this code work again i have to power off my motor.
    can anyone tell me what can be wrong.
    I am planning on having 3-4 push buttons for different length of move.

    Please help me out.

    thanks.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Moving stage using udp command in qt gui

    Given that "FL10000" is a string of ASCII characters, and you say the header is two bytes, then I would expect those two bytes to be two ASCII characters also. Try this:
    Qt Code:
    1. QByteArray datagram("07FL10000\r");
    To copy to clipboard, switch view to plain text mode 
    That is, the first two bytes have value 48 and 55 decimal.

  3. #3
    Join Date
    Jan 2016
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Moving stage using udp command in qt gui

    Quote Originally Posted by ChrisW67 View Post
    Given that "FL10000" is a string of ASCII characters, and you say the header is two bytes, then I would expect those two bytes to be two ASCII characters also. Try this:
    Qt Code:
    1. QByteArray datagram("07FL10000\r");
    To copy to clipboard, switch view to plain text mode 
    That is, the first two bytes have value 48 and 55 decimal.

    this didnt work,

    my code works[with first 2 bytes as 0 and 7] but only once..I have to restart the system and compile the program again or i have to disable the connection and re-enable it everytime. do I have to use socket close or something? what will be syntax for that.
    Last edited by ankit.1g@gmail.com; 28th January 2016 at 20:34.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,481
    Thanks
    252
    Thanked 711 Times in 701 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Moving stage using udp command in qt gui

    i have to disable the connection and re-enable it everytime
    Did you read the documentation (and example) for using QUdpSocket? You are creating a new UDP socket on the stack every time you execute the button click slot. The firswt time around, this works, but maybe the second time around the old socket is still connected (even though UDP is supposed to be connectionless). Try creating a single QUdpSocket instance as a member of your MainWindow class instead.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Moving stage using udp command in qt gui

    It moved != it is working correctly... Even the first time

    If the two bytes 0x00, 0x07 represent the length of the following command string then:
    • Should the length include or exclude the trailing CR? Currently it would exclude the CR, which might leave the controller with a spurious byte in a buffer when it receives the next command datagram.
    • Should the length value include the two bytes of the length indicator itself? Different spurious bytes in the buffer.
    • Is the byte order correct? 0x00, 0x07 could mean 7 or 1792 decimal. Might still be waiting for more bytes to arrive.

  6. #6
    Join Date
    Jan 2016
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Moving stage using udp command in qt gui

    Quote Originally Posted by ChrisW67 View Post
    It moved != it is working correctly... Even the first time

    If the two bytes 0x00, 0x07 represent the length of the following command string then:
    • Should the length include or exclude the trailing CR? Currently it would exclude the CR, which might leave the controller with a spurious byte in a buffer when it receives the next command datagram.
    • Should the length value include the two bytes of the length indicator itself? Different spurious bytes in the buffer.
    • Is the byte order correct? 0x00, 0x07 could mean 7 or 1792 decimal. Might still be waiting for more bytes to arrive.
    the command is as:
    Header = 07 (two bytes)
    FL1000
    <cr> (ASCII carriage return) = 13.

    They only have C# example in the manual which forms the send bytes as:
    Qt Code:
    1. sendBytes[0] = 0;
    2. sendBytes[1] = 7;
    3. Byte[] SCLstring = Encoding.ASCII.GetBytes(“RV”); // for sendin RV command///
    4. // copy string to the byte array
    5. System.Array.Copy(SCLstring, 0, sendBytes, 2, SCLstring.Length);
    6. // insert terminator
    7. sendBytes[sendBytes.Length - 1] = 13; // CR
    8. // send it to the drive
    9. udpClient.Send(sendBytes, sendBytes.Length);
    To copy to clipboard, switch view to plain text mode 



    should i create array of specific length then?

    Quote Originally Posted by d_stranz View Post
    Did you read the documentation (and example) for using QUdpSocket? You are creating a new UDP socket on the stack every time you execute the button click slot. The firswt time around, this works, but maybe the second time around the old socket is still connected (even though UDP is supposed to be connectionless). Try creating a single QUdpSocket instance as a member of your MainWindow class instead.
    How can I do that...? creating in main window? can you please give me an example.
    thank you
    Last edited by ankit.1g@gmail.com; 1st February 2016 at 14:39.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Moving stage using udp command in qt gui

    Quote Originally Posted by ankit.1g@gmail.com View Post
    the command is as:
    Header = 07 (two bytes)
    FL1000
    <cr> (ASCII carriage return) = 13.
    This is different to your first post. Fewer zeroes.
    They only have C# example in the manual which forms the send bytes as:
    Qt Code:
    1. sendBytes[0] = 0;
    2. sendBytes[1] = 7;
    3. Byte[] SCLstring = Encoding.ASCII.GetBytes(“RV”); // for sendin RV command///
    4. // copy string to the byte array
    5. System.Array.Copy(SCLstring, 0, sendBytes, 2, SCLstring.Length);
    6. // insert terminator
    7. sendBytes[sendBytes.Length - 1] = 13; // CR
    8. // send it to the drive
    9. udpClient.Send(sendBytes, sendBytes.Length);
    To copy to clipboard, switch view to plain text mode 
    I am no C# guru, but this looks like it overwrites the 'V' with the <CR> Unless the array is a fixed length prefilled with spaces or GetBytes() returns a trailing terminator byte and that is counted in the length. Have you tried mimicking this command?
    should i create array of specific length then?
    No, you should create a QByteArray of the correct length for a correct command. We have no way to know whether what you are sending is correct. That will be somewhere in the documentation for the device. I have given you a list of generic ways it may be wrong.
    How can I do that...? creating in main window? can you please give me an example.
    thank you
    You create a QUdpSocket member variable in the main window class, exactly as d_stranz wrote. Then you use that member variable instead of creating a new local object everytime you need one. No magic involved, just C++ 101.
    Qt Code:
    1. class MainWindow: public QMainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. MainWindow(QWidget *p = 0);
    6. private:
    7. QUdpSocket *m_socket;
    8. }
    9.  
    10.  
    11.  
    12. MainWindow::MainWindow(QWidget *p = 0):
    13. {
    14. ...
    15. m_socket = new QUdpSocket(this);
    16. }
    17.  
    18. // elsewhere
    19. m_socket->writeDatagram(datagram, ...);
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to ChrisW67 for this useful post:

    ankit.1g@gmail.com (1st February 2016)

  9. #8
    Join Date
    Jan 2016
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Moving stage using udp command in qt gui

    Quote Originally Posted by ChrisW67 View Post
    This is different to your first post. Fewer zeroes.

    I am no C# guru, but this looks like it overwrites the 'V' with the <CR> Unless the array is a fixed length prefilled with spaces or GetBytes() returns a trailing terminator byte and that is counted in the length. Have you tried mimicking this command?

    No, you should create a QByteArray of the correct length for a correct command. We have no way to know whether what you are sending is correct. That will be somewhere in the documentation for the device. I have given you a list of generic ways it may be wrong.

    You create a QUdpSocket member variable in the main window class, exactly as d_stranz wrote. Then you use that member variable instead of creating a new local object everytime you need one. No magic involved, just C++ 101.
    Qt Code:
    1. class MainWindow: public QMainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. MainWindow(QWidget *p = 0);
    6. private:
    7. QUdpSocket *m_socket;
    8. }
    9.  
    10.  
    11.  
    12. MainWindow::MainWindow(QWidget *p = 0):
    13. {
    14. ...
    15. m_socket = new QUdpSocket(this);
    16. }
    17.  
    18. // elsewhere
    19. m_socket->writeDatagram(datagram, ...);
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot...!!! It turns out that binding to any one port solved my problem.
    but having qbytes with correct length and making socket once is very nice suggestion thanks a lot for the example.

Similar Threads

  1. Replies: 1
    Last Post: 15th May 2015, 21:47
  2. Replies: 3
    Last Post: 2nd December 2012, 14:10
  3. Stage active event from QML file
    By AbinaThomas in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2012, 05:05
  4. How to make fewer errors at the stage of code writing. Part N3. QT.
    By Andrey_Karpov in forum General Discussion
    Replies: 0
    Last Post: 13th July 2011, 07:39
  5. Replies: 1
    Last Post: 12th April 2010, 12:00

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.