Chinaunix首页 | 论坛 | 博客
  • 博客访问: 60110
  • 博文数量: 12
  • 博客积分: 845
  • 博客等级: 军士长
  • 技术积分: 445
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-25 15:03
文章分类

全部博文(12)

文章存档

2012年(12)

我的朋友

分类: 嵌入式

2012-05-07 15:39:58

今天来使用上一次gprs AT命令来上网做一个实例

同样用的是qben M23gprs模块

 

这是在上次界面改进的,只是界面没有美观优化(界面是用QCreate绘制的)

同样gprs模块是通过串口通信的,首先设置串口

下面实现代码

首先是main.cpp文件

#include 

#include 

int main( int argc, char **argv )

{

    QApplication App( argc, argv );

    GprsClient Gprs;

    Gprs.show();

    return App.exec();

}

下面是gprsclient.h文件

#ifndef GPRSCLIENT_H

#define GPRSCLIENT_H

#include 

#include 

#include 

#include 

#include 

#include 

#include 

namespace Ui {

    class GprsClient;

}

class GprsClient : public QWidget

{

    Q_OBJECT

public:

    explicit GprsClient(QWidget *parent = 0);

    ~GprsClient();

    QTimer *timer;

    void SetSerialPort();

    void SetBaudRate();

    void SetDataBit();

    void SetStopBit();

    void SetCheckOut();

    bool setSerial(int fd,int BaudRate, int DataBit, int CheckOut, int StopBit);

    bool OpenPort();

    int SetConnectGprs(char buf_ip[]);

private slots:

    virtual void ChangeSerialPort();

    virtual void ChangeBaudRate();

    virtual void ChangeDataBit();

    virtual void ChangeStopBit();

    virtual void ChangeCheckOut();

    virtual void GprsConnect();

    virtual void SendMsg();

    virtual void RecvMsg();

private:

    Ui::GprsClient *ui;

    int SerialPort;

    int DataBit;

    int BaudRate;

    int StopBit;

    int CheckOut;

    int SerialFd;

    bool OpenFlag;

};

#endif // GPRSCLIENT_H

下面是gprsclient.cpp

#include "gprsclient.h"

#include "ui_gprsclient.h"

#include 

GprsClient::GprsClient(QWidget *parent) :

    QWidget(parent),

    ui(new Ui::GprsClient),

    SerialPort(0),

    DataBit(0),

    BaudRate(0),

    StopBit(0),

    CheckOut(0),

    SerialFd(-1),

    OpenFlag(true)

{

    QTextCodec::setCodecForTr(QTextCodec::codecForName("gbk"));

    ui->setupUi(this);

    timer = new QTimer();

    SetSerialPort();    //添加设置串口号

    SetBaudRate();      //添加设置波特率

    SetDataBit();       //添加设置数据位

    SetStopBit();       //添加设置停止位

    SetCheckOut();      //添加设置校验位

    connect(ui->SerialPortBox,SIGNAL(currentIndexChanged(int)),\

            this,SLOT(ChangeSerialPort()));

    connect(ui->BaudRateBox,SIGNAL(currentIndexChanged(int)),\

            this,SLOT(ChangeBaudRate()));

    connect(ui->DataBitBox,SIGNAL(currentIndexChanged(int)),\

            this,SLOT(ChangeDataBit()));

    connect(ui->StopBitBox,SIGNAL(currentIndexChanged(int)),\

            this,SLOT(ChangeStopBit()));

    connect(ui->CheckOutBox,SIGNAL(currentIndexChanged(int)),\

            this,SLOT(ChangeCheckOut()));

    connect(ui->ConnectServerButton,SIGNAL(clicked()),this,SLOT(GprsConnect()));

    connect(ui->SendButton, SIGNAL( clicked() ), this, SLOT( SendMsg() ));

    connect(timer,SIGNAL(timeout()),this,SLOT(RecvMsg()));

}

GprsClient::~GprsClient()

{

    delete ui;

}

void GprsClient::SetSerialPort()

