2

I am trying use a HMI display. If I want to send a data, for example I want to write "TOPWAY" to 0x00000080 address it should like that:

    Serial.write(0xaa); // packet head
    Serial.write(0x42); // VP_N16 string write command
    Serial.write(0x00); // VP_N16 address
    Serial.write(0x00);
    Serial.write(0x00);    
    Serial.write(0x80); 
    Serial.write(0x54); //T
    Serial.write(0x4f); //O
    Serial.write(0x50); //P
    Serial.write(0x57); //W
    Serial.write(0x41); //A
    Serial.write(0x59); //Y
    Serial.write(0x00); //string end with "\0" 
    Serial.write(0xcc); // packet tail
    Serial.write(0x33); // packet tail
    Serial.write(0xc3); // packet tail
    Serial.write(0x3c); // packet tail

I want to make a method like SendString(String abc) to send that like above. I need convert string a hex array and call in Serial.write(array[i]). Can you hep me?

4
  • 2
    There is no such thing as a hex array. Everything is all just numbers. Even letters are numbers. Your string is already an array.
    – Majenko
    Jul 28 at 7:30
  • 2
    Serial.print("TOPWAY"); should do it
    – Juraj
    Jul 28 at 8:18
  • @Juraj you mean like that for total query like above: Serial.write(0xaa); Serial.write(0x42); Serial.write(0x00); Serial.write(0x00); Serial.write(0x00); Serial.write(0x80); Serial.print("TOPWAY"); Serial.write(0x00); //string end with "\0" Serial.write(0xcc); Serial.write(0x33); Serial.write(0xc3); Serial.write(0x3c); . if Not? please show me as answer not in comment.
    – mehmet
    Jul 28 at 14:37
  • the answer is below
    – Juraj
    Jul 28 at 14:44
5

I don't know anything about the particular display, but based on the information provided I hope this is at least shows the foundation of one way you could approach a final solution.

Update:

  • Incorporated great improvements and a fix from Edgar in the comments.
  • String overload.
  • Display baud rate suggested by mehmet
#define DISPLAY_DEVICE Serial
#define DISPLAY_DEVICE_BAUD 9600

static uint8_t displayPktStart[] = {
    0xaa, // packet headchar
    0x42, // VP_N16 string write command
    0x00, // VP_N16 address
    0x00, // ...Fill
    0x00,
    0x80
  };

static uint8_t displayPktEnd[] = {
    0x00, // End of text
    0xcc, // packet tail
    0x33, // packet tail
    0xc3, // packet tail
    0x3c // packet tail
  };


void writeDisplay(const char* text)
{
  // Write packet headers
  DISPLAY_DEVICE.write(displayPktStart, sizeof displayPktStart);

  // Send text
  DISPLAY_DEVICE.print(text);

  // Write packet tail
  DISPLAY_DEVICE.write(displayPktEnd, sizeof displayPktEnd); 
}

void writeDisplay(const String& text)
{
  writeDisplay(text.c_str());
}

void setup()
{
  DISPLAY_DEVICE.begin(DISPLAY_DEVICE_BAUD);
}

void loop() 
{
  String msg = "TOPWAY";
  writeDisplay(msg);

  delay(2000);
  writeDisplay("Peanuts taste like chicken");
  delay(500);
  writeDisplay("");
  delay(1000);
  writeDisplay("No. Seriously");
  delay(2000);
}

6
  • 3
    1. Instead of writing a loop, you can DISPLAY_DEVICE.write(displayPktStart, sizeof displayPktStart);, and likewise for displayPktEnd. 2. You mean DISPLAY_DEVICE.print(text);, not println(). 3. Serial.print() will not send the terminating \0, you may want to add that to displayPktEnd. Jul 28 at 9:39
  • @EdgarBonet Thanks. Updated. Jul 28 at 10:04
  • Thanks A lot. I've added DISPLAY_DEVICE.begin(9600); in setup
    – mehmet
    Jul 28 at 14:51
  • 1
    @mehmet I hope my update matches your expectations. Jul 28 at 15:29
  • @voidPointer well, I don't want to bother you too much, but I have one more question. if I want to use dynamic address, such as "000080" instead of Serial.write(0x00); Serial.write(0x00); Serial.write(0x80); how to add this method, like writeDisplay(msg, "000080");
    – mehmet
    Jul 28 at 19:51

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.