String algo

I will be receiving data serially from another pc,and it will be in this format:value of a followed by a followed by value of b followed by.
an eg:100a51.454b68c
Now what i want is to create variables and store different values under them,for eg var_a should have 100 and so.
Length of string is not fixed so i will be assuming say i read 25 bytes into a string variable ,process it and then again read next string and process.
However i may encounter strings like:string 1:100a20b2
next time i read c45d
so,what sought of an algo shall i use?

nipunreddevil wrote:
> I will be receiving data serially from another pc,and it will be in this
> format:value of a followed by a followed by value of b followed by.
> an eg:100a51.454b68c
> Now what i want is to create variables and store different values under
> them,for eg var_a should have 100 and so.
> Length of string is not fixed so i will be assuming say i read 25 bytes
> into a string variable ,process it and then again read next string and
> process.
> However i may encounter strings like:string 1:100a20b2
> next time i read c45d
> so,what sought of an algo shall i use?

You’re quite vague about the expected input. With more elaborate examples
it would be easier to geve advice.

Regular expression are made for this
In e.g. Python:

>>> m = re.search(’(\d+)a(\d+).(.*)’,‘100a51.454b68c’)
>>> for i in range(4):
… print m.group(i)

100a51.454b68c
100
51
454b68c

But if other values for a and b can be expected, the regexp has to be
changed of course.

Sample i/p
2.23323y5.45545p9.565656r78.54545x98.56565y
Now this is one packet.
I will be getting packets continuously serially.
Now what i thought was reading a specific number of bytes from serial port and then appending them to a string and then apply an algo to take various components out.
eg var1=2.23323
var2=5.45545
etc
Now since i don’t know the size of packet,i am assuming closest value to it to be number of read bytes.
So what shall my algo be like?

nipunreddevil wrote:
> Sample i/p
> 2.23323y5.45545p9.565656r78.54545x98.56565y
> Now this is one packet.
> I will be getting packets continuously serially.

Isn’t there some sort of a sync marker between packets?

> Now what i thought was reading a specific number of bytes from serial
> port and then appending them to a string and then apply an algo to take
> various components out.
> eg var1=2.23323
> var2=5.45545

Does the last ‘98.56565y’ belong to the next var1 or is that just extra
data?

> etc

The ‘etc’ bit is still far from clear…

> Now since i don’t know the size of packet,i am assuming closest value
> to it to be number of read bytes.

“Assuming” is the mother of all screw-ups…

> So what shall my algo be like?

First you need a clear definition of the expected input, with all exceptions.
Sofar you have been giving two, very distinct, examples that partially
exclude one another. If it’s not clear to you what data you’re getting from
the serial port, you’re asking for help on an algoritme too soon.

Theo

i can use any sort of marker between two packets,i will be the person sending data as well after reading it from some devices.But packet length is not constant.
each packet has this format:
201.535a56.65b4.56c89.565d
another packet could be :
4a5b6c7d

nipunreddevil wrote:
> i can use any sort of marker between two packets,

If you want to use Python, then I would suggest ’
’ (0xA) so that Serial.readlines()
can easily read a packet a time.

i will be the person
> sending data as well after reading it from some devices.But packet
> length is not constant.

That shouldn’t be a problem with a defined EOL marker.

> each packet has this format:
> 201.535a56.65b4.56c89.565d
> another packet could be :
> 4a5b6c7d

You keep changing the fscking input data!
First it was ‘a’,‘b’, ‘c’ etc between th data values, then ‘r’, ‘x’ and
‘y’, and now it’s back to ‘a’, ‘b’, ‘c’.

Anyway, with this regexp you get the data out, for this latest sample anyway.

^(\d+.?\d*)a(\d+.?\d*)b(.*)

m = re.search(’^(\d+.?\d*)a(\d+.?\d*)b(.*)’,‘201.535a56.65b4.56c89.565d’)
>>> for i in range(4):
… print m.group(i)

201.535a56.65b4.56c89.565d
201.535
56.65
4.56c89.565d

>>> m = re.search(’^(\d+.?\d*)a(\d+.?\d*)b(.*)’,‘4a5b6c7d’)
>>> for i in range(4):
… print m.group(i)

4a5b6c7d
4
5
6c7d

Your algoritme would be:
ser = serial.Serial()
vars = re.compile(’^(\d+.?\d*)a(\d+.?\d*)b(.*)’) # Setup and compile the regexp
while True:
packet = ser.readlines() # Read until EOL
m = re.search(vars, packet) # Run the regexp on the packet
var1 = m.group(1) # take out the values
var2 = m.group(2)
… # Do stuff.
ser.close()

Thanks A LOT.How would using index be for this problem

nipunreddevil wrote:
> Thanks A LOT.How would using index be for this problem

Do you mean an iterator as in a loop?
That would be a lot more complicated, slower and basically
a ‘WTF’ way of programming.
If you don’t believe me: try to make a version with an
iterator and compare.

Theo

Thanks a lot.your program works great,my idea was bad.thanks a lot.