{

    ui->SerialPortBox->addItem("PC_COM_0");

    ui->SerialPortBox->addItem("PC_COM_1");

    ui->SerialPortBox->addItem("ARM_COM_0");

    ui->SerialPortBox->addItem("ARM_COM_1");

    ui->SerialPortBox->addItem("ARM_COM_2");

}

void GprsClient::SetBaudRate()

{

    ui->BaudRateBox->addItem("4800");

    ui->BaudRateBox->addItem("9600");

    ui->BaudRateBox->addItem("115200");

}

void GprsClient::SetDataBit()

{

    ui->DataBitBox->addItem("7");

    ui->DataBitBox->addItem("8");

}

void GprsClient::SetStopBit()

{

    ui->StopBitBox->addItem("1");

    ui->StopBitBox->addItem("2");

}

void GprsClient::SetCheckOut()

{

    ui->CheckOutBox->addItem(tr("无校验"));

    ui->CheckOutBox->addItem(tr("偶校验"));

    ui->CheckOutBox->addItem(tr("奇校验"));

}

void GprsClient::ChangeSerialPort()

{

    /*0->pc-0,1->pc-1,2->arm-0,3->arm-1,4->arm-2*/

    SerialPort = ui->SerialPortBox->currentIndex();//取当前串口号的box下标

}

void GprsClient::ChangeBaudRate()

{

    /*0->4800,1->9600,2->115200*/

    BaudRate = ui->BaudRateBox->currentIndex();//取当前波特率的box下标

}

void GprsClient::ChangeDataBit()

{

    /*0->7bit,1->7bit*/

    DataBit = ui->DataBitBox->currentIndex();//取当前数据位的box下标

}

void GprsClient::ChangeStopBit()

{

    /*0->1bit,1->2bit*/

    StopBit = ui->StopBitBox->currentIndex();//取当前停止位的box下标

}

void GprsClient::ChangeCheckOut()

{

    /*0->无校验,1->偶校验,2->奇校验*/

    CheckOut = ui->CheckOutBox->currentIndex();//取当前校验的box下标

}

/**********************************************************************

  **function:     GprsConnect()连接gprs服务器

  **parameters:   SerialFd,BaudRate,DataBit,CheckOut,StopBit

  **return value: NUll

  **time:         2011-12-25 01:37

  **author:       tiydy

  **********************************************************************/

void GprsClient::GprsConnect()

{

    char NetRcvBuf[300];

    if( OpenFlag )

    {

        bool OpenSerialFlag;

        OpenSerialFlag = OpenPort();

        if( !OpenSerialFlag )

        {

            perror("com open error");

            return ;

        }

        OpenSerialFlag = setSerial(SerialFd, BaudRate, DataBit, CheckOut, StopBit);

        if( !OpenSerialFlag )

        {

            perror("com set error");

            return ;

        }

        if( ui->ServerIpLine->text().isEmpty() || \

                ui->PortNumberLine->text().isEmpty()||\

                ui->UserNameLine->text().isEmpty())

        {

            QMessageBox::warning(this, tr("warning"), tr("pleace input server ip!"),0,0);

            return ;

        }

        /*设置ip跟网络:AT$DESTINFO="192.168.0.1",'1',8010这里的ip和端口号是服务器的*/

        QString IpSet = "AT$DESTINFO=\""+ui->ServerIpLine->text()\

                +'"'+','+'1'+','+ui->PortNumberLine->text();

        //用命令设置gprs模块上网

        SetConnectGprs(IpSet.toAscii().data());

        //把自己的名称发送到服务器

        QString name(ui->UserNameLine->text());

        int netw = write(SerialFd,name.toAscii().constData(),name.length());

        if( (netw < 0) )

        {

            perror("write error");

            return ;

        }

        //清除串口缓存

        tcflush(SerialFd,TCIOFLUSH);

        sleep(1);

        //读取服务器返回来的信息

        int netr = read(SerialFd,NetRcvBuf,300);

        if( (netr < 0) )

        {

            perror("Read error");

            return ;

        }

        if((strstr(NetRcvBuf,"CONNECT")))

        {

            ui->StatLabel->setText(tr("已连接"));

            OpenFlag = false;

            timer->start( 1000 );

        }

        else

        {

            QMessageBox::warning(this, tr("warning"), tr("connect error plase ag"), 0, 0);

            return ;

        }

    }

    else

    {

        timer->stop();

        OpenFlag = true;

        ::close( SerialFd );

        ui->StatLabel->setText(tr("未连接"));

    }

}

