Arduino Forum

Using Arduino => Programming Questions => Topic started by: adwsystems on Aug 06, 2017, 04:43 am

Title: Serial.readBytesUntil()
Post by: adwsystems on Aug 06, 2017, 04:43 am
Does the program stall and wait for Serial.readBytesUntil() to complete or is this command interrupt driven?
Title: Re: Serial.readBytesUntil()
Post by: pert on Aug 06, 2017, 04:53 am
It's a blocking function. It just sits there waiting until the terminator character is detected, the determined length has been read, or it times out. The default timeout is 1000 ms but you can change it via Serial.setTimeout():
https://www.arduino.cc/en/Reference/StreamSetTimeout (https://www.arduino.cc/en/Reference/StreamSetTimeout)
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 06, 2017, 04:54 am
Thank you.

That's what I thought or was afraid of.
Title: Re: Serial.readBytesUntil()
Post by: sterretje on Aug 06, 2017, 07:23 am
or was afraid of.
Why ;) You can easily work around that by modifying Robin2's example #2 in Serial Input Basics (http://forum.arduino.cc/index.php?topic=396450.0). You can even add a non-blocking timeout if needed.
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 06, 2017, 01:52 pm
Because the program was written by someone else using readBytesUntil(). The functionality I need to add needs to use serialEvent(), so I will have to rewrite the majority of the program including parsing and retesting the modified existing functionality. It is what it is.
Title: Re: Serial.readBytesUntil()
Post by: sterretje on Aug 06, 2017, 02:28 pm
Although I have never used serial event, it should be straight forward; mix it with Robin's code and you should be done.

Why the need for serial event? Do you have any blocking code?
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 06, 2017, 02:32 pm
Yes. Serial.readBytesUntil()

There will be "commands" coming in the serial port at any random time therefore Serial.readBytesUntil() cannot be used.
Title: Re: Serial.readBytesUntil()
Post by: sterretje on Aug 06, 2017, 02:41 pm
No, that's not what I meant. Any others (e.g. a for loop that takes ages due to complex calculations)? If not, I have great difficulty understanding why you need serial event unless the current code also uses it and it's the easiest adjustment.
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 06, 2017, 02:47 pm
Let me ask this:

What happens if I add one Serial.readBytesUntil() to the program to wait for serial input; not knowing when the input will be received?
Title: Re: Serial.readBytesUntil()
Post by: Robin2 on Aug 06, 2017, 03:11 pm
Let me ask this:

What happens if I add one Serial.readBytesUntil() to the program to wait for serial input; not knowing when the input will be received?
It will wait until its timeout expires for the "until" character and then it will return. I think the default timeout is 1 second.


You have received a lot of advice that you do not seem to have been able to make use of so you need to post your complete program so we can understand what you are having a problem with.

...R
Title: Re: Serial.readBytesUntil()
Post by: sterretje on Aug 06, 2017, 03:13 pm
It will wait till the requested character is received. If it's already received, there will not be a delay. If the requested character is not received within the given time, it will result in corrupted data and a delay.

It's all the things that you don't want but I guess that you already figured that out.
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 06, 2017, 03:30 pm
It's all the things that you don't want but I guess that you already figured that out.
I have other things to do during the timeout period.

You have received a lot of advice that you do not seem to have been able to make use of so you need to post your complete program so we can understand what you are having a problem with.

...R
Actually I haven't received any "advice...to make use of". I have received valuable information on the operation of the Serial.readBytesUntil() function.

At this time, the program is still a work in progress. I'm working to convert the PLX-DAQ sample program from Serial.readBytesUntil() to serialEvent(), to add it in to an existing program. Using Serial.readBytesUntil() to wait for commands via the serial port will have a large detrimental impact on the execution of the current program.
Title: Re: Serial.readBytesUntil()
Post by: sterretje on Aug 06, 2017, 03:39 pm
Actually I haven't received any "advice...to make use of".
Maybe read reply #3 again.
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 06, 2017, 03:54 pm
Maybe read reply #3 again.
OK. Read it again. Still  haven't received any "advice...to make use of". Reply #3 is almost irrelevant at Robin2 doesn't like serialEvent() and provides no examples to that end.

Thanks for the link though. I do like read other solutions, even if irrelevant, as they may provide an idea to morph and use myself.
Title: Re: Serial.readBytesUntil()
Post by: Robin2 on Aug 06, 2017, 04:01 pm
OK. Read it again. Still  haven't received any "advice...to make use of". Reply #3 is almost irrelevant at Robin2 doesn't like serialEvent() and provides no examples to that end.
This sounds like a problem for every solution.

Post your complete program and then maybe someone can suggest something useful.

...R
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 06, 2017, 06:21 pm
OK. Read it again. Still  haven't received any "advice...to make use of". Reply #3 is almost irrelevant at Robin2 doesn't like serialEvent() and provides no examples to that end.

Do you know what serialEvent() is?  It's just an empty function that gets called after loop.  It's a convenient place to put serial handling code,  but there's nothing special about it.  You could put the same code at the end of your loop function and get the exact same end result. 

So the methods in @Robin2's thread DO apply, you can put the code wherever you want.  If you're really married to using the serialEvent function then do it.  It makes absolutely no difference.  It works exactly the same as having that code in loop. 

Now what's the problem?
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 06, 2017, 06:25 pm
Code: [Select]
int main(void)
{
init();

initVariant();

#if defined(USBCON)
USBDevice.attach();
#endif

setup();
   
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
       
return 0;
}



See where serialEventRun gets called?  That calls serialEvent if Serial.available() returns greater than 0.  IT doesn't do anything magic and IMHO it makes the code harder to maintain because it is being called by this "hidden" function.  Weren't you against all of that the other day? 

There's never any case where you *need* to us serialEvent. 
Title: Re: Serial.readBytesUntil()
Post by: sterretje on Aug 06, 2017, 06:52 pm
I don't know if Robin likes serial event or not. And it does not matter.

The principle behind his examples is that it's non-blocking. So you have time to do other stuff till the full message is received and hence no need to use serial event. The only reason, as I stated in reply #7 is if your current code uses it and it's easier to implement in which case you can mix and match.
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 06, 2017, 11:06 pm
This sounds like a problem for every solution.

Post your complete program and then maybe someone can suggest something useful.

...R
Careful what you ask for.

Code: [Select]
/*
int i = 0;

void setup() {

  // open serial connection
    Serial.begin(9600);

    //Serial.println("CLEARDATA"); // clears sheet starting at row 2
    Serial.println("CLEARSHEET"); // clears sheet starting at row 1
   
  // define 5 columns named "Date", "Time", "Timer", "Counter" and "millis"
    Serial.println("LABEL,Date,Time,Timer,Counter,millis");

  // set the names for the 3 checkboxes
    Serial.println("CUSTOMBOX1,LABEL,Stop logging at 250?");
    Serial.println("CUSTOMBOX2,LABEL,Resume log at 350?");
    Serial.println("CUSTOMBOX3,LABEL,Quit at 450?");

  // check 2 of the 3 checkboxes (first two to true, third to false)
    Serial.println("CUSTOMBOX1,SET,1");
    Serial.println("CUSTOMBOX2,SET,1");
    Serial.println("CUSTOMBOX3,SET,0");
}

void loop() {

    // simple print out of number and millis. Output e.g.,: "DATA,DATE,TIME,TIMER,4711,13374,AUTOSCROLL_20"
      Serial.println( (String) "DATA,DATE,TIME,TIMER," + i++ + "," + millis() + ",AUTOSCROLL_20" );
      // alternative writing method:
      /*   Serial.print("DATA,DATE,TIME,TIMER,");
        Serial.print(i++); Serial.print(",");
        Serial.println(millis());
        Serial.print(","); Serial.println("SCROLLDATA_20"); */

    // clear some cells in Excel (rectangle range from B10 to D20)
      if(i==100)
        Serial.println("ClearRange,B,10,D,20");

    // do a simple beep in Excel on PC
      if(i==150)
        Serial.println("BEEP");

    // read a value (in this case integer) from Excel (from a sheet by name)
      if(i==200)
      {
        Serial.println("CELL,GET,FROMSHEET,Simple Data,E,4"); // ==> request value from sheet
          // Serial.println("CELL,GET,E4"); ==> short version to read from active sheet in Excel
        int readvalue = Serial.readStringUntil(10).toInt(); // get response. Note: the '10' is important! Always use but never change ;-)
        Serial.println( (String) "Value of cell E4 is: " + readvalue); // result displayed in Excel DirectDebugWindow to double check
      }

    // check value of custombox1 on PLX DAQ in Excel and if
    // checkbox is checked then send the command to pause logging
      if(i==250)
      {
        Serial.println("CUSTOMBOX1,GET");
        int stoplogging = Serial.readStringUntil(10).toInt();
        // this information can be seen in the direct debug window on PLX DAQ in Excel
        Serial.println( (String) "Value of stoplogging/checkbox is: " + stoplogging);
        if(stoplogging)
          Serial.println("PAUSELOGGING");
      }

    // get a true random number from the computer
      if(i==300)
      {
        Serial.println("GETRANDOM,-4321,12345"); // between -4321 to 12345
        int rndseed = Serial.readStringUntil(10).toInt();
        Serial.println( (String) "Got random value '" + rndseed + "' from Excel" );
        // Note: this information is not posted to the Excel sheet because "DATA" is missing
        // instead this information can be seen in the direct debug window
      }

    // and now resume logging
      if(i==350)
      {
        Serial.println("CUSTOMBOX2,GET");
        int resumelogging = Serial.readStringUntil(10).toInt();
        if(resumelogging)
          Serial.println("RESUMELOGGING");
      }   

    // post to specific cells on default sheet as well as named sheet
      if(i==400)
      {
        Serial.println("CELL,SET,G10,400 test 1 string"); // default sheet active in PLX DAQ Excel
        Serial.println("CELL,SET,ONSHEET,Simple Data,G,11,400 test 2 string"); // named sheet available in PLX DAQ Excel
      }
       
    // and for forced quit of Excel with saving the file first
      if(i==123)
      {
        Serial.println("CUSTOMBOX3,GET");
        if(Serial.readStringUntil(10).toInt()) {
          Serial.println("SAVEWORKBOOKAS,450-Lines-File");
          Serial.println("FORCEEXCELQUIT");
        }
        else
          Serial.println("No forced Excel quit requested!");
      }
}


OK. Here is the start of my program. I haven't been able to add my code because the blocking commands keep fouling it up. So I need to remove all the blocking commands.  Hmmm...Sounds like the point of the post #1.

I haven't a clue how this helps answer the original question. But if you would like to help rewrite, thanks.

Although on an aspect more to the original post, does Serial.readBytesUntil() get interrupted while waiting by ISRs?
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 06, 2017, 11:19 pm
Although on an aspect more to the original post, does Serial.readBytesUntil() get interrupted while waiting by ISRs?
Yes it does. 



I read your whole post.  It seems like all you need to do is to read your serial line in a non-blocking fashion.  Why do you not like the methods in the Serial Input Basics thread that was linked earlier?  They seem like they'd do exactly what you want. 


For example change this:

Code: [Select]
if(i==200)
      {
        Serial.println("CELL,GET,FROMSHEET,Simple Data,E,4"); // ==> request value from sheet
          // Serial.println("CELL,GET,E4"); ==> short version to read from active sheet in Excel
        int readvalue = Serial.readStringUntil(10).toInt(); // get response. Note: the '10' is important! Always use but never change ;-)
        Serial.println( (String) "Value of cell E4 is: " + readvalue); // result displayed in Excel DirectDebugWindow to double check
      }


 to just fill a static char buffer with characters as they arrive and then have an if test for the 10 to indicate it is done and put that last line inside it so it only runs when the message is complete. 
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 06, 2017, 11:21 pm
Really, an even better solution would be to have all the serial handling code in its own function and call it from the top or bottom of loop every time.  Then instead of having to write the non-blocking receive out in each of those blocks, you could just use a boolean flag to let them know when their data was ready.  Each one of those blocks in your code could simply check the flag to see if they had got their answer back and move on if not. 
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 06, 2017, 11:23 pm
Really, an even better solution would be to have all the serial handling code in its own function and call it from the top or bottom of loop every time.  Then instead of having to write the non-blocking receive out in each of those blocks, you could just use a boolean flag to let them know when their data was ready. 
Even better, why not combine the non-blocking code into one entire subroutine and have that routine automatically executed when data is available?
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 06, 2017, 11:28 pm
Even better, why not combine the non-blocking code into one entire subroutine and have that routine automatically executed when data is available?
By what definition of automatically executed?  Remember, you're going to write non-blocking code now, so it does you no good to know that you got your transmission when you're off in the middle of loop somewhere.  You only need that data at the point in loop where you need the data.  The whole point is that now loop will be fast enough to hit the point where you need the data again really soon.  I think you're chasing a problem that doesn't exist. 

So yeah, you wrap that call to the serial handling function in a Serial.available check or you check in that function.  It doesn't make much difference. 
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 06, 2017, 11:30 pm
Note:  If you were talking about serialEvent, remember that it doesn't do anything automatically.  It just gets called at the end of loop in a if(Serial.available()) just like the function I'm talking about writing.  The only difference is that the whole thing is hidden in main and IIRC you were pretty upset about that a couple of days ago.  I was talking about having it called from the loop function where you can see it. 
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 06, 2017, 11:35 pm
Automatically executed as in interrupt driven, not via a polled function call.

Code: [Select]
int main(void)
{
init();

initVariant();

#if defined(USBCON)
USBDevice.attach();
#endif

setup();
   
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
       
return 0;
}



See where serialEventRun gets called?  That calls serialEvent if Serial.available() returns greater than 0.  IT doesn't do anything magic and IMHO it makes the code harder to maintain because it is being called by this "hidden" function.  Weren't you against all of that the other day? 

There's never any case where you *need* to us serialEvent. 
Seems like we have just had this conversation. In a previous post I was informed serialEvent() was interrupt driven. You have shown proof to the contrary.

If Arduino.h is calling serialEventRun() and Serial.available() is called in loop(), then isn't the same thing being checked twice?
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 06, 2017, 11:41 pm
If Arduino.h is calling serialEventRun() and Serial.available() is called in loop(), then isn't the same thing being checked twice?
The compiler would optimize out the one that's not being used. 
Title: Re: Serial.readBytesUntil()
Post by: Robin2 on Aug 06, 2017, 11:41 pm
In a previous post I was informed serialEvent() was interrupt driven.
Post a link to the post that said that.

Right through this Thread people have been telling you that it is not - including in my Serial Input Basics (http://forum.arduino.cc/index.php?topic=396450.0)

Careful what you ask for.
I don't see any serialEvent() or readBytesUntil() in the code in that Reply

...R
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 06, 2017, 11:47 pm
I'm not search four years worth the posts. It's not that important. It is what I was told. Both of you are showing with the program serialEvent() is not an ISR. At that time I was just starting and didn't know the Arduino lingo well. Likely I didn't ask the question using the correct lingo, which is very important on this site (if the words in the question are not just right, people fly off in every direction with their responses). Until recently the word 'interrupt' hasn't appeared in this thread nor in Serial Input Basics. Neither say serialEvent() is or is not an ISR.

I'm fine with changing that thought process, it is a matter of having the correct information.

You are correct there is no serialEvent() nor readBytesUntil(). It is the original code that needs to be modified to remove the five readStringUntil() instances. Unless I'm about to find out that readStringUntil() is non-blocking.

P.S. My bad. I asked about the wrong function. My mistake.
Title: Re: Serial.readBytesUntil()
Post by: westfw on Aug 07, 2017, 01:33 am
Here's the core of a non-blocking read-line thing.  Just call it "reasonable often" in loop; it returns a non-zero value when its seen a line terminator...   Don't forget that the Serial core is interrupt driven but has a relatively small buffer...

Code: [Select]
uint8_t parseGetline_nb(void)
{
    int c;

    c = Serial.read();
    switch (c) {
    case 127:
    case CTRL('H'):
    /*
     * Destructive backspace: remove last character
     */
    if (inptr > 0) {
        Serial.print("\010 \010");
        linebuffer[--inptr] = 0;
    }
    break;
    case CTRL('R'):
    /*
     * Ctrl-R retypes the line
     */
    Serial.print("\r\n");
    Serial.print(linebuffer);
    break;
    case CTRL('U'):
    /*
     * Ctrl-U deletes the entire line and starts over.
     */
    Serial.println("XXX");
    memset(linebuffer, 0, sizeof(linebuffer));
    inptr = 0;
    break;
    case CTRL('M'):
    Serial.println();            /* Echo newline too. */
    return inptr;
    default:
    /*
     * Otherwise, echo the character and put it into the buffer
     */
    linebuffer[inptr++] = c;
    Serial.write(c);
    case -1:
    /*
     * No character present; don't do anything.
     */
    return 0;
    break;
    }
}


Code: [Select]
void setup() {
  while (!Serial)
    delay(500);
  Serial.begin(115200);
  Serial.print(F("Enter command: "));
  parseReset();
  pinMode(13, OUTPUT);
}

boolean delay_without_delaying(unsigned long &since, unsigned long time) {
  // return false if we're still "delaying", true if time ms has passed.
  // this should look a lot like "blink without delay"
  unsigned long currentmillis = millis();
  if (currentmillis - since >= time) {
    since = currentmillis;
    return true;
  }
  return false;
}

int red, blue, green;
unsigned long ledtime;

void loop() {
  char *p;
  int8_t cmd;
  int n;

  if (parseGetline_nb()) {
    do {
      enum {
        CMD_RED, CMD_GREEN, CMD_BLUE, CMD_RESET  // make sure this matches the string
      };
      cmd = parseKeyword(PSTR("red green blue reset")); // look for a command.

      if (cmd >= 0) {
        n = parseNumber();
      }
      switch (cmd) {
        case CMD_RED:
          red = n;
          break;
        case CMD_BLUE:
          blue = n;
          break;
        case CMD_GREEN:
          green = n;
          break;
        case CMD_RESET:
          red = green = blue = 0;
          break;
        case PARSER_EOL:
          Serial.print("RED = "); Serial.print(red);
          Serial.print(" GREEN = "); Serial.print(green);
          Serial.print(" BLUE= "); Serial.println(blue);
          break;
        default:
          Serial.println("Invalid command");
          break;
      }
    } while (cmd >= 0);
    parseReset();
    Serial.print(F("Enter command: "));
  } // if line
 
  static int ledstate = false;
  if (delay_without_delaying(ledtime, 500)) {
    ledstate = !ledstate;
    digitalWrite(13, ledstate);
  }
}
Title: Re: Serial.readBytesUntil()
Post by: westfw on Aug 07, 2017, 01:39 am
(The rest of the "parser library", such as it is, is at https://github.com/WestfW/parser (https://github.com/WestfW/parser))
Title: Re: Serial.readBytesUntil()
Post by: Robin2 on Aug 07, 2017, 09:34 am
Likely I didn't ask the question using the correct lingo, which is very important on this site (if the words in the question are not just right, people fly off in every direction with their responses).
I don't think we impose a standard of accuracy that is as high as that required by the compiler.


I still have no idea why you object to using the straightforward non-blocking code in Serial Input Basics (http://forum.arduino.cc/index.php?topic=396450.0). It works.

...R
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 07, 2017, 01:41 pm
I was heading down the serialEvent() road as I had previous been informed it was interrupt driven.

As for Serial Input Basics, I'm working to reverse engineer it now to understand how to incorporate in the PLX-DAQ program to then incorporate it into the main program.

This is my first pass. It is not optimized but functional and seems to be working with PLX-DAQ. There's too much code in loop(), but it is a start.

Code: [Select]

int i = 0;

String inputString = "";         // a String to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup()
 {
  // open serial connection
   Serial.begin(9600);
   pinMode(3, INPUT_PULLUP);
   inputString.reserve(200);
 }

void StartTransfer()
 {
    //Serial.println("CLEARDATA"); // clears sheet starting at row 2
    Serial.println("CLEARSHEET"); // clears sheet starting at row 1
   
  // define 5 columns named "Date", "Time", "Timer", "Counter" and "millis"
    Serial.println("LABEL,Date,Time,Timer,Counter,millis");

  // set the names for the 3 checkboxes
    Serial.println("CUSTOMBOX1,LABEL,Stop logging at 250?");
    Serial.println("CUSTOMBOX2,LABEL,Resume log at 350?");
    Serial.println("CUSTOMBOX3,LABEL,Quit at 450?");

  // check 2 of the 3 checkboxes (first two to true, third to false)
    Serial.println("CUSTOMBOX1,SET,1");
    Serial.println("CUSTOMBOX2,SET,1");
    Serial.println("CUSTOMBOX3,SET,0");

    i=0;
 }
 
void SendData()
 {
  static boolean wait_for_response = false;
 
 // simple print out of number and millis. Output e.g.,: "DATA,DATE,TIME,TIMER,4711,13374,AUTOSCROLL_20"
  if (!wait_for_response)
   { Serial.println( (String) "DATA,DATE,TIME,TIMER," + i++ + "," + millis() + ",AUTOSCROLL_20" ); }

    // clear some cells in Excel (rectangle range from B10 to D20)
  if (i==100)
   { Serial.println("ClearRange,B,10,D,20"); }

    // do a simple beep in Excel on PC
  if (i==150)
   { Serial.println("BEEP"); }

    // read a value (in this case integer) from Excel (from a sheet by name)
  if (i==200)
   {
    if (!wait_for_response)
     {
      Serial.println("CELL,GET,FROMSHEET,Simple Data,E,4");  // ==> request value from sheet
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         Serial.println( (String) "Value of cell E4 is: " + inputString.toInt()); // result displayed in Excel DirectDebugWindow to double check
         stringComplete = false;
         inputString = "";
         wait_for_response = false;
        }
      } 
   }

    // check value of custombox1 on PLX DAQ in Excel and if
    // checkbox is checked then send the command to pause logging
  if (i==250)
   {
    if (!wait_for_response)
     {
      Serial.println("CUSTOMBOX1,GET");  // ==> request value from sheet
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         int stoplogging =  inputString.toInt();
         Serial.println( (String) "Value of stoplogging/checkbox is: " + stoplogging); // result displayed in Excel DirectDebugWindow to double check
         if(stoplogging)
           Serial.println("PAUSELOGGING");
         stringComplete = false;
         inputString = "";
         wait_for_response = false;
        }
      } 
   }

    // get a true random number from the computer
  if (i==300)
   {
    if (!wait_for_response)
     {
      Serial.println("GETRANDOM,-4321,12345"); // between -4321 to 12345
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         int rndseed = inputString.toInt();
         Serial.println( (String) "Got random value '" + rndseed + "' from Excel" );
         // Note: this information is not posted to the Excel sheet because "DATA" is missing
         // instead this information can be seen in the direct debug window
         stringComplete = false;
         inputString = "";
         wait_for_response = false;
        }
      } 
   }

    // and now resume logging
  if (i==350)
   {
    if (!wait_for_response)
     {
      Serial.println("CUSTOMBOX2,GET");
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         int resumelogging = inputString.toInt();
         if(resumelogging)
           Serial.println("RESUMELOGGING");
         stringComplete = false;
         inputString = "";
         wait_for_response = false;
        }
      } 
   }

    // post to specific cells on default sheet as well as named sheet
  if (i==400)
   {
    Serial.println("CELL,SET,G10,400 test 1 string"); // default sheet active in PLX DAQ Excel
    Serial.println("CELL,SET,ONSHEET,Simple Data,G,11,400 test 2 string"); // named sheet available in PLX DAQ Excel
   }
       
  // and for forced quit of Excel with saving the file first
  if (i==123)
   {
    if (!wait_for_response)
     {
      Serial.println("CUSTOMBOX3,GET");
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         if (inputString.toInt())
          {
           Serial.println("SAVEWORKBOOKAS,450-Lines-File");
           Serial.println("FORCEEXCELQUIT");
          }
          else
           {
            Serial.println("No forced Excel quit requested!");
           }
         stringComplete = false;
         inputString = "";
         wait_for_response = false;
        }
      } 
   }
 
 }

void ReadSerial()
 {
  char inChar = (char)Serial.read();
    // add it to the inputString:
  inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
  if (inChar == 10)
   { stringComplete = true; }
 }
 
void loop()
 {
  static boolean transfer_data = false;
  int start_transfer;
 
  start_transfer = digitalRead(3);
 
  if ((start_transfer==1) && (transfer_data == false))
   {
    transfer_data = true;
    StartTransfer();
   }
   else if ((start_transfer==0) && (transfer_data == true))
    {
     transfer_data = false;
    }

  if (transfer_data)
   {
    SendData();
   }
  if (Serial.available())
   { ReadSerial(); }
 }
Title: Re: Serial.readBytesUntil()
Post by: Robin2 on Aug 07, 2017, 03:25 pm
This is my first pass.
You could replace these two lines in your loop() function
Code: [Select]
 if (Serial.available())
   { ReadSerial(); }

with
Code: [Select]
recvWithEndMarker();
if you include my function in your program.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings (http://www.cplusplus.com/reference/cstring/) - char arrays terminated with 0.

...R
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 07, 2017, 10:01 pm
You could replace these two lines in your loop() function
Code: [Select]
if (Serial.available())
   { ReadSerial(); }

with
Code: [Select]
recvWithEndMarker();
if you include my function in your program.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings (http://www.cplusplus.com/reference/cstring/) - char arrays terminated with 0.

...R
First option didn't work so well as it wasn't compatible with anything except the example. On a leap of faith, I did take you second suggestion. I have never used the String class before, but it was in the example and worked the first try. I am more familiar with cstrings anyhow.

Code: [Select]

int i = 0;

char inputString[200];         // a String to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup()
 {
  // open serial connection
   Serial.begin(9600);
   pinMode(3, INPUT_PULLUP);
 }

void StartTransfer()
 {
    //Serial.println("CLEARDATA"); // clears sheet starting at row 2
    Serial.println("CLEARSHEET"); // clears sheet starting at row 1
   
  // define 5 columns named "Date", "Time", "Timer", "Counter" and "millis"
    Serial.println("LABEL,Date,Time,Timer,Counter,millis");

  // set the names for the 3 checkboxes
    Serial.println("CUSTOMBOX1,LABEL,Stop logging at 250?");
    Serial.println("CUSTOMBOX2,LABEL,Resume log at 350?");
    Serial.println("CUSTOMBOX3,LABEL,Quit at 450?");

  // check 2 of the 3 checkboxes (first two to true, third to false)
    Serial.println("CUSTOMBOX1,SET,1");
    Serial.println("CUSTOMBOX2,SET,1");
    Serial.println("CUSTOMBOX3,SET,0");

    i=0;
 }
 
void SendData()
 {
  static boolean wait_for_response = false;
 
 // simple print out of number and millis. Output e.g.,: "DATA,DATE,TIME,TIMER,4711,13374,AUTOSCROLL_20"
  if (!wait_for_response)
   { Serial.println( (String) "DATA,DATE,TIME,TIMER," + i++ + "," + millis() + ",AUTOSCROLL_20" ); }

    // clear some cells in Excel (rectangle range from B10 to D20)
  if (i==100)
   { Serial.println("ClearRange,B,10,D,20"); }

    // do a simple beep in Excel on PC
  if (i==150)
   { Serial.println("BEEP"); }

    // read a value (in this case integer) from Excel (from a sheet by name)
  if (i==200)
   {
    if (!wait_for_response)
     {
      Serial.println("CELL,GET,FROMSHEET,Simple Data,E,4");  // ==> request value from sheet
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         Serial.println( (String) "Value of cell E4 is: " + atoi(inputString)); // result displayed in Excel DirectDebugWindow to double check
         stringComplete = false;
         ClearInputString();
         wait_for_response = false;
        }
      } 
   }

    // check value of custombox1 on PLX DAQ in Excel and if
    // checkbox is checked then send the command to pause logging
  if (i==250)
   {
    if (!wait_for_response)
     {
      Serial.println("CUSTOMBOX1,GET");  // ==> request value from sheet
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         int stoplogging =  atoi(inputString);
         Serial.println( (String) "Value of stoplogging/checkbox is: " + stoplogging); // result displayed in Excel DirectDebugWindow to double check
         if(stoplogging)
           Serial.println("PAUSELOGGING");
         stringComplete = false;
         ClearInputString();
         wait_for_response = false;
        }
      } 
   }

    // get a true random number from the computer
  if (i==300)
   {
    if (!wait_for_response)
     {
      Serial.println("GETRANDOM,-4321,12345"); // between -4321 to 12345
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         int rndseed = atoi(inputString);
         Serial.println( (String) "Got random value '" + rndseed + "' from Excel" );
         // Note: this information is not posted to the Excel sheet because "DATA" is missing
         // instead this information can be seen in the direct debug window
         stringComplete = false;
         ClearInputString();
         wait_for_response = false;
        }
      } 
   }

    // and now resume logging
  if (i==350)
   {
    if (!wait_for_response)
     {
      Serial.println("CUSTOMBOX2,GET");
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         int resumelogging = atoi(inputString);
         if(resumelogging)
           Serial.println("RESUMELOGGING");
         stringComplete = false;
         ClearInputString();
         wait_for_response = false;
        }
      } 
   }

    // post to specific cells on default sheet as well as named sheet
  if (i==400)
   {
    Serial.println("CELL,SET,G10,400 test 1 string"); // default sheet active in PLX DAQ Excel
    Serial.println("CELL,SET,ONSHEET,Simple Data,G,11,400 test 2 string"); // named sheet available in PLX DAQ Excel
   }
       
  // and for forced quit of Excel with saving the file first
  if (i==123)
   {
    if (!wait_for_response)
     {
      Serial.println("CUSTOMBOX3,GET");
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         if (atoi(inputString))
          {
           Serial.println("SAVEWORKBOOKAS,450-Lines-File");
           Serial.println("FORCEEXCELQUIT");
          }
          else
           {
            Serial.println("No forced Excel quit requested!");
           }
         stringComplete = false;
         ClearInputString();
         wait_for_response = false;
        }
      } 
   }
 }

void ClearInputString()
 {
  int x;
  for (x=-0; x++; x<sizeof(inputString))
   { inputString[x]='\0'; }
 }
 
void ReadSerial()
 {
  static byte inputStringIDX = 0;
 
  char inChar = Serial.read();
  if (inputStringIDX < (sizeof(inputString) -1))
   {
    if (inChar == 10)
     {
      inputString[inputStringIDX] = '\0';
      inputStringIDX=0;
      stringComplete = true;
     }
     else
      {
       inputString[inputStringIDX] = inChar;
       inputStringIDX+=1;
      }
   }
 }
 
void loop()
 {
  static boolean transfer_data = false;
  int start_transfer;
 
  start_transfer = digitalRead(3);
 
  if ((start_transfer==1) && (transfer_data == false))
   {
    transfer_data = true;
    StartTransfer();
   }
   else if ((start_transfer==0) && (transfer_data == true))
    {
     transfer_data = false;
    }

  if (transfer_data)
   {
    SendData();
   }
  if (Serial.available())
   { ReadSerial(); }
 }
Title: Re: Serial.readBytesUntil()
Post by: Robin2 on Aug 07, 2017, 11:35 pm
First option didn't work so well as it wasn't compatible with anything except the example.
As you have not posted the program in which you tried it I have no idea why it did not work for you. It does work for me and for other Forum members.

...R
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 07, 2017, 11:39 pm
As you have not posted the program in which you tried it I have no idea why it did not work for you. It does work for me and for other Forum members.

...R
Why would I post broke code? Your suggestion was to copy and paste the Serial Basics into the previously posted code and replace the if statement with the function call. I did. I pasted in Example 3 (a more complete system) It didn't work. The example was written for a very specific format and structure which is wrong for my application. By the time I removed what didn't apply I wasn't left with much, including a compilable (is that word?) sketch.

As you suggest, it isn't that difficult, please feel free to move ahead. I already spent more than 4 hours reverse engineering the sketch to the basic the tactics and then to adapt it to create the working program posted.

Your example are nice but very specific. I don't see any time I could or would have been able to use them without extensive heavy modifications.

Do you see anything wrong with the posted sketch? Any gaps or improvements you suggest while you are critiquing it?
Title: Re: Serial.readBytesUntil()
Post by: Robin2 on Aug 08, 2017, 12:15 am
Why would I post broke code?
WTF would be the point of posting working code?

If you post the broken code we may be able to tell you how to fix it.

...R
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 08, 2017, 01:10 am
WTF would be the point of posting working code?
For the same reason you posted working code. As an example of how to do something, or as a solution to a problem, or to show you have solved someone else's problem.

As you have not posted the program in which you tried it I have no idea why it did not work for you. It does work for me and for other Forum members.

...R
OK. Here is exactly what you wanted. The original program with your function substituted for the if (Serial.available()) statement.

Code: [Select]

int i = 0;

String inputString = "";         // a String to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup()
 {
  // open serial connection
   Serial.begin(9600);
   pinMode(3, INPUT_PULLUP);
   inputString.reserve(200);
 }

void StartTransfer()
 {
    //Serial.println("CLEARDATA"); // clears sheet starting at row 2
    Serial.println("CLEARSHEET"); // clears sheet starting at row 1
   
  // define 5 columns named "Date", "Time", "Timer", "Counter" and "millis"
    Serial.println("LABEL,Date,Time,Timer,Counter,millis");

  // set the names for the 3 checkboxes
    Serial.println("CUSTOMBOX1,LABEL,Stop logging at 250?");
    Serial.println("CUSTOMBOX2,LABEL,Resume log at 350?");
    Serial.println("CUSTOMBOX3,LABEL,Quit at 450?");

  // check 2 of the 3 checkboxes (first two to true, third to false)
    Serial.println("CUSTOMBOX1,SET,1");
    Serial.println("CUSTOMBOX2,SET,1");
    Serial.println("CUSTOMBOX3,SET,0");

    i=0;
 }
 
void SendData()
 {
  static boolean wait_for_response = false;
 
 // simple print out of number and millis. Output e.g.,: "DATA,DATE,TIME,TIMER,4711,13374,AUTOSCROLL_20"
  if (!wait_for_response)
   { Serial.println( (String) "DATA,DATE,TIME,TIMER," + i++ + "," + millis() + ",AUTOSCROLL_20" ); }

    // clear some cells in Excel (rectangle range from B10 to D20)
  if (i==100)
   { Serial.println("ClearRange,B,10,D,20"); }

    // do a simple beep in Excel on PC
  if (i==150)
   { Serial.println("BEEP"); }

    // read a value (in this case integer) from Excel (from a sheet by name)
  if (i==200)
   {
    if (!wait_for_response)
     {
      Serial.println("CELL,GET,FROMSHEET,Simple Data,E,4");  // ==> request value from sheet
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         Serial.println( (String) "Value of cell E4 is: " + inputString.toInt()); // result displayed in Excel DirectDebugWindow to double check
         stringComplete = false;
         inputString = "";
         wait_for_response = false;
        }
      } 
   }

    // check value of custombox1 on PLX DAQ in Excel and if
    // checkbox is checked then send the command to pause logging
  if (i==250)
   {
    if (!wait_for_response)
     {
      Serial.println("CUSTOMBOX1,GET");  // ==> request value from sheet
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         int stoplogging =  inputString.toInt();
         Serial.println( (String) "Value of stoplogging/checkbox is: " + stoplogging); // result displayed in Excel DirectDebugWindow to double check
         if(stoplogging)
           Serial.println("PAUSELOGGING");
         stringComplete = false;
         inputString = "";
         wait_for_response = false;
        }
      } 
   }

    // get a true random number from the computer
  if (i==300)
   {
    if (!wait_for_response)
     {
      Serial.println("GETRANDOM,-4321,12345"); // between -4321 to 12345
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         int rndseed = inputString.toInt();
         Serial.println( (String) "Got random value '" + rndseed + "' from Excel" );
         // Note: this information is not posted to the Excel sheet because "DATA" is missing
         // instead this information can be seen in the direct debug window
         stringComplete = false;
         inputString = "";
         wait_for_response = false;
        }
      } 
   }

    // and now resume logging
  if (i==350)
   {
    if (!wait_for_response)
     {
      Serial.println("CUSTOMBOX2,GET");
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         int resumelogging = inputString.toInt();
         if(resumelogging)
           Serial.println("RESUMELOGGING");
         stringComplete = false;
         inputString = "";
         wait_for_response = false;
        }
      } 
   }

    // post to specific cells on default sheet as well as named sheet
  if (i==400)
   {
    Serial.println("CELL,SET,G10,400 test 1 string"); // default sheet active in PLX DAQ Excel
    Serial.println("CELL,SET,ONSHEET,Simple Data,G,11,400 test 2 string"); // named sheet available in PLX DAQ Excel
   }
       
  // and for forced quit of Excel with saving the file first
  if (i==123)
   {
    if (!wait_for_response)
     {
      Serial.println("CUSTOMBOX3,GET");
      wait_for_response = true;
     }
     else
      {
       if (stringComplete == true)
        {
         if (inputString.toInt())
          {
           Serial.println("SAVEWORKBOOKAS,450-Lines-File");
           Serial.println("FORCEEXCELQUIT");
          }
          else
           {
            Serial.println("No forced Excel quit requested!");
           }
         stringComplete = false;
         inputString = "";
         wait_for_response = false;
        }
      } 
   }
 
 }

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void recvWithEndMarker()
 {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
   
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void loop()
 {
  static boolean transfer_data = false;
  int start_transfer;
 
  start_transfer = digitalRead(3);
 
  if ((start_transfer==1) && (transfer_data == false))
   {
    transfer_data = true;
    StartTransfer();
   }
   else if ((start_transfer==0) && (transfer_data == true))
    {
     transfer_data = false;
    }

  if (transfer_data)
   {
    SendData();
   }
  recvWithEndMarker();
 }
Title: Re: Serial.readBytesUntil()
Post by: Robin2 on Aug 08, 2017, 09:37 am
OK. Here is exactly what you wanted. The original program with your function substituted for the if (Serial.available()) statement.
Thank you. However you have not told me what happens when you tried to run it.

You don't seem to have included the necessary variable declarations. And I don't see any code in the rest of your program that responds to the variable newData.

...R
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 08, 2017, 12:51 pm
Your instructions were to include your function and replace the if statement with the function call. I did. It compiles but doesn't respond or send anything. SO there is more to it than just adding your function and calling it. After I go through and rework it to include one variable set or the other (mine or yours), then it works. You asked to use your function, I did. You asked for the broken code to be posted. I did. I have not followed you on most of your posts, so I figured you wanted the broken code to fix it yourself. After several hours work, I have the working program I posted. Why did you want to walk through this exercise?

Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 08, 2017, 05:31 pm
Man this guy is awfully combative.  Why don't you just leave him alone and let him learn by himself and with Google.  He obviously doesn't want to be taught anything here.

OP:  why do you keep posting here if you don't want to learn anything?  There's another section called gigs and collaborations where you can pay someone to just fix your code without worrying about people trying to help you learn to do it on your own.
Title: Re: Serial.readBytesUntil()
Post by: PaulS on Aug 08, 2017, 05:36 pm
Quote
There's another section called gigs and collaborations where you can pay someone to just fix your code without worrying about people trying to help you learn to do it on your own.
Even in Gigs and Collaboration, I tend to try to get people to learn to do it (whatever it is) themselves, so the worry that someone would want OP to learn something is not unfounded.
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 08, 2017, 05:49 pm
Yeah but at least there he can preface his post with something like, "I have no interest in learning to do this myself please just fix me code" and then he doesn't have to worry that someone might try to teach him something.  Or he could even add, "If you try to help me learn I'll be overly literal with your direction and then get combative when you try to figure out what part I don't understand because it makes me feel dumb and I can't handle being confronted with anything where someone else knows more than me" and then folks like you and I would just avoid him. At least that would help to eliminate the combative attitude.
Title: Re: Serial.readBytesUntil()
Post by: Robin2 on Aug 08, 2017, 06:46 pm
Your instructions were to include your function and replace the if statement with the function call. I did. It compiles but doesn't respond or send anything. SO there is more to it than just adding your function and calling it.
As you had not told me that you did not understand my examples I assumed that you did and would know how to incorporate one of them.

Code: [Select]
Why did you want to walk through this exercise?
Because I wanted to ensure that you had not found a problem in my example code that I would need to fix.

...R
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 08, 2017, 09:50 pm
If you all recall, the original post was if Serial.readBytesUntil() and/or readStringUntil() was blocking or non-blocking. We have ventured three or more degrees away from the original post. Questions has been asked an some answers given. It has been quite a meandering conversation. I'm fine with it all and even revealed some additional items about the "Arduino IDE" that weren't readily available.

Quote
He obviously doesn't want to be taught anything here.

OP:  why do you keep posting here if you don't want to learn anything?  There's another section called gigs and collaborations where you can pay someone to just fix your code without worrying about people trying to help you learn to do it on your own.
Though I appreciate the conversation, there are 44 posts and only 4 of which are useful or provide any information or teaching. I am here to learn, but there are 40 posts that are a lot of "do this." No explanation as to how or why; ie., no teaching. (I have a pet peeve about taking classes at $$$$$$$$ per credit, only to sit for 3 hours per week and listen to the professor read from the textbook. I want to be taught, not read to.)

Code: [Select]
Why did you want to walk through this exercise?
Because I wanted to ensure that you had not found a problem in my example code that I would need to fix.
...R
I can't tell if it works or not as it was not a module I could use verbatim. So I guess its a no win-no lose.

I thank you all for the information on serialEvent(), Serial.readxxxxuntil(), and the "Arduino IDE". All the information that I did not see (might have been there and I didn't see it), but needed to know.

I would not mind a few folks that are using PLX-DAQ to trying the resultant and post your thoughts. NetDevil, have you seen the post? Do you have any feedback?[quote author=Delta_G
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 08, 2017, 10:17 pm
Quote
(I have a pet peeve about taking classes at $$$$$$$$ per credit, only to sit for 3 hours per week and listen to the professor read from the textbook. I want to be taught, not read to.)
If you would take a slightly different view of this, you'd see that this is what that looks like.  You do your best to stick in the functions that @Robin is talking about and when it doesn't work you post it back up and someone helps you figure out what you did wrong.  Then you get to learn from your mistakes, which is really the best way to learn.  You get to see what parts you don't understand and you get to ask questions about things until you do understand it.  And understanding IS the goal of learning right?

What you don't do is just say, "I tried that, didn't work, gimme something else".  If we would just take your code, fix it, and hand it back to you all nice and neat and working, that would be like the professor just reading the answers to you.  From this quote, you obviously understand how that works. 

In order for the student to learn, he must want to learn.  You do the learning.  The teacher does the teaching.  But the teacher can't make you learn it.  They can only try to help facilitate that process. 
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 08, 2017, 10:21 pm
Also keep in mind that you didn't pay anyone here anything and nobody here owes you anything.  If we wanted to start posting cute cat videos all over this thread that doesn't violate any agreement that anyone ever had with you.  Now, I don't think things are going to go to that level.  That would be ridiculous.  But I don't think you can really get mad because the FREE help you got from a bunch of strangers wasn't up to your standard. 
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 08, 2017, 11:56 pm
If you would take a slightly different view of this, you'd see that this is what that looks like.  You do your best to stick in the functions that @Robin is talking about and when it doesn't work you post it back up and someone helps you figure out what you did wrong.  Then you get to learn from your mistakes, which is really the best way to learn.  You get to see what parts you don't understand and you get to ask questions about things until you do understand it.  And understanding IS the goal of learning right?

What you don't do is just say, "I tried that, didn't work, gimme something else".  If we would just take your code, fix it, and hand it back to you all nice and neat and working, that would be like the professor just reading the answers to you.  From this quote, you obviously understand how that works.  
You hit the nail on the head. The person trying to "teach" needs to provide more than just the answers. To learn and understand the explanation needs to be provided. Only in four posts was anything explained, especially including the post on serialeventrun. The remainder of the posts were 'here is the answer, just do it'.

Once you (all) got me the correct information on serialEvent() (ie., that I was misled to believe it was an ISR). I moved on to the Serial.available() method as mentioned. I did say, I tried the code [mentioned[ and that it didn't work. At the same time, I spent the time figuring out how to get it to work. I bother posting the non-working sketch because I wanted to, and did, figure out how to make it work. So I skipped a few steps (including posting the non-working code originally, though did finally after repeated requests), and instead posted "AH HA!" I got it working and here is the result.

Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 09, 2017, 12:17 am
You hit the nail on the head. The person trying to "teach" needs to provide more than just the answers. To learn and understand the explanation needs to be provided. Only in four posts was anything explained, especially including the post on serialeventrun. The remainder of the posts were 'here is the answer, just do it'.
You're still putting the onus on the teacher.  IT is the STUDENTS responsibility to learn.  Robin has shown more than a few people how to work with Serial data.  He was telling you to "try this" not as an answer but as a learning exercise.  If you can't see that, then you're going to have a hard time learning to code.  What you're asking for is, "tell me the explanation" or in other words, "read it to me."  But as we've already discussed, that's not what you want.  Would it make more sense to you had Robin said, "Try this and you might learn something" instead of just "Try this".  To me it seems like most of the wasted space on this thread isn't people giving you bad advise, it's people trying to get through your combative attitude towards the advise they gave. 

US- "Here try this way"
YOU- "I don't want to"
US- "Just try it, it will work if you do it right"
YOU- "No, I don't like that method I want this other one"
US- "But it won't work for you, why don't you at least try this"
YOU -"Well I copied it and it didn't work, poo on you why aren't you trying to help me"
US- "We are, show us what went wrong and we can help you fix it right"
YOU- "But I wanted something I could just copy in.  You didn't say there'd be more work"
US- "Well you obviously did something wrong, why don't you let us help you?"
YOU- "Why would I ever show you what I tried"
US-  "So we can help you understand and fix it."
YOU- "I don't want you to just explain it to me and give me the answer, I want you to just explain it to me and give me the answer"
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 09, 2017, 12:53 am
Imagine a different story:



US - "Here try this"
YOU - "I tried it, but it didn't work.  Here look at it.  Can you show me what I did wrong?"
US - "Oh you left this little thing out.  Fix it here and try again"
YOU - "OK, but it still gives this little error"
US - "Oh, because you did this little thing wrong.  Here is an explanation for why it works this way and not that"
YOU - "Oh now I get it.  Cool now I can parse Serial in any program I ever write."
EVERYONE TOGETHER - "Yaaaaayyyy"


Wouldn't that have been better?  The only difference is a student who is trying to learn instead of being combative about what he thinks he is owed by a bunch of strangers he hasn't ever done diddly-squat for. 
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 09, 2017, 01:11 am
Imagine a different story:

US - "Here try this"
YOU - "I tried it, but it didn't work.  Here look at it.  Can you show me what I did wrong?"
US - "Oh you left this little thing out.  Fix it here and try again"
YOU - "OK, but it still gives this little error"
US - "Oh, because you did this little thing wrong.  Here is an explanation for why it works this way and not that"
YOU - "Oh now I get it.  Cool now I can parse Serial in any program I ever write."
EVERYONE TOGETHER - "Yaaaaayyyy"

Wouldn't that have been better? The only difference is a student who is trying to learn instead of being combative about what he thinks he is owed by a bunch of strangers he hasn't ever done diddly-squat for.
Sure. Too bad that wasn't the way the thread started. The student asked a question and got an explanation then was handed an answer over and over to question that was never asked without explanation of why that was the way to do it. Yes, if pushed, I push back. Explain and I will follow. As for parsing serial, I don't know how many serial parsers I have written. In this particular case, I was trying to find the shortest way to convert a working program from using blocking code to non-blocking code without breaking it too much while trying to avoid starting all over since the blocking program works with the PC program already.

I'm putting the onnus on both. It is a two way street. The student must be able to learn and the teacher must provide the information (not the answer) to learn from. 40+ posts here are the teacher providing the answer key, not the explanation. SerialInputBasics explains the code but not the basis behind it. I had to reverse engineer it to understand the basis in order to adapt it to the PLX-DAQ program. From the first post, it was a matter of how to do it the Arduino way. As we have discussed elsewhere, the issue I have is getting the background on the differences doing it the Arduino way versus coding everything from scratch. I don't need to learn to code, I need to learn to program the Arduino way. (P.S. I have eclipse downloaded, but won't have the time in the next two projects to make it the primary IDE)

Almost but not quite, missing some context, and some lines far off I'm not sure they landed in this thread.
US- "Here try this way"
YOU- "I don't want to"
US- "Just try it, it will work if you do it right"
YOU- "No, I don't like that method I want this other one"
US- "But it won't work for you, why don't you at least try this"
YOU -"Well I copied it and it didn't work, poo on you why aren't you trying to help me"
US- "We are, show us what went wrong and we can help you fix it right"
YOU- "But I wanted something I could just copy in.  You didn't say there'd be more work"
US- "Well you obviously did something wrong, why don't you let us help you?"
YOU- "Why would I ever show you what I tried"
US-  "So we can help you understand and fix it."
YOU- "I don't want you to just explain it to me and give me the answer, I want you to just explain it to me and give me the answer"
(retaining the perspective for continuity)

This is a little more realistic.
YOU - "Is Serial.readxxxUntil() blocking"
US - "Yes. Do it this way"
YOU - "I want to use serialEvent()"
US- "Here try this way"
YOU- "I don't want to. I want to use serialEvent()"
US- "Just try it, it will work if you do it right" (won't everything)
YOU- "No, I don't like that method I want to use serialEvent()" (recall, I was expecting serialEvent() to be an ISR)
US- "Show us your code."
YOU - *Here's the code.
US - ' Why don't you "have all the serial handling code in its own function"
YOU - "Like encapsulated in serialEvent() as shown in the reference section?
US - "serialEvent is not and ISR. See here's the code."
YOU - "Oh! OK. Thanks for setting me straight. Here's my new code using Serial.available()'
US - "WHY DIDN'T YOU JUST USE THE CODE IN THE LINK?"
YOU -"Fine. I'll try it..........Well I copied it. It didn't work, poo on you. I fixed it myself."
US- "Show us what went wrong and we can help you fix it right"
YOU- "I was told what to change, copy and go. There was so much much more to it, I reverse engineered it and implemented the most basal concepts."
US- "Well you obviously did something wrong, why don't you show us the broken program and let us help you?"
YOU- (confused) "Why do you want to see the broken code after I already have it working? How about critiquing the final working code?"
US-  "So we can help you understand and fix it."
YOU - "But I have already fixed it. If you insist in working through it yourself then help yourself. Here's the code from the point it was broke."


If you can't see that, then you're going to have a hard time learning to code.
Learning to code? I hope I have a good grasp on programming after nearly 40 years. What I don't have a grasp on is the integration of the "Arduino way" including those items behind the scenes or not documented as we have previously discussed.
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 09, 2017, 01:22 am
How was anyone supposed to guess thatyou wanted it because it was interrupt driven?  I finally guessed at that and put it out there just in case that's what you were thinking.  Up to that point we had probably assumed that someone with 40 years of programming would be smart enough to read the docs.
Title: Re: Serial.readBytesUntil()
Post by: Delta_G on Aug 09, 2017, 01:24 am
But it's cool.  Since you seem like you'd rather just be combative I'll just leave you with your misconceptions in the future and go help someone who actually understands how advise on a FREE forum works.
Title: Re: Serial.readBytesUntil()
Post by: adwsystems on Aug 09, 2017, 01:42 am
After 25 years of posting on forums, I know well how they work. I have learned to keep the questions and responses short and to the point (recent posts aside) or the responders are liable to wonder off in irrelevant directions. See post #1, one line that's it.

That's difference between responding to posts by beginners versus the experience people. The beginners want the water, the experienced people want to know how to get to the water and why they should go that way. If you only give them the water and don't give them the directions and why you should follow them, they get resistive. All the "problems" in this thread stem from that concept.

40 years experience has nothing to do with reading documentation that doesn't exist. We both agree that level of information is not there.

Also a two-way miss. I was proceeding on the explanation I previously received (serialEvent is an ISR). Your educated guess was correct, that I was under the impression that serialEvent() is an ISR. But why would I ask a question I already have an answer to? How many times have you not asked a question because you already had the answer, only to find out you had the wrong information. Once you set me straight we moved along, but I was still badgered with "USE THE CODE IN THE LINK" without any explanation why. Until I submitted, tried it, failed, and then made it work. Then I was criticized for not asking for help when the code didn't work at first and then for not posting the broken code (which was fixed before I thought to post; ie. learning on my own).

I'm not sure whom is being combative in this thread, it seems like there is plenty to share.