最近在项目中使用TTS
串口的SYN6288
语音合成模块来输出中文语音,遇到一些问题,尤其是协议中的校验和计算的问题,折腾了好长时间。
使用的芯片如下图所示:
首先在电脑的串口上调试通过,串口的设置参数如下图所示:
上图中主要需要注意的就是 波特率,数据位,停止位,控制这几个地方。
对于使用Python
脚本调试的情况,可以参考如下的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import serial import struct PORT = "/dev/tty.SLAB_USBtoUART" # configure the serial connections (the parameters differs on the device you are connecting to) ser = serial.Serial( port=PORT, baudrate=9600, xonxoff=False, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS ) print ser ser.isOpen() s=struct.pack("5B32sB", 253,0,35,1,0,'\xBB\xB6\xD3\xAD\xB9\xE2\xC1\xD9\xB0\xB2\xC8\xBB\xD6\xAE\xBC\xD2\xD6\xC7\xC4\xDC\xB5\xE7\xD7\xD3\xB9\xA4\xD7\xF7\xCA\xD2\x0D\x0A',241) ser.write(bytearray(s)) #ser.write(b'\xFD\x00\x21\x01\x01\xBB\xB6\xD3\xAD\xB9\xE2\xC1\xD9\xB0\xB2\xC8\xBB\xD6\xAE\xBC\xD2\xD6\xC7\xC4\xDC\xB5\xE7\xD7\xD3\xB9\xA4\xD7\xF7\xCA\xD2\xF1') print ser.read() |
对于使用C
开发的情况,参考如下的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#define VOICE_UAER_BAND_RATE (9600u) #define VOICE_CMD_PREFIX (0xFD) #define VOICE_CMD_PLAY (0x01) #define VOICE_BGM (0x01) typedef struct __attribute__((__packed__)) _VOICE_CMD { uint8_t cmd_prefix; /*fixed head*/ uint16_t len; /* message length*/ uint8_t ctrl_cmd; /* control command*/ uint8_t bgm; /*background music*/ char msg[0]; /*message content*/ uint8_t xor_checksum; /*checksum*/ }VOICE_CMD; typedef struct _VOICE_CMD* PVOICE_CMD; static char VOICE_BUFFER[sizeof(VOICE_CMD) + SCIF_UART_TX_FIFO_MAX_COUNT]; const static char begin_work[4] = {0xBF,0xAA,0xCA,0xBC }; //const static char testBuf[36] = {0xFD,0x00,0x21,0x01,0x01,0xBB,0xB6,0xD3,0xAD,0xB9,0xE2,0xC1,0xD9,0xB0,0xB2,0xC8,0xBB,0xD6,0xAE,0xBC,0xD2,0xD6,0xC7,0xC4,0xDC,0xB5,0xE7,0xD7,0xD3,0xB9,0xA4,0xD7,0xF7,0xCA,0xD2,0xF1}; //const static char contentBuf[30] = {0xBB,0xB6,0xD3,0xAD,0xB9,0xE2,0xC1,0xD9,0xB0,0xB2,0xC8,0xBB,0xD6,0xAE,0xBC,0xD2,0xD6,0xC7,0xC4,0xDC,0xB5,0xE7,0xD7,0xD3,0xB9,0xA4,0xD7,0xF7,0xCA,0xD2}; #define sw16(x) \ ((short)( \ (((short)(x) & (short)0x00ffU) << 8 ) | \ (((short)(x) & (short)0xff00U) >> 8 ) )) static PVOICE_CMD genVoiceCmd(char* buffer,uint8_t len) { PVOICE_CMD cmd = (PVOICE_CMD)VOICE_BUFFER; uint8_t i = 0; char* xor_buf = (char*)cmd; uint8_t xor = 0; char* xor_sum = (char*)(cmd->msg) + len; memset(xor_buf,0,sizeof(VOICE_CMD)); cmd->cmd_prefix = VOICE_CMD_PREFIX; cmd->len = len + sizeof(VOICE_CMD) - sizeof(uint8_t) - sizeof(uint16_t); /*real_length - prefix - length*/ cmd->len = sw16(cmd->len); cmd->ctrl_cmd = VOICE_CMD_PLAY; cmd->bgm = VOICE_BGM ; memcpy(cmd->msg,buffer,len); for (i = 0;i< (len + sizeof(VOICE_CMD)) -1;i++){ xor ^= xor_buf[i]; } *xor_sum = xor; // memcpy(cmd,testBuf,sizeof(testBuf)); return cmd; } static uint8_t readVoiceCmdLen(PVOICE_CMD voice_cmd) { return sw16(voice_cmd->len) + sizeof(uint8_t) + sizeof(uint16_t); //return sizeof(testBuf); } |
下面是淘宝上的卖家提供的模块资料,可以在本站下载。