-1

I'm writing the character 1 from a node.js XBee using the code sp.write("1"); to my Arduino's XBee. Then on my Arduino's XBee I read it like this (inside a loop):

  if (XBee.available()) {
    do {
      r = XBee.read(); 
      Serial.print(r);
    } 
    while (r != -1);

And the input char r shows up as 1� instead of just 1. Why is this and how can I remove the added invalid character from the char? Also is this char now 2 chars, how does this work? Thanks.

EDIT:

Arduino code:

#include <SoftwareSerial.h>

// Global variables
int AnalogPin = A2; // has much less jumpy readings than A0 (maybe A0 slightly damage from overuse)
int ledPin = 13;
SoftwareSerial XBee(2,3); // RX, TX

// Setup
void setup() {
  Serial.begin(9600);
  pinMode(AnalogPin, INPUT);
  XBee.begin(9600);
  for (int i=0; i<100; i++) 
    Serial.print("\n"); 
  // Random ID
  Serial.println("Listening...");
}

// MAIN LOOP
void loop() {
  char r;
  // Allow data reception while waiting for handshake confirmation
  while (1) { 
      if (XBee.available()) {
        do {
          r = XBee.read(); 
          Serial.println(r);
        } 
        while (r != -1); 
      }
      delay(500);
        if (r == 'Q') {
          Serial.println(r);
          Serial.println();
          Serial.println("Turning LED OFF.");
          digitalWrite(ledPin, LOW);
          Serial.println();
          exit(1);
        }
       else if (r == '1') {
          Serial.println(r);
          Serial.println();
          Serial.println("Turning LED on.");
          digitalWrite(ledPin, HIGH);
          Serial.println();
        }     
  }
} // end main loop

Node code:

var SerialPort = require('serialport');
var express = require('express');
app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var ON_DEATH = require('death');

    /* ------------- Connecting to Serial Port ---------------- */
    var portName = '/dev/cu.usbserial-AD01SSII',
    portConfig = {
        baudRate: 9600,
    };

    var sp;
    sp = new SerialPort(portName, portConfig);
    sp.on("open", function (err) {

        console.log("Sending LED on signal.");
        console.log("Ctrl+C: quit and send LED off signal.");
        sp.write('1');

        // Cleanup when termination signals are sent to process
        ON_DEATH(function(signal, err) {
            var death_msg = 'Q';
            sp.write(death_msg);
            sp.close();   
            console.log("\n\nSending reset signal to nodes.\n\n")
            process.exit();
        })
*/
  }); // end open

All I'm really trying to do here is turn an LED on and off when I start or end the node program. Maybe I'm just too exhausted to think straight.

7
  • In passing a char to Serial.println(), this code cannot produce the claimed output. Oct 8 '16 at 8:38
  • What happens now is it produces this: 1 � (new line in between) So that the last r value is the invalid char instead of the 1 that I want it to be.
    – Austin
    Oct 8 '16 at 8:39
  • Maybe with what was originally there using Serial.print, but not with the code currently in your question using Serial.println it doesn't! Oct 8 '16 at 8:40
  • It really does.. I'm running it right now. Actually, I forgot to remove the comment ending thing. But besides that it's the same.
    – Austin
    Oct 8 '16 at 8:40
  • It produces it with a newline, which is not what your questions says. Your question must be accurate, literally at character level, if you want help. Oct 8 '16 at 8:43
4
 do {
      r = XBee.read(); 
      Serial.print(r);
    } 
    while (r != -1);

Think about it. Since you tested for XBee.available() you will have at least one iteration of this loop. And then you do a second read, and print it, before finding that the second character is -1.

You need to re-order your loop, or your tests. The second character will be what -1 looks like on your monitor.


A better loop would be:

  while (XBee.available()) {
      r = XBee.read(); 
      Serial.println(r);
  }

This is the standard way of looping and reading. You won't get -1 from the read here, because if something is available, you will get that something.

3
  • I think the -1 means nothing is coming in?
    – Austin
    Oct 8 '16 at 8:46
  • K i just removed all of the while loop and it works a bit. will keep playing with it.
    – Austin
    Oct 8 '16 at 8:49
  • That's right: -1 means no input.
    – Nick Gammon
    Oct 8 '16 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.