2 years, 1 month ago.

define int value from uart data

Hi,

I would like to create a program who read serial data and assign data in a int value like this

UART DATA (example) : 12 34 data1 <-| | | |-> data2

data1 = 12 and data2 = 34

Actually i can read uart data and store it in value[100] in ASCII, i try to convert it into int value by converting the ASCII value to integer

Can you help me ?

Fred

My software:

int main() {

char value[100]; Value 0-1 => data1 || Value 2-3 => data2 int index=0; char ch;

var user int data1 = 0; int data2 = 0;

pc. printf("--Startup !--\r\n"); while(1) {

do { if (device.readable()) if there is an character to read from the device { ch = device.getc(); read it if (index<100) just to avoid buffer overflow value[index++]=ch; put it into the value array and increment the index } } while (ch!=' '); loop until the '\n' character

value[index]='\x0'; add un 0 to end the c string

Convert Ascii to int => -48

value[0] = value[0] - 48; value[1] = value[1] - 48; value[2] = value[2] - 48; value[3] = value[3] - 48;

pc.printf("data10 = %d%d, data20 = %d%d ", value[1], value[0], value[3], value[2]);

data1 = value[1]*10 + value[0]; data2 = value[3]*10 + value[4];

pc.printf("data1 = %d , data2 = %d \r\n", data1, data2);

index = 0; data1 = 0; data2 = 0; }}

Comment on this question
Be the first to answer this question.

You need to log in to post a question