- Joined
- Jan 22, 2008
- Messages
- 53,027
- Helped
- 14,788
- Reputation
- 29,863
- Reaction score
- 14,266
- Trophy points
- 1,393
- Location
- Bochum, Germany
- Activity points
- 300,708
Can't see a definition of PORT_CONTROL_TYPE, so I could only guess about the problem.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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, ",");
sprintf(pct.str_direction[idx++],"%s",...)
sprintf(*pct.str_direction[idx++],"%s",...)
sprint((*pct).some_char_pointer,
or better
sprint(pct->some_char_pointer,
sprintf(pct->str_direction,...
str_direction[idx++]
str_direction[idx++]
str_direction[idx]
splitstr_a() splits string using "\r" delimiter.
splitstr_b() has to extract different fields of a line using "'," as delimiter.
sprintf()0/code]
code in
[code]
splitstr_a()
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++; } }
token
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++; } }
idx
strtok() for the first time.
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].This is my new code and it compiles fine.
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, ",");