Results 1 to 20 of 31

Thread: send binary file over serial port with QSerialDevice

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Location
    IT
    Posts
    20
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default send binary file over serial port with QSerialDevice

    Hi all,
    I'm using the class QSerialDevice in order to send datas over the serial port.
    I compiled this class and it works. I write a word and it sends over the serial port.

    Now I need to transfer a binary file.
    Inside the QSerialDevice class there are these functions:
    qint64 write(const char *data, qint64 maxSize);
    qint64 write(const char *data);
    qint64 write(const QByteArray &byteArray);

    Please can you help me to send whole binary file and send the datas using one of these functions and Qfile?
    I think it is easy but I'm not expert with data streams.

    Thank you in advance

    ilpaso

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: send binary file over serial port with QSerialDevice

    Please can you help me to send whole binary file and send the datas using one of these functions and Qfile?
    Sure, if you explain what problem you have.
    And if the problem is - that you have no idea how this is done, then first read:
    QIODeviceand
    QDataStream and QTextStream.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2010
    Location
    IT
    Posts
    20
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: send binary file over serial port with QSerialDevice

    Hi high_flyer,
    thank you for your replay.

    I open the file and I convert the file into Qbytearray variable with the function "QByteArray QIODevice::readAll()".
    After this I use the function "qint64 write(const QByteArray &byteArray)" to send the datas.

    I don't know if this is the right way (or the better way). It seems do not work.

    Thank you

    ilpaso

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: send binary file over serial port with QSerialDevice

    It seems do not work.
    QSerialDevice is not a Qt class, but a custom class made by someone.
    Usually these class will come with some documentation on how to use them - did you check that?
    Usually they also include some examples, you might want to have a look at those too.
    I have never used QSerialDevice, so I can't tell if its any good.
    I have however used QextSerialPort, and it works quite well.

    The problem you have can be all sorts of things - from failing in QSerialDevice to bad serial configuration on your part (baud rate stop bits etc).
    You deliver hardly any data as to the checks you made to all the various things that can go wrong, so its really hard to tell.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Sep 2010
    Location
    IT
    Posts
    20
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: send binary file over serial port with QSerialDevice

    Ok I understand the problem to answer to a not specific question.
    QSerialDevice has working examples but I can only send text. So the boud rate and all the serial port settings are ok.
    In the examples there is nothing for an entire binary file.

    There is not a complete documentation but I read this class derived from QIODevice class.

    I don't know if this is enough in order to help me.

    Thank you

    ilpaso

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: send binary file over serial port with QSerialDevice

    There is no large diference between sending textual or binary data.
    Read about QDataStream.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Sep 2010
    Location
    IT
    Posts
    20
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: send binary file over serial port with QSerialDevice

    thanks high_flyer for your answer,
    I read about QDataStream. As you can understand I'm a newbie in Qt.
    It is easy read a file with QDataStream but I'm not able to send the stream to the serial port.
    QSerialDevice inherits from QIODevice and send over serial port should not be difficoult but I need some clues in order to read the binary file (don't modify it) and send it over the port.
    QextSerialPort is more famous than QSerialDevice but there is the same problem.

  8. #8
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: send binary file over serial port with QSerialDevice

    I'm not able to send the stream to the serial port.
    Do you mean nothing is transmitted over the serial port ? Or only part of the datastream ? Or is your problem on the receiving side ? What do you have on the receiving side ? Another computer or some sort of electronics device ? If you connected it to another com-port, you can check with any 'hyperterminal' application if something is received. An oscilloscope may help to check what is transmitted also.

    If you create a small QByteArray with readable text in it, and then use "qint64 write(const QByteArray &byteArray)", does it get sent or not ?

    Best regards,
    Marc

  9. The following user says thank you to marcvanriet for this useful post:

    ilpaso (7th December 2010)

  10. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: send binary file over serial port with QSerialDevice

    QSerialDevice inherits from QIODevice and send over serial port should not be difficoult but I need some clues in order to read the binary file
    If you tell us where exactly your difficulty is, we could suggest solutions.
    The best way would be to show us what it is you did, what your intent was, and what is the problem you have with the code.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #10
    Join Date
    Sep 2010
    Location
    IT
    Posts
    20
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: send binary file over serial port with QSerialDevice

    Hi all,
    thanks for the help. I explain you my intent and the problems.
    The intent is to connect my Qt application to a serial printer with its own protocoll. In order to do this I've to send 2 files: the first is a binary file and the second is a text file with some commands inside. I've some commands in order to check the connection and the number of bytes I sent.
    The connection is a Rs232 with a XON/XOFF control.
    I tried 2 classes: QextSerialPort and QSerialDevice. My choice was on QSerialDevice but the 2 classes are similar.
    I creare a QByteArray reading the Qfile "textfile.bin" and I send it with "qint64 write(const QByteArray &byteArray)" function. It works!
    The problem is to send the "imageBinary.bin" with the same procedure. My Qt application returns "32Kbyte sent!" but the device receives only 7Kbyte. I'm not able to see what there is inside the file with a text editor because this is binary file.
    I'm sure the printer is ok because with its windows software it works.

    I suppose the problem is in the conversion from the binary file to QbyteArray. So I read something about QDataStream and QTextStream. I suppose QDataStream reads raw datas without a conversion.
    Another hypothesis could be that the QSerialDevice class has bug but I don't think.

    Maybe this is enough in order to help me.
    Thank you very much for your help and sorry for my Qt incompetence and my English.

    ilpaso

  12. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: send binary file over serial port with QSerialDevice

    Have you configured the serial port to work with XON/XOFF flow control?

    Do you stop sending data when XOFF is signalled to you?, and resume when XON is signalled?

    I assume that your application has a layer between the serial port driver (QSerialDevice) and your application to manage the flow control between your application and the printer ? Otherwise the QSerialPort will transmit all 32KB in one go, and the printer will ignore most of it as it was not ready.

  13. The following user says thank you to squidge for this useful post:

    ilpaso (7th December 2010)

  14. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: send binary file over serial port with QSerialDevice

    QSerialDevice has working examples but I can only send text. So the boud rate and all the serial port settings are ok.
    The serial settings (such as baud rate, flow control etc) are specific to the connection you are targeting - in your case the printer.
    So if you have correct settings for the examples, it doesn't mean they are correct for your device.
    Do you know what your serial settings should be?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  15. The following user says thank you to high_flyer for this useful post:

    ilpaso (7th December 2010)

  16. #13
    Join Date
    Sep 2010
    Location
    IT
    Posts
    20
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: send binary file over serial port with QSerialDevice

    Hi squidge,
    This night I'll read all the source code of the QSerialDevice in order to understand where is the problem. Often I think that a "ready to use" class is ready to use but often is not.
    I set the XON/XOFF and I was sure that the control was implemented inside the QSerialDevice class.

    bye
    ilpaso

Similar Threads

  1. Serial Port communication
    By mgurbuz in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2011, 02:38
  2. How Do I get the “friendly” name of serial port
    By sudheer168 in forum Qt Programming
    Replies: 14
    Last Post: 25th January 2010, 01:27
  3. data from serial port
    By bhe in forum Newbie
    Replies: 4
    Last Post: 3rd May 2009, 10:19
  4. serial port programming
    By sujatashooter in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2008, 15:51
  5. Serial Port
    By b1 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2007, 02:05

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.