需要知道如何通过 Arduino Uno 上的 SoftwareSerial 发送 HEX 值

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

我正在尝试编写 Android UNO 代码以通过 SoftwareSerial 发送 HEX 值,但是当观察串行监视器时,输出不是我所期望或想要的,我还没有足够的经验来知道是什么下一步。

不知道从哪里来的FFFF能消灭掉

我想要的输出是:
AA130120
AA0400AE

我得到的是:
FFFFFFFFAA1311E
FFFFFFAA20

我的代码:

#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);
}

能否将串口监视器中的波特率设置为 9600Bd,运行这段代码,看看它是否会在串口监视器中输出您期望的字节?

#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);
}