0

I'm new to this site and python. I've been working on a project that needs to extract variables from incoming serial data and eventually display it. I'm currently working on parsing the data and I'm having a bit of trouble. The serial data looks like this for example:

a3b5f45c9g8a4c10f64;
f4h87d34k9h4j3d3;
h6f54a12a13a14a15b12b13;

There's multiple variables denoted by the letter before the value and they arrive in no particular order. About once a second the line breaks with a semi-colon. The same variable can appear multiple times or not at all per line. The closest I've gotten is using regex to find the value between the identifying letter and the next non-number, if that makes sense. The problem I'm having is that it only returns the first match and then stops. I need the variable to be constantly updated. I've been scratching my head for the past few days, any guidance is greatly appreciated.

import serial
import re

ser = serial.Serial('COM6', 9600, timeout=2)

while True:
  data_raw = ser.readline()
  print(data_raw)

  apples = re.search('a(.+?)\D', data_raw)
  if apples:
    applesvar = apples.group(1)
    print applesvar

  cherries = re.search('c(.+?)\D', data_raw)
  if cherries:
    cherriesvar = cherries.group(1)
    print cherriesvar


ser.close

1 Answer 1

1

You are almost there. By using the first line of your example

line = 'a3b5f45c9g8a4c10f64'

re.findall('a(.+?)\D', line)
['3', '4']

re.findall('c(.+?)\D', line)
['9', '10']
Sign up to request clarification or add additional context in comments.

2 Comments

That looks like it's returning an array of values. For example if my variable is applesvar it would need to equal 3 then 4. I don't see how that would work with an array. I apologize if I'm misunderstanding.
I am giving you a bigger picture. And I believe you got that. Do your due diligence and figure out the rest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.