-1

Below is my code. I want to send the hex data serially to some device and also expecting a reply. I want to see the reply in hex form on a serial monitor. Any comment will be appreciated. Thanks.

const byte numChars = 88;
word receivedChars[numChars]; // an array to store the received data
boolean newData = false;

void setup() {
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
}

void loop() {
  uint8_t message[] = {0xA5, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x0d };
  Serial.write(message, sizeof(message));
  delay(500);
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  word rc;
  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();
    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    } else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    Serial.print("data ");
    Serial.println(receivedChars);
    newData = false;
  }
}

The error I get is call of overloaded println(word [88]) is ambiguous. Thank you for your help.

  • The print class doesn't know how to print an array of 88 bytes. You need to print each byte individually in turn in a form you can understand (is the reply ASCII? If not you will need to display it as numbers in some form). – Majenko Jan 8 '18 at 20:19
  • 4
    data is not sent in hex, it is sent in bytes. ... hex is only for human readability of bytes. .... 0xA5 and 165 are exactly the same thing – jsotola Jan 8 '18 at 20:38
  • 1
    Replace "word" → "char". – Edgar Bonet Jan 8 '18 at 21:00
  • while (Serial.available() > 0 && newData == false) and Serial.begin(9600); you always received single byte (Eight-bit time delay is mandatory)! – dsgdfg Jan 9 '18 at 6:32
0

Here is your code fixed: if you want to send numbers encoded as hex strings that's exactly what you have to do on both ends. The standard function itoa() is what converts numbers to their string representation, but operating this way you are wasting a lot of resources because to send the number 1 (which can be accomplished by sending exactly one byte) you are sending the string 0x01 (which now requires 4 bytes to be transferred).

const byte numChars = 88;
word receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {   
    uint8_t message[] = {0xA5, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x0d };

    char buf[4];
    for (uint8_t i = 0; i < sizeof(message); i++) {
        itoa(message[i], buf, 16));
        Serial.write(buf, sizeof(buf));
    }
    delay(500);

    recvWithEndMarker();
    showNewData();
}

#define END_MARKER '\n'
void recvWithEndMarker() {
    static byte ndx = 0;
    char rc;

    while (Serial.available() > 0 && !newData) {
        rc = Serial.read();

        if (rc != END_MARKER) {
            receivedChars[ndx++] = rc;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        } else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData) {
        Serial.print("data ");
            for (unsigned int i = 0; i < numChars; i++) {
            Serial.print(receivedChars[i], HEX);
        }
        Serial.println();
        newData = false;
    }
}
  • Thanks for all of you guys, Roberto the code you corrected shows ? in the place of 0x... of every byte is there any solution for it. – Ali khan Jan 9 '18 at 17:29
  • Is that only the 0x part? Are all other parts of the data exchange correct? – Roberto Lo Giacco Jan 10 '18 at 19:57
  • only 0x part and I have also another issue that when I move the volume knob then I don't receive any data serially until it reaches at about 95 percent after the further move I receive data. I want to receive data at every position movement – Ali khan Jan 11 '18 at 18:43
  • What volume knob are you referring to?!? – Roberto Lo Giacco Jan 11 '18 at 19:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.