You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • I know that you do not have responses ending with /n. But there is or are some thing(s) equivalent from which you can derive when a response is completely received.

    I took a look at the wiki of the device: https://www.dfrobot.com/wiki/index.php/Voice_Module_SKU:_DFR0534

    The response is always: <0xAA><cmd><noOfResponseBytes><rspByte0><rspByte1>...

    Together with the pre-amble of 0xAA we can detect begins of a new command response in any string (We can safely assume that no response value has AA w/ a valid command as next byte in the response value(s), and to start with, we omit checking for valid command valid response data lengths against lookup tables).

    I can see something like this:

    // --- process response
    function proc(rsp) {
      // response processing code goes here...
    }
    
    // [0] [1] [2] [3]
    // AA  01  01  <statusValueByte> - status response
    var cAA = String.fromCharCode(0xAA); // hex AA char
      , pAA = 0   // pos of AA +1 (+1 to make check easy)
      , dta = "", // received but not yet processed data
      , dL        // dta length
      , rDL       // response's data length
      , rsp       // response
      ;
    s.setup(9600,{rx:B6, tx:B5});
    s.on('data', function(d) {
      dta += d;
      if (    (pAA=dta.indexOf(cAA)+1)  // --> cmd byte if contains 0xAA
           && ((dL=dta.length)>(++pAA)) // --> response length byte
           && (dL>=pAA+1+(rDL=dta.charCodeAt(pAA))) // contains resp
         ) { 
        rsp = dta.substr(pAA-2,3+rDL); // complete response
        dta = dta.substr(pAA+1+rDL);
        proc(rsp);                     // process response
      }
    });
    
About

Avatar for allObjects @allObjects started