打印

基于MD251 Opencpu模块做的GPRS数传方案 短信,GPRS部分参考例程

[复制链接]
2158|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
play_2008|  楼主 | 2013-5-26 22:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 play_2008 于 2013-5-26 22:46 编辑

短信的发送和接收都很简单,发送只需要调用一个接口函数,接收只需要注册一个回调函数就可以接收

/*SMS send request result callback function 短信发送后的返回*/
void  oa_sms_send_req_cb(os_sms_result result)
{
    OA_DEBUG_USER("%s: result=%d",__func__,result);
}

/* Example 1:send 7bit sms content "123ABC" to address "13812345678" 发送7bin 英文短信参考*/
void os_sms_send_demo_7bit(void)
{
    oa_sms_send_req(oa_sms_send_req_cb,"13812345678", "123ABC", oa_strlen((oa_char *)"123ABC"),OA_SMSAL_DEFAULT_DCS);
}

/* Example 2:send 8bit sms content "0x01,0x02,0x03,0xAA,0xBB,0xCC" to address "13812345678"  发送8bit 16进制数参考*/
void os_sms_send_demo_8bit(void)
{
    oa_uint8 smsSendData[]={0x01,0x02,0x03,0xAA,0xBB,0xCC,0x00};
    oa_sms_send_req(oa_sms_send_req_cb,"13812345678",  (oa_char *)smsSendData, 6,OA_SMSAL_8BIT_DCS);
}

/* Example 3:send UCS2 sms content "测试ABC" to address "13812345678"  发送中文短信参考*/
void os_sms_send_demo_ucs2(void)
{
    oa_char sendUcs2Str[100]={0};

    /*First,convert GB2312 coding to UCS2 coding*/
    oa_chset_convert(OA_CHSET_GB2312,
                                   OA_CHSET_UCS2,
                                   "测试ABC",
                                  sendUcs2Str,
                                   20);
    oa_sms_send_req(oa_sms_send_req_cb,"13812345678", sendUcs2Str, oa_wstrlen((oa_wchar *)sendUcs2Str),OA_SMSAL_UCS2_DCS);
}

相关帖子

沙发
play_2008|  楼主 | 2013-5-26 22:41 | 只看该作者
本帖最后由 play_2008 于 2013-5-26 22:48 编辑

短信接收部分
先注册一个回调
    oa_sms_rcv_ind_register(oa_sms_rcv_ind_handler);

回调函数可以接收中文 英文和16进制短信
/*****************************************************************/
/*------------------sms received indication handler refer framework
PARAM:
deliver_num: SMS MO address
timestamp: sms timestamp
                  timestamp[0]= nYear;
                  timestamp[1]= nMonth;
                  timestamp[2]= nDay;
                  timestamp[3]= nHour;
                  timestamp[4]= nMin;
                  timestamp[5]= nSec;

data: sms text data
len: sms data length,
        if dcs=7-bit or 8-bit, is the bytes length,
        if dcs=UCS2,is the unicode length
dcs: refer oa_smsal_dcs_enum
RETURN:
   OA_TRUE: the sms be handled by openat user, and will be be saved
   OA_FALSE:the sms is not openat user cared
******************************************************************/
oa_bool oa_sms_rcv_ind_handler(oa_char * deliver_num, oa_char * timestamp, oa_uint8 * data, oa_uint16 len, oa_smsal_dcs_enum dcs)
{
    OA_DEBUG_USER("SMS received:deliver_num=%s,timestamp=%d-%d-%d-%d:%d:%d,dcs=%d,len=%d,data=%s",\
    deliver_num,*(timestamp+0),*(timestamp+1),*(timestamp+2),*(timestamp+3),*(timestamp+4),*(timestamp+5),dcs,len,data);

    if(dcs == OA_SMSAL_DEFAULT_DCS)
    {
            /*handle ascii sms text. 7bit英文*/
    }
    else if(dcs == OA_SMSAL_UCS2_DCS)
   {
            /*handle unicode sms text. 中文*/
    }
          else
   {
          /*handle 8-bit sms text 16进制.*/
          }
        return OA_TRUE;/*delete current SMS from memory*/
        //return OA_FALSE;/*do not delete this SMS, user can handle it*/
   /*Warning: if user return OA_FALSE to save current sms in memory,
      he should pay attention to the memory capacity.
      Please use at+cpms to confirm current memory capacity and, when the memory is full,
      user will receive AT command response "+OPENAT: SMS FULL" from PS port
      and system will clean all the SMS in order to receive more SMS.*/
}
/************************/

