Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] How to split a string ?

Status
Not open for further replies.
Can't see a definition of PORT_CONTROL_TYPE, so I could only guess about the problem.
 

@FvM

Thanks for replying.

Here is the code of PORT_CONTROL_TYPE.

My code changed a little.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#define Max_Pins 32
 
typedef struct {
   unsigned char pin_states[Max_Pins];
   unsigned char hours[Max_Pins];
   unsigned char minutes[Max_Pins];
   unsigned char seconds[Max_Pins];
   unsigned int milli_seconds[Max_Pins];
   long milli_seconds_delay_counter[Max_Pins];
   long milli_seconds_delay_count[Max_Pins];
   unsigned task_completed[Max_Pins];
   char pin_state[Max_Pins];
   unsigned char fileName[3][20];
   char messages[10][64];
   char txt[3][32];
   char buf[96];
   char buffer[64];
   char usart_receive_state_counter;
   unsigned char usart_buffer[Uart_Buffer_Size];
   unsigned int usart_buffer_index;
   unsigned char extracted_data[80][26];
   char a;
   char b;
   char c;
   char d;
   char e;
   char f;
   char g;
   char h;
   unsigned char str_direction[7];
   unsigned char str_port[2];
   unsigned char str_pin[2];
   unsigned char str_pin_state[2];
   unsigned char str_hours[3];
   unsigned char str_minutes[3];
   unsigned char str_seconds[3];
   unsigned char str_milli_seconds[4];
 
}PORT_CONTROL_TYPE;
 
PORT_CONTROL_TYPE my_port_control;
 
char instr[] = "OUTPUT,A,1,0,02,01,00,00\rOUTPUT,A,1,0,02,01,00,00\rOUTPUT,A,1,0,02,01,00,00\r$";
 
void splitstr_a(unsigned char *_inbuf, unsigned char _outbuf[][26], char *_delim) {
   unsigned char *token;
   int idx = 0;
   unsigned char _tmpbuf[128];
   
   strcpy(_tmpbuf, _inbuf);
 
   token = strtok (_tmpbuf, _delim);
 
   while(token != 0) {
      sprintf(_outbuf[idx], "%s", token);
      token = strtok(0, _delim);
      #ifdef DEBUG
         UART1_Write_Text(_outbuf[idx]);
         UART1_Write(CR);
      #endif
      idx++;
   }
}
 
