Page 1 of 1

How to read variables over UART/RS232?

Posted: Wed Jun 01, 2011 10:18 pm
by Niro
Dear all,

I have a problem reading values of variables over UART/RS232 from my ATMega328. I want to transmit the name of the variable maybe as string from a terminal program to the microcontroller and then send the value of this variable back over RS232 to the terminal?
Does anyone know how to recognize the variables name from the sent string with FlowCode or C and then use it with the microcontroller?
I wasn't able to convert the string into a 'real' usable variables name.

Many thanks!
Niro

Re: How to read variables over UART/RS232?

Posted: Thu Jun 02, 2011 8:13 am
by Steve
There's no direct way to do this. You would need to do something like the following (assuming VAR_NAME and VAR_VAL are the two things transmitted over RS232):

Code: Select all

read VAR_NAME

if (VAR_NAME = "MyVar1") then VAR_VAL = MyVar1
if (VAR_NAME = "MyVar2") then VAR_VAL = MyVar2
if (VAR_NAME = "MyVar3") then VAR_VAL =  MyVar3
(...etc...)

send VAR_VAL
As you need to hardcode the actual variable names in this construct, you might as well use a single byte value to identify the variable. Then you could use a switch statement on this variable value to set your variables (assuming VAR_IDX is the byte value read over RS232 which identifies the variable):

Code: Select all

read VAR_IDX

switch (VAR_IDX)
  case 1: VAR_VAL = MyVar1
  case 2: VAR_VAL = MyVar2
  case 3: VAR_VAL = MyVar3
  (...etc...)

send VAR_VAL

Re: How to read variables over UART/RS232?

Posted: Thu Jun 02, 2011 1:17 pm
by Niro
Hi Steve,

unfortunately, thats the answer I was expecting :-(
So, I'm going to hardcode it...

Best regards,
Niro