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(); 
    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.


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() {
  pinMode(AnalogPin, INPUT);
  for (int i=0; i<100; i++) 
  // Random ID

void loop() {
  char r;
  // Allow data reception while waiting for handshake confirmation
  while (1) { 
      if (XBee.available()) {
        do {
          r = XBee.read(); 
        while (r != -1); 
        if (r == 'Q') {
          Serial.println("Turning LED OFF.");
          digitalWrite(ledPin, LOW);
       else if (r == '1') {
          Serial.println("Turning LED on.");
          digitalWrite(ledPin, HIGH);
} // 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.");

        // Cleanup when termination signals are sent to process
        ON_DEATH(function(signal, err) {
            var death_msg = 'Q';
            console.log("\n\nSending reset signal to nodes.\n\n")
  }); // 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.

  • In passing a char to Serial.println(), this code cannot produce the claimed output. Commented Oct 8, 2016 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
    Commented Oct 8, 2016 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! Commented Oct 8, 2016 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
    Commented Oct 8, 2016 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. Commented Oct 8, 2016 at 8:43

1 Answer 1

 do {
      r = XBee.read(); 
    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(); 

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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