⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isomodem_at.c

📁 modem数据通信源码
💻 C
字号:
#include "44b.H"
#include "EPT32.H"
#include "Nucleus.H"
#include "string.h"
#include "switch.h"    

#include "interdrive.h"
#include "common_exp.h"
#include "ISOModem_DRV.h"
#include "Modem_IODrv.h"
#include "exports.h"
#include "IODrive.h"
#include "Regist.h"

#include "ISOModem.h"
#include "loopbuf.h"

extern EM_modem_STS    EG_modem_STS;
uchar	EI_HexStrToDig( char* pcsHexStr,int *piDig);

int EI_WriteUart(char *buf,unsigned int len)
{
    unsigned int i = 0;
    
    for(i=0;i<len;i++)
    {
        //uprintf("[%2x]",buf[i]);
        EA_ucWriteDeviceMs(EG_modem_STS.uarthandle,1,10,buf+i);
    }
    
    return EM_SUCCESS;
}

void EI_ClearUartRecvBuf(void)
{
    loop_reset(EG_modem_STS.serial_recvbuf);
}

int EI_ReadUart_OK(unsigned int time)
{
    unsigned int tout;
    char at_resp[100];
    unsigned int pos=0;
    UNSIGNED time_start,time_cur;
    int ret;
    
    if(time==0)
        tout = 100*3;
    else
        tout = 100*time;
    
    memset(at_resp,0x00,sizeof(at_resp));
    time_start = NU_Retrieve_Clock();    
    while(1)
    {
        time_cur = NU_Retrieve_Clock();        
        ret = loop_read(EG_modem_STS.serial_recvbuf,at_resp+pos,1);
        if(ret>0)
            pos+=ret;
        if(pos>=sizeof(at_resp)||time_cur>=(time_start+tout))
            break;
        if(strstr(at_resp,"\x0d\x0aOK\x0d\x0a"))
        {            
            return EM_SUCCESS;
        }
        if(strstr(at_resp,"ERROR"))
            return EM_ERROR;
        NU_Sleep(1);
    }
    if(strstr(at_resp,"\x0d\x0aOK\x0d\x0a"))
        return EM_SUCCESS;
    else
        return EM_ERROR;
}

int EI_SendAT_OK(char *atcmd,unsigned int time)
{
    if(EG_modem_STS.online!=EM_ONLINE_AT
        &&EG_modem_STS.online!=EM_ONLINE_OFFLINE
        )
    {
        uprintf("send AT:%s fail,not in AT state\r\n",atcmd);
        return EM_ERROR;
    }
    EI_SendAT(atcmd);
    return EI_ReadUart_OK(time);
}

int EI_SendATE0_OK(void)
{
    if(EG_modem_STS.online!=EM_ONLINE_AT
        &&EG_modem_STS.online!=EM_ONLINE_OFFLINE
        )
    {
        uprintf("send AT:ATE0 fail,not in AT state\r\n");
        return EM_ERROR;
    }
    EI_SendAT("ATE0\r");
    {
        char at_resp[100];
        unsigned int pos=0;
        UNSIGNED time_start,time_cur;
        int ret;
        unsigned int tout = EM_TIME_TICK_IN1S/2;
        
        memset(at_resp,0x00,sizeof(at_resp));
        time_start = NU_Retrieve_Clock();
        while(1)
        {
            time_cur = NU_Retrieve_Clock();
            ret = loop_read(EG_modem_STS.serial_recvbuf,at_resp+pos,1);
            if(ret>0)
                pos+=ret;
            if(pos>=sizeof(at_resp)||time_cur>=(time_start+tout))
                break;
            if(strstr(at_resp,"\x0d\x0aOK\x0d\x0a"))
                return EM_SUCCESS;
            if(strstr(at_resp,"ERROR"))
                return EM_ERROR;
            NU_Sleep(1);
        }
        if(strstr(at_resp,"\x0d\x0aOK\x0d\x0a"))
            return EM_SUCCESS;
        else
            return EM_ERROR;
    }
}

void EI_SendAT(char *atcmd)
{
    uprintf("send AT cmd: %s\r\n",atcmd);
    EI_ClearUartRecvBuf(); 
    EI_WriteUart(atcmd,strlen(atcmd));
}

