1

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?

mehmet
  • 297
  • 2
  • 19

1 Answers1

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); }

voidPointer
  • 305
  • 1
  • 6