Note: This is the beginning of a new category on my blog, which it the 'Tips and Tricks' category. This is where I share small tips on all the other subjects I discuss here.
On my post about RS232 communication using microcontrollers (here), I explained how to send and receive one character (one Byte) at a time. Now I wanted to send a whole string using RS232. Since a string is essentially an array of characters in C, we just have to loop through the array and send one char at a time as usual, which is done in the following code.
This works well when you know the length of the string and only one string needs to be sent. When there are several strings, writing a loop for each of them isn't a good programming method. The better way is to write a separate method to handle string transmission.
The problem is, once in a separate method, you don't know the length of the string to iterate through it. We have to find the length of each string passed to the method. Since strings are actually char arrays, this means finding the length of the array. If we were using a language like Java, we might have been able to call something like myArray.length to get the length. But unfortunately, C doesn't have such a method. But it does have an operator named sizeof. What this operator does is, it returns an integer constant that gives in Bytes of how much memory space is used by its operand.
When applied to a type identifier, the sizeof returns the size of that type.
e.g. :
When we apply it to an array, it returns the total number of Bytes in the array.
e.g. :
Now, all we need to do to get the length of an array is to divide the sizeof the array by the sizeof its type.
e.g. :
So, here's a sample code that sends two strings using RS232. It has a separate method, sendString() which accepts any char array, checks its length and iterates through it properly.
So, I hope this explained few things on mikroC that might help you code easily. Feel free to express your ideas about this in the comments :)
On my post about RS232 communication using microcontrollers (here), I explained how to send and receive one character (one Byte) at a time. Now I wanted to send a whole string using RS232. Since a string is essentially an array of characters in C, we just have to loop through the array and send one char at a time as usual, which is done in the following code.
char *mystr;
unsigned int i;
void main(){
Usart_Init(9600);
mystr = "Testing";
for(i=0; i<5; i++){
Usart_Write(mystr[i]);
Delay_ms(500);
}
}
This works well when you know the length of the string and only one string needs to be sent. When there are several strings, writing a loop for each of them isn't a good programming method. The better way is to write a separate method to handle string transmission.
The problem is, once in a separate method, you don't know the length of the string to iterate through it. We have to find the length of each string passed to the method. Since strings are actually char arrays, this means finding the length of the array. If we were using a language like Java, we might have been able to call something like myArray.length to get the length. But unfortunately, C doesn't have such a method. But it does have an operator named sizeof. What this operator does is, it returns an integer constant that gives in Bytes of how much memory space is used by its operand.
When applied to a type identifier, the sizeof returns the size of that type.
e.g. :
sizeof(char) // returns 1
sizeof(int) // returns 2
sizeof(unsigned long) // returns 4
sizeof(float) // returns 4
sizeof(int) // returns 2
sizeof(unsigned long) // returns 4
sizeof(float) // returns 4
When we apply it to an array, it returns the total number of Bytes in the array.
e.g. :
int a[10];
sizeof(a); // returns 20, which is, 10 * sizeof(int)
sizeof(a); // returns 20, which is, 10 * sizeof(int)
Now, all we need to do to get the length of an array is to divide the sizeof the array by the sizeof its type.
e.g. :
int a[10];
int length = sizeof(a)/sizeof(int); // returns 10
int length = sizeof(a)/sizeof(int); // returns 10
So, here's a sample code that sends two strings using RS232. It has a separate method, sendString() which accepts any char array, checks its length and iterates through it properly.
char mystring[] = "Testing 1, 2, 3";
char mystring2[] = "Testing 4, 5, 6";
unsigned int i;
int lengthofstr;
void sendString(char sendstr[]);
void main(){
Usart_Init(9600);
while(1){
sendString(mystring);
Delay_ms(1000);
sendString(mystring2);
}
}
void sendString(char sendstr[]){
lengthofstr = sizeof(sendstr)/sizeof(char);
for(i=0; i<lengthofstr; i++){
Usart_Write(sendstr[i]);
Delay_ms(500);
}
}
So, I hope this explained few things on mikroC that might help you code easily. Feel free to express your ideas about this in the comments :)
The new category is great Timi. It gave me some ideas as well. By the way, nice work on the code box. :)
ReplyDelete
ReplyDeleteHi,
I think it's easier to use the standard C function "strlen"
Regards,
;)
sendString() never work correct!!!
ReplyDeletelengthofstr = sizeof(sendstr)/sizeof(char);
there mistake!!! sizeof???? Author! Correct your mistake!
lengthofstr = sizeof(sendstr)/sizeof(char);
ReplyDelete1. why you divide by .....sizeof(char)????? it indy code! need simple lengthofstr = sizeof(sendstr);
and
2. Are you sure, sizeof(senderstr) return length of string???? NO!!!! You need replace sizeof to strlen.
And now working code!!!
Before post, check and run really code!