unsigned int EI_CalcCrLf(char *buf)
{
    unsigned int pos = 0;
    unsigned int cnt = 0;
    
    if(strlen(buf)>64*1024)     /* I think it is a impossible buf len */
        return 0;
    while(*(buf+pos))
    {
        if(buf[pos]==0x0d&&buf[pos+1]==0x0a)
            cnt++;
        pos++;
    }
    return cnt;
}

uchar EI_cx_AT_TRV(float	*pfEcho)
{
    char at_resp[100];
    unsigned int pos=0;
    UNSIGNED time_start,time_cur;
    int ret;
    unsigned int tout = 100*1;
    
	if(pfEcho==NU_NULL)
		return EM_ERROR;
    
    if(EI_SendATE0_OK()!=EM_SUCCESS)
        return EM_ERROR;
        
    EI_SendAT("AT-TRV\r");
    
	memset(at_resp,0x00,sizeof(at_resp));
    time_start = NU_Retrieve_Clock();
    while(1)
    {
        time_cur = NU_Retrieve_Clock();
        ret = loop_read(EG_modem_STS.serial_recvbuf,at_resp+pos,1);
        if(ret>0)
            pos+=ret;
        if(pos>=sizeof(at_resp)||time_cur>=(time_start+tout))
            break;
        if(strstr(at_resp,"\x0d\x0aOK\x0d\x0a"))
            break;
        if(strstr(at_resp,"ERROR"))
            return EM_ERROR;
        NU_Sleep(1);
    }
    if(strstr(at_resp,"\x0d\x0aOK\x0d\x0a"))
    {
        *pfEcho=(float)atof(at_resp+2);     /* skip first \x0d\x0a */
        return EM_SUCCESS;
    }
    return EM_ERROR;
}

uchar EI_cx_AT_IDC(float	*pfEcho)
{
    char at_resp[100];
    unsigned int pos=0;
    UNSIGNED time_start,time_cur;
    int ret;
    unsigned int tout = 100*1;
    
	if(pfEcho==NU_NULL)
		return EM_ERROR;
    
    if(EI_SendATE0_OK()!=EM_SUCCESS)
        return EM_ERROR;
        
    EI_SendAT("AT-IDC\r");
    
	memset(at_resp,0x00,sizeof(at_resp));
    time_start = NU_Retrieve_Clock();
    while(1)
    {
        time_cur = NU_Retrieve_Clock();
        ret = loop_read(EG_modem_STS.serial_recvbuf,at_resp+pos,1);
        if(ret>0)
            pos+=ret;
        if(pos>=sizeof(at_resp)||time_cur>=(time_start+tout))
            break;
        if(strstr(at_resp,"\x0d\x0aOK\x0d\x0a"))
            break;
        if(strstr(at_resp,"ERROR"))
            return EM_ERROR;
        NU_Sleep(1);
    }
    if(strstr(at_resp,"\x0d\x0aOK\x0d\x0a"))
    {
        *pfEcho=(float)atof(at_resp+2);     /* skip first \x0d\x0a */
        return EM_SUCCESS;
    }
    return EM_ERROR;
}

uchar EI_si_AT_R6C(int	*piEcho)
{
    char at_resp[100];
    unsigned int pos=0;
    UNSIGNED time_start,time_cur;
    int ret;
    unsigned int tout = 100*1;

	if(piEcho==NU_NULL)
        return EM_ERROR;
    
	if(EI_SendATE0_OK()!=EM_SUCCESS)
        return EM_ERROR;
        
    EI_SendAT("AT:R6C\r");
    
	memset(at_resp,0x00,sizeof(at_resp));
    time_start = NU_Retrieve_Clock();
    while(1)
    {
        time_cur = NU_Retrieve_Clock();
        ret = loop_read(EG_modem_STS.serial_recvbuf,at_resp+pos,1);
        if(ret>0)
            pos+=ret;
        if(pos>=sizeof(at_resp)||time_cur>=(time_start+tout))
            break;
        if(strstr(at_resp,"\x0d\x0aOK\x0d\x0a"))
            break;
        if(strstr(at_resp,"ERROR"))
            return EM_ERROR;
        NU_Sleep(1);
    }
    if(strstr(at_resp,"\x0d\x0aOK\x0d\x0a"))
    {
        return(EI_HexStrToDig(at_resp+2,piEcho));
    }
    
    return EM_ERROR;
}

