Linux下的gprs无线上网笔记整理

来源:互联网 发布:手机淘宝怎么贷款 编辑:程序博客网 时间:2024/06/02 17:37

本文是Linux操作系统下的GPRS模块实现无线上网的功能,代码及方法很难懂所以要耐心的读代码,相信你能够学到很多。

 

linux下通过gprs模块拨号上网

首先从/usr/share/doc/ppp-2.4.4/scripts中cp ppp-on,ppp-off,ppp-on-dialer三个脚本到/home/a/ppp。然后做如下修改:

在ppp-on里

1.改电话号码为*99***1#

2.将账号与密码清除

3.修改DIALER_SCRIPT的路径为/home/a/ppp/ppp-on-dialer

4.把下设备改成/dev/ttyS0,速率改为115200

5.将crtscts参数去掉,

在ppp-on-dialer里把帐号密码那块去掉。

下面是对脚本的一些说明。当然都是网上搜来的了。:-)

/home/a/ppp/ppp-on

#!/bin/sh

#

# Script to initiate a ppp connection. This is the first part of the

# pair of scripts. This is not a secure pair of scripts as the codes

# are visible with the 'ps' command. However, it is simple.

#

# These are the parameters. Change as needed.

TELEPHONE=*99***1# # The telephone number for the connection

ACCOUNT= # The account name for logon (as in 'George Burns')

PASSWORD= # The password for this account (and 'Gracie Allen')

LOCAL_IP=0.0.0.0 # Local IP address if known. Dynamic = 0.0.0.0

REMOTE_IP=0.0.0.0 # Remote IP address if desired. Normally 0.0.0.0

NETMASK=255.255.255.0 # The proper netmask if needed

#

# Export them so that they will be available at 'ppp-on-dialer' time.

export TELEPHONE ACCOUNT PASSWORD

#

# This is the location of the script which dials the phone and logs

# in. Please use the absolute file name as the $PATH variable is not

# used on the connect option. (To do so on a 'root' account would be

# a security hole so don't ask.)

#

DIALER_SCRIPT=/home/a/ppp/ppp-on-dialer

#

# Initiate the connection

#

# I put most of the common options on this command. Please, don't

# forget the 'lock' option or some programs such as mgetty will not

# work. The asyncmap and escape will permit the PPP link to work with

# a telnet or rlogin connection. You are welcome to make any changes

# as desired. Don't use the 'defaultroute' option if you currently

# have a default route to an ethernet gateway.

#

exec /usr/sbin/pppd debug lock modem /dev/ttyS0 115200 \

asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP \

noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT

exec /usr/sbin/pppd debug lock modem /dev/ttyS0 115200 \

asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP \

noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT这个命令的作用是:(可以使用man pppd查看帮助)

运行/usr/sbin/pppd程序,给它传入命令行参数的意义是:

/dev/ttyS0: 拨号所用的MODEM所连接的串口(ttyS0是串口1,ttyS1是串口2...)

115200 : MODEM的通信速率(57600 BPS)

modem : 用MODEM进行拨号。

nocrtscts : 无流控

lock : 锁定这个设备

debug :输出调试信息

defaultroute : 此拨号连接作为默认路由

connect /home/a/ppp/ppp-on-dialer:使用/home/a/ppp/ppp-on-dialer进行拨号

/home/a/ppp/ppp-on-dialer

#!/bin/sh

#

# This is part 2 of the ppp-on script. It will perform the connection

# protocol for the desired connection.

#

exec chat -v \

TIMEOUT 3 \

ABORT '\nBUSY\r' \

ABORT '\nNO ANSWER\r' \

ABORT '\nRINGING\r\n\r\nRINGING\r' \

'' \rAT \

'OK-+++\c-OK' ATH0 \

TIMEOUT 30 \

OK AT+IPR=115200 \

OK AT+CGDCONT=1,"IP","CMNET" \

OK AT+CGACT=1,1 \

OK ATDT*99***1# \

CONNECT '' \

# ogin:--ogin: $ACCOUNT \

# assword: $PASSWORD

chat命令的帮助请参考man -a chat查看。

ABORT BUSY

ABORT ERROR

ABORT NO CARRIER

ABORT NO DIALTONE

ABORT RING

OK AT+IPR=115200

OK AT+CGACT=1,1

/home/a/ppp/ppp-off

#!/bin/sh

##############################################

# Determine the device to be terminated.

#

if [ "$1" = "" ]; then

DEVICE=ppp0

else

DEVICE=$1

fi

###############################################

# If the ppp0 pid file is present then the program is #running. Stop it.

if [ -r /var/run/$DEVICE.pid ]; then

kill -INT `cat /var/run/$DEVICE.pid`

#

# If the kill did not work then there is no process running for this

# pid. It may also mean that the lock file will be left. You may wish

# to delete the lock file at the same time.

if [ ! "$?" = "0" ]; then

rm -f /var/run/$DEVICE.pid

