/* ********************************************************************************
* File : gprs_test.c
* Author : YangShuo
* Data : 2009-10-14
* Function : this is a simple test of sending a short message via GPRS module
* *******************************************************************************/
* 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>
#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 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"
#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;
* 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
{
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;
}
{
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;
}
{
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;
}
{
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_cflag &= ~CSTOPB;
}
else if (nStop == 2)
{
new_tio.c_cflag |= CSTOPB;
}
new_tio.c_cc[VTIME] = 0;
new_tio.c_cc[VMIN] = 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;
}
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;
}
* 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};
* 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();
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);
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, 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);
sleep(2);
printf("finish sending message.\n");
#endif
close(tty_fd);
#endif
close(tty_fd);
return 0;
}
}
int main()
{
send_message("hello world!");
{
send_message("hello world!");
return 0;
}
}