/**********************************************************************

  **function:     SendMsg()发送数据

  **parameters:   void

  **return value: NUll

  **time:         2011-12-25 01:37

  **author:       tiydy

  **********************************************************************/

void GprsClient::SendMsg()

{

    if( ui->SendMsgText->toPlainText().isEmpty() )

    {

        QMessageBox::warning(this, tr("warning"), tr("msg is empty!"), 0, 0);

        return ;

    }

    //用户名

    //接收用户

    //发送信息内容

    QString str = ui->UserNameLine->text()+':'+\

                  ui->RecvNameLine->text()+':'+\

                  ui->SendMsgText->toPlainText();

    char sendBuf[300];

    strcpy(sendBuf, str.toAscii().constData() );

    ::write(SerialFd,sendBuf,300);

    ui->SendMsgText->clear();

    ui->RecvMsgList->addItem( str );

}

void GprsClient::RecvMsg()

{

    char recvBuf[200];

    int ret = read(SerialFd,recvBuf,200);

    if( ret == 0 )

    {

        perror("recv error");

        return ;

    }

    QString str( recvBuf );

    ui->RecvMsgList->addItem(str);

}

/**********************************************************************

  **function:     setSerial(int fd,int BaudRate, int DataBit, int CheckOut, int StopBit)设置串口

  **parameters:   newtio,oldtio

  **return value: if ok return true else false

  **time:         2011-12-25 01:37

  **author:       tiydy

  **********************************************************************/

bool GprsClient::setSerial(int fd,int BaudRate, int DataBit, int CheckOut, int StopBit)

{

        struct termios newtio,oldtio;

        if  ( tcgetattr( fd,&oldtio)  !=  0) {

                perror("SetupSerial 1");

                return false;

        }

        bzero( &newtio, sizeof( newtio ) );

        newtio.c_cflag  |=  CLOCAL | CREAD;

        newtio.c_cflag &= ~CSIZE;

        switch( BaudRate )

        {

                case 0://波特率为4800

                        cfsetispeed(&newtio, B4800);

                        cfsetospeed(&newtio, B4800);

                        break;

                case 1://波特率为9600

                        cfsetispeed(&newtio, B9600);

                        cfsetospeed(&newtio, B9600);

                        break;

                case 2://波特率为115200

                        cfsetispeed(&newtio, B115200);

                        cfsetospeed(&newtio, B115200);

                        break;

                default:

                        cfsetispeed(&newtio, B9600);

                        cfsetospeed(&newtio, B9600);

                        break;

        }

        switch( DataBit )

        {

                case 0://数据位为7位

                        newtio.c_cflag |= CS7;

                        break;

                case 1://数据位为8位

                        newtio.c_cflag |= CS8;

                        break;

        }

        switch( CheckOut )

        {

                case 0 ://无校验

                        newtio.c_cflag |= PARENB;

                        newtio.c_cflag |= PARODD;

                        newtio.c_iflag |= (INPCK | ISTRIP);

                        break;

                case 1 ://偶校验

                        newtio.c_iflag |= (INPCK | ISTRIP);

                        newtio.c_cflag |= PARENB;

                        newtio.c_cflag &= ~PARODD;

                        break;

                case 2 ://奇校验

                        newtio.c_cflag &= ~PARENB;

                        break;

        }

        switch( StopBit )

        {

            case 0: //一位停止位

                    newtio.c_cflag &=  ~CSTOPB;

                    break;

            case 1: //两位停止位

                    newtio.c_cflag |=  CSTOPB;

                    break;

            default : break ;

        }

        newtio.c_cc[VTIME]  = 0;

        newtio.c_cc[VMIN] = 0;

        tcflush(fd,TCIFLUSH);

        if((tcsetattr(fd,TCSANOW,&newtio))!=0)

        {

                perror("com set error");

                return false;

        }

    return true;

}

/**********************************************************************

  **function:     OpenPort()打开串口

  **parameters:   SerialPort

  **return value: if ok return true else false

  **time:         2011-12-25 01:37

  **author:       tiydy

  **********************************************************************/

bool GprsClient::OpenPort()

{

    switch( SerialPort )

    {

        case 0 :

        SerialFd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);

                break;

        case 1 :

        SerialFd = open("/dev/ttyS1",O_RDWR|O_NOCTTY|O_NDELAY);

                break;

        case 2 :

        SerialFd = open("/dev/s3c2410_serial0",O_RDWR|O_NOCTTY|O_NDELAY);

                break;

        case 3 :

        SerialFd = open("/dev/s3c2410_serial1",O_RDWR|O_NOCTTY|O_NDELAY);

                break;

        case 4 :

        SerialFd = open("/dev/s3c2410_serial2",O_RDWR|O_NOCTTY|O_NDELAY);

                break;

        default : SerialFd = -1;

                break;

    }

    if( SerialFd < 0 )

    {

        perror("com open error");

        return false;

    }

    return true;

}

int GprsClient::SetConnectGprs(char buf_ip[])

{

        char data1[50];//="AT+CGDCONT=1,\"IP\",\"CMNET\"\r";

        char data2[50];//="AT%CGPCO=1,\"PAP,,\",1\r";

        char data3[20];//="AT$NOSLEEP=1\r";

        char data4[20];//="AT+CNMI=2,1,,1\r";

        char data5[20];//="ATD*97#\r";

        int tmp=0;

         strcpy(data1,"AT+CGDCONT=1,\"IP\",\"CMNET\"\r");

         tmp=write(SerialFd,data1,strlen(data1));

         if(tmp==-1)

            return 1;

         else

            printf("cmnet successed\n");

         sleep(1);

         strcpy(data2,"AT%CGPCO=1,\"PAP,,\",1\r");

         tmp=write(SerialFd,data2,strlen(data2));

         if(tmp==-1)

            return 1;

         else

            printf("pap successed\n");

         sleep(1);

         strcpy(data3,"AT$NOSLEEP=1\r");

         tmp=write(SerialFd,data3,strlen(data3));

         if(tmp==-1)

            return 1;

         else

            printf("nosleep successed\n");

         sleep(1);

         strcpy(data4,"AT+CNMI=2,1,,1\r");

         tmp=write(SerialFd,data4,strlen(data4));

         if(tmp==-1)

            return 1;

         else

            printf("sms successed\n");

         sleep(1);

         tmp=strlen(buf_ip);

         buf_ip[tmp]='\r';

         tmp=write(SerialFd,buf_ip,strlen(buf_ip));

         if(tmp==-1)

            return 1;

         else

            printf("IP PORT successed\n");

         sleep(1);

         strcpy(data5,"ATD*97#\r");

         tmp=write(SerialFd,data5,strlen(data5));

         if(tmp==-1)

            return 1;

         else

            printf("*97# successed\n");

         sleep(1);

         return 0;

}

下面是工程文件GprsClient.pro

HEADERS += \

    gprsclient.h 

SOURCES += \

    gprsclient.cpp \

    main.cpp 

FORMS += \

gprsclient.ui               

用QCreate制作一个gprsclient.ui的界面文件(qt4平台)

把上述文件放到Liunx下的一个目录下设置好qt4的平台,再执行qmake,make就ok运行了

具体说明可以看下上期文档和这里代码注释

 

了解更多请关注:
粤嵌教育:

粤嵌新浪微博:

粤嵌腾讯微博:

阅读(1484) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~