View previous topic :: View next topic |
Author |
Message |
maria100
Joined: 01 Feb 2012 Posts: 63
|
Data parsing help |
Posted: Thu Mar 29, 2012 4:22 pm |
|
|
Hello Forum. I'm working on a project that receives data from UART1 and save it to an 24LC1025 ext eeprom. The project run fine, but now the next step for it is to parse the data that i receive from the serial port. I have been scratching my head for 2-3 days and tried some basic if, or routines but I get stuck every time. Maybe I'm just choosing a bad way.
The goal that I want to achieve:
-I have three phrases (with known start format) that I need to write to eeprom only, say:
STARTF1= A126
STARTF2= CD1E
STARTF3= FF20
When a start format is detected, I need to keep ONLY the start format+the following 30 chars that comes after that start format, then write them both in the EEPROM in a single line as:
A1260000000000AAAAAAAAAAFFFFFFFFFF
If for example within that 30 chars after one start format is detected another start format occurs, the 30 char counter will be reset so i will save:
A126(startformat)+AAAA(1-29chars)+FF20||CD1E||A126 (another start format is detected in the 30 char string)+30 next following chars.
or
Another possible way when a fresh start format is detected within that 30 chars, the old buffer will be write and a new buffer will be created with the new string. Thank you very much. Hope somebody can give me a hand. I promise that I'm not lazy or such, and I ask questions only when I go out of solutions. Good Luck forum! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19590
|
|
Posted: Fri Mar 30, 2012 4:43 am |
|
|
You don't give any real description of the 'start format'. How many bytes is this?. How many possible varieties?. Is the data in text, or direct binary?. Is it just the three byte pairs you show, or three sets of four characters?. Do these change?.
Sounds a perfect excuse for a state machine.
Best Wishes |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Fri Mar 30, 2012 5:31 am |
|
|
The start formats are 3 and they never change and they are represented in hexadecimal.The data after the start string ( 30 hexa chars) changes. Start strings are 3 hexa-chars long ( FFFFFF) |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Fri Mar 30, 2012 7:14 am |
|
|
I thought I understood this until you explained it.
You say "Start strings are 3 hexa-chars long ( FFFFFF)" but your first posting showed 4 characters per string-- A126, CD1E or FF20. And it's still not clear how much data each of them contains.
How many bytes come in over the serial line to create a single start sequence, and how many bytes do you want to store to the EEPROM at a time? Bytes. Forget hexa-chars. |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Fri Mar 30, 2012 7:59 am |
|
|
Sorry for the confusions. I said in the first time two bytes and the second time three bytes because two of the start-sequences consist of 2 bytes(FFFF) and one consist of 3 bytes ( FFFFFF).
I have said hexadacimal because the bytes must be read AS HEX to make a correct interpretation of the data.
I wanna store in the eeprom a record per/ page.
RECORD EXAMPLE:
F1F2F3(start sequence)+next 30 bytes after the start sequence is detected.
Hope you understand now. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Fri Mar 30, 2012 9:36 am |
|
|
Why not something like:
Code: |
//Start phrases
const unsigned int8 F1_START[] = {0x00,0xA1,0x26};
const unsigned int8 F2_START[] = {0x00,0xCD,0x1E};
const unsigned int8 F3_START[] = {0x00,0xFF,0x20};
#define F1_START_LEN sizeof(F1_START)
#define F2_START_LEN sizeof(F2_START)
#define F3_START_LEN sizeof(F3_START)
#define MAX_DATA 30
typedef enum { WAITING=0,PARSE_F1,PARSE_F2,PARSE_F3,GET_DATA } state_type;
|
Then use an algorithm similar to:
Code: |
if(kbhit()){
value = getc();
switch(state){
case WAITING:
if(value == F1_START[0]){
state = PARSE_F1;
start_byte_count = 1;
}else if(value == F2_START[0]){
state = PARSE_F2;
start_byte_count = 1;
}else if(value == F3_START[0]){
state = PARSE_F3;
start_byte_count = 1;
}
break;
case PARSE_F1:
if(value == F1_START[start_byte_count]){
start_byte_count++;
if(F1_START_LEN == start_byte_count){
state = GET_DATA;
data_count = 0;
}
}else{
state = WAITING;
}
break;
case PARSE_F2:
if(value == F2_START[start_byte_count]){
start_byte_count++;
if(F2_START_LEN == start_byte_count){
state = GET_DATA;
data_count = 0;
}
}else{
state = WAITING;
}
break;
case PARSE_F3:
if(value == F3_START[start_byte_count]){
start_byte_count++;
if(F3_START_LEN == start_byte_count){
state = GET_DATA;
data_count = 0;
}
}else{
state = WAITING;
}
break;
case GET_DATA:
data[data_count++] = value;
if(MAX_DATA == data_count){
state = WAITING;
}
break;
}
}
|
Granted that is untested code, but might give you some ideas. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19590
|
|
Posted: Fri Mar 30, 2012 9:37 am |
|
|
So, what are the start sequences?. Are they fixed values that you will always be looking for, or something coming from somewhere else?. You showed originally 3*2 byte sequences, now you are talking about 2*2byte, and 1*3byte.
Values don't _ever_ 'have to be read as HEX'. Hex is just a representation of a binary value. 0x41='A'=0b01000001. All are representations of exactly the same number. The value is the value, nothing else.
Best Wishes |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Fri Mar 30, 2012 9:59 am |
|
|
Jeremiah , i would definitely try your example , looks very promising.
Ttelmah , what I`m trying to say ( and Jeremiah got the ideea already )
is that the start bytes that I am searching for , they have the values that i said in hexadecimal , not in other format(but this , as you said it doesnt matter, numbers are numbers, ex. 0xAAAA=1010101010101010). And about the sequences what I`m trying to say is that are 3 possible start ways ( two of them has the following format : 0xFFFFFF and the other one just 0xFFFF). |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Fri Mar 30, 2012 10:31 am |
|
|
mind you that example is incomplete. It just provides a skeleton. It doesn't save which start sequence you got or do anything special for each one. It just provides a means for differentiating between them. You would still need to fill in some code if you plan on doing something different based on each start sequence (or at least save which one you got to a variable to work with later). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19590
|
|
Posted: Fri Mar 30, 2012 10:47 am |
|
|
As I said, it looks like a perfect case for a state machine, and this is what Jeremiah is showing.
The only complexity would come, since you also talk about the possibility of start sequences inside the data itself. If this can happen, potentially you might need multiple buffers, if you get a sequence that carries repeated starts all the way through.
Big problem/question though is timing. Remember it takes a few mSec to write to an external EEPROM (basically to write either a byte, or a page if using multi byte writes). Are you happy that you have the time intervals needed to complete the writes. This is where devices like FRAM 'win' hand's down.
Best Wishes |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Fri Mar 30, 2012 11:17 am |
|
|
Yup , timing could be a problem , well..I will post after i get some result. Thank you all for your attention!!! |
|
|
|