Results 1 to 5 of 5

Thread: serial port command doesn't work

  1. #1
    Join Date
    Jul 2015
    Posts
    18
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default serial port command doesn't work

    Hello everyone, I'm having problems of getting the development boards do whatever I want through commands sent through serial port.

    So before I wrote my Qt GUI program, I had tested my commands with a program named RealTerm Terminal that I found online, and everything worked fine, but then when I started building my Qt program and started to send the exact same commands through serial port, the board did receive the data, but it didn't do anything at all.

    For example, on RealTerm, I sent this command to my board : 1 7 2 100 0 110 4, which in hex is 01 07 02 64 00 6E 04, and it set the board ID to 100 as expected, the board also sent back an acknowledgement line, so no problem there.

    However, when I used these lines of code to send the same command to the board on my Qt program:
    Qt Code:
    1. char panID[] = {0x01, 0x07, 0x02, 0x64, 0x00, 0x6E, 0x04};
    2. serial->write(panID);
    To copy to clipboard, switch view to plain text mode 

    The board received the data (it flashed), but that was all, the panID wasn't set to 100, and the board didn't send any acknowledgement back.

    Can anyone help me to point out the problem here? I'm perplexed by this strange behavior. Thank you.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: serial port command doesn't work

    Hard to say just by reading those two lines of code. What is 'serial'? What is 'write'?

    I am going to take a shot in the dark here: does, by any chance, the method WhateverClassSerialIsAnInstanceOf::write(const char *s) write to the serial port the bytes starting at s, up to and including the first NUL byte? If it is so, then your code only writes the 5 first bytes of panID.

  3. #3
    Join Date
    Jul 2015
    Posts
    18
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: serial port command doesn't work

    Thank you for your reply and sorry for not providing enough information.

    I used QSerialport to write this program, and serial was an instance of that class, I declared it above those lines of codes
    Qt Code:
    1. QSerialPort *serial = new QSerialPort(this);
    To copy to clipboard, switch view to plain text mode 
    , and "write" is just a function of the class, writing data to the port.

    What you said can be a possibility, but I'm not really sure, do you have any suggestion on how to fix it? I would greatly appreciate your help

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: serial port command doesn't work

    Thanks for the clarification. QSerialPort is derived from QIODevice, whose write(const char *) method indeed writes a NUL-terminated string. In order to specify the number of bytes to write, call the write(const char *, qint64) method instead:
    Qt Code:
    1. serial->write(panID, sizeof(panID));
    To copy to clipboard, switch view to plain text mode 
    and check the return value to make sure everything was written.

  5. The following user says thank you to yeye_olive for this useful post:

    hovuquocan1997 (20th July 2015)

  6. #5
    Join Date
    Jul 2015
    Posts
    18
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: serial port command doesn't work

    Thank you very much! It worked! I really appreciate your help, my time was running out and you saved my life.

Similar Threads

  1. Serial read misses to read data from the serial port
    By mania in forum Qt for Embedded and Mobile
    Replies: 11
    Last Post: 18th August 2014, 09:49
  2. Replies: 0
    Last Post: 6th March 2014, 08:38
  3. qt serial port
    By saman_artorious in forum Newbie
    Replies: 9
    Last Post: 24th April 2013, 17:19
  4. Replies: 12
    Last Post: 19th July 2012, 22:22
  5. Serial Port
    By b1 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2007, 03:05

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.