uchar	EI_HexStrToDig( char* pcsHexStr,int *piDig)
{
    uint	i=0;
    uchar	ucValue=0;
    uchar	ucFindDigit=EM_ERROR;
	if((pcsHexStr==NU_NULL)||(piDig==NU_NULL))
	{
		return EM_ERROR;
	}
	*piDig=0;
	while(pcsHexStr[i]!=0)
	{
		if((pcsHexStr[i]>='0')&&(pcsHexStr[i]<='9'))
		{
			ucValue=pcsHexStr[i]-0x30;
			ucFindDigit=EM_SUCCESS;
		}else if((pcsHexStr[i]>='A')&&(pcsHexStr[i]<='F'))
		{
			ucValue=pcsHexStr[i]-0x37;
			ucFindDigit=EM_SUCCESS;
		}else if((pcsHexStr[i]>='a')&&(pcsHexStr[i]<='f'))
		{
			ucValue=pcsHexStr[i]-0x57;
			ucFindDigit=EM_SUCCESS;
		}
		else if(ucFindDigit==EM_SUCCESS)
		{
			break;
		}
		if(ucFindDigit==EM_SUCCESS)
		{
			(*piDig)=(*piDig)*0x10+ucValue;
		}
		i++;
	}
	if(ucFindDigit==EM_SUCCESS)
		return EM_SUCCESS;
	else
		return EM_ERROR;
}

uchar EI_At_TillStr(char *atcmd,char *resp,unsigned int time)
{
    if(EG_modem_STS.online!=EM_ONLINE_AT
        &&EG_modem_STS.online!=EM_ONLINE_OFFLINE
        )
    {
        uprintf("send AT:%s fail,not in AT state\r\n",atcmd);
        return EM_ERROR;
    }
    EI_SendAT(atcmd);
    {
        char at_resp[200];
        unsigned int pos=0;
        UNSIGNED time_start,time_cur;
        int ret;
        unsigned int tout = time*EM_TIME_TICK_IN1S;
        
        memset(at_resp,0x00,sizeof(at_resp));
        time_start = NU_Retrieve_Clock();
        while(1)
        {
            time_cur = NU_Retrieve_Clock();
            ret = loop_read(EG_modem_STS.serial_recvbuf,at_resp+pos,1);
            if(ret>0)
                pos+=ret;
            if(pos>=sizeof(at_resp)||time_cur>=(time_start+tout))
                break;
            if(strstr(at_resp,resp))
                return EM_SUCCESS;
            if(strstr(at_resp,"ERROR"))
                return EM_ERROR;
            NU_Sleep(1);
        }
        if(strstr(at_resp,resp))
            return EM_SUCCESS;
        else
            return EM_ERROR;
    }
}

uchar EI_SendAT_OK_Recv(char *atcmd,char *resp,unsigned int max,unsigned int time)
{
    if(EG_modem_STS.online!=EM_ONLINE_AT
        &&EG_modem_STS.online!=EM_ONLINE_OFFLINE
        )
    {
        uprintf("send AT:%s fail,not in AT state\r\n",atcmd);
        return EM_ERROR;
    }
    EI_SendAT(atcmd);
    {
        char at_resp[1024];
        unsigned int pos=0;
        UNSIGNED time_start,time_cur;
        int ret;
        unsigned int tout = time*EM_TIME_TICK_IN1S;
        
        memset(at_resp,0x00,sizeof(at_resp));
        time_start = NU_Retrieve_Clock();
        while(1)
        {
            time_cur = NU_Retrieve_Clock();
            ret = loop_read(EG_modem_STS.serial_recvbuf,at_resp+pos,1);
            if(ret>0)
                pos+=ret;
            if(pos>=sizeof(at_resp)||time_cur>=(time_start+tout))
                break;
            if(strstr(at_resp,"\x0d\x0aOK\x0d\x0a"))
            {
                memcpy(resp,at_resp,pos>max?max:pos);
                return EM_SUCCESS;
            }
            if(strstr(at_resp,"ERROR"))
                return EM_ERROR;
            NU_Sleep(1);
        }
        if(strstr(at_resp,"\x0d\x0aOK\x0d\x0a"))
        {
            memcpy(resp,at_resp,pos>max?max:pos);
            return EM_SUCCESS;
        }
        else
            return EM_ERROR;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -