Linux下串口GPRS设备的短信读取发送与数据处理

#include  //标准输入输出定义
#include  //标准函数库定义
#include  //Unix 标准函数定义
#include  //文件控制定义
#include 
#include 
#include  //PPSIX 终端控制定义
#include 
#include 
#include  //错误号定义

static int fd_gsm;
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
	struct termios newtio,oldtio;
	if  ( tcgetattr( fd,&oldtio)  !=  0) { 
		perror("SetupSerial 1");
		return -1;
	}
	bzero( &newtio, sizeof( newtio ) );
	newtio.c_cflag  |=  CLOCAL | CREAD;
	newtio.c_cflag &= ~CSIZE;

	switch( nBits )
	{
	case 7:
		newtio.c_cflag |= CS7;
		break;
	case 8:
		newtio.c_cflag |= CS8;
		break;
	}

	switch( nEvent )
	{
	case 'O':
		newtio.c_cflag |= PARENB;
		newtio.c_cflag |= PARODD;
		newtio.c_iflag |= (INPCK | ISTRIP);
		break;
	case 'E': 
		newtio.c_iflag |= (INPCK | ISTRIP);
		newtio.c_cflag |= PARENB;
		newtio.c_cflag &= ~PARODD;
		break;
	case 'N':  
		newtio.c_cflag &= ~PARENB;
		break;
	}

	switch( nSpeed )
	{
	case 2400:
		cfsetispeed(&newtio, B2400);
		cfsetospeed(&newtio, B2400);
		break;
	case 4800:
		cfsetispeed(&newtio, B4800);
		cfsetospeed(&newtio, B4800);
		break;
	case 9600:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	case 19200:
		cfsetispeed(&newtio, B19200);
		cfsetospeed(&newtio, B19200);
		break;
	case 115200:
		cfsetispeed(&newtio, B115200);
		cfsetospeed(&newtio, B115200);
		break;
	case 460800:
		cfsetispeed(&newtio, B460800);
		cfsetospeed(&newtio, B460800);
		break;
	default:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	}
	if( nStop == 1 )
		newtio.c_cflag &=  ~CSTOPB;
	else if ( nStop == 2 )
	newtio.c_cflag |=  CSTOPB;
	newtio.c_cc[VTIME]  = 0;//重要
	newtio.c_cc[VMIN] = 100;//返回的最小值  重要
	
/*	newtio.c_cc[VTIME] = 30;    //30 seconds
    	newtio.c_cc[VMIN] = 100;     //least bytes
    	newtio.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);//非规范模式

	newtio.c_cc[VTIME] = 30;    //30 seconds
	newtio.c_cc[VTIME] = 3;    //3 seconds
*/
	tcflush(fd,TCIFLUSH);
	if((tcsetattr(fd,TCSANOW,&newtio))!=0)
	{
		perror("com set error");
		return -1;
	}
	printf("set done!\n\r");
	return 0;
}

int read_info(char *buf)//rt3[100]
{
	int nux=0;
	nux=read(fd_gsm,buf,1);

	if(nux!=0)//如果读到非零0个数据
	{
		while(buf[0]!='\0')
		{		
			buf++;
			ir++;
			read(fd_gsm,buf,1);
			usleep(50);
		}
	}
	else
	{
		printf("read zero !\n");
	}
	tcflush(fd_gsm,TCIOFLUSH);//清空读写缓存//
	return 0;
}

int main(void)
{
	int set_gsm;
	int i=0;
	char reply[128];
	char data[123];
	char *d1=NULL;
	char *d2=NULL;
	char *d3=NULL;
	char *indx=NULL;
	char rdmsg[123]="AT+CMGR=";
	char cde[123];


	

	fd_gsm=open("/dev/ttyUSB0", O_RDWR|O_NONBLOCK);//打开串口

	if (fd_gsm==-1)

		exit(1);



	set_gsm=set_opt(fd_gsm,19200,8,'N',1);//设置串口属性

	if (set_gsm==-1)

		exit(1);



	write(fd_gsm,"AT\r",3);
	sleep(2);
	bzero(reply,128);
	read_info(reply);
	printf("%s\n",reply);

	write(fd_gsm,"AT+CMGF=1\r",strlen("AT+CMGF=1\r"));
	sleep(2);
	bzero(reply,128);
	read_info(reply);
	printf("%s\n",reply);

	write(fd_gsm,"AT+CNMI=2,1\r",strlen("AT+CNMI=2,1\r"));
	sleep(2);
	bzero(reply,128);
	read_info(reply);
	printf("%s\n",reply);
	
	while(1)
	{
		//read COM data
		bzero(reply,128);
		read_info(reply);
		printf("%d times reading %s\n",i++,reply);
		
		if(reply[2]=='+'&&reply[3]=='C')//数据有换行符,+CMTI: "MT",40,"+C"非第0元素开始
		{
			//sleep(2);
			indx=reply;
			indx=indx+14;
			printf("Index of MSG %s\n",indx);
			strcat(indx,"\r");//40 + \r
			strcat(rdmsg,indx);//at+cmgr= + 40 +\r 
			write(fd_gsm,rdmsg,strlen(rdmsg));
			sleep(2);
			bzero(reply,128);
			read_info(reply);
			printf("%s\n",reply);

			sscanf(reply,"%*[^\"]\"%*[^\"]\"%*[^\"]\"%*[^\"]\"%*[^\"]\"%*[^\"]\"%s",data);//获取短信内容
			printf("message:%s\n",data);
			d1=strtok(data,",");
			d2=strtok(NULL,",");
			d3=strtok(NULL,",");
			printf("d1 %s\nd2 %s\nd3 %s\n",d1,d2,d3);
		}
		sleep(2);
	}

/*
write(fd_gsm,"AT+CMGS=18888888888\r",strlen("AT+CMGS=18888888888\r"));
sleep(2);
bzero(reply,128);
read_info(reply);
printf("%s\n",reply);

write(fd_gsm,"12345",strlen("12345"));
sleep(2);
write(fd_gsm,"\x1a",1);
bzero(reply,128);
read_info(reply);
printf("%s\n",reply);
*/
	close(fd_gsm);//关闭设备文件
	return 0;
} 

