0

I am using python on a Raspberry Pi. One of my function's argument will be a string containing an unknown amount of numbers.

I need to extract them all and put them in different variables. I will then pass those variables as arguments to :

t.write(serial.to_byte([OtherVar1, OtherVar2, OtherVar3, EXTRACTEDVar1, EXTRACTEDVar2, EXTRACTEDVAR3, etc])

I have decided (but it can change upon recommendation) that the string will have the following format: NUMBER,NUMBER,NUMBER... each number is going to be separated by a ,.

Additionnal difficulty: Each number represents a 16 bit value that has to be put in 2 separate bytes. (So 8 would give me VAR1=0x00 and VAR2=0x08)

How can I extract them? Is there a way to generate a undefined amount of variables in a loop?

For those familiar with MODBUS, I am trying to create the MODBUS function 16, Write Multiple Registers. And I don't know how to deal with the unknown amount of Data I have to send.

Thank you for your help

dome
  • 820
  • 7
  • 20
Sacha
  • 1

1 Answers1

0

You can do this:

# number is your string of numbers
splitted = numbers.split(',')
result = [(num[:2],num[2:]) for num in splitted]

Note that result contains tuples of string, then you will need to convert them to a number depending on which base you will use.

dome
  • 820
  • 7
  • 20