Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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.

Latest threads

Back
Top