0

( raspbian, rp3)

I can send a string from PC console to my GUI (tkinter). An example:

$SK039AR878HT010;

( I send this command manually from console for testing purpose, but in future my console will be replaced by a machine)

This command basically indicates: SK=3.9, AR=87.8 and HT=10% ( progress bar). These values goes to the GUI and set these on the GUI accordingly.

In parallel, the GUI continuously also sends a similar string to the console ( in response to what I sent:$SK039AR878HT010;)

$ST039HT10;

(ST is set on the GUI manually)

Now the problem is the communication protocol says, there should be three digits after each variable. But as can be seen that for HT , it is followed by two digits i.e 10. The correct format is

$ST039HT010;

To achieve the same with ST ( as you can see 039 a three digit is there, which represent 3.9), I did this:

            if self.App.stval<10:

               self.App.port.write(serial.to_bytes(str.encode('$'+ 'ST'+'0' + str(int(self.App.stval*10))+'HT'+ str(int(self.App.mpb["value"]))+';' )) )
            else:
                self.App.port.write(serial.to_bytes(str.encode('$'+ 'ST' + str(int(self.App.stval*10))+'HT'+str(int(self.App.mpb["value"]))+';' )) )

here stval is value of ST ( set temperature, done from GUI) which is 3.9 deg C in above case.

That is, whenever I have ST value (stval) less than 10 , I insert a '0'. ST is thus followed by three digits.

I am unable to develop a logic , (also as I am new to python) as to how to do the same three digit formatting with HT? HT is a value on progress bar ( whole number) , and cant go beyond 100%, while ST is room temperature which will hardly exceed 50% ( can be decimal).

I will really appreciate for helping me out in this regard.

gpuguy
  • 4,607
  • 17
  • 67
  • 125
  • 1
    Possible duplicate of [Best way to format integer as string with leading zeros?](https://stackoverflow.com/questions/733454/best-way-to-format-integer-as-string-with-leading-zeros) – Mad Physicist Jul 20 '18 at 04:18

1 Answers1

1

You can do this by basic string formatting.

stval = '$ST39HT10'
st_values = stval.split('ST')[1].split('HT')
st_values = ['0' + value if len(value)<3 else value for value in st_values ]
new_stval = '$ST'+ st_values[0] + 'HT' + st_values[1]
print(new_stval)

output

$ST039HT010

If the stval is not a string, please use str(htval).zfill(3) to pad zeroes to the value.

JISHAD A.V
  • 361
  • 1
  • 9
  • Thank you very much for your comment. But actually here stval is value of ST ( set temperature, done from GUI) which is 3.9 deg C in above case. It is not the string as in your code. – gpuguy Jul 20 '18 at 04:26
  • Okay. Then please use padding of zeroes. `str(htval).zfill(3)` . As htval is a real number it will work. – JISHAD A.V Jul 20 '18 at 04:52
  • zfill(3) solved my problem. Thank you very much. Please post this in your answer so that I can select it as accepted answer. – gpuguy Jul 21 '18 at 03:03
  • It is added to the answer. Thanks. – JISHAD A.V Jul 23 '18 at 09:10