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

📄 3gtestrun.c

📁 3G module test program
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************
Copyright (C), 2007, Compal Electronics Co., Ltd.
All rights reserved.

$Workfile: 3GTestRun.c $
$Revision: 1.1 $
$Author: kenkc_chen $
$Date: 2007/12/17 06:23:13 $

Description: 3G test program. Use 3 AT-command: "AT+CGMI","AT+CPIN?"
            "AT+COPS?"

$Log: 
**************************************************************/

//===================================================
// head files
//===================================================
#include "3GTest.h"
#include "dtmux_common.h"

//===================================================
// Global variables 
//=================================================== 

extern int gFilePointer; // Descriptor of file
int gComFp;              // pointer of device file
//char msg[BUFFER_LEN];const unsigned char port_call = 1;


//===================================================
// Functions
//===================================================
/*****************************************
Function:     DtOpenCom
Description:  Open a special port for datang module
Parameter: 
    	port: port number
Return:
    SUCCEED: succeed to open files
      ERROR: failed to open files
Remark:
******************************************/
int DtOpenCom(unsigned char port) 
{  
    if(dtmux_init(DT_DEVICE_NAME, NULL) != SUCCEED)    {        printf("Can't init 3G device.\n");        return ERROR;    }    
    // Try to open a device port
    if (dtmux_port_open(port) != SUCCEED)    {		printf("Can't open 3G port %d.\n", port);
		return ERROR;	}	   
    return SUCCEED;  
}  

/*****************************************
Function:     DtReadCom
Description:  read data from datang 3G device
Parameter: 
     buffer[input]: buffer of data read from device
    readLen[input]: length of data want to read
Return:
    SUCCEED: read succeed
      ERROR: read failed
Remark:
******************************************/
int DtReadCom(unsigned char port, unsigned char *buffer, int readLen)  
{  
    unsigned int  timeout; //  timeout of ms
    int real_ReadLen; // length of data really read from device
    
    timeout =10000; // set time for timeout (ms)
    
   real_ReadLen = dtmux_port_read( port, buffer ,readLen, timeout);

    if (real_ReadLen > ZERO_LEN)
    {
        return SUCCEED;
    }
    else
    {
        return ERROR; // Read nothing
    }
}

/*****************************************
Function:     DtWriteCom
Description:  Write data to datang 3G device
Parameter: 
    buffer[input]: buffer of data want to write
Return:
    SUCCEED: write succeed
      ERROR: write failed
Remark:
******************************************/
int DtWriteCom(unsigned char port, unsigned char *buffer)  
{  
    if (strlen(buffer) <= ZERO_LEN)
    {
        printf("String is empty!\n");
        return ERROR;
    }
    int writeLen;
    writeLen = dtmux_port_write(port, buffer, strlen(buffer));  
    if (writeLen <= ZERO_LEN)  
    {
        printf("Not all chars were sended\n");
        return ERROR;
    }
    return SUCCEED;
}

/*****************************************
Function:     DtSendAndRev
Description:  send command then reveive data for Datang module
Parameter: No    
Return:
    SUCCEED: test succeed
      ERROR: test failed
Remark:
******************************************/int DtSendAndRev(unsigned char *cmd, unsigned char *rev)
{
    // Send AT command to device    if (DtWriteCom(port_call, cmd) != SUCCEED)    {        printf("wirte failed!\n"); // read nothing 
        return(ERROR);    }
 
    usleep(DELAY_MS_TIME);
    if (DtReadCom(port_call, rev, BUFFER_LEN) != SUCCEED) // read from device
    {
        printf("read failed!\n"); // read nothing 
        return(ERROR);
    }    printf("%s\n",rev);    return(SUCCEED);
}/*****************************************
Function:     OpenCom
Description:  Open a device file
Parameter: 
    deviceName[input]: Path of device file
Return:
    SUCCEED: succeed to open files
      ERROR: failed to open files
Remark:
******************************************/
int OpenCom(const char *deviceName) 
{  
    struct termios newTio; // Attributions of terminal 
    
    // Try to open a device file
    if ((gComFp = open(deviceName, O_RDWR|O_EXCL|O_NONBLOCK|O_NOCTTY)) == FAIL)
    { 
        printf("Can't open device %s.\n", deviceName);
        return ERROR;
    }
   
    // Get the default attributions
    tcgetattr(gComFp, &newTio);    
    bzero(&newTio, sizeof(newTio));  
    
    // Setting control mode flag  
    newTio.c_cflag |= CREAD;
    newTio.c_cflag &= ~CLOCAL;  
    newTio.c_cflag &= ~PARENB;
    newTio.c_cflag &= ~PARODD;  
    newTio.c_cflag &= ~CSTOPB;  
    newTio.c_cflag &= ~CSIZE;    
    newTio.c_cflag |= CS8;  
    newTio.c_oflag |= OPOST;  
  
    // Setting input, output and local mode flag  
    newTio.c_iflag &= ~(IGNCR | ICRNL | IUCLC | INPCK | IXON | IXANY | IGNPAR );
    newTio.c_oflag &= ~(OPOST | OLCUC | OCRNL | ONLCR | ONLRET);
    newTio.c_lflag &= ~(ICANON | XCASE | ECHO | ECHOE | ECHONL);
    newTio.c_lflag &= ~(ECHO | ECHOE);

    // Set timeout, but it's said no use.
    newTio.c_cc[VMIN] = 1;  // Do not update the options and do it
    newTio.c_cc[VTIME] = 0; // Timeout 0 seconds
    newTio.c_cc[VEOF] = 1;

    // Set baudrate    if ( strcmp(deviceName,VI_DEVICE_NAME) == 0 )    {        cfsetispeed(&newTio, BAUDRATE_2);          cfsetospeed(&newTio, BAUDRATE_2);    }        else    {
        cfsetispeed(&newTio, BAUDRATE_1);  
        cfsetospeed(&newTio, BAUDRATE_1);
    }
    // Enable the setting
    tcflush(gComFp, TCIFLUSH);

    // Set attributions    
    if (tcsetattr(gComFp, TCSANOW, &newTio) != SUCCEED)
    {
        printf("Setup Serial error!\n");
        return ERROR;   
    }    
   
    return SUCCEED;  
}  

