通过串口操作GPRS模块的编程

/* ********************************************************************************
 * File  : gprs_test.c
 * Author : YangShuo
 * Data  : 2009-10-14
 * Function : this is a simple test of sending a short message via GPRS module
 * *******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <sys/times.h>
#include <errno.h>
#include <termios.h>
#include <iconv.h>
#define TTY_NAME  "/dev/ttyS0"
#define BUF_SIZE 1024
#define CMD_CMGF "AT+CMGF=1\r"
#define CMD_CMGS "AT+CMGS=\"+8615060136081\"\r"
#define DEFAULT_MSG "Welcome To Ubuntu9.04!\x1a"
static volatile int tty_fd; //file descriptor of serial port
/******************************************
 * Function  : set the parameter of UART
 * return -1 : Failed
 * return 0  : Success
******************************************/
int set_tty_option(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
 struct termios new_tio, old_tio;
 if (tcgetattr(fd, &old_tio) != 0) //save the old parameter
 {
  perror("Setup Serial 1");
  return -1;
 }
 
 bzero(&new_tio, sizeof(new_tio));
 new_tio.c_cflag |= CLOCAL | CREAD; //set local mode and enable recevie function
 new_tio.c_cflag &= ~CSIZE;  //mask the character size bits
 switch (nBits)
 {
  case 7:  new_tio.c_cflag |= CS7; break; //data: 7bits
  case 8:  new_tio.c_cflag |= CS8; break; //data: 8bits
  default: break;
 }
 switch (nEvent)
 {
  case 'O':  //奇校验
   new_tio.c_cflag |= PARENB;
   new_tio.c_cflag |= PARODD;
   new_tio.c_iflag |= (INPCK | ISTRIP);
   break;
  case 'E':  //偶校验
   new_tio.c_iflag |= (INPCK | ISTRIP);
   new_tio.c_cflag |= PARENB;
   new_tio.c_cflag &= ~PARODD;
   break;
  case 'N':  //无校验
   new_tio.c_cflag &= ~PARENB;
   break;
 }
 switch (nSpeed)  //set the Baudary
 {
  case 2400:
   cfsetispeed(&new_tio, B2400);
   cfsetospeed(&new_tio, B2400);
   break;
  case 4800:
   cfsetispeed(&new_tio, B4800);
   cfsetospeed(&new_tio, B4800);
   break;
  case 9600:
   cfsetispeed(&new_tio, B9600);
   cfsetospeed(&new_tio, B9600);
   break;
  case 115200:
   cfsetispeed(&new_tio, B115200);
   cfsetospeed(&new_tio, B115200);
   break;
  case 460800:
   cfsetispeed(&new_tio, B460800);
   cfsetospeed(&new_tio, B460800);
   break;
  default:
   cfsetispeed(&new_tio, B9600);
   cfsetospeed(&new_tio, B9600);
   break;
 }
 if (nStop == 1)  //set the one bit stop
 {
  new_tio.c_cflag &= ~CSTOPB;
 }
 else if (nStop == 2)
 {
  new_tio.c_cflag |= CSTOPB;
 }
 new_tio.c_cc[VTIME] = 0;
 new_tio.c_cc[VMIN] = 0;
 tcflush(fd, TCIFLUSH);  //refresh received data buf don't read them
 if (tcsetattr(fd, TCSANOW, &new_tio) != 0)
 {
  perror("serial port set error.");
  return -1;
 }
 printf("serial port set done!\n");
 return 0;
}
/***********************************************
 * Function : intialize serial port
 * Return -1 : Failed
 * Return 0 : Success
***********************************************/
int init_tty(void)
{
 if ((tty_fd = open(TTY_NAME, O_RDWR)) == -1)
 {
  perror("open serial port failed.");
  return -1;
 } 
 set_tty_option(tty_fd, 115200, 8, 'N', 1);
}
/***********************************************
 * Function : intialize serial port
 * Return -1 : Failed
 * Return 0 : Success
***********************************************/
int send_message(const char *text)
{
 char buf_cmd[BUF_SIZE] = {0};
 char buf_rcv[BUF_SIZE] = {0};
 int nread;
 init_tty();
 write(tty_fd, CMD_CMGF, strlen(CMD_CMGF));
 sleep(1);
 nread = read(tty_fd, buf_rcv, BUF_SIZE);
 sleep(1);
 printf("nread = %d,CMD_CMGF rcv: %s\n", nread, buf_rcv);
#if 1
 write(tty_fd, CMD_CMGS, strlen(CMD_CMGS));
 sleep(1);
 read(tty_fd, buf_rcv, BUF_SIZE);
 sleep(1);
 printf("CMD_CMGS rcv: %s\n", buf_rcv);
 write(tty_fd, DEFAULT_MSG, strlen(DEFAULT_MSG));
 sleep(2);
 printf("finish sending message.\n");
#endif
 close(tty_fd);
 return 0;
}
int main()
{
 send_message("hello world!");
 return 0;
}

部分源程序如下: #define TARGET_GLOBAL 1 #include "target.h" /***************************************************************************************** *函数名称:usart0_Initial(void) *函数功能:初始化串口 *入口函数:无 *出口函数:无 *****************************************************************************************/ void usart0_Initial(void) { /************TH1=256-(K*oscFreq)/384*BaudRate************/ SCON=0x50; //SM0=0;SM1=1;REN=1; TI=0; //数据发送状态清零 RI=0; //数据接收状态清零 PCON=0; //不加倍 TH1=0xF3; //bode=2400 TL1=0XF3; TMOD=0X20; //TIMER1 MODER2; ET1=0; //屏蔽timer1溢出中断 TR1=1; //启动timer1 ES=0; //屏蔽串口中断 } /***************************************************************************************** *函数名称:send_char(uchar txd) *函数功能:串口发送一个字符 *入口函数:要发送的一个字符txd *出口函数:无 *****************************************************************************************/ void send_char(uchar txd) { TI=0; SBUF=txd; while(!TI); // 等特数据传送 TI=0; // 清除数据传送标志 } /***************************************************************************************** *函数名称:send_str(uchar *str,uchar len) *函数功能:串口发送一行字符串 *函数入口:字符串指针*str,字符串长度len *函数出口:无 *****************************************************************************************/ void send_str(uchar *str,uchar len) { uchar i; for(i=0;i<len;i++) send_char(str[i]); } /***************************************************************************************** *函数名称:get_char(void) *函数功能:串口接收一个字符 *函数入口:串口发送的字符rxd *函数出口:无 *****************************************************************************************/ uchar get_char(void) { uchar temp; RI=0; while(!RI); // 等特数据传送 temp=SBUF; RI=0; // 清除数据传送标志 return temp; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值