Search code examples
python-3.xpyserial

Concatenate multiple row serial string


I have successfully written a string of ASCII commands to send over RS232 to a thermal label printer, the code gets executed perfectly although I would like to automatise the code so that when a value or string gets stored into a variable it gets printed as part of the ASCII command.

I have done a little research and tried to look into the old posts but I wasn't able to retrieve any information related to my issue.

The first issue I encountered was the presence of multiple quotations inside the string that I believe to have been fixed by adding the triple quotes syntax. The second issue is that I would like to concatenate the string that is also on multiple rows.

What would be the best approach to substitute the value to be printed in the string from the value of the variable so that it successfully concatenates and send multiple rows of data?

This is my code:

    serial_port.write  (b'SIZE 780,516\r\n'\
                        b'GAP 2 mm,0\r\n'\
                        b'DIRECTION 1\r\n'\
                        b'FOORMFEED\r\n'\
                        b'CLS\r\n'\
                        b'TEXT 25,50,"0",0,10,10,"Quality"\r\n'\
                        b'TEXT 25,50,"0",0,10,10,"Grade"\r\n'\
                        b'TEXT 25,75,"0",0,10,10,"Date and Time"\r\n'\
                        b'TEXT 25,100,"0",0,10,10,"Total Weight (Kg)"\r\n'\
                        b'PRINT 1,1\r\n')

And I would like to substitute the string and achieve achieve something like this:

    quality = 'Quality No 1'
    text_1 = """ b'TEXT 25,25,"0",0,10,10," """
    text_end = """ "\r\n' """             

    serial_port.write  (b'SIZE 780,516\r\n'\
                        b'GAP 2 mm,0\r\n'\
                        b'DIRECTION 1\r\n'\
                        b'FOORMFEED\r\n'\
                        b'CLS\r\n'\
                        text_1 + quality + text_end\
                        b'TEXT 25,50,"0",0,10,10,"Grade"\r\n'\
                        b'TEXT 25,75,"0",0,10,10,"Date and Time"\r\n'\
                        b'TEXT 25,100,"0",0,10,10,"Total Weight (Kg)"\r\n'\
                        b'PRINT 1,1\r\n')

In order to make the printer print Quality No 1.

So far I wasn't able to successfully concatenate the string without getting a syntax error, I have tried:

text_1 + quality + text_end\

(text_1 + quality + text_end)\

str(text_1 + quality + text_end)\

And this one returns cannot mix bytes and no bytes literals:

""" b'TEXT 25,25,"0",0,10,10," """ + quality + """ "\r\n' "\

I am a bit out of ideas, anyone got any idea on how to make this work? I hope I have explained the problem properly.


Solution

  • What you want to do is called string formatting and there are multiple ways to do it, for example f-strings. One complication is that byte strings (which I assume you require for your serial port communication) do not support this. So you could use a regular string and then encode that before sending. For that, you need to explicitly specify the encoding, e.g. ascii:

    quality = 'Quality No 1'
    commands = 'SIZE 780,516\r\n' + \
               'GAP 2 mm,0\r\n' + \
               'DIRECTION 1\r\n' + \
               'FOORMFEED\r\n' + \
               'CLS\r\n' + \
               f'TEXT 25,50,"0",0,10,10,"{quality}"\r\n' + \
               'TEXT 25,50,"0",0,10,10,"Grade"\r\n' + \
               'TEXT 25,75,"0",0,10,10,"Date and Time"\r\n' + \
               'TEXT 25,100,"0",0,10,10,"Total Weight (Kg)"\r\n' + \
               'PRINT 1,1\r\n'
    serial_port.write(commands.encode('ascii'))