0

this is my first time asking a question so please have some patience with me.

I'm trying to read the content of a text file which is meant to be sent to a printer. In the middle of the characters that define how the label is printed there are control characters, STX, SOH, CR, LF. In my example, I will read the contents of the file and pass them to a data structure(array) in the memory, append some data to it and later send it to the printer directly, erasing the data in that structure.

The function that does this is the following:

void ClientThread::readFile2Structure(bool GoodOrBad)
{
    int i = 0;
    int j = 0;

    // Clean structure



    // here clean the structure

    // According to the name comes from the arguments.
    if(GoodOrBad == 1)
    {
        labelFile.setFileName(labelPathGood);
        //qDebug() << "Fez o set filepath" << labelPathGood;
    }
    else if(GoodOrBad == 0)
    {
        labelFile.setFileName(labelPathBad);
        //qDebug() << "Fez o set filepath" << labelPathBad;
    }

    if (!labelFile.open(QIODevice::ReadOnly))
    {
        qDebug() << "Unable to open label definition file PrinterGood.ini/PrinterBad.ini!";
        return;
    }
    else
    {
        while (!labelFile.atEnd())
        {
           temporaryStructure[i] = labelFile.readLine();
           i++;
        }

        // for debug purposes only!
        qDebug() << "Structure";

        for(j=0; j<=i; j++)
        {
            qDebug() << temporaryStructure[j];
        }
    }
}

The data structure has the following definition: QByteArray temporaryStructure[50];

The file to be sent is: STX f259 CRLF SOH D STX L CRLF STX L CRLF D11 CRLF PC CRLF SC CRLF H25 CRLF 1W1c66000004100202000016036 CRLF

The data in bold are the control characters.

In my console, when I execute the function above, all the control characters appear as small squares and CRLF does not even appear.

Why are these different control characters appearing as small squares and are these going to be modified with the copying to a QByteArray array?

I'm afraid that nothing comes out of the printer and I still haven't got one to test with, FreeSerialPort Monitor will be the way to see if everything is coming out right.

Thank you for your attention.

mwerschy
  • 1,698
  • 1
  • 15
  • 26
LDS
  • 43
  • 5
  • I have to ask: what did you expect to see, when printing non-printable characters (those, whose ascii code is less then 32)? – Amartel May 14 '13 at 15:35
  • The control characters don't appear because there is no matching character to display; therefore it just prints a small square to show something is there. As for CRLF, it's a carriage return followed by a line feed, so I would expect to see the characters following the CRLF on the next line, if the console interprets them that way. Sometimes only one of CR or LF is required (can't remember which) for some consoles. – TheDarkKnight May 14 '13 at 15:35
  • @Merlin069 Line feed (which is "\n") is unix-way of beginning a new line, carriage return + line feed ("\r\n") - is windows-way. – Amartel May 14 '13 at 15:40
  • @Amartel: I thought each character had a representation regard of its value. I should have research that! Thanks – LDS May 14 '13 at 16:03
  • @Merlin069: I'm using Qt Creator(Win7), clearly that is one console that does not support them. – LDS May 14 '13 at 16:07
  • Regarding the second part of my question, "are these going to be modified with the copying to a QByteArray array?" After that, I will send the entire thing over to the printer through the serial port, I will post the result here. – LDS May 14 '13 at 16:08
  • @LDS Unicode has a lot of characters and, as far as I know, there is no font, which represents all of them. Characters, which are not represented (including most non-printable characters), are printed, using special glyph, most commonly - a square. Answering the second part of your question - `QByteArray` contains raw data and it is not modified on copying. – Amartel May 14 '13 at 16:10
  • @Amartel, thanks that's what I was referring to (unix vs windows). LDS, as Amartel says, QByteArray should not be modifying anything. It's exactly what it says - an array of bytes. – TheDarkKnight May 14 '13 at 16:21
  • There's a lot more info on CR LF here: http://stackoverflow.com/questions/1552749/difference-between-cr-lf-lf-and-cr-line-break-types – TheDarkKnight May 14 '13 at 16:25
  • @Amartel: I already managed to append all the info. Despite that, between appended text, QByteArray method append also appended a \CR\LF. I don't know if this will have some influence in the final test, but I'll give it a try and see what's coming out of it. – LDS May 15 '13 at 11:36
  • @Amartel: Yeah! Definitely it had influence. Between the original data of each line, and the data that i appended there is a \CR and a \LF. This results in nothing being printed, since the printer recognizes that as an end of command. I will have to find a way to read the line, concatenate with whatever I want to put there, and then simply copy the whole line. – LDS May 16 '13 at 07:55
  • @LDS you could use `byteArray.replace('\n', ""); byteArray.replace('\r', "");` – Amartel May 16 '13 at 08:25
  • @Amartel: thanks! Precious tip. Everytime a QString is appended to a QByteArray a \CR\LF is also appended. Everytime I read a line, I replace the \r\n with "" and after I append the data I need. – LDS May 16 '13 at 15:58
  • How can I mark and answer as valid, as this thread as answered? – LDS May 16 '13 at 15:59
  • You could mark my answer as valid ^_^ – Amartel May 17 '13 at 05:11

1 Answers1

1

Unicode has a lot of characters and, as far as I know, there is no font, which represents all of them. Characters, which are not represented (including most non-printable characters), are printed, using special glyph, most commonly - a square.

Answering the second part of your question - QByteArray contains raw data and it is not modified on copying.

In order to remove CR and LF you could use:

byteArray.replace('\n', "");
byteArray.replace('\r', "");
Amartel
  • 4,248
  • 2
  • 15
  • 21