I am using the Arduino Micro and an Xbee S2C module to receive commands from another Xbee connected to my PC through XCTU. When I send a string of text to the Ardunio I would like to print the received data to the Serial Monitor window. I have attached the code I am using and for some reason, it will print the 1st byte and the remainder of the data will be printed on a new line.
For example, if I send the data "TEST#1" to the Arduino from XCTU, "T" will be printed on one line followed by "EST#1" on a separate line.
I have monitored the data using a logic analyzer and all of the data is being sent at the same time. I have used this same exact setup with an Atmega88 and Atmel Studio with no issues so I believe it has something to do with the Arduino Functions.
Any help would be appreciated.
#define GREEN_LED 13
#define GREEN_LED_OUTPUT pinMode(GREEN_LED, OUTPUT)
#define GREEN_LED_ON digitalWrite(GREEN_LED, HIGH) // turn the LED on (HIGH is the voltage level)
#define GREEN_LED_OFF digitalWrite(GREEN_LED, LOW) // turn the LED off by making the voltage LOW
///////Variables//////////////
int totalBytes = 0;
void setup()
{
GREEN_LED_OUTPUT;
GREEN_LED_OFF;
Serial.begin(9600); // Open Serial Monitor Port and set BAUD rate to 9600
Serial1.begin(9600); // Open Serial Port for Xbee and set BAUD rate to 9600
}
void loop()
{
if (Serial1.available() > 0) // Check to see if data is available
{
totalBytes = Serial1.available(); // Find out how much data is available
char message[totalBytes] = {0}; // Local array of char, totalBytes = # of elements, initialize elements to 0
Serial1.readBytes(message, totalBytes); // Fill up message array with serial data
Serial.print("I received: "); // Print caption
Serial.write(message, totalBytes); // Print contents of message
Serial.println(); // Move to next line
message[totalBytes] = {0}; // Reset data in message array
}
}