What is the serial monitor actually doing?

Alright, so my program looks like this now:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

uint8_t autobaud[]={0x55}; //command 1 autobaud
uint8_t arr[]={0x42, 0x10, 0xCC}; //command 2 change background to color 0x10CC
uint8_t new_arr[]={0x00,0x00,0x00,0x00,0x00,0xFF}; // command 3 draw a circle at x= 0x50 y=0x3F radius=0x22 color=0x001F

void setup()
{
  delay(1000);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9600);
  delay(1000);
      dostuff(autobaud, sizeof(autobaud));
      dostuff(arr, sizeof(arr));
      delay(2000);
      dostuff(new_arr, sizeof(new_arr));
}

void loop()
{
  serial_response();
}

void dostuff(uint8_t *buffer, int len)
{
  mySerial.write(buffer, len);  // Serial.write wants the array pointer and the number of elements
 // Serial.print("Command: ");
 // Serial.write(buffer, len);
 // Serial.println("|");
  serial_response();
}

void  serial_response(){
  byte incomingByte=0;
    while(mySerial.available() <= 0); //Do nothing until there are bytes to be read. 
   //    Serial.print("Response: ");
incomingByte=mySerial.read();
  if(incomingByte==0x06){
   Serial.print(incomingByte, HEX); //when there are bytes, print them to the monitor. 
   digitalWrite(13, HIGH);
   delay(500);
   digitalWrite(13, LOW);
   delay(500);
 }
   else if(incomingByte==0x15){
   Serial.println("NAK");
 }
   
   else{
   Serial.println(incomingByte, HEX);
   }
   
 }

I have intentionally put in command #3 improperly, so I could make sure I was getting 0x15, or negative acknowledgement of a bad command. The screen is doing exactly what it is supposed to, and this is the output from the monitor:

6655
FE
NAK
AB
FE

66 is confirmation of command 1 and 2.
NAK is confirmation of a bad command, command 3.
I don't know what the other junk is. Is it normal for devices like this to spit out junk data like that?

Hmmm, I changed this:
while(mySerial.available() == 0);

to this:
while(mySerial.available() <= 0);

That change had no affect at all, since the available() function returns the number of bytes in the buffer, and there can not be fewer than 0 bytes in the buffer.

I don't know what the other junk is. Is it normal for devices like this to spit out junk data like that?

Normally, devices do not spit out random junk. The data means something, even if that is not apparent to you. Try printing a space after each value, so you can see where the values break. 6655 could be 6, 6, 5, 5 or 66, 5, 5, or 6, 65, 5, or 665, 5, or a number of other combinations.

PaulS:

Hmmm, I changed this:
while(mySerial.available() == 0);

to this:
while(mySerial.available() <= 0);

That change had no affect at all, since the available() function returns the number of bytes in the buffer, and there can not be fewer than 0 bytes in the buffer.

yep, you're right. Since I was unplugging the device each time to program it, i had to plug it in at the right time in order for the display to process the data correctly. Needless to say, that produced some randomness in the 'results'. Once I determined that the device was operating with some consistency, I was able to write a few more things, so now I don't have to unplug it, and there is much less randomness in the display output :smiley:

PaulS:

I don't know what the other junk is. Is it normal for devices like this to spit out junk data like that?

Normally, devices do not spit out random junk. The data means something, even if that is not apparent to you. Try printing a space after each value, so you can see where the values break. 6655 could be 6, 6, 5, 5 or 66, 5, 5, or 6, 65, 5, or 665, 5, or a number of other combinations.

I know you saw this, but for anyone else who is watching, this discussion is becoming more and more about the logic and composition of the code. As such, I started a new thread here: http://arduino.cc/forum/index.php/topic,106427.0.html