使用特权

评论回复
板凳
play_2008|  楼主 | 2013-5-26 22:44 | 只看该作者
GPRS 系统消息的处理
//Socket Notify Event indication handler framework
void oa_soc_notify_ind(void *inMsg)
{
    oa_int32 ret = 0;   
    oa_app_soc_notify_ind_struct *soc_notify = (oa_app_soc_notify_ind_struct*) inMsg;

    //if other application's socket id, ignore it.
    if(soc_notify->socket_id != g_soc_context.socket_id)
    {
       GPRS_DEBUG("%s:sock_id=%d unknow, event_type=%d!",__func__,soc_notify->socket_id,soc_notify->event_type);   
       return;
    }
   
    switch (soc_notify->event_type)
    {
        case OA_SOC_WRITE:
        {
           /* Notify for send data*/
           if (soc_notify->result == OA_TRUE)
           {
              g_soc_context.is_blocksend = OA_FALSE;
             //resend the data ,else will lost data
              GPRS_DEBUG("%s:sock_id=%d resend!",__func__,soc_notify->socket_id);
              oa_soc_send_req( );
           }
           else
           {
             GPRS_DEBUG("%s:sock_id=%d send fail err=%d!",__func__,soc_notify->socket_id,soc_notify->error_cause);
             oa_soc_close_req( );
           }     
           
           break;
        }
            
        case OA_SOC_READ:
        {
            /* Notify for received data */
            if (soc_notify->result == OA_TRUE)
            {
                do
                {
                    oa_memset(gprs_rx_buffer, 0 , (OA_MAX_SOC_RCV_BUFLEN*sizeof(oa_uint8)));

                    //received gprs data, read data for protocol
                    ret = oa_soc_recv(soc_notify->socket_id , (oa_uint8*)gprs_rx_buffer, OA_MAX_SOC_RCV_BUFLEN, 0);
                    GPRS_DEBUG("%s:sock_id=%d read ok ret=%d!",__func__,soc_notify->socket_id,ret);

                    if(ret > 0)
                    {   
                        // read data length=ret,
                        oa_soc_gprs_recv((oa_uint8*)gprs_rx_buffer, ret);
                    }
                    else
                    {
                        //read data error or block
                    }  
                }while(ret>0); //Make sure use  do{...}while  to read out all received data.
            }
            else
            {
              GPRS_DEBUG("%s:sock_id=%d read fail err=%d!",__func__,soc_notify->socket_id,soc_notify->error_cause);
              oa_soc_close_req( );
            }     
            break;
        }
            
        case OA_SOC_CONNECT:
        {
            if (soc_notify->result == OA_TRUE)
            {
                GPRS_DEBUG("%s:sock_id=%d connect ok!",__func__,soc_notify->socket_id);

                //if connect OK, goto query gprs_tx data and send
               oa_sleep(100); //just connected need delay 100ms then send gprs data, else may lost data

                g_soc_context.state = OA_SOC_STATE_ONLINE;
                oa_soc_send_req( );
            }
            else
            {
                GPRS_DEBUG("%s:sock_id=%d connect fail err=%d!",__func__,soc_notify->socket_id,soc_notify->error_cause);
                oa_soc_close_req( );
            }
            break;
        }      
            
        case OA_SOC_CLOSE:
        {
            // the socket is closed by remote server, use soc_close to close local socketID, else will lead OA_SOC_LIMIT_RESOURCE

            GPRS_DEBUG("%s:sock_id=%d close,error_cause=%d",__func__,soc_notify->socket_id,soc_notify->error_cause);

            oa_soc_close_req();

            break;
        }
            
        case OA_SOC_ACCEPT:
            break;
            
        default:
            break;
    }

    oa_soc_start_heartbeat_timer();
}

使用特权

评论回复
地板
TIIDU| | 2016-5-31 16:58 | 只看该作者
不错

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

4

帖子

1

粉丝