Introduction
When debugging serial communication, a problem arises, that is, how to realize the connection of two strings in C language. The first reaction is that this is not easy, just use strcat, who knows that with in-depth study, found some problems that they did not normally notice.
text
Demand:
It is hoped that the instruction of 0x01 0X11 0x100 x13 0x01 0x12 will be sent to the serial port, but the instruction is composed of two instructions: 0x01 0X11 0x100 x13 and 0x01 0x12.
Programme 1:
Using strcat:
Function prototype: extern char *strcat(char *dest, const char *src);
The implementation code is as follows:
1 #include <ansi_c.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 main() 6 { 7 //the first command 8 unsigned char cCommandFirst[6] = "\x01\x11\x10\13"; 9 10 //the second command 11 const unsigned char cCommandSecond[2] = "\x01\0x12"; 12 13 //connect two strings 14 strcat(cCommandFirst,cCommandSecond); 15 16 //show string 17 for(int i = 0; i < 6; i ++) 18 { 19 printf("%X\t",cCommandFirst); 20 } 21 22 getchar(); 23 }
Output: 1.11.10.13.1.12
This was supposed to be the end, but since the second instruction will be 0x00 in the process of use, the following scenario appears:
1 #include <ansi_c.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 main() 6 { 7 //the first command 8 unsigned char cCommandFirst[6] = "\x01\x11\x10\13"; 9 10 //the second command 11 const unsigned char cCommandSecond[2] = "\x00\0x12"; 12 13 //connect two strings 14 strcat(cCommandFirst,cCommandSecond); 15 16 //show string 17 for(int i = 0; i < 6; i ++) 18 { 19 printf("%X\t",cCommandFirst); 20 } 21 22 getchar(); 23 }
Output: 1.11.10.13.0
Why did the last instruction added all become zero?
Analyzing the prototype of strcat function:
1 char *strcat(char *dest, const char *src) 2 { 3 assert(src && dest); 4 char *tmp = dest; 5 while (*dest) 6 dest++; 7 while ((*dest++ = *src++) != '\0'); 8 return tmp; 9 }
Through analysis, it can be found that in the second while loop, its judgment debugging is whether the second instruction appears\0, which happens to occur when the second instruction is sent.
Programme 2:
To solve these problems, we try to use the memcpy function:
Function prototype: void *memcpy(void *dest, const void *src, size_t n);
First, look at the implementation of functions.
1 void* memcpy(void* dest, const void* src, size_t count) 2 { 3 assert(src && dest); 4 5 char* tmp_dest = (char*)dest; 6 7 const char* tmp_src = (const char*)src; 8 9 10 while (count--) 11 *tmp_dest++ = *tmp_src++; 12 13 return dest; 14 }
From the implementation, it can be found that the judgment debugging when connecting two strings is to intercept the length of the second instruction according to the requirement.
The following two string connections are implemented using memcpy:
1 #include <ansi_c.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 main() 6 { 7 //the first command 8 unsigned char cCommandFirst[6] = "\x01\x11\x10\13"; 9 10 //the second command 11 const unsigned char cCommandSecond[2] = "\x00\0x12"; 12 13 //connect two strings 14 //strcat(cCommandFirst,cCommandSecond); 15 memcpy(cPosition + 4,cdata,2);//because the first commands have four items. so we connect the second one from +4 address. 16 17 //show string 18 for(int i = 0; i < 6; i ++) 19 { 20 printf("%X\t",cCommandFirst); 21 } 22 23 getchar(); 24 }
Output: 1.11.10.13.0.12
Perfect solution!
Conclusion:
When strcat is used to connect two strings, if the content of'\ 0'appears in the second character, this is strcat function failure. You need to use the memcpy function to connect two strings.