SoftwareSerial Transmit Problem

I am trying to control multiple DF players independently with help of <softwareserial.h> library from 1 Arduino.
In my code even though I succeed to play and pause only the first DF player with sending 'A' and 'B' commands from Serial Monitor, when I try to send play command to second DF player both players are start to playing. So I think ...Serial.end() comment is useless. I also try to use SoftwareSerial: listen() command ,but because I transmit data, not receiving ( It says that command for receiving from selected port) I failed again. I am open for any solution. Thanks...

#include "SoftwareSerial.h"
SoftwareSerial mySerial(12, 13);
SoftwareSerial mySerial2(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]

# define ACTIVATED LOW


String incomingStr = "";

void setup () {

  Serial.begin(9600);

}

void loop () {

  while ( Serial.available( ) ) {

    incomingStr = Serial.readString();


    if (incomingStr.charAt(0) == 'A')
    {

      mySerial.begin(9600);
      play();
      delay(1000);
      mySerial.end();

    }

    if (incomingStr.charAt(0) == 'B')
    {

      mySerial.begin(9600);
      pause();
      delay(1000);
      mySerial.end();

    }

    if (incomingStr.charAt(0) == 'C')
    {

      mySerial2.begin(9600);
      play();
      delay(1000);
      mySerial2.end();

    }
    if (incomingStr.charAt(0) == 'D')
    {
      mySerial2.begin(9600);
      pause();
      delay(1000);
      mySerial2.end();

    }
  }
}

void pause()
{
  execute_CMD(0x0E, 0, 0);
  delay(500);
}

void play()
{
  execute_CMD(0x0D, 0, 1);
  delay(500);
}

void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
  // Calculate the checksum (2 bytes)
  word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
  // Build the command line
  byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
                            Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte
                          };
  //Send the command line to the module
  for (byte k = 0; k < 10; k++)
  {
    mySerial.write( Command_line[k]);
    mySerial2.write( Command_line[k]);
  }
}

Your design is strange. Why create and destroy SoftwareSerial objects, when you can always decide in program logic, which object you use, and what you do/don't send to it?

Don't try to turn it off and on with begin and end methods, and continue to send data to it.

1 Like

Ideally you would go for a MEGA because you can use real hardware serial
The modules do speak back to you (binary protocol) but only the last Software Serial port that you began is listening.

try something like this

#include "SoftwareSerial.h"
SoftwareSerial playerOne(10, 11);
SoftwareSerial playerTwo(12, 13);

void setup () {
  Serial.begin(115200);  // 9600 is sooooo 20th century
  playerOne.begin(9600);
  playerTwo.begin(9600); // only this one will be listening
}

void loop () {
  switch (Serial.read()) {
    case 'A': play(playerOne);  break;
    case 'B': pause(playerOne); break;
    case 'C': play(playerTwo);  break;
    case 'D': pause(playerTwo); break;
    default: Serial.println(F("unknown command"));
  }
}

void pause(Stream& s) {
  execute_CMD(s, 0x0E, 0, 0);
}

void play(Stream& s) {
  execute_CMD(s, 0x0D, 0, 1);
}

// binary interface to the module

# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00

void execute_CMD(Stream& s, byte CMD, byte Par1, byte Par2) {
  word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
  byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
                            Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte
                          };
  s.write(Command_line, sizeof Command_line);
  delay(500);
}

fully untested, typed here.

Make sure your Serial monitor is opened at 115200 bauds.

1 Like

Thanks for suggestion !

It works. Thank you . Actually I need more than two so even MEGA is not enough. Thanks again it helped a lot...

good to hear ! have fun

1 Like

The Mega has 4 hardware UARTS
How many do you need ?

Ten would be enough

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.