|
楼主 |
发表于 2019-10-6 20:14
|
显示全部楼层
试试这个哈
[mw_shl_code=arduino,true]//要输出的字符串
char buffer[]="宇音天下";
//实际汉字长度
#define BSIZE (sizeof(buffer)/3)
//存放转化后的汉字 Unicode值
char character[BSIZE * 2];
//根据字符串计算计算出来的送到串口的值
char output[BSIZE*2+6];
void setup() {
Serial1.begin(9600);
Serial.begin(9600);
delay(5000);
}
void loop() {
//首先输出一次Arduino 原始字符串 UTF8 的值
for (int i =0;i<BSIZE*3;i++) {
Serial.print(buffer&0xFF,HEX);
Serial.print(' ');
}
Serial.println(' ');
//将 UTF8 转化为 Unicode
for (int i =0;i<BSIZE;i=i+1) {
character[i*2]=((buffer[i*3]&0xF)<<4)+((buffer[i*3+1]>>2)&0xF);
character[i*2+1]=((buffer[i*3+1]&0x3)<<6)+(buffer[i*3+2]&0x3F);
Serial.print(character[i*2]&0xFF,HEX);
Serial.print(' ');
Serial.print(character[i*2+1]&0xFF,HEX);
Serial.print(' ');
}
Serial.println("");
output[0]=0xFD;
output[1]=(BSIZE*2+3)>>8;
output[2]=((BSIZE*2+3)&0xFF);
output[3]=0x01;
output[4]=0x03;
//把字符串定义搬过去
for (int i=0;i<BSIZE*2;i++) {
output[i+5]=character;
}
//计算一个校验和
output[BSIZE*2+5]=output[0];
for (int i=1;i<BSIZE*2+5;i++) {
output[BSIZE*2+5]=output[BSIZE*2+5] ^ output;
}
for (int i =0;i<BSIZE*2+6;i++) {
Serial.print(output&0xFF,HEX);
Serial.print(' ');
Serial1.write(output);
}
Serial.println(' ');
delay(15000);
}
[/mw_shl_code] |
|