Need to know how to send HEX values via SoftwareSerial on Arduino Uno

Viewed 1396

I’m trying to write Android Uno code to send HEX values via SoftwareSerial but when watching the Serial Monitor the output isn’t what I expected or wanted, and I am not experienced enough yet to know what to do next.

I don’t know where the FFFFs come from to be able to eliminate them.

My desired output is:

<br>
AA130120<br>
AA0400AE

What I get is:

<br>
FFFFFFFFAA1311E<br>
FFFFFFAA20

My code:

#include <SoftwareSerial.h>
// Define the RX and TX pins to establish UART communication with the MP3 Player Module.
#define Pin_RX 0 // to RX
#define Pin_TX 1 // to TX
#define d_sleep 10000

// Define the required MP3 Player Commands:
static byte ccPlay[]   = {0xAA, 0x02, 0x00, 0xAC}; // Play:
static byte ccSetVol[] = {0xAA, 0x13, 0x01, 0x20}; // Set Volume (00-30):  

// Define the Serial MP3 Player Module.
SoftwareSerial MP3(Pin_RX, Pin_TX);

void setup() {
  // Initiate the serial monitor.
  Serial.begin(9600);
}

void loop() { // Play a song.
  ccSetVol[3] = {0x1E};
  send_command_to_MP3_player(ccSetVol, 4);
  delay(d_sleep);
  send_command_to_MP3_player(ccPlay, 4);
  delay(d_sleep);
}

void send_command_to_MP3_player(int8_t command[], int len){
  //Serial.print("\nMP3 Command => ");
  for (int i=0; i<len; i++) {
    MP3.write(command[i]);
    Serial.print(command[i], HEX); 
  }
  delay(100);
}
1 Answers

Could you set the baud rate in the serial monitor to 9600Bd, run this code, and see if it spits out the bytes you expect in the serial monitor?

#define d_sleep 10000

static byte ccPlay[]   = {0xAA, 0x02, 0x00, 0xAC};
static byte ccSetVol[] = {0xAA, 0x13, 0x01, 0x20};

void send_command_to_MP3_player(byte command[], int len) {
  for (int i=0; i<len; i++) {
    //Serial.println(command[i], HEX);
    Serial.print(command[i], HEX);
  }
  //Serial.println();
  delay(100);
}

void setup() {
  Serial.begin(9600);
  delay(1500);
}

void loop() {
  ccSetVol[3] = 0x1E;
  send_command_to_MP3_player(ccSetVol, 4);
  delay(d_sleep);
  send_command_to_MP3_player(ccPlay, 4);
  delay(d_sleep);
}
Related