void splitstr_b(char _inbuf[][26], PORT_CONTROL_TYPE *pct , char *_delim) {
   char *token;
   int idx = 0;
   int idx_outer = 0;
   int idx_inner = 0;
   char _tmpbuf[80][26];
 
   while(_inbuf[idx]) {
     strcpy(_tmpbuf[idx], _inbuf[idx]);
     idx++;
   }
 
   token = strtok (_tmpbuf[idx_outer], _delim);
 
   idx = 0;
   while(token != 0) {
      sprintf(pct.str_direction[idx++], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct.str_port[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct.str_pin[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(*pct.str_pin_state[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(*pct.str_hours[idx] "%s",, token);
      token = strtok(0, _delim);
      sprintf(*pct.str_minutes[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(*pct.str_seconds[idx], "%s", token);
 
      token = strtok(0, _delim);
      idx++;
   }
}
 
//Usage
splitstr_a(&instr, &my_port_control.extracted_data[0], "\r");
splitstr_b(&my_port_control.extracted_data[0], &my_port_control, ",");

 

Several issues

Code:
sprintf(pct.str_direction[idx++],"%s",...)
sprintf(*pct.str_direction[idx++],"%s",...)
are both wrong C syntax.

The expected sprint argument is a string respectively char pointer (*char).

Code:
sprint((*pct).some_char_pointer,
or better
sprint(pct->some_char_pointer,

A legal sprintf target would be
Code:
sprintf(pct->str_direction,...

I don't understand the purpose of
Code:
str_direction[idx++]

In any case, it's incorrect syntax, because it's a character rather than a character pointer.
 

Ok. Thank you.

This

Code:
str_direction[idx++]

was a typo.

It was actually

Code:
str_direction[idx]

Code:
splitstr_a() splits string using "\r" delimiter.

Code:
splitstr_b() has to extract different fields of a line using "'," as delimiter.


Code:
sprintf()0/code]

code in 

[code]
splitstr_a()

is working fine.

This is my new code and it compiles fine. I have to test it now.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void splitstr_b(char _inbuf[][26], PORT_CONTROL_TYPE *pct , char *_delim) {
   char *token;
   int idx = 0;
   int idx_outer = 0;
   int idx_inner = 0;
   char _tmpbuf[80][26];
 
   while(_inbuf[idx]) {
     strcpy(_tmpbuf[idx], _inbuf[idx]);
     idx++;
   }
 
   token = strtok (_tmpbuf[idx_outer], _delim);
 
   idx = 0;
   while(token != 0) {
      sprintf(pct->str_direction[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_port[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_pin[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_pin_state[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_hours[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_minutes[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_seconds[idx], "%s", token);
 
      token = strtok(0, _delim);
      idx++;
   }
}

 

Don't seem you are misusing the sprint formatting operator %s which due to being comma separated, it is not binded to any string expression ? I am unsubscribing from this thread because you are still not able to pose the problems in a clear way, for example indicating exactly what line of error is linked to with part of the code; good luck.
 

@andre_teprom

Code is very big (700 lines). I can't post full code. The error issue is solved by FvM. I have new issue with the same function and it is related to sprintf(). I will ask it in a few minutes. I am trying different things to get it work.

In the previous problem the error was related to usage of pct struct inside function. That was solved by FvM.

All my previous questions are solved. My new question is, in this code while() loop doesn't execute. Why ?

Code:
token

is always 0x00000000


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void splitstr_b(unsigned char _inbuf[][26], PORT_CONTROL_TYPE *pct , char *_delim) {
   char *token;
   int idx = 0;
   unsigned char _tmpbuf[80][26];
 
   while(_inbuf[idx][0]) {
     strcpy(_tmpbuf[idx], _inbuf[idx]);
     idx++;
   }
 
   token = strtok (_tmpbuf[idx], _delim);
 
   idx = 0;
   while(token != 0) {
      sprintf(pct->str_direction[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_port[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_pin[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_pin_state[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_hours[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_minutes[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_seconds[idx], "%s", token);
 
      token = strtok(0, _delim);
      idx++;
   }
}



- - - Updated - - -

Ok. Found one bug.

Code:
idx

was not reset before calling

Code:
strtok() for the first time.

Will see if it works.
 
Last edited:

This is my new code and it compiles fine.
It won't compile with the previously shown definition of struct PORT_CONTROL_TYPE. I guess you have changed some elements to arrays of string, similar to extracted_data[80][26].

Generally I agree with andre_teprom that it is rather ineffective to dig through code snippets without seeing the whole picture. Furthermore the code is complex enough to stumble upon non obvious design faults. Tracing code execution with the ARM debugger or a simulator would be my preferred method to find the errors. If you are not yet familiar with debugging methods, it's time to learn about it.
 

Here is the code used for splitting string.

Now, all issues are solved. I followed FvMs method. The array was wrong in struct. I needed 2D arrays.

The MCU has 80 pins. and so every pin of MCU is controlled using timer delays.

I can't post full code of the project. My client will not allow it.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#define Max_Pins 32
 
#define Uart_Buffer_Size        8
#define Mmc_Card_Buffer_Size 2048
 
typedef struct {
   unsigned char pin_states[Max_Pins];
   unsigned char hours[Max_Pins];
   unsigned char minutes[Max_Pins];
   unsigned char seconds[Max_Pins];
   unsigned int milli_seconds[Max_Pins];
   long milli_seconds_delay_counter[Max_Pins];
   long milli_seconds_delay_count[Max_Pins];
   unsigned task_completed[Max_Pins];
   char pin_state[Max_Pins];
   unsigned char fileName[3][20];
   char messages[10][64];
   char txt[3][32];
   char buf[96];
   char buffer[64];
   char usart_receive_state_counter;
   unsigned char usart_buffer[Uart_Buffer_Size];
   unsigned int usart_buffer_index;
   unsigned char extracted_data[80][26];
   unsigned char str_direction[80][7];
   unsigned char str_port[80][2];
   unsigned char str_pin[80][2];
   unsigned char str_pin_state[80][2];
   unsigned char str_hours[80][3];
   unsigned char str_minutes[80][3];
   unsigned char str_seconds[80][3];
   unsigned char str_milli_seconds[80][4];
}PORT_CONTROL_TYPE;
 
PORT_CONTROL_TYPE my_port_control;
 
char instr[] = "OUTPUT,A,1,0,02,01,00,00\rOUTPUT,A,1,0,02,01,00,00\rOUTPUT,A,1,0,02,01,00,00\r";
 
void splitstr_a(unsigned char *_inbuf, unsigned char _outbuf[][26], char *_delim) {
   unsigned char *token = 0;
   int idx = 0;
   unsigned char _tmpbuf[128];
   
   strcpy(_tmpbuf, _inbuf);
 
   token = strtok(_tmpbuf, _delim);
 
   while(token != 0) {
      sprintf(_outbuf[idx], "%s", token);
      token = strtok(0, _delim);
      #ifdef DEBUG
         UART1_Write_Text(_outbuf[idx]);
         UART1_Write(CR);
      #endif
      idx++;
   }
}
 
void splitstr_b(unsigned char _inbuf[][26], PORT_CONTROL_TYPE *pct , char *_delim) {
   char *token = 0;
   int idx = 0;
   unsigned char _tmpbuf[80][26];
 
   while(_inbuf[idx][0]) {
     strcpy(_tmpbuf[idx], _inbuf[idx]);
     idx++;
   }
 
   idx = 0;
   token = strtok(_tmpbuf[idx], _delim);
 
   while((token != 0) && (_tmpbuf[idx][0])) {
      sprintf(pct->str_direction[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_port[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_pin[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_pin_state[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_hours[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_minutes[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_seconds[idx], "%s", token);
      token = strtok(0, _delim);
      sprintf(pct->str_milli_seconds[idx], "%s", token);
 
      idx++;
      token = strtok(_tmpbuf[idx], _delim);
   }
}
 
//usage
splitstr_a(&instr, &my_port_control.extracted_data[0], "\r");
splitstr_b(&my_port_control.extracted_data[0], &my_port_control, ",");

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top