Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Saving incoming data from serial port to txt file
Forum Updated to NodeBB v4.3 + New Features

Saving incoming data from serial port to txt file

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vitorino
    wrote on last edited by
    #1

    Hi, I need to save incoming data from my serial port into a txt file. The data is constantly coming and just stop when a button is clicked. The serial part is working but the saving into txt no. Does someone can explain it with code? Tks

    @void SaveLog::saveFile(QString LogTobeSaved)
    {
    FileTxt->open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(FileTxt);
    out<<LogTobeSaved;
    FileTxt->close();
    }@

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on last edited by
      #2

      Maybe something like this:

      @
      void SaveLog::saveFile(QString LogTobeSaved)
      {
      FileTxt->open(QIODevice::WriteOnly | QIODevice::Text);
      FileTxt->write(LogTobeSaved.toLatin1());
      FileTxt->close();
      }
      @

      Not sure why you need the QTextStream object.

      You might want to make sure the function is only called once. If you call this with an empty string it would clear the file.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SysTech
        wrote on last edited by
        #3

        Without knowing what you are getting over your serial port this is a bit hard. As Rondog said, if you got a few bytes or a string, wrote them to the file then got more data you'd tromp on the previously written stuff.

        QSerialPort has a function "bytesAvailable". You could look at that and continue to append your serial port data into a QByteArray or a QString. When you are ready then use the function above to write it all at once.

        One last thing to think about is file write privileges. You don't really specify what FileTxt is. Is it in the writeable area? See QStandardPaths which can give you paths to writeable areas.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved