• I think I see the problem - you're setting a timeout to read back the data EVERY TIME you read from the serial port, so you end up with a crapload of timeouts. The number of reads you get before it dies at 9600 baud is (based on a quick estimate) consistent with storing the data (in a simple array, so it wastes tons of memory) AND creating a timeout for every byte. The solution is to only set the timeout once.

    var LED_ORANGE = LED1;
    var LED_GREEN  = LED2;
    var LED_RED    = LED3;
    var LED_BLUE   = LED4;
    var arr=null;  
    var timeout = null; //variable for timeout
    function Toggle_Led(LED) {
     on = !on;
     digitalWrite(LED, on);
     return 1;
    }
    function Show(str) {
      console.log('**' + str);
    }
    function Thread1() {
      Toggle_Led(LED_ORANGE);
    }
    var Counter = 0;
    Show("Start!");
    // Тест UART
    //==================================================================
    Serial2.setup(9600, {rx:A3,tx:A2});
    //USB.setup(9600, {dm:A11,dp:A12});
    Serial2.on('data', function (data) {  // Receive function
      console.log(Counter + ") send");
      process.memory(); //This does nothing, other than possibly slow down code execution - did you mean to log it with console.log()? 
      if (data.length > 0) {  
        digitalWrite(LED_GREEN, 1);
        //console.log(data + "\n");
        
        if (arr == null)  {
          arr = new Array();
        }
        arr.push(data);          // Add new element
        // Таймаут приёма
        //==============================================================
        if (timeout == null) {  //If we haven't started the timeout yet, start it now, since we're getting data
            timeout=setTimeout(function (e) {
              console.log(process.memory());
              if (arr == null)  return;
              //for (var i=0; i<arr.length; i++)
              //{
              //  console.log(arr[i]);
              //}
              var str = arr.join("");
              console.log(str);
              
              arr = null;
              Serial2.println(str);        // Send
          
              digitalWrite(LED_GREEN, 0);
              digitalWrite(LED_RED, 0);
            }, 5000);
        } //Close the if...
        //==============================================================
      } else {
        digitalWrite(LED_RED, 1);
        console.log("Bad");
      }
      
      Counter++;
    });
    //==================================================================
    setInterval(Thread1,3000);
    Show("Stop!");
    save();
    

    For better memory usage, something like this would be a good start - by not creating the array and just building the string as it comes.

    var LED_ORANGE = LED1;
    var LED_GREEN  = LED2;
    var LED_RED    = LED3;
    var LED_BLUE   = LED4;
    var str = ""; //no array, just a string.
    var timeout = null; //variable for timeout
    function Toggle_Led(LED) {
     on = !on;
     digitalWrite(LED, on);
     return 1;
    }
    function Show(str) {
      console.log('**' + str);
    }
    function Thread1() {
      Toggle_Led(LED_ORANGE);
    }
    var Counter = 0;
    Show("Start!");
    // Тест UART
    //==================================================================
    Serial2.setup(9600, {rx:A3,tx:A2});
    //USB.setup(9600, {dm:A11,dp:A12});
    Serial2.on('data', function (data) {  // Receive function
      console.log(Counter + ") send");
      process.memory();
      if (data.length > 0) {  
        digitalWrite(LED_GREEN, 1);
        str += data;
        //console.log(data + "\n");
        // Таймаут приёма
        //==============================================================
        if (timeout == null) {  //If we haven't started the timeout yet, start it now, since we're getting data
            timeout=setTimeout(function (e) {
              console.log(process.memory());
              if (str == "")  return;
              console.log(str);
              Serial2.println(str);        // Send
              digitalWrite(LED_GREEN, 0);
              digitalWrite(LED_RED, 0);
              timeout=null; //clear the timeout variable at the end
              str=""; //clear the string
            }, 5000);
        } 
        //==============================================================
      } else {
        digitalWrite(LED_RED, 1);
        console.log("Bad");
      }
      
      Counter++;
    });
    //==================================================================
    setInterval(Thread1,3000);
    Show("Stop!");
    save();
    

    Also, if you want it to work when running from flash, you might have to put the Serial2.on(...); inside an onInit() function. I don't recall whether Espruino will remember callbacks like that on save() or whether you need to set them in onInit().

    (Note: My code is untested, might have typo/etc in it. Edit: just fixed one of them)

    Edit: Even when it's "successful" in the case you've shown above, it looks like it's leaking oodles of memory....

About

Avatar for DrAzzy @DrAzzy started