What difference between Serial.print and saving data into variable?

Hi Programmers,

I am trying to read data from Futaba servo motor to check a current position.
Some Futaba servo provides TTL communication for control servo motor and read data of current status, position, speed, Id, etc..
I am using Arduino UNO and a serial interface board that I made it using 74HC125. And I am using SoftwareSerial library.
You can see the attached my whole code.

It is working to communicate between my mac and servo motor, sending command to control servo motor and reading a status data from servo.
below code is part of read and display on the serial monitor, it is working.

Serial.print(ServoSerial.read(),HEX);
Serial.print(",");

you can see the attached image "Success to read data"

But I have a problem to read data and save it into variable. When I saved data into variable, it seems there are wrong data.

data = (ServoSerial.read(),HEX);
Serial.print(data*); *
Serial.print(","); [/b]
you can see the attached image "Fail to read #1"
And I tried a different way to read using 'string'
char received = (char)(ServoSerial.read(),HEX); //add
inData += received; //add
inData += ",";
Serial.print(inData); //add
you can see the attached image "Fail to read #2"
I wonder what is difference between using Serial.print with serial monitor and saving data into variable or string.
Please advise or comments for me.
Thank you.
FutabaServoReadTest.ino (2.97 KB)



Hi pwoong

It would help to post your full code (use the code tags - the "#" button above the smileys).

But looking at your extracts, try this:

data = ServoSerial.read();
Serial.print(data, HEX);

Regards

Ray

HI Ray,

Thank you for quick comment. I tried your suggestion, but it didn't work, same result. :cold_sweat:

//---------------------------------------------------------------------------
//#include <icrmacros.h>
//#include <NewSoftSerial.h>

#include <SoftwareSerial.h>
SoftwareSerial ServoSerial(2,3);
//NewSoftSerial ServoSerial(2,3);

byte modelnum[]={0xFA, 0xAF, 0x01, 0x0F, 0x00, 0x02, 0x00, 0x0C};
byte currVOLT[]={0xFA, 0xAF, 0x01, 0x0F, 0x34, 0x02, 0x00, 0x38};
byte mem30to41[]={0xFA, 0xAF, 0x01, 0x0B, 0x00, 0x00, 0x01, 0x0B};

  // Servo Restart
 byte ServoReStart[] = {0xFA, 0xAF, 0x0A, 0x20, 0xFF, 0x00, 0x00, 0xDE};
 
 // baudrate 38400
byte baud38400[] = {0xFA, 0xAF, 0x01, 0x00, 0x06, 0x01, 0x01, 0x04, 0x03};
// ROM Write
byte RomWrite[] = {0xFA, 0xAF, 0x01, 0x40, 0xFF, 0x00, 0x00, 0xBE};
// Servo Restart
byte ServoreStart[] = {0xFA, 0xAF, 0x01, 0x20, 0xFF, 0x00, 0x00, 0xDE};

byte readCurrentAngel[] = {0xFA, 0xAF, 0x01, 0x0F, 0x2A, 0x02, 0x00, 0x26}; //Read memory from N0.42 ~ to No.59
byte TrqOn [] = {0xFA, 0xAF, 0x01, 0x00, 0x24, 0x01, 0x01, 0x01, 0x24}; // Torque ON Packet 
byte data[] = {0};
String inData;

void setup()
{
  pinMode(13, OUTPUT);
  ServoSerial.begin(57600);  //It should be less than 57600 to communication
  Serial.begin(57600);
  delay(1000);
 
}

void loop()
{
  int rx, tx;
  
  rx = ServoSerial.available();
  if(rx > 6)
  {
    digitalWrite(13, LOW);
    
    for(int i=0;i<rx;i++)
    {
      //Serial.print(ServoSerial.read(),HEX);
      //Serial.print(",");
      data[i] = (ServoSerial.read(),HEX);  //save serial data to DATA buffer  add
      //char received = (char)(ServoSerial.read(),HEX);   //add
      //Serial.print(received,HEX);  //add
      //Serial.print(",");      //add
      //inData += received;   //add
      //inData += ",";
            
    }
    
    ///*
    for(int i=0;i<rx;i++)   //display on serial monitor  add
    {
      Serial.print(data[i],HEX);  //add
      Serial.print(",");      //add
    } // */
    //Serial.print(inData);  //add
    Serial.print("\n");
    ServoSerial.flush();
  
   
  }
  
   tx = Serial.available();
   if(tx>0) 
   {
      switch(Serial.read()) 
      {
        case 65://"A"
          digitalWrite(13,HIGH);//LED??
          send_msg(modelnum,8);//?????
         
          break;
        case 66://"B"
          digitalWrite(13,HIGH);//LED??
          send_msg(currVOLT,8);//?????
         
          break;
        case 67://"C"
          digitalWrite(13,HIGH);//LED??
          send_msg(readCurrentAngel,8);//?????30??41
          
          break;
        case 68://"D"
          digitalWrite(13,HIGH);//LED??
          send_msg(mem30to41,8);
          
          break;
        default:
          for(int i=0;i<4;i++) 
          {
            digitalWrite(13,HIGH);
            delay(200);
            digitalWrite(13,LOW);
            delay(200);
          }
          break;
      }
      Serial.flush();//????????
    }
 }

void send_msg(byte msg[], int n) {
  ServoSerial.write(msg,n);
   
}

//---------------------------------------------------------------------------

byte data[] = {0};

This creates a 1-element array of byte and initialises that 1 element to 0.

You will need to decide how many bytes you need to store in data and declare it with at least that size. For example:

#define BUFFER_SIZE 32
...
byte data[BUFFER_SIZE] = {0};

And don't forget to change the Serial.read() statements to this:

data[i] = ServoSerial.read();

Thank you.

It is working now!!