Results 1 to 4 of 4

Thread: how can 20 Bytes be put into a string[20] in Qt and then be sent by a serial port

  1. #1
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default how can 20 Bytes be put into a string[20] in Qt and then be sent by a serial port

    Hi
    I have two questions. I need first to put 20 bytes into a string[20]. In C++ i did that by this code:
    Qt Code:
    1. string[0]='ab'; //ab is in Hex (0xab)
    2. string[1]='50'; //50 is in Hex (0x50)
    3. .
    4. .
    5. .
    6. string[19]='54' //54 is in Hex
    To copy to clipboard, switch view to plain text mode 

    After filling string[20], i must send it by serial port.
    My first question is: how could i write above code in Qt to send by a serialport?
    and my second question is: can i use QByteArray and then use QSerialPort::write(QByteArray)?

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

    Default Re: how can 20 Bytes be put into a string[20] in Qt and then be sent by a serial port

    The answer to the second question is yes.

    The answer to the first is in two parts:
    1. The code you present does not do what you describe assuming that "string" is an array of char. 'ab' is not a character literal equal to 0xab.
    2. When you correct that, you can use QByteArray in the same way.

    Your program will still be in C++: Qt is not a language.
    Last edited by ChrisW67; 18th November 2016 at 08:01.

  3. #3
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how can 20 Bytes be put into a string[20] in Qt and then be sent by a serial port

    Quote Originally Posted by ChrisW67 View Post
    When you correct that, you can use QByteArray in the same way.
    Thanks Chris. Then must i use QByteArray::append() to fill 20 bytes into a QByteArray? is this garantide to send exactly 20 bytes to another device by serial port?

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

    Default Re: how can 20 Bytes be put into a string[20] in Qt and then be sent by a serial port

    You can use QByteArray::append() and if you append 20 bytes to an empty QByteArray then that's how many bytes will be sent. Using append() will extend the array to add the new bytes.

    You can use a number of different QByteArray constructors or QByteArray::resize() to create an "empty" 20 byte buffer and operator[] to address individual bytes.in this way the QByteArray code could even look like the bare C-style code you started with.

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

    Alex22 (20th November 2016)

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, 08:49
  2. qt serial port
    By saman_artorious in forum Newbie
    Replies: 9
    Last Post: 24th April 2013, 16:19
  3. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 14:12
  4. Serial Port
    By b1 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2007, 02:05
  5. Serial Port access in Qt
    By Doug Broadwell in forum Newbie
    Replies: 1
    Last Post: 30th November 2006, 09:45

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.