0
votes

I m working on code to send and receive messages from a GSM modem connected to my laptop via a serial port using python.

In my program I have created a serial object named phone.

Using phone I read and write into a list x which stores the AT commands,response and status report that I get during execution.

For example, consider the following to check the default operator of the Sim card in my GSM modem

AT+COPS? #AT command

+COPS: 0,0,"VOD" #Response

OK #Status

Now i need to parse information which i receive in x.

I was trying to use regex to parse the strings stored in x. However the problem is that it stores each character as a separate element in the list. So essentially there are no strings. How can i group characters which are present in the list into strings so i can apply regular expressions to parse my data and understand whats going on.

For the same command above, the data is stored in x as follows:

0 A
1 T
2 +
3 C
4 O
5 P
6 S
7 ?
8 
9 
10 

11 +
12 C
13 O
14 P
15 S
16 :
17  
18 0
19 ,
20 0
21 ,
22 "
23 V
24 O
25 D
26 "
27 
28 

29 
30 

31 O
32 K
33 
34 

Or is my approach just completely wrong?

Please help!

2

2 Answers

1
votes

You could just convert the list into a string by doing

myRegexableString = "".join(x)
0
votes

If you want to extract the data from 'x' this should work. No need for regexp.

def read_message(x):
    message_list = []
    lines = x.split('\n')
    for line in lines:
        index_past_number = 0
        for i, char in enumerate(line):
            if char.isdigit():
                index_past_number += 1
        line = line[index_past_number:] #add one to ignore the space
        line = line.strip() #strip the newline
        message_list.append(line)
    return ''.join(message_list)

Hope this helped.