通过车载终端采集GPS数据
源代码在线查看: message.c
/*****************************************************
*调度消息读写文件
******************************************************/
#include "global.h"
#include "message.h"
#include "24c512.h"
#include "string.h"
#include "struct_def.h"
#include "rcv_process.h"
#include "trans_process.h"
#include "common_function.h"
#include "centerinterface.h"
#include "commoninterface.h"
#include "menuProcess.h"
#define MESSAGE_BASE_ADDRESS 0x400
#define MESSAGE_MAX_LENGTH 256
MESSAGEINFO messageInfo[3];
char messageBuffer[MESSAGE_MAX_LENGTH];
/*0x11:该消息未读;0x22:该消息已读;其他:消息无效*/
void record_init(char block)
{
unsigned char i;
unsigned int address;
unsigned char value[3];
MESSAGEINFO *msgPtr;
if( block==1)
{
msgPtr = &messageInfo[0];
msgPtr->baseAddress = MESSAGE_BASE_ADDRESS;
}
else if( block == 2 )
{
msgPtr = &messageInfo[1];
msgPtr->baseAddress = MESSAGE_BASE_ADDRESS + 0x1400;
}
else if( block == 3 )
{
msgPtr = &messageInfo[2];
msgPtr->baseAddress = MESSAGE_BASE_ADDRESS + 0x1400 * 2;
}
else
return;
/*告警记录初始化*/
msgPtr->entrys = 0;
msgPtr->id = 0;
msgPtr->noRead = 0;
msgPtr->current = 0;
for(i=0; i {
address = i * MESSAGE_MAX_LENGTH + msgPtr->baseAddress;
c512_config_read(address, value, 2);
if( (value[1]==0x11) || (value[1]==0x22) )
{
msgPtr->entrys ++;
if( i==0 )
{
msgPtr->id = value[0];
}
else
{
if( msgPtr->id != value[0] )
{
msgPtr->current = i;
msgPtr->entrys = MESSAGE_RECORD_MAX;
break;
}
}
msgPtr->current ++;
msgPtr->id ++;
}
else
{
msgPtr->current = i;
break;
}
}
for(i=0; i {
address = i * MESSAGE_MAX_LENGTH + msgPtr->baseAddress;
c512_config_read(address, value, 2);
if( value[1]==0x11 )
msgPtr->noRead ++;
}
msgPtr->current = msgPtr->current % MESSAGE_RECORD_MAX;
}
/*index 1-20*/
void message_read(char block, unsigned char index, MESSAGE *ptr)
{
unsigned int address;
unsigned char tmp;
MESSAGEINFO *msgPtr;
if( block==1)
{
msgPtr = &messageInfo[0];
}
else if( block == 2 )
{
msgPtr = &messageInfo[1];
}
else if( block == 3 )
{
msgPtr = &messageInfo[2];
}
else
return;
tmp = index;
if( (tmp > MESSAGE_RECORD_MAX) || (tmp == 0) )
{
ptr->state = 0; /*index值超过最大记录数*/
return;
}
if( index > msgPtr->current )
{
tmp = MESSAGE_RECORD_MAX + msgPtr->current - index;
}
else
{
tmp = msgPtr->current - index;
}
address = 1+ tmp * MESSAGE_MAX_LENGTH + msgPtr->baseAddress;
c512_config_read(address, (char *)ptr, sizeof(MESSAGE) );
tmp = ptr->state;
if( tmp == 0x11 )
{
ptr->state = 0x1;
if( (block==2) || (block == 3) )
{
make_ack_message( ((char*)ptr)+21 );
set_message_readFlag(block, index);
}
}
else if( tmp == 0x22 )
{
ptr->state = 2;
}
else
{
ptr->state = 0;
}
}
void set_message_readFlag(char block, unsigned char index)
{
unsigned int address;
unsigned char tmp;
MESSAGEINFO *msgPtr;
unsigned char readFlag;
if( block==1)
{
msgPtr = &messageInfo[0];
}
else if( block == 2 )
{
msgPtr = &messageInfo[1];
}
else if( block == 3 )
{
msgPtr = &messageInfo[2];
}
else
return;
if( (index > MESSAGE_RECORD_MAX) || (index == 0) )
{
return;
}
if( index > msgPtr->current )
{
tmp = MESSAGE_RECORD_MAX + msgPtr->current - index;
}
else
{
tmp = msgPtr->current - index;
}
address = 1+ tmp * MESSAGE_MAX_LENGTH + msgPtr->baseAddress;
c512_config_read(address, &readFlag, 1 );
if( readFlag == 0x11 )
{
readFlag = 0x22;
c512_config_write(address, &readFlag, 1 );
if( msgPtr->noRead )
msgPtr->noRead --;
}
}
void message_convert(char *okPtr, char *inPtr)
{
unsigned char *srcPtr, *destPtr;
unsigned char isChinese;
srcPtr = inPtr;
destPtr = okPtr;
while( *srcPtr )
{
if( *srcPtr >= 0x80 )
isChinese = 1;
else
isChinese = 0;
*destPtr++ = *srcPtr++;
if( *srcPtr>=0x80)
{
if( isChinese )
*destPtr++ = *srcPtr++;
else
{
*destPtr++ = 0x20;
}
}
else if( isChinese==0 )
{
*destPtr++ = *srcPtr++;
}
}
*destPtr = 0;
}
/*陈剑填加,需要返回生成的消息内容的长度*/
unsigned char message_convert_tolength(char *okPtr, char *inPtr)
{
unsigned char *srcPtr, *destPtr;
unsigned char isChinese;
unsigned char nRet;
nRet = 0;
srcPtr = inPtr;
destPtr = okPtr;
while( *srcPtr )
{
if( *srcPtr >= 0x80 )
isChinese = 1;
else
isChinese = 0;
*destPtr++ = *srcPtr++;
nRet++;
if( *srcPtr>=0x80)
{
if( isChinese )
*destPtr++ = *srcPtr++;
else
{
*destPtr++ = 0x20;
}
}
else if( isChinese==0 )
{
*destPtr++ = *srcPtr++;
}
nRet++;
}
*destPtr = 0;
nRet++;
return nRet;
}
/*block 1:调度 2告警 3运营计划*/
void message_write(char block, char *ptr, unsigned char length, long msgNo)
{
unsigned int address;
char *tmpPtr;
MESSAGEINFO *msgPtr;
if( block==1)
{
msgPtr = &messageInfo[0];
}
else if( block == 2 )
{
msgPtr = &messageInfo[1];
}
else if( block == 3 )
{
msgPtr = &messageInfo[2];
}
else
return;
if( length >= 210 )
return;
msgPtr->noRead ++;
msgPtr->entrys ++;
if( msgPtr->noRead > MESSAGE_RECORD_MAX )
msgPtr->noRead = MESSAGE_RECORD_MAX;
if( msgPtr->entrys > MESSAGE_RECORD_MAX )
msgPtr->entrys = MESSAGE_RECORD_MAX;
tmpPtr = messageBuffer;
*tmpPtr++ = msgPtr->id++;
*tmpPtr++ = 0x11;
get_term_data( tmpPtr );
tmpPtr += 3;
get_term_time( tmpPtr);
tmpPtr += 3;
*tmpPtr = 0;
tmpPtr += 14;
memcpy(tmpPtr, &msgNo, 4);
tmpPtr += 4;
*(ptr+length) = 0;
message_convert(tmpPtr, (char*)ptr);
/*写入告警记录*/
address = msgPtr->current * MESSAGE_MAX_LENGTH + msgPtr->baseAddress;
c512_config_write(address, messageBuffer, 128);
delayMs(10);
c512_config_write(address+128, messageBuffer+128, 128);
msgPtr->current = (msgPtr->current + 1) % MESSAGE_RECORD_MAX;
}
/*获取消息处理队列的状态*/
/*flag : 取值为*/
void get_message_state(char block, char *value, unsigned char flag)
{
MESSAGEINFO *msgPtr;
if( block==1)
{
msgPtr = &messageInfo[0];
}
else if( block == 2 )
{
msgPtr = &messageInfo[1];
}
else if( block == 3 )
{
msgPtr = &messageInfo[2];
}
else
return;
if (MSG_TOTALNUM == flag)
*value = msgPtr->entrys;
else
*value = msgPtr->noRead;
}
static unsigned char pollingIndex;
void message_polling(void)
{
static unsigned char timeCount;
unsigned char tmp;
unsigned char i, index;
unsigned int address;
timeCount ++;
if( timeCount < 5)
return;
if( messageInfo[0].noRead == 0 )
return;
index = messageInfo[0].current;
for(i=0; i {
index = index % MESSAGE_RECORD_MAX;
address = 1+ index * MESSAGE_MAX_LENGTH + messageInfo[0].baseAddress;
c512_config_read(address, &tmp, 1 );
if( tmp == 0x11 )
{
c512_config_read(address, &messageBuffer, sizeof(MESSAGE) );
messageBuffer[0] = 1;
//此处需要调用弹出菜单
get_dispatchMsgMenuPopUp();
pollingIndex = index;
break;
}
index ++;
}
}
char message_pollingIndex(void)
{
unsigned char rc;
rc = MESSAGE_RECORD_MAX + messageInfo[0].current - pollingIndex;
if( rc > 20 )
rc = rc - 20;
return(rc);
}
void getPollingMsg(Byte* pMsg)
{
MESSAGE* pSrcMsg;
Byte i, addrValue;
SET_WORKSTATUS* pStatus = (SET_WORKSTATUS*)pMsg;
pSrcMsg = (MESSAGE*)messageBuffer;
memcpy(pStatus->id , pSrcMsg->id, 4);
//将日期输入到缓存
addrValue = 0;
for (i=0; i {
bcd_to_ascii((pStatus->content)+addrValue, pSrcMsg->date[i]);
addrValue += 2;
if (i != 2)
pStatus->content[addrValue] = '-';
else
pStatus->content[addrValue] = ' ';
addrValue++;
}
for (i=0; i {
bcd_to_ascii((pStatus->content)+addrValue, pSrcMsg->time[i]);
addrValue += 2;
if (i == 0)
pStatus->content[addrValue] = ':';
else
pStatus->content[addrValue] = ' ';
addrValue++;
}
pStatus->content[addrValue] = ' ';
addrValue++;
pStatus->len = addrValue;
memcpy((pStatus->content)+addrValue, pSrcMsg->memo, strlen(pSrcMsg->memo));
pStatus->len += strlen(pSrcMsg->memo);
}