Frage

Hi I have a leonardo and am trying to communicate with a device. I need to send the following hex code FF 01 AA I tried to store it as an array of bytes and loop through it but it doesn't seem to work. I am also expecting some reply from the device.

int byteReceived;
int byteSend;
int byteSentSuccessfully;
uint8_t byteToSend[3] = {
       0xFF,
  0x01,
  0xAA,

    };
   char buffer[8];
void setup()   
{
  // Start the hardware serial
  Serial1.begin(9600);

}

void  loop()
{
  byteSentSuccessfully = Serial1.write(byteToSend,sizeof(byteToSend));
  if (byteSentSuccessfully)
  {
    Serial.println("Wrote to device");

  }

  if (Serial1.available()){

    byteReceived = Serial1.readBytes( buffer,sizeof(buffer)); 
    if (byteReceived){
      Serial.println("She is talking to me");//check on com port

    }
  }
}//END OF MAIN LOOP

I also tried to send it in a characters, but no luck-

void setup() {
  Serial.begin(9600);
delay(10000);//enough time to start up my com port monitor
  String myStr = "FF0101AAAC";
  char* buffer = (char*) malloc(sizeof(char)*myStr.length()+1);

  Serial.println("Using toCharArray");
  myStr.toCharArray(buffer, myStr.length()+1);

  Serial.println(buffer);

  free(buffer);

  Serial1.write(buffer);
}

void loop() {
 //only need to do this once

}
War es hilfreich?

Lösung

Have you tried

while (!Serial1) { }

after Serial1.begin in the setup loop?

Also, if you are trying to communicate with a device over RS485 what converter are you using?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top