Where is this "U" coming from? ( 1v81.664 )

Posted on
  • This code was doing something mysterious:

    
    var AzzyRF={};
    
    AzzyRF.Serial=Serial2;
    
    Serial2.setup(9600, { rx: A3, tx : A2 });
    
    AzzyRF.onData = function(data) {
    	if (data=="#" && AzzyRF.datastring) {
    		if (AzzyRF.datastring) {
    			AzzyRF.Serial.print(AzzyRF.datastring);
    			AzzyRF.datastring="";
    		}
    	} else if (data==">" && AzzyRF.datastring) {
    		//TODO
    	} else if (data=="\n" || data=="\r"){
    		if (AzzyRF.inString!="") {
    			AzzyRF.outputFormat(AzzyRF.inString);
    			AzzyRF.inString="";
    		}
    	} else {
    		AzzyRF.inString+=data;
          if (AzzyRF.timeout > 0) {
            clearTimeout(AzzyRF.timeout);
    	}
          AzzyRF.timeout=setTimeout("AzzyRF.output­Format(AzzyRF.inString);AzzyRF.inString=­'';AzzyRF.timeout=0;",1000);
        }
    };
    AzzyRF.timeout=0;
    AzzyRF.receive_timeout=0;
    AzzyRF.inString="";
    AzzyRF.Serial.on('data',AzzyRF.onData);
    
    AzzyRF.writeA24 = function(addr,data) {
    	if (data.length > 16) {
    		throw "Data too long";
    	} else {
          var tstr="";
    		//tstr=E.toString([(addr>>8)&255,addr&25­5,data.length]);
            tstr+=String.fromCharCode((addr>>8)&255,­addr&255,data.length);
    		//tstr+=E.toString(data);
            tstr+=data;
    		AzzyRF.datastring=tstr;
    		AzzyRF.Serial.print("AT+24WL\r");
    	}
    };
    
    AzzyRF.outputFormat = function(text) {
      if (text=="") return;
      console.log(text);
      var outstr="";
      if (text.charAt(0)=='+' || text.charAt(0)=='=') {
    	  var len=text.length;
    	  for (var i=1;i<len;i+=2) {
    	    //console.log(i);
    	    //console.log(text.substr(i,2));
    	    var tnum=parseInt(text.substr(i,2),16);
    	    if (!isNaN(tnum)) {
    	      outstr+=String.fromCharCode(tnum);
    	    }
    	  }
    	  if (outstr!="") {
    	  	//console.log(outstr);
    	  	console.log((text.charAt(0)=='+'?"receiv­ed":"output:"));
    	  	console.log(E.toUint8Array(outstr));
    		}
    	}
      if (outstr!="") { 
    //bug is here - obviously, outstr should always be "" at this point, 
    //since if the first character of 'text' isn't + or =, I don't do anything 
    //with it, and outstr was just set to ''
      //console.log(outstr);
        console.log("other message: "+outstr);
    
      }
    };
    
    

    (the bug is that at the bottom, it should be checking and printing text, since it hasn't put anything into outstr at that point, so it should always be "" )...

    BUT IT'S NOT!

    Serial input of:

    +00550405

    Would yield:

    
    +00550405
    received
    new Uint8Array([0, 85, 4, 5])
    other message: U
    
    

    Am I just daft and missing something? Where is that capitol U coming from? How could outstr EVER not be an empty string at the point where we check it and decide to print the 'other message' bit?!

    Observed with 1v81.664

    Oops - wrong section. Should be in Javascript or General

  • Not sure I get you... so outstr is new Uint8Array([0, 85, 4, 5]), and you see U which is the 85, but the other characters are control characters that don't get displayed.

    Did you intend there to be an else before if (outstr!="") {?

  • Ahh! Yes, I thought I had an else there, ave that's why nothing made sense

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Where is this "U" coming from? ( 1v81.664 )

Posted by Avatar for DrAzzy @DrAzzy

Actions