本帖最后由 w6850916 于 2014-6-5 16:42 编辑
有个问题困扰了我两天了,问题是这样的,在STM32上移植了ucosii,让后用串口控制GSM模块,移植正常控制正常,也能接收发送短信,后来我想用消息队列来处理接收的短信,于是用了消息队列,现在遇到的问题是,在接收到第一条短信后压入消息堆栈,任务从消息堆栈里取出来是对的,但是从第二条消息开始,消息的第一个字节就出错了,我试了我能想到的各种办法都没有解决,前来求高人指点,或者来讨论下,真没思路了。相关代码如下:
typedef enum {
TYPE_NONE = 0,
TYPE_SMS_DATA,
TYPE_RING,
TYPE_RESET,
TYPE_SEND_AT,
TYPE_SEND_SMS,
TYPE_SMS_COMING,
} GsmTaskMessageType;
typedef struct {
GsmTaskMessageType type;
void (*handlerFunc)(GsmTaskMessage *);
} MessageHandlerMap;
static const MessageHandlerMap __messageHandlerMaps[] = {
{ TYPE_SMS_DATA, __handleSMS },
{ TYPE_SMS_COMING, __handleReadyAcceptSMS},
{ TYPE_RING, __handleRING },
{ TYPE_NONE, NULL },
};
static __inline void __gmsReceiveSMSData(unsigned char data) {
if (data == 0x0A) {
buffer[bufferIndex++] = 0;
if(strncmp("+CMGR:", buffer, 6) == 0){
isSMS = 2;
} else if(isSMS == 2) {
GSMAutoReportMap fix ={NULL,TYPE_SMS_DATA};
GsmTaskMessage *message = __gsmCreateMessage(p->type, buffer, bufferIndex);
printf("bufferIndex %d\n",bufferIndex);
if (OS_NO_ERR != OSQPost(__Queue, message)) {
OSMemPut(SMSCommentBuf, message);
printf("E\n");
}
isSMS = 0;
}
bufferIndex = 0;
} else if (data != 0x0D) {
buffer[bufferIndex++] = data;
}
}
void USART2_IRQHandler(void) {
unsigned char data;
if (USART_GetITStatus(USART2, USART_IT_RXNE) == RESET) {
return;
}
data = USART_ReceiveData(USART2);
USART_SendData(USART1, data);
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
if (isSMS) {
__gmsReceiveSMSData(data);
return;
}
if (data == 0x0A) {
buffer[bufferIndex++] = 0;
if (bufferIndex >= 2) {
const GSMAutoReportMap *p;
for (p = __gsmAutoReportMaps; p->prefix != NULL; ++p) {
if (strncmp(p->prefix, buffer, strlen(p->prefix)) == 0) {
GsmTaskMessage *message = __gsmCreateMessage(p->type, buffer, bufferIndex);
if (OS_NO_ERR != OSQPost(__Queue, message)) {
OSMemPut(SMSCommentBuf, message);
}
break;
}
}
if (p->prefix == NULL) {
printf("Index=%d\n",bufferIndex);
ATCommandGotLineFromIsr(buffer, bufferIndex);
}
}
bufferIndex = 0;
} else if (data != 0x0D) {
buffer[bufferIndex++] = data;
}
}
}
GsmTaskMessage *__gsmCreateMessage(unsigned int type, const char *dat, int len) {
GsmTaskMessage *message = OSMemGet(SMSCommentBuf, &err);
if (message != NULL) {
message->type = type;
message->length = len;
memcpy(&message[1], dat, len);
}
return message;
}
void App_TaskGu900d(void *p_arg) { //GU900D调试进程
char * alarmparameter;
(void)p_arg;
ATCommandRuntimeInit();
__gsmInitHardware();
__SmsRuntimeInit();
while(1){
printf("\r\n GU900 OPEN \r\n");
if(Gu900dInit() == TRUE) break;
Gu900dOff();
OSTimeDlyHMSM(0,0,10,0);
}
OSTimeDlyHMSM(0,0,2,0);
while(1){
printf("\r\n CHECK SIMCARD \r\n");
if(CheckSIM() == TRUE) break;
OSTimeDlyHMSM(0,0,5,0);
}
for(;;) {
GsmTaskMessage *message;
printf("Gsm: loop again\n");
message = OSQPend(__Queue, OS_TICKS_PER_SEC*60 , &err);
if(NULL != message){
const MessageHandlerMap *map = __messageHandlerMaps;
printf("type-> %x \n",message->type);
for (; map->type != TYPE_NONE; ++map) {
if (message->type == map->type) {
printf("useful information \n");
map->handlerFunc(message);
break;
}
}
__gsmDestroyMessage(message);
}
}
}
标注蓝色的部分发送的消息到消息队列,任务读取每次都正确,但是红色部分只能是第一次正确,第二条开始就出错了,但都是第一个字节出错,有知道错在哪里的不?搞死我了都。
|