#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#include <sys/timeb.h>
#include <sys/io.h>
#include <sys/time.h>
#include <fcntl.h>
#include <asm/ioctls.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#define PIN_BASE 32
#define AT91_PIN_PA(n) (PIN_BASE + 0x00 + n)
#define AT91_PIN_PB(n) (PIN_BASE + 0x20 + n)
#define AT91_PIN_PC(n) (PIN_BASE + 0x40 + n)
#define AT91_PIN_PD(n) (PIN_BASE + 0x60 + n)
#define AT91_PIN_PE(n) (PIN_BASE + 0x80 + n)
// 9g25 new driver
#define CMD_INIT_OUT 0xC00BE01
#define CMD_INIT_IN 0xC00BE02
#define CMD_SET_VAL 0xC00BE03
#define CMD_GET_VAL 0xC00BE04
#define TRUE 0
#define FALSE -1
#define SPEED_NUM 4
#define BAUDRATE 115200
int fd_gpio;
int fd;
int speed_arr[SPEED_NUM][2] = {
{9600,B9600},
{38400,B38400},
{19200,B19200},
{115200,B115200}};
int set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < SPEED_NUM; i++){
if (speed == speed_arr[i][0]){
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i][1]);
cfsetospeed(&Opt, speed_arr[i][1]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0){
perror("tcsetattr fd");
return FALSE;
}
tcflush(fd,TCIOFLUSH);
return TRUE;
}
}
return FALSE;
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0){
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits){
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return (FALSE);
}
switch (parity){
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}
switch (stopbits){
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
options.c_iflag &= ~(IXON | IXOFF | IXANY);
options.c_iflag &= ~(INLCR | IGNCR | ICRNL);
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 100; // set timeouts 0.5 seconds
options.c_cc[VMIN] = 0; //Update the options and do it NOW
options.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
options.c_oflag &= ~OPOST;
options.c_cflag |= (CLOCAL | CREAD);// | CRTSCTS);
tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0){
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
int OpenDev(char *Dev)
{
int fd = open(Dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (-1 == fd){
perror("Can't Open Serial Port");
return -1;
}
return fd;
}
int WriteBuf(int fd,char buf[],int num)
{
int count;
count = write(fd,buf,num);
return count;
}
int init(void)
{
if(!(fd = open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NONBLOCK)))
{
fprintf(stderr, " /dev/ttyS0 : %s\n", strerror(errno));
return -1;
}
if(fd>0){
if(set_speed(fd,BAUDRATE) == TRUE){
printf("(port%c)speed = %d\n", 'A',BAUDRATE);
}
else{
printf("(port%c) set_speed error\n",'A');
}
}
else{
printf("Can't Open Serial Port!\n");
exit(0);
}
if (set_Parity(fd,8,1,'N')== FALSE){
printf("Set Parity Error\n");
exit(1);
}
return 0;
}
int main(int argc, char **argv)
{
unsigned int i,j;
char *sendData = "AT\r";
char rcvdata[128] = {};
int ret;
init();
fd_gpio = open("/dev/gpio", O_RDWR | O_NONBLOCK);
if ( fd_gpio < 0 )
{
perror("open /dev/gpio error\n");
return -1;
}
ioctl(fd_gpio,CMD_INIT_OUT,(AT91_PIN_PA(4)<<1)|0); //POW
ioctl(fd_gpio,CMD_INIT_OUT,(AT91_PIN_PA(28)<<1)|1); //ONOFF
ioctl(fd_gpio,CMD_INIT_OUT,(AT91_PIN_PA(20)<<1)|1); //RST
ioctl(fd_gpio,CMD_SET_VAL,(AT91_PIN_PA(4)<<1)|0);
ioctl(fd_gpio,CMD_SET_VAL,(AT91_PIN_PA(28)<<1)|1);
ioctl(fd_gpio,CMD_SET_VAL,(AT91_PIN_PA(20)<<1)|1);
ioctl(fd_gpio,CMD_INIT_IN,(AT91_PIN_PA(18)<<1)|0);
ioctl(fd_gpio,CMD_INIT_IN,(AT91_PIN_PA(16)<<1)|0);
printf(" gprs power on!! \r\n");
ioctl(fd_gpio,CMD_SET_VAL,(AT91_PIN_PA(4)<<1)|1);
usleep(200000);
ioctl(fd_gpio,CMD_SET_VAL,(AT91_PIN_PA(28)<<1)|0);
sleep(1);
ioctl(fd_gpio,CMD_SET_VAL,(AT91_PIN_PA(28)<<1)|1);
sleep(5);
printf("sleep........................\n");
printf("start test gprs\n");
while(1)
{
ret = WriteBuf(fd,sendData,sizeof(sendData));
if(ret > 0)
{
printf("gprs sendData :%s\n",sendData);
}
for(i=0;i<10;i++)
{
sleep(2);
ret = read(fd,rcvdata,6);
if(ret > 0)
{
printf("gprs recvData :%s\n",rcvdata);
}
}
sleep(1);
}
close(fd);
return 0;
}
linux下 串口与gprs模块通信
需积分: 12 124 浏览量
2018-02-23
17:06:13
上传
评论
收藏 7KB ZIP 举报
greengate0510
- 粉丝: 1
- 资源: 3
最新资源
- VoNR应答掉话率分析处理案例.docx
- 基于matlab实现数字图像相关,该程序试图通过比较变形前和变形后的两张图像来计算应力和应变GUI.rar
- 基于matlab实现数字基带传输系统的matlab仿真源码及附属文件.rar
- 基于matlab实现生成高斯散斑图.rar
- ZBlog 宁静致远主题压缩包
- 基于matlab实现水声通信仿真代码,包括时间反转镜均衡,各种调制解调,信道仿真扥等.rar
- 基于matlab实现水声通信中的psk方法,信道为多径信道,接收段进行均衡处理,是较为完整的psk水声通信系统仿真.rar
- 基于matlab实现水声信道传输特性进行仿真的程序,包括水声信道特性对时反镜稳健性能影响等仿真程序 .rar
- 基于matlab实现水声信道仿真程序 用于水下声信道的仿真 .rar
- GEE训练-如何确定影像数据集多个指定时间范围的影像数量(sentinel-1数据为例)含全部代码.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