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?
1 Answer 1
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);
}
-
31. Instead of writing a loop, you can
DISPLAY_DEVICE.write(displayPktStart, sizeof displayPktStart);
, and likewise fordisplayPktEnd
. 2. You meanDISPLAY_DEVICE.print(text);
, notprintln()
. 3.Serial.print()
will not send the terminating0円
, you may want to add that todisplayPktEnd
.Edgar Bonet– Edgar Bonet2021年07月28日 09:39:59 +00:00Commented Jul 28, 2021 at 9:39 -
@EdgarBonet Thanks. Updated.voidPointer– voidPointer2021年07月28日 10:04:15 +00:00Commented Jul 28, 2021 at 10:04
-
Thanks A lot. I've added DISPLAY_DEVICE.begin(9600); in setupmehmet– mehmet2021年07月28日 14:51:51 +00:00Commented Jul 28, 2021 at 14:51
-
1@mehmet I hope my update matches your expectations.voidPointer– voidPointer2021年07月28日 15:29:08 +00:00Commented Jul 28, 2021 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– mehmet2021年07月28日 19:51:05 +00:00Commented Jul 28, 2021 at 19:51
Explore related questions
See similar questions with these tags.
Serial.print("TOPWAY");
should do it