Linux下串口GPS设备的GPRMC数据处理

#include
#include
#include
#include
#include
#include
#include
#include
#include

struct gps_info
{
	double utc_time;
	char status;
	double latitude_value;
	char latitude;
	double longtitude_value;
	char longtitude;
	double utc_data;
}rmc_info;//GPRMC数据

int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
	struct termios newtio,oldtio;
	if  ( tcgetattr( fd,&oldtio)  !=  0)
		{ 
		perror("SetupSerial 1");
		return -1;
		}
	bzero( &newtio, sizeof( newtio ) );
	newtio.c_cflag  |=  CLOCAL | CREAD;
	newtio.c_cflag &= ~CSIZE;

	switch( nBits )
	{
	case 7:
		newtio.c_cflag |= CS7;
		break;
	case 8:
		newtio.c_cflag |= CS8;
		break;
	}

	switch( nEvent )
	{
	case 'O':
		newtio.c_cflag |= PARENB;
		newtio.c_cflag |= PARODD;
		newtio.c_iflag |= (INPCK | ISTRIP);
		break;
	case 'E': 
		newtio.c_iflag |= (INPCK | ISTRIP);
		newtio.c_cflag |= PARENB;
		newtio.c_cflag &= ~PARODD;
		break;
	case 'N':  
		newtio.c_cflag &= ~PARENB;
		break;
	}

	switch( nSpeed )
	{
	case 2400:
		cfsetispeed(&newtio, B2400);
		cfsetospeed(&newtio, B2400);
		break;
	case 4800:
		cfsetispeed(&newtio, B4800);
		cfsetospeed(&newtio, B4800);
		break;
	case 9600:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	case 115200:
		cfsetispeed(&newtio, B115200);
		cfsetospeed(&newtio, B115200);
		break;
	case 460800:
		cfsetispeed(&newtio, B460800);
		cfsetospeed(&newtio, B460800);
		break;
	default:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	}
	if( nStop == 1 )
		newtio.c_cflag &=  ~CSTOPB;
	else if ( nStop == 2 )
	newtio.c_cflag |=  CSTOPB;
	newtio.c_cc[VTIME]  = 0;//
	newtio.c_cc[VMIN] = 100;//
/*
	newtio.c_cc[VTIME] = 30;    //30 seconds
	newtio.c_cc[VMIN] = 100;     //least bytes
	newtio.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);//非规范模式

	newtio.c_cc[VTIME] = 30;    //30 seconds
	newtio.c_cc[VTIME] = 3;    //3 seconds
*/
	tcflush(fd,TCIFLUSH);
	if((tcsetattr(fd,TCSANOW,&newtio))!=0)
	{
		perror("com set error");
		return -1;
	}
	printf("set done!\n\r");
	return 0;
}

int main()
{
	int i;
	int fd_gps;
	int set_gps;
	int nread_gps;
	char tmp;
	char *buff_gps;
	char *rmc_str;
    char buff_gps[512];//Edit by 2013.8.9
    char *tmp=buff_gps];//Edit by 2013.8.9

	fd_gps=open("/dev/ttyUSB0",O_RDWR|O_NONBLOCK);//打开串口
	if (fd_gps==-1)
		exit(1);

	set_gps=set_opt(fd_gps,4800,8,'N',1);//设置串口属性
	if (set_gps==-1)
		exit(1);

while(1)
	{
/*		memset(buff_gps,0,512);
		nread_gps=read(fd_gps,buff_gps,512);//读串口

		printf("%s\n\n\n",buff_gps);
		if(nread_gps>0)
		{
			if((rmc_str = strstr(buff_gps,"$GPRMC"))!=NULL)
				{
					for(i=0;i
	

留个记号

#!/bin/bash
a=$1
b=$2
echo "$a,$b"
if [ $a -gt $b ]
then
echo "$a>$b"
else
echo "$b>$a"
fi
#!/bin/bash
folder=$PWD
echo "$folder"
count=0
for files in *
do
if
[ -f "$files" ]
then
count=`expr $count + 1`
fi
done
echo $count
#!/bin/bash
echo -n "input number:"
read num
nbak=$num
i=0
reverse=""
while [ $nbak -ne 0 ]
do
i=$[ $nbak % 10]
nbak=$[ $nbak / 10]
reverse="$reverse$i"
done
echo "$reverse"
#!/bin/bash
if [ $1 = "" ] || [ $2 =  ""]
then
echo "enter parameter"
exit 1
fi
source=$PWD/$1
desert=$2
echo "$source move to $desert"
if [ -e $desert$1 ]
then
until [ !-e $desert$1 ]
do
echo "files has been deleted"
done
else
mv $PWD/$1 $desert/$1
fi

 汝鬼耶,抑人耶?