0

I am developing code on Arduino to read 9 bytes of data from other controller.

I am getting same arrya elements which is transmitted from controller, but when I convert the same it gives me garbage values, Please help.

Below is my code snippet:

int Test::ID_001(void) {
  int result = 1;  
  int i = 0;
  char FWV_AB[84];
  digitalWrite(DIR485,HIGH);
  delay(100);
  delay(10);

  int res = proto485Write(RS485_ADDRESS, CMD_COMMAND_FW_AB, CMD_ACTION_FW_AB, 0x0000);      // Send msg to controller board

  Serial.print("msgsend: ");
  Serial.println(" ");
  Serial.println("Received message from controller is: "); 

  int  bufferIn1[9] = { int(0x00), int(0x00), int(0x00), int(0x00), int(0x00), int(0x00), int(0x00), int(0x00), int(0x00) };
  digitalWrite(DIR485,LOW);
  delay(100);
  delay(10);

  delay(DELAY);
  if (Serial2.available()== 0) {Serial.print("Error01 "); result = 0;} // Error for response timeout
  while(Serial2.available() > 0)
   {
   bufferIn1[i] = Serial2.read();

   Serial.print(bufferIn1[i],DEC);     // PRINTS CORRECT VALUES HERE SAME AS SENT BY CONTROLLER 
   Serial.print(" ");
   i++;
   if(i == 9){
   Serial.println("Controller board version is: "); 
   for(int j=3;j<7;j++){
    Serial.print(int(bufferIn1[j]-48)); } // HERE I AM CONVERTING VALUES INTO READABLE FORM
   }   
   } 
  digitalWrite(DIR485, HIGH);
  delay(100);
 return result;
}

GETTING VALUES AS "9 49 51 48 48 48 50 48 54" while serial read takes place. MY DATA is "48 48 48 50" i.e. START FROM 3rd POSITION TO 6th POSITION OF RECEIVED BYTE. IT SHOULD PRINT AS "0002" but it gives me "GQQQ" some times "s)))" and such garbage data instead of 0002 value. Kindly guide.

Thanks you

  • @Juraj thank you for reply.... by using Serial.write it gives same results as before. – swanand Apr 5 at 6:47
  • my conclusion is that the output is from a different version of the sketch. sketch doesn't upload? – Juraj Apr 5 at 6:51
  • @Juraj I am able to upload the sketch, can be some issue related to time delay? I am not able to figure it out. As when I read the serial input using "Serial.print(bufferIn1[i],DEC);" line it gives perfect results. but I need to separate the data from that array & convert into readable form. But after first read even if I am trying to read it for second time the same values it gives garbage data. Plese guide – swanand Apr 5 at 7:02
  • the snippet in Question can't print nothing else then numbers. print(int( ) ) will never print G or Q – Juraj Apr 5 at 7:12
  • after writing line as "Serial.print(int(bufferIn1[j]-48));" it should give number as 0002 but it gives some random number like "20187" – swanand Apr 5 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.