Arduino can't receive a string from Processing

Hello,

I am having trouble receiving variables from Processing to Arduino.

My code aims to tell the Arduino to stop/start/set the system.

assuming I want to set the system processing will execute the following:

s="<2"+t_min_icp.getText()+","+t_max_icp.getText()+","+ t_max_drain.getText()+","+t_drain_rate.getText()+">";
myPort.write(s);

the texts are taken from text fields.

My Arduino code receives the first char, but does not seem to receive the others..

for the following Arduino code


#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

bool st = false;
bool set = false;
bool overide = false;

char pre_Data[80];
char *post_Data[80];
float final_Data[80];

char *var = NULL;
byte index;
char inChar;
int stat;
int lower_Pressure;
char s[30];


void setup()
{
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  while (Serial.available() > 0) {
    Serial.read();
  }
  // Other stuff...
}

void loop()
{
    // Read all serial data available, as fast as possible
    while (Serial.available() > 0)
    {
    inChar = Serial.read();
    if (inChar == SOP)
    {
      inChar = Serial.read();
      index = 0;
      pre_Data[index] = '\0';
      started = true;
      ended = false;
      if (inChar == '0') {
        digitalWrite(LED_BUILTIN, LOW);
        st = false;
      }
      else if (inChar == '1') {
        digitalWrite(LED_BUILTIN, HIGH);
        st = true;
      }
      else if (inChar == '2') {
        digitalWrite(LED_BUILTIN, HIGH);
        set = true;
    }
    }
    else if (inChar == EOP)
    {
    ended = true;
    break;
    }
    else if (index < 30)
    {
    if (set == true || overide == true) {
      pre_Data[index] = inChar;
      pre_Data[index] = '\0';
    }
    index++;
    }
    }

    if (started && ended)
    {
    if (set == true)
    {
    var = strtok(pre_Data, ",");
    index = 0;
    while (var != NULL)
    {
      post_Data[index] = var;
      index++;
      var = strtok(NULL, ",");
    }
    for (int i = 0; i < index; i++) {
      final_Data[index] = atof(post_Data[index]);
    }
    lower_Pressure = final_Data[0];
    set=false;
    }
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    pre_Data[index] = '\0';
    }
    if (lower_Pressure == 3)
    {
    digitalWrite(LED_BUILTIN, HIGH);
    }
}

this return 255 (space in ASCII) instead of '2'.

What am I doing wrong?

Welcome to the forum

Please post your full sketch, using code tags when you do

Have you tried printing what you are sending in Processing ?
Does it look like it should ?

You are not checking that data is available before you try to read it. If no data is available, you will get 255 (==-1, which, BTW, is not a space character in ASCII, that is 32).

Hey, I edited my full Arduino sketch.

I checked and processing prints the right string.

I am sending a long string, something like <23.0,7.0,5.0> so if I didn't get to >, the transmission did not end.

That doesn't mean it's available to read yet. At 9600 baud, each character takes about 1ms to send or receive. A typical Arduino can execute 16,000 instructions in that time.

I would suggest to study Serial Input Basics to handle this

I added:

     while(!(Serial.available())){}

In the problematic space and it seems to solve the problem. Thank you.

I already read the whole thing and it didn't help me in this case. Thank you

well, may be you need to read it again? there is sample code to receive data with a start and end marker.... that's what you want

(see // Example 3 - Receive with start- and end-markers)

I would, 9600 is pretty slow! But that may not fix the problem.

while (!Serial.available());

There are dangers to this approach. If the expected data is not sent or not received for some reason, your code will hang.

Maybe you read it, but it's clear you did not understand it. If you had, you would not have needed to ask for help with this problem. I suggest you read it again and see if you could have found the answer there, now that you understand the problem you were having better.

I will try again. Thanks again for your help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.