获取cellID

typedef struct
{
    char c[12];
    char    iLac[4];
    char s[4];
    char iId[4];
} TCREG_DATA;

char* GetCREG( char * comPort )
{
    HANDLE hCom;
    int bufpos;
    DCB dcb;
    COMMTIMEOUTS to;
    DWORD nWritten;
    DWORD event;
    DWORD nRead;
    static char outbuf[20], buf[256];

    BYTE comdevcmd[2]= {0x84, 0x00};
    WCHAR m_sCom[12] = {0};
    mbstowcs(m_sCom,comPort,strlen(comPort));

    hCom= CreateFile( m_sCom ,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);

    if (hCom==NULL || hCom==INVALID_HANDLE_VALUE)
    {
        hCom= NULL;
        return NULL;
    }

    if (!GetCommState(hCom, &dcb))
    {
        return "ERROR:GetCommState Failed";
    }

    dcb.BaudRate= CBR_115200;
    dcb.ByteSize= 8;
    dcb.fParity= false;
    dcb.StopBits= ONESTOPBIT;

    if (!SetCommState(hCom, &dcb))
    {
        return "ERROR:SetCommState Failed";
    }

    EscapeCommFunction(hCom, SETDTR);
    EscapeCommFunction(hCom, SETRTS);
    GetCommTimeouts(hCom, &to);
    //if (!EscapeCommFunction(hCom, SETDTR))
    //{
    //  return "-4";
    //}
    //if (!EscapeCommFunction(hCom, SETRTS))
    //{
    //  return "-5";
    //}  
    //if (!GetCommTimeouts(hCom, &to))
    //{
    //  return "-6";
    //}

    to.ReadIntervalTimeout= 0;
    to.ReadTotalTimeoutConstant= 200;
    to.ReadTotalTimeoutMultiplier= 0;
    to.WriteTotalTimeoutConstant= 20000;
    to.WriteTotalTimeoutMultiplier= 0;

    SetCommTimeouts(hCom, &to);
    //if (!SetCommTimeouts(hCom, &to))
    //{
    //  return "-7";
    //}

    if (!SetCommMask(hCom, EV_RXCHAR))
    {
        return "-8";
    }

    DWORD rildevresult=0,nReturned=0;

    if (!DeviceIoControl (hCom,0xAAAA5679L, comdevcmd, sizeof(comdevcmd),0,0,0,0))
    {
        return "-9";
    }

    bufpos = 0;

    strcpy(outbuf,"AT+creg=2/r");

    if (!WriteFile(hCom, outbuf, 10, &nWritten, NULL))  
    {
        return "-10";
    }

    if (nWritten != 10)
    {
        return "-11";
    }

    if (!WaitCommEvent(hCom, &event, NULL))
    {
        return "-12";
    }

    while(1)
    {
        if (!ReadFile(hCom, buf+bufpos, 256 - bufpos, &nRead, NULL))
        {
            return "-13";
        }

        if (nRead == 0)
            break;

        bufpos += nRead;

        if (bufpos >= 256)
            break;
    }

    strcpy(outbuf,"AT+creg?/r");

    if (!WriteFile(hCom, outbuf, 9, &nWritten, NULL))  
    {
        return "-14";
    }

    if (nWritten != 9)
    {
        return "-15";
    }

    if (!WaitCommEvent(hCom, &event, NULL))
    {
        return "-16";
    }

    while(1)
    {
        if (!ReadFile(hCom, buf+bufpos, 256 - bufpos, &nRead, NULL))
        {
            return "-17";
        }

        if (nRead == 0)
            break;

        bufpos += nRead;

        if (bufpos >= 256)
            break;
    }

    puts(buf);

    rildevresult = 0;
    if (!EscapeCommFunction(hCom, CLRDTR))
    {
        return "-4";
    }

    if (hCom!=NULL)
    {
        CloseHandle(hCom);
        hCom= NULL;
    }

    char* cregResponse = strpbrk( buf, "CREG/0" );

    return cregResponse;
}

void Get_Cellid(void)
{
    char m_sTemp[12] = {0};
    strcat(m_sTemp,"COM");
    for(int i = 9; i > 0; -- i)
    {
        char ch1;
        _itoa(i,&ch1,10);
        strcat(m_sTemp,&ch1);
        strcat(m_sTemp,":");

        TCREG_DATA* pData = (TCREG_DATA*)GetCREG(m_sTemp);
        if(!pData)
            continue;

        char szNum1[8] = {0};
        char szNum2[8] = {0};

        strcpy(szNum1,pData->iLac);
        strcpy(szNum2,pData->iId);

        int iLac = (int)strtol(szNum1,NULL,16);
        int iId  = (int)strtol(szNum2,NULL,16);

        TCREG_DATA m_sCell;
        if (iLac && iId)
        {
            sprintf(m_sCell.iLac,"%06d",iLac);
            sprintf(m_sCell.iId,"%06d",iId);
            break;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值