Send commands via RS232 shield

Hello,
I understand this topic has been covered in many different situations, but I cannot find the right case (or at least I haven't so far). I really need a clue on this.

I have the RS232 shield ( this one ) connected to arduino UNO and I am trying to send simple commands to a player via RS232 cable.
After several tries and many copy/paste from this forum I came to this situation:

#include <SoftwareSerial.h>
SoftwareSerial mySerial (2, 3);
byte Stop[] = {0x86, 0x00, 0x00, 0x00, 0x00, 0x52};

void sendCommand(const char *cmd){
  for(int i = 0; i <sizeof(cmd); i++){
     mySerial.write(cmd[i]);
  }
}

void setup(){
   mySerial.begin(9600);
   Serial.begin(9600);      
}

void loop() {
        sendCommand(Stop);  
        Serial.println(mySerial.read());  
}

I only get a sequence of numbers from the serial monitor but nothing happens on the player
I am sure the HEX sequence of the command is correct (the format from the manual is attached) but still I don't get any result.
What's my mistake?

Thank you all so much for any suggestion

Antonio

cmd is a pointer; on an Uno, the size of a pointer is always 2.

Your function needs an additional argument as shown below. And you need to pass the size of the array as an argument when calling the function.

void sendCommand(const char *cmd, int len){

And use len instead of the sizeof inside the function.

Thank You sir!
Actually your answer saved me. Since I got no errors I supposed the method was correct. To keep it simple I skipped the external function and it worked.
The code to send the array of hex with the mentioned shield is

#include <SoftwareSerial.h>
#define rx 2 //define what pin rx is going to be
#define tx 3 //define what pin tx is going to be
SoftwareSerial mySerial(rx, tx); //232_TX,232_RX

byte Play[] = {0x86, 0x00, 0x00, 0x02, 0x00, 0x44};
byte Stop[] = {0x86, 0x00, 0x00, 0x00, 0x00, 0x52};


void setup() {
      mySerial.begin(9600);
      Serial.begin(9600);
}



void loop() {
  for (int i = 0; i < 255; i++) {
    mySerial.write(Play, 6);
  }        
  delay(2000);
  for (int i = 0; i < 255; i++) {
    mySerial.write(Stop, 6);
  }
  delay(2000);
}

Thank you again sterretje. Solved.

Antonio

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