Serial Decode with Nick Gammon Example - Please Help - I have read a lot but...

I am an infrequent Arduino user. My project is parsing a stream of (one direction only) serial data (TTL; 2400, 8,N,1 true polarity) that consists of a fixed length packet - 15 bytes plus null coming from a basic stamp device that ultimately I want the Mega to parse and act upon. The transmitted Bytes represent ASCII. I have the device connected to my Mega 2560 RX2 plus a ground and I am using a common DC power source. I confirmed the signal integrity and code with my scope decoding at the Mega RX2 input.

Basic Stamp Device test transmission = : “ABCDEFGHIJ1234!” terminated with “/0”

The code I used came from Nick Gammons page on buffered serial plus some mods (which undoubtedly I messed up)

// how much serial data we expect before a null
const unsigned int MAX_INPUT = 15;

void setup ()
  Serial.begin (9600);
  Serial2.begin (2400);

void loop()
  while (Serial2.available () > 0)
    processIncomingByte ( ());

void processIncomingByte (const byte inByte)
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;
  switch (inByte)
    case '\0':   // end of fixed length packet
      input_line [input_pos] = "/0";  // packet terminating null byte
      input_pos = 0; // reset buffer for next time 

      // keep adding if not full 
      if (input_pos < (MAX_INPUT - 1))
        input_line [input_pos++] = inByte;

    }  // end of switch
  } // end of processIncomingByte

The code does print out 15 bytes but its all wrong. I also tried by changing the sending Basic Stamp serial polarity from true to inverted but the mega serial monitor readout was identical.


I thought this must be some numerical shift because of the ASCII but what no sense is there is an odd duplicate (151, 151) so a numerical shift seems unlikely.

Any help appreciated.


      input_line [input_pos] = "/0";  // packet terminating null byte

That is NOT a NULL. A NULL is a SINGLE character - '\0'.

As you have no doubt observed, anonymous printing sucks. Identify what you are printing, and I'm sure that you'll learn something.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

The second example should work if you change the end marker to '\0'


Thanks for catching the mistake. I had it correct elsewhere. I changed that line it from "/0" to '/0' and got a compiler error:

"warning: overflow in implicit constant conversion [-Woverflow]"

Since i know that when '/0' is reached the position (0-15) is 15, I replaced '/0' with 15 and it compiled but I get the identical values on the serial monitor as before.

Does the serial input to Mega expect an inverted or true polarity?

Thanks for your patience

k1jos: Thanks for catching the mistake. I had it correct elsewhere. I changed that line it from "/0" to '/0' and got a compiler error:

BECAUSE... its not '/0' its '\0'

-jim lee

Thanks, another sloppy typo on my part. Turns out that entire line is not doing anything in my example and was a left over cut and paste from Nick Gammon’s buffer example.

I have isolated the problem to signal polarity. I changed the Basic Stamp to a different output pin which doesn’t have a line driver and the Mega reads the serial input with my code example OK. The Basic Stamp apparently does not allow control of polarity to its dedicated Sout pin which goes to a line driver that inverts. I will next add an open collector inverter between the two and see hwo that works out.

Is there a software method to invert the received data from the Mega RX pin or must it be done with hardware inverter?