obtain data from char string

Discussion in 'Embedded Systems and Microcontrollers' started by gehan_s, Dec 1, 2013.

  1. gehan_s

    Thread Starter Member

    Sep 17, 2012
    38
    0
    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 ( (Unknown Language)):
    1. send_s = "#DCMC#" + "," + kp + "," + ki + "," + kd + "," + v + "," + i + "," + t + "," + s + "," + "\n";
    2. if (!serialPort1.IsOpen)
    3. return;
    4. buff = send_s.ToCharArray();
    5. 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
     
  2. Art

    Distinguished Member

    Sep 10, 2007
    785
    61
    Because you have control of the program that is sending, it should be easy to receive.

    Any numeric character's hex representation, is just it's decimal value + 0x30h
    Subtract that from any digit you receive, and that's the decimal digit value restored.

    If a single number has multiple digits, you have delimited them with commas,
    so any digit place to the left of the last digit gets multiplied by 10, 100, 1000, etc.
    and added to the last digit.
    There's a C function for that, I've always done it myself.
     
Loading...