📄 gprslib.c
字号:
#include "44b.h"
#include "44blib.h"
#include "gprs.h"
#include "gb2312-unicode.h"
int gprs_ctrl_value = 0x0;
int gprs_ctrl_value1 = 0x0;
/* 接收数据缓冲区 */
char gprs_recv_buf[GPRS_RECV_CMD_MAX_BUF];
int gprs_recv_read = 0;
int gprs_recv_write = 0;
int int_bak;
/********************************************************************
// Function name : gprs_disable_int
// Description : 关中断
// Return type : void
// Argument :
*********************************************************************/
void gprs_disable_int()
{
int_bak = rINTMSK;
rINTMSK = BIT_GLOBAL;
}
/********************************************************************
// Function name : gprs_enable_int
// Description : 开中断
// Return type : void
// Argument :
*********************************************************************/
void gprs_enable_int()
{
rINTMSK = int_bak;
}
/********************************************************************
// Function name : gprs_uart_ctrl
// Description : GPRS使用串口1,串口0用于显示,GPRS初始化之前请
// 调用该函数进行初始化
// Return type : void
// Argument : int uart = 0x01
*********************************************************************/
void gprs_uart_ctrl(int uart)
{
gprs_ctrl_value &= ~GPRS_CONTROL_MASK_UART;
gprs_ctrl_value |= uart;
gprs_ctrl_value1 |= 0x20;
*(unsigned char *)GPRS_CONTROL_ADDR = gprs_ctrl_value;
*(unsigned char *)0x0a000006 = gprs_ctrl_value1;
}
/********************************************************************
// Function name : gprs_send_cmd
// Description : 发送GPRS命令字串
// Return type : void
// Argument : char *cmdstring
*********************************************************************/
void gprs_send_cmd(char *cmdstring)
{
// disable int
gprs_disable_int();
// select uart 1
Uart_Select(0);
// send command
Uart_Printf("Send -> %s\n", cmdstring);
// select uart 1
Uart_Select(1);
// send command
Uart_Printf(cmdstring);
// enable int
gprs_enable_int();
}
/********************************************************************
// Function name : gprs_recv_char
// Description : 从GPRS接收字符,只能从中断服务函数中调用
// Return type : void
// Argument :
*********************************************************************/
void gprs_recv_char()
{
char ch;
do
{
// disable int
gprs_disable_int();
// select uart 1
Uart_Select(1);
// receive command
ch = Uart_GetKey();
// enable int
gprs_enable_int();
if(ch == 0)
{
return;
}else
{
gprs_recv_buf[gprs_recv_write] = ch;
gprs_recv_write ++;
if(gprs_recv_write >= GPRS_RECV_CMD_MAX_BUF)
gprs_recv_write = 0;
if(gprs_recv_write == gprs_recv_read)
{
// 缓冲区以满
gprs_recv_read ++;
if(gprs_recv_read >= GPRS_RECV_CMD_MAX_BUF)
gprs_recv_read = 0;
}
}
}while(1);
}
/********************************************************************
// Function name : gprs_recv_cmd
// Description :
// Return type : int:
// GPRS_OK -- 接收到命令
// GPRS_ERR -- 未接收到命令
// Argument : char *cmd:返回的命令
*********************************************************************/
int gprs_recv_cmd(char *cmd)
{
int loopcnt = 0;
int ncount = 0;
while(1)
{
if(gprs_recv_read == gprs_recv_write)
{
ncount ++;
if(ncount >= 10000)
{
cmd[loopcnt++] = 0;
return GPRS_ERR;
}
continue;
}
ncount = 0;
if( (gprs_recv_buf[gprs_recv_read] == '\r') || (gprs_recv_buf[gprs_recv_read] == '\n'))
{
cmd[loopcnt ++] = 0;
gprs_recv_read ++;
if(gprs_recv_read >= GPRS_RECV_CMD_MAX_BUF)
gprs_recv_read = 0;
if(strlen(cmd))
{
TRACE("\nRecv <- ");
TRACE(cmd);
TRACE("\n");
}
return GPRS_OK;
}else
{
cmd[loopcnt ++] = gprs_recv_buf[gprs_recv_read];
gprs_recv_read ++;
if(gprs_recv_read >= GPRS_RECV_CMD_MAX_BUF)
gprs_recv_read = 0;
}
}
}
/********************************************************************
// Function name : Uart1_RxInt
// Description : 串口1接收中断服务程序
// Return type : void
// Argument : void
*********************************************************************/
void Uart1_RxInt(void)
{
rI_ISPC=BIT_URXD1;
gprs_recv_char();
}
/********************************************************************
// Function name : gprs_init
// Description : GPRS初始化函数
// Return type : void
// Argument :
*********************************************************************/
void gprs_init()
{
// 串口初始化
Uart_Init(MCLK,115200);
pISR_URXD1=(unsigned)Uart1_RxInt;
// uart0 -> db9 uart1 -> gprs
gprs_uart_ctrl(0x01);
// GPC10 -> Power On/Off
// GPRS模块电源控制
rPCONC |= (1 << 20);
rPCONC &= ~(1 << 21);
timer_init();
// 打开UART TX中断
rINTMSK &= ~BIT_URXD1;
}
/********************************************************************
// Function name : gprs_uninit
// Description : gprs模块退出
// Return type : void
// Argument :
*********************************************************************/
void gprs_uninit()
{
rINTMSK=BIT_GLOBAL;
// disable int
gprs_disable_int();
Uart_Select(0);
// enable int
gprs_enable_int();
}
/********************************************************************
// Function name : gprs_pwr_on_off
// Description : GPRS复位操作
// Return type : void
// Argument : int bon
// GPRS_PWR_ON 开启
// GPRS_PWR_OFF 0 关闭
*********************************************************************/
void gprs_pwr_on_off(int bon)
{
static status_pwr = GPRS_PWR_OFF;
int loopcnt;
if(bon == GPRS_PWR_ON)
{
// if(status_pwr == GPRS_PWR_ON)
// return;
rPDATC |= (1 << 10 );
Delay(20);
rPDATC &= ~(1 << 10 );
Delay(15000);
rPDATC |= (1 << 10 );
status_pwr = GPRS_PWR_ON;
}else
{
// if(status_pwr == GPRS_PWR_OFF)
// return;
rPDATC |= (1 << 10 );
Delay(20);
rPDATC &= ~(1 << 10 );
Delay(15000);
rPDATC |= (1 << 10 );
status_pwr = GPRS_PWR_OFF;
}
}
/********************************************************************
// Function name : gprs_analyze_msg
// Description : 分析接收到的GPRS命令
// Return type : int
// Argument : char * message
*********************************************************************/
int gprs_analyze_msg(char * message)
{
if (strstr(message, "OK") != 0)
{
if (strstr(message, "+CMGS:") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_SMS_SEND_RESPONSE, 0, 0);
}
else if (strstr(message, "+CMGR:") != 0)
{
return AT_RECV_MSG_CMGR;
}
else if (strstr(message, "+CSQ:") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_SIGNAL_STRENGTH, 0, 0);
}
else if (strstr(message, "+CBC:") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_BATTERY_LEVEL, 0, 0);
}
else if (strstr(message, "+CRSL:") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_RINGER_LEVEL, 0, 0);
}
else if (strstr(message, "+CREG:") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_NETWORK_REGISTRATION, 0, 0);
}
else if (strstr(message, "+CGMI") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_MANUFACTURER_ID, 0, 0);
}
else if (strstr(message, "+CGMM") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_MODEL_ID, 0, 0);
}
else if (strstr(message, "+CGMR") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_REVISION_ID, 0, 0);
}
else if (strstr(message, "+CNUM") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_SUBSCRIBER_NUMBER, 0, 0);
}
else if (strstr(message, "+CSCS") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_TE_CHARACTER_SET, 0, 0);
}
else if (strstr(message, "+CCLK") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_TIME, 0, 0);
}
else if (strstr(message, "+CRC") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_CELLULAR_RESULT_CODE, 0, 0);
}
else if (strstr(message, "+CR") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_REPORTING_CONTROL, 0, 0);
}
else if (strstr(message, "S0") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_S0, 0, 0);
}
else if (strstr(message, "S7") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_S7, 0, 0);
}
else if (strstr(message, "S8") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_S8, 0, 0);
}
else if (strstr(message, "S10") != 0)
{
// threadData.m_rmReceMsg.m_strMessage1 = message;
// ::PostMessage(threadData.m_hWnd, WM_S10, 0, 0);
}
else
{
return AT_RECV_MSG_OK;
}
}
else if (strstr(message, "+CMGR:") != 0)
{
return AT_RECV_MSG_CMGR;
}
else if (strstr(message, "RING") != 0)
{
// 振铃
return AT_RECV_MSG_RING;
}
else if (strstr(message, "NO CARRIER") != 0)
{
// 无载波
return AT_RECV_MSG_NO_CARRIER;
}
else if (strstr(message, "ERROR") != 0)
{
// 错误
return AT_RECV_MSG_ERROR;
}
else if (strstr(message, "CMTI") != 0)
{
// NEW SMS
return AT_RECV_MSG_CMTI;
}else if (strstr(message, ">") != 0)
{
return AT_RECV_GET_MSG;
}
return AT_RECV_MSG_NULL;
}
// 多字节字符转换为宽字符
// 返回: 宽字符数据长度
int MultiByteToWideChar(
unsigned int CodePage,
unsigned int dwFlags,
char * lpMultiByteStr,
int cbMultiByte,
unsigned short * lpWideCharStr,
int cchWideChar)
{
int retval =0;
char * lpCurrentUnixLCType = 0;
if ( 0 == CodePage )
{
unsigned int nIndex = 0;
if ( cbMultiByte == -1)
{
cbMultiByte = strlen(lpMultiByteStr) + 1;
}
if (cchWideChar == 0)
{
retval = cbMultiByte;
goto EXIT;
}
if ( cbMultiByte > cchWideChar )
{
retval = 0;
goto EXIT;
}
for (nIndex=0; nIndex < cbMultiByte; nIndex++ )
{
int i;
if ((lpMultiByteStr[nIndex] < 0x80))
{
lpWideCharStr[retval] = lpMultiByteStr[nIndex];
retval+=1;
}
else
{
i = 0;
lpWideCharStr[retval] = '?';
while(1)
{
if(gb2312_unicode_tran_table[i].gb2312==0 && gb2312_unicode_tran_table[i].unicode==0)
break;
if(gb2312_unicode_tran_table[i].gb2312 == (((lpMultiByteStr[nIndex] << 8) + lpMultiByteStr[nIndex + 1]) - 0x8080))
{
lpWideCharStr[retval] = gb2312_unicode_tran_table[i].unicode;
retval++;
nIndex++;
break;
}
i ++;
}
}
}
goto EXIT;
}
EXIT:
return retval;
}
// wcslen
int wcslen(const short *string)
{
int nChar = 0;
if ( !string )
{
return 0;
}
while (*string++)
{
nChar++;
}
return nChar;
}
// 宽字符转换为多字符
int WideCharToMultiByte(
unsigned int CodePage,
unsigned int dwFlags,
unsigned short * lpWideCharStr,
int cchWideChar,
char * lpMultiByteStr,
int cbMultiByte,
char * lpDefaultChar,
int * lpUsedDefaultChar)
{
int retval =0;
char * lpCurrentUnixLCType = 0;
if (0 == CodePage)
{
unsigned int nIndex = 0;
if ( cchWideChar == -1)
{
cchWideChar = wcslen(lpWideCharStr) + 1;
}
if (cbMultiByte == 0)
{
/* cbMultiByte is 0, we must return the lenght of
the destination buffer in bytes */
retval = cchWideChar;
goto EXIT;
}
if ( cchWideChar > cbMultiByte )
{
retval = 0;
goto EXIT;
}
/* perform a reverse lookup on the PAL_CP_1252 table */
for (nIndex=0 ; nIndex < cchWideChar; nIndex++ )
{
int i;
if ((lpWideCharStr[nIndex] < 0x80))
{
lpMultiByteStr[retval] = (unsigned char) lpWideCharStr[nIndex];
retval++;
}
else
{
i = 0;
lpMultiByteStr[retval] = '?';
while(1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -