Ok, I think I understand now. Does your Arduino "called" program need to run forever, responding to the caller program? If so, how about something like this, then?
This assumes that every "line" contains an ASCII number terminated by a hex A.
void setup(){}
// retrieves an ASCII number up to and including the terminal 0xA
long parsenumber()
{
int c;
byte ret = 0;
while (true)
{
while (!Serial.available())
;
c = Serial.read();
if (c == 0xA)
break;
ret = 10 * ret + c - '0';
}
return ret;
}
void loop()
{
// first number xmitted is function number - should be 0 or 1
long function = parsenumber();
if (function == 0)
{
bool arg = parsenumber() == 1 ? true : false;
// Here you can call function1(arg);
}
else
{
bool arg1, arg2;
byte arg3;
long arg4;
arg1 = parsenumber() == 1 ? true : false;
arg2 = parsenumber() == 1 ? true : false;
arg3 = (byte)parsenumber();
arg4 = parsenumber();
// Here you can call function2(arg1, arg2, arg3, arg4);
}
}
This algorithm will choke on the 0xD end of transmission you propose. You'll have to either remove that or change the algorithm.
Just as an example, if you wanted to call function1 with argument "true" and function2 with arguments "false", "true", 66, 142850, you would send a stream like this:
'0', 0xA // the next function being called is #1 (0)
'1', 0xA // the argument is 1 (true)
'1', 0xA // the next function being called is #2 (1)
'0', 0xA // first argument is 0 (false)
'1', 0xA // second argument is 1 (true)
'6', '6', 0xA // third argument is 66 (the byte)
'1', '4', '2', '8', '5', '0', 0xA // last argument is 142850 (long)
etc.
Regards,
Mikal