echo "ERROR: Removed stale pid file"

exit 1

fi

#

# Success. Let pppd clean up its own junk.

echo "PPP link to $DEVICE terminated."

exit 0

fi

#

# The ppp process is not running for ppp0

echo "ERROR: PPP link is not active on $DEVICE"

exit 1

 

 

 

int main(void)
{

u8_t buf[MAX_READ_LEN + 1];
int len,pd;
sys_err err;

int * fd = NULL;
int *linkstateCx = NULL;

/*
*init basic parametes of ourselves
*/
GPIOInit(); //GPIO
口初始化
sys_timer_init(); //系统时钟初始化
local_sys_init(); //这里面有串口的初始化,定时器的初始化等
lwip_init(); //lwip协议的初始化

DEBUG_EVENT("main()\r\n"); //DEBUG_EVENT是一个向串口输出提示信息的函数

/*
*start config GPRS Modem, such as HUAWEI GTM900, MC35i,etc.
*/
err = active_Modem(); //
激活GPRS模块

if(err NEQ SYS_ERR_OK)
{
DEBUG_EVENT("Modem open failed! we must shut down system!\r\n");
shut_system();
}
DEBUG_EVENT("Modem open successful!\r\n");
DEBUG_EVENT("System block 30 seconds!\r\n");//
等待30秒,等待SIM卡找到网络

/*
*wait for GPRS Modem serching network,
* initinalize: such as SMS, Telephone Book etc.
*/
second_sleep(30);

err = init_Modem(); //
写一些基本的AT命令
if(err NEQ SYS_ERR_OK)
{
DEBUG_EVENT("Modem basic AT Commands initialize failed! we must shut down system\r\n");
shut_system();
}
DEBUG_EVENT("Modem basic AT Commands initialize successful!\r\n");

/*
*start Dial GPRS Number, such as: *99***1#
*/
start_tcpip: //start_tcpip
是一个标号
err = dial_GPRS(); //
ATD*99***1#

if(err NEQ SYS_ERR_OK)
{
DEBUG_EVENT("Dial GPRS Failed,we must shut down system!\r\n");
shut_system();
}
DEBUG_EVENT("Dial GPRS successful, now we start PPP Config!\r\n");

/*
*start config PPP,and send LCP packet to GPRS Modem
*/
pd = pppOpen((void*)fd, &pppStateCallback, (void *)linkstateCx);/*send lcp req to peer*/

while(1)
{
/*
*check if one timer is overflow, if so, run it
*/
check_sys_timer();

/*
*check the receive buffer of Modem
*/
if(get_modem_datalen() > 0)//
如果modem收到信息
{
DEBUG_EVENT(" Receive some data from Modem:\r\n");
len = get_modem_data(buf); //
接收下来
pppInProc(pd, buf, len); //进行处理
}

/*
*check the receive buffer of User Equipment
*/
if(get_user_datalen() > 0) //
如果用户向modem发信息
{
DEBUG_EVENT("Receive some data from User Eqipment!\r\n");

len = get_user_data(buf);//
接收下来

if(get_Mytcpip_state() EQ UDP_ESTABLISHED)//判断存在的是哪种连接
{
if( udp_write(buf, len) NEQ ERR_OK)
{
DEBUG_ERR("Send some data to UDP Peer, but failed!\r\n");
//add you code here.
//store the data
}
}
else if(get_Mytcpip_state() EQ TCP_ESTABLISHED))//
判断存在的是哪种连接

{
if( tcp_write(tcp_active_pcbs, (const void *)buf, len, 1) NEQ ERR_OK)
{
DEBUG_ERR("Send some data to TCP Peer, but failed!\r\n");
//add you code here.
//store the data
}
}
else
{
DEBUG_EVENT("No TCP or UDP Connect exist!drop the data\r\n");
}
}

/*
*check the state of ppp
*if the link is DEAD, I deal it very simple,
*do not release the source of PPP/TCP/IP/UDP etc.
*just call the function: lwip_init(). but it work.
*then goto the head of main func.
*The state of DCD should be checked here,but I will ignore it.
*/
if(ppp_dead(pd) EQ TRUE)//
如果链路死去
{
lwip_init();
set_Mytcpip_state(PPP_DEAD);
goto start_tcpip; //
回去重新拨号
}

/*
*check the state of my tcpip
*/
switch(get_Mytcpip_state())//
检查目前的状态
{
case PPP_ESTABLISHED: //
这个是刚握完手的状态
if(get_sys_err() < MAX_SYS_ERR_ALLOW)
{
tcpip_open(); //
所以握完手之后要打开tcp_ip(这个函数里可以选择是用udp还是tcp,我选的
udp)
break;
}
else
set_Mytcpip_state(TCPIP_STATE_UNKNOWN);

case TCPIP_STATE_UNKNOWN:
shut_system();
break;

default:
break;
}

}
}

原创粉丝点击