/*****************************************
Function:     ReadCom
Description:  read data from device
Parameter: 
     buffer[input]: buffer of data read from device
    readLen[input]: length of data want to read
Return:
    SUCCEED: read succeed
      ERROR: read failed
Remark:
******************************************/
int ReadCom(unsigned char *buffer, int readLen)  
{  
    fd_set readSet;
    struct timeval timeout; // strut of timeout
    int realReadLen; // length of data really read from device
    int ret; // value for return from select() 
    
    timeout.tv_sec = ZERO_LEN; // set time for timeout (s)
    timeout.tv_usec = DELAY_MS_TIME; // set time for timeout (us)
    
    // set block for reading 
    FD_ZERO(&readSet);
    FD_SET(gComFp, &readSet);

    // wait for ready state for reading
    ret=select(gComFp+1, &readSet, NULL, NULL, &timeout);
    if (ret)
    {
        // read data from device
        realReadLen = read(gComFp, buffer, readLen);
        if (realReadLen > ZERO_LEN)
        {  
            buffer[realReadLen+1] = '\0';
            return SUCCEED;
        }
        else
        {
            return ERROR; // Read nothing
        }
    }
    else 
    {
        return ERROR; // Not ready for reading after timeout
    }
}

/*****************************************
Function:     WriteCom
Description:  Write data to device
Parameter: 
    buffer[input]: buffer of data want to write
Return:
    SUCCEED: write succeed
      ERROR: write failed
Remark:
******************************************/
int WriteCom(const unsigned char *buffer)  
{  
    if (strlen(buffer) <= ZERO_LEN)
    {
        printf("String is empty!\n");
        return ERROR;
    }
    int writeLen;
    writeLen = write(gComFp, buffer, strlen(buffer));  
    if (writeLen <= ZERO_LEN)  
    {
        printf("Not all chars were sended\n");
        return ERROR;
    }
    return SUCCEED;
}

/*****************************************
Function:     SendAndRev
Description:  send command then reveive data
Parameter: No    
Return:
    SUCCEED: test succeed
      ERROR: test failed
Remark:
******************************************/
int SendAndRev(const unsigned char *cmd, unsigned char *rev)
{
    // Send AT command to device    if (WriteCom(cmd) != SUCCEED)    {        printf("write failed!\n"); // read nothing 
        return(ERROR);    }
       
    usleep(DELAY_MS_TIME);

    if (ReadCom(rev, BUFFER_LEN) != SUCCEED) // read from device
    {
        printf("read failed!\n"); // read nothing 
        return(ERROR);
    }   
  
    return(SUCCEED);
}

/*****************************************
Function:     ModuleDetect
Description:  3G module detect
Parameter: No    
Return:
    SUCCEED: test succeed
      ERROR: test failed
Remark:
******************************************/
int ModuleDetect(int modDec)
{
    unsigned char buffer[BUFFER_LEN]; // buffer for read
    unsigned char commandStr[]="AT+CGMI\r";    if( DATANG == modDec)    {        if (DtSendAndRev(commandStr, buffer) == ERROR)
        {
            return(ERROR);
        }        if (strstr(buffer, "OK") == NULL || strstr(buffer, "DTMobile") == NULL)        {
            printf("3G module detect failed!\n"); // return error
            return(ERROR);

⌨️ 快捷键说明

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