0

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.

floxia
  • 67
  • 1
  • 7
  • Does this answer your question? [String formatting: % vs. .format vs. f-string literal](https://stackoverflow.com/questions/5082452/string-formatting-vs-format-vs-f-string-literal) – Jan Wilamowski Jan 05 '22 at 06:49
  • @JanWilamowski, thanks for your reply, I had a look at the post you have recommended and it contains quite a fair bit of information and I'm not going to lie looks like hieroglyphs to me. I am new to the coding world so I don't understand where and what I should be looking for in that post. Could you kindly point me in the right direction? – floxia Jan 05 '22 at 06:55

1 Answers1

1

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'))
Jan Wilamowski
  • 3,308
  • 2
  • 10
  • 23
  • Thank you so much for solving this,, I was't expecting something so easy, I definitely have to read more into the f string and how it works, I am sure you are correct about the byte thing, I will try this and see how the printer responds! – floxia Jan 05 '22 at 07:45