移远M26通信模块TCP连接服务器的C语言设计

C语言AT指令数据结构体设计

typedef struct
{
    uint16 try_cnt;         		/**< 最大尝试次数 */
    uint16 timeout;        		   /**<延时时间ms */
    const char* patc;			   /**

AT指令:

static const wlm_routine_t the_wlm_routine[] =
{
    //初始化
    {1, 1500,  NULL, NULL, NULL, NULL},     //延时1.5秒
    //软复位
    {3, 5000,  "AT+CFUN=1,1\r\n", "OK", NULL, NULL},
    {1, 2000,  NULL, NULL, NULL, NULL},     //延时2秒
    {10, 1500,  "AT\r\n", "OK", NULL, NULL},
    {3, 5000,  "ATE0\r\n", "OK", NULL, NULL},
    {1, 1500,  NULL, NULL, NULL, NULL},     //延时1.5秒

    //网络注册
    {4, 6000,  "AT+CPIN?\r\n", "READY\r\n\r\nOK", NULL, NULL},
    {3, 10000, "AT+CREG?\r\n", "OK", NULL, NULL},
    {1, 1000,  NULL, NULL, NULL, NULL},    //延时1秒
    //{3, 6000,  "AT+CSQ\r\n", "OK", NULL, NULL},
    
    //TCPIP配置
    {3, 3000,  "AT+QIFGCNT=0\r\n", "OK", NULL, NULL},
    //使用移动或联通的SIM卡建立TCP连接, 此命令可以省掉
    {3, 6000,  "AT+QICSGP=1,\"CMNET\"\r\n", "OK", NULL, NULL},
    {3, 5000,  "AT+QIHEAD=0\r\n", "OK", NULL, NULL},
    {3, 3000,  "AT+QINDI=1\r\n", "OK", NULL, NULL},
    {3, 5000,  "AT+QITCFG=3,2,512,1\r\n", "OK", NULL, NULL},

    //TCP 连接建立
    {1, 5000,  "AT+QICLOSE\r\n", NULL, NULL, NULL}, //断开连接, 不判断返回值"OK"
    {2, 12000, "AT+QIDEACT\r\n", "DEACT OK", NULL, NULL}, // IP INITIAL
    {3, 6000,  "AT+QIREGAPP\r\n", "OK", NULL, NULL}, // IP START
    {2, 15000, "AT+QIACT\r\n","OK", NULL, NULL}, // IP GPRSACT
    
    // TCP连接
    {2, 12000, "AT+QIOPEN=\"TCP\",\"47.100.192.207\",\"12000\"\r\n", "CONNECT OK", NULL, NULL}
};

TCP连接服务器代码

typedef enum
{
    WLM_CMD_IDLE = 0,
    WLM_CMD_RUN,
    WLM_CMD_WAIT,
    WLM_CMD_DONE,
} wlm_cmd_e;

/*读GPRS 通道数据*/
int wlm_read(void);

status_t 
wlm_linkup(uint8 cmd)
{
    const wlm_routine_t* pwlm = NULL;

    if (wlm_link_state >= ARRAY_SIZE(the_wlm_routine))
    {
        return OK;
    }
    
    pwlm = &the_wlm_routine[wlm_link_state];
    
    /*读GPRS 通道数据*/
    wlm_read();

    if (memstr(wlm_buf, wlm_len, pwlm->pecho)) //比较字符串,判断是否收到正确数据
    {
        printf("wlm echo: %s\r\n", wlm_buf);
        the_wlm_cmd = WLM_CMD_IDLE;
        wlm_link_state ++;
        if (wlm_link_state >= ARRAY_SIZE(the_wlm_routine))
        {
        }
        return ER;
    }

    switch (the_wlm_cmd)
    {
        case WLM_CMD_IDLE:
            wlm_cnt = 0;
            wlm_time = 0;
            the_wlm_cmd = WLM_CMD_RUN;
            break;
            
        case WLM_CMD_RUN:
            if (pwlm->patc)
            {
                //发送数据
                UART_Write(WLM_UART, (uint8 *)pwlm->patc, strlen(pwlm->patc));
                printf("wlm atc:[%d] %s", wlm_cnt + 1, pwlm->patc);
            }
            else
            {
                printf("wlm atc: delay\r\n");
            }
            wlm_cnt ++;
            wlm_time = Tick_Get();
            the_wlm_cmd = WLM_CMD_WAIT;
            break;

        case WLM_CMD_WAIT:
            if (Tick_Get() < (wlm_time + MS2TICK(pwlm->timeout)))
	        {//未超时
	            break;
	        }

            if (!pwlm->pecho)
            {
                the_wlm_cmd = WLM_CMD_IDLE;
                wlm_link_state ++;
                break;
            }

            if (wlm_cnt >= pwlm->try_cnt) //超出最大次数
            {
                //复位
                break;
            }

            the_wlm_cmd = WLM_CMD_RUN;
            break;

        case WLM_CMD_DONE:
            break;
            
        default:
            the_wlm_cmd = WLM_CMD_IDLE;
            break;
    }

    return ER;
}

你可能感兴趣的:(通信模块)