Page 1 of 2 12 LastLast
Results 1 to 20 of 31

Thread: send binary file over serial port with QSerialDevice

  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

  17. #14
    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

    It may well be done there or in the OS itself.

    Regardless however, the serial device must be told that this is what you want.

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

    ilpaso (7th December 2010)

  19. #15
    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

    Quote Originally Posted by squidge View Post

    Regardless however, the serial device must be told that this is what you want.
    In the device technical specifications they wrote "XON/XOF" flow control.

  20. #16
    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

    Does it also tell you the other parameters, such as baud rate, data bits, stop bits ?

    You might also find it useful to post your code that doesn't work.

  21. #17
    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

    People, ilapso was able to send a text file and get readable output on the printer. So baudrate and such are OK. It's not a trivial issue.

    Ilapso,

    Don't be too hard on yourself. Serial communications is always a pain.

    The XON/XOFF is rarely used and probably not in the samples. When and how do you set this ? XON/XOFF is implemented in the operating system, and QSerialDevice sets the corresponding flags when opening the serial port.

    Can you set the printer to some other kind of handshaking ? E.g. RTS/CTS.

    A quick-and-dirty fix may be to send the image data in smaller portions and wait a bit in between, so that the printer is always able to process the data.

    As a test, you could set handshaking off, transmit the binary image, and see if the printer sends and XOFF back. You could also do the XON/XOFF handshaking yourself in software, sending smaller portions of data and checking if XON/XOFF is received.

    Best regards,
    Marc

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

    ilpaso (7th December 2010)

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

    Unhappy Re: send binary file over serial port with QSerialDevice

    thank you marcvanriet,
    you have understood all my problems!
    I do not understand why a class for serial communication is not ready to manage the xon/xoff flow control (or other flow control) in a easy way and so I think I'm not able to use it.
    I'll try to send the file in smaller partitions. This is a good idea!

    This is my code:
    I set the Xon/Xoff:
    Qt Code:
    1. if (!port->setFlowControl(AbstractSerial::FlowControlXonXoff)) {
    2. qDebug() << "Set flow " << AbstractSerial::FlowControlXonXoff << " error.";
    3. return;
    4. }
    To copy to clipboard, switch view to plain text mode 
    .....
    inside the function "writeBinaryFile(QString fileName)" I send the datas
    Qt Code:
    1. {
    2. QFile file(fileName);
    3. if (!file.open(QIODevice::ReadOnly)){
    4. qDebug() << "Open file fault";
    5. start(false);
    6. port->close();
    7. return;
    8. }
    9. QByteArray data = file.readAll();
    10. quint64 r = port->write(data);
    11. port->waitForBytesWritten(responseTimeout);
    12. port->waitForReadyRead(responseTimeout);
    13. cout<<r<<endl;
    14. if (r == quint64(data.size())) qDebug() << "Writed text file.";
    15. else qDebug() << "Bytes writed small: " << r;
    16. }
    To copy to clipboard, switch view to plain text mode 

    when I send a small text file all is ok. When I send a bigger binary file (32KByte) printer returns only 7Kbyte read.

    regards
    ilpaso
    Last edited by ilpaso; 7th December 2010 at 22:49.

  24. #19
    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

    Seems OK at first sight.

    By the way, it doesn't matter if you first open the port and then do port->setFlowControl or not. If you do the settings first and then open the port, QSerialDevice will make the settings on the newly opened port.

    You mentioned that your application says that 32KB is sent, and that the printer only receives 7 KB, right ? So your application says "Writed text file" but the printer hasn't received it ?

    You could take two serial ports and see what is received on the other side using e.g. teraterm. You could also take 2 other serial ports and make them listen on the transmit and receive line of the serial port of your printer to find out what is going on.

    QSerial device uses overlapped reads and writes it seems, which makes everything more complex. QSerialDevice handles the complexity for you however, so it shouldn't make a difference. QExtSerialport uses blocking reads and writes. Maybe you could try QExtSerialPort anyway.

    I once used a very basic serial class from Thierry Schneider. It is very basic, so not much can go wrong. You find it in attachment. You just have to set dcb.fOutX and dcb.fInX to 1 to make it use Xon/Xoff.

    Hope this helps,
    Best regards,
    Marc
    Attached Files Attached Files

  25. #20
    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

    Quote Originally Posted by marcvanriet View Post
    You mentioned that your application says that 32KB is sent, and that the printer only receives 7 KB, right ? So your application says "Writed text file" but the printer hasn't received it ?
    This is my last test:
    I set the Flow control OFF.
    I created a signal/slot system with SIGNAL:readyRead() and SLOT:"readTheResponse".
    This showed a Xoff char as input and after a while an Xon char.
    If I set the XON/XOFF flow control the QSerialDevice hid the XON/XOFF chars in the input data but I don't know how it manages this event.


    Quote Originally Posted by marcvanriet View Post
    I once used a very basic serial class from Thierry Schneider. It is very basic, so not much can go wrong. You find it in attachment. You just have to set dcb.fOutX and dcb.fInX to 1 to make it use Xon/Xoff.
    Thank you very much. I'll try it if I don't solve with QSerialDevice

    Regards
    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.