obtain data from char string

Status
Not open for further replies.

gehan_s

Member
Hi all,

I am sending data from a GUI (designed using C#) to a PIC microcontroller. Each data is separated by a "," and each data string is terminated by a "\n". This is how I am doing it.

Code:
send_s = "#DCMC#" + "," + kp + "," + ki + "," + kd + "," + v + "," + i + "," + t + "," + s + "," + "\n"; 
if (!serialPort1.IsOpen) 
return; 
buff = send_s.ToCharArray(); 
serialPort1.Write(buff, 0, buff.Length);

The values kp, ki, kd, v, i, t, s are integers converted into characters. The phrase #DCMC# acts as an identifier so if the incoming string starts with "#DCMC#" then accept the string.

How do I obtain the integer data from this string? (I am using mikroC PRO for PIC)

Regards
 
It's comma de-limited... This means you can use the comma in the function "strtok()"

This ids an example taken from the net

C:
#include<string.h>
#include<stdio.h>

int main(){

   constchar str[80]="This is - www.tutorialspoint.com - website";

   constchar s[2]="-";


   char*token;
   /* get the first token */
   token = strtok(str, s);
   /* walk through other tokens */

   while( token != NULL ){
      printf(" %s\n", token );  
      token = strtok(NULL, s);
   }
   return(0);
}


Here the token is "-"
Token is a pointer to the new string..

Once you have done this number = atoi(token); can be used.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…