使用写文件方法发送(str直接填中文即可)
int KeyboardSerialTalk::ttsSend(QString str)
{
QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
QTextCodec *gbk = QTextCodec::codecForName("GBK");
QByteArray gbk_byte;
qDebug()<<"str:"<fromUnicode(utf8->toUnicode(str.toLatin1())).toHex();
gbk_byte = gbk->fromUnicode(utf8->toUnicode(str.toLatin1()));
int len = gbk_byte.length();
int ret;
char buf[1024] = {0};
buf[0] = 0xFD ; //构造帧头FD
buf[1] = 0x00 ; //构造数据区长度的高字节
buf[2] = len+2; //构造数据区长度的低字节
buf[3] = 0x01 ; //构造命令字:合成播放命令
buf[4] = 0x01; //文本编码格式:01:GBK 03:unioncode
memcpy(buf+5, gbk_byte.data(), len);
buf[len+5]=0x0d;
buf[len+6]=0x0a;//
ret = write(keyboard_fd, buf, len+7);
sleep(1);
return ret;
}
使用ManageSerialPort串口类发送
void MainWindow::ttsSend(QString str)
{
QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
QTextCodec *gbk = QTextCodec::codecForName("GBK");
QByteArray gbk_str = gbk->fromUnicode(utf8->toUnicode(str.toLatin1()));
qDebug()<<"str:"<
QByteArray buf;
buf[0] = 0xFD ; //构造帧头FD
buf[1] = 0x00 ; //构造数据区长度的高字节
buf[2] = gbk_str.length()+2; //构造数据区长度的低字节
buf[3] = 0x01 ; //构造命令字:合成播放命令
buf[4] = 0x01; //文本编码格式:0x01 GBK 0x03 unioncode
buf += gbk_str;
buf[str.length()+5]=0x0d;
buf[str.length()+6]=0x0a;//
m_pSerialPortPark->sendData(buf);
}