Liby’s Blog

 汝鬼耶,抑人耶?

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

without comments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<termios.h>
#include<string.h>
 
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;
 
	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<strlen(rmc_str);i++)
						{
							if (rmc_str[i] == '\n')
								{
								rmc_str[i] = '\0'; //replace ‘\n’ with null
								}
						 }
printf("%s\n",rmc_str);
				}//end of if
		//$GPRMC,030111.085,V,0000.0000,N,00000.0000,E,,0.00,150209,,,N*68
		//E,123456,0.00, 逗号中间有数据
		//sscanf(rmc_str,"%*[^,],%lf,%c,%lf,%c,%lf,%c,%*[^,],%*[^,],%lf",&rmc_info.utc_time,&rmc_info.status,&rmc_info.latitude_value,&rmc_info.latitude,&rmc_info.longtitude_value,&rmc_info.longtitude,&rmc_info.utc_data);
		//E,,0.00, 逗号中间没有数据
 
 
sscanf(rmc_str,"%*[^,],%lf,%c,%lf,%c,%lf,%c,,%*[^,],%lf",&rmc_info.utc_time,&rmc_info.status,&rmc_info.latitude_value,&rmc_info.latitude,&rmc_info.longtitude_value,&rmc_info.longtitude,&rmc_info.utc_data);
 
		printf("utc time :%lf\n",rmc_info.utc_time);
		printf("status :%c\n",rmc_info.status);
		printf("latitude :%c\n",rmc_info.latitude);
		printf("latitude value: %lf\n",rmc_info.latitude_value);
		printf("longtitude :%c\n",rmc_info.longtitude);
		printf("longtitude value: %lf\n",rmc_info.longtitude_value);
		printf("utc data :%lf\n",rmc_info.utc_data);
		}
		sleep(1);//睡眠
	}
	close(fd_gps);
	return 0;
}

Written by liby

五月 20th, 2012 at 10:03 上午

Posted in 技事巧事

Tagged with ,

留个记号

without comments

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
a=$1
b=$2
echo "$a,$b"
if [ $a -gt $b ]
then
echo "$a>$b"
else
echo "$b>$a"
fi
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash
folder=$PWD
echo "$folder"
count=0
for files in *
do
if
[ -f "$files" ]
then
count=`expr $count + 1`
fi
done
echo $count
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/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"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/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

Written by liby

四月 11th, 2012 at 3:10 下午

Posted in 技事巧事

Tagged with ,

并发TCP服务器端与客户端

with one comment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
 
#define PORT_SERVER 9898
 
int main()
{
	int socket_fd,fd;
	int nread;
	int size;
	char buffer[1024];
	struct sockaddr_in ad_s,ad_c;
 
 
	if((socket_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		fprintf(stderr,"socket error\n");
		exit(1);
	}
 
	bzero(&ad_s,sizeof(ad_s));
	ad_s.sin_family=AF_INET;
	ad_s.sin_addr.s_addr=htonl(INADDR_ANY);
	ad_s.sin_port=htons(PORT_SERVER);
 
	if(bind(socket_fd,(struct sockaddr *)&ad_s,sizeof(ad_s))==-1)
	{
		fprintf(stderr,"bind error\n");
		exit(1);
	}
 
	if(listen(socket_fd,5)==-1)
	{
		fprintf(stderr,"listen error\n");
		exit(1);
	}
 
	while(1)
	{
		size=sizeof(struct sockaddr_in);
		bzero(buffer,1024);
		if((fd=accept(socket_fd,(struct sockaddr *)&ad_c,&size))==-1)
		{
			fprintf(stderr,"accept error\n");
			exit(1);
		}
		printf("server connect from %s\n",inet_ntoa(ad_c.sin_addr.s_addr));
		if(fork()==0)
		{
		if((nread=read(fd,buffer,1024))==-1)
		{
			fprintf(stderr,"read error\n");
			exit(1);
		}
		buffer[nread]='\0';
		printf("server receive %s\n",buffer);
		exit(0);
		}
		else
			close(fd);
	}
	close(socket_fd);
	exit(0);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
 
#define PORT_CLIENT 9898
 
int main(int argv,char *argc[])
{
	struct hostent *host;
	if(argv<2)
	{
		perror("parameter error\n");
		exit(1);
	}
	if((host=gethostbyname(argc[1]))==NULL)
	{
		fprintf(stderr,"gethostname error\n");
		exit(1);
	}
 
	int sock_fd;
	struct sockaddr_in ad_s;
	char buffer[1024];
 
	if((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		fprintf(stderr,"socket error\n");
		exit(1);
	}
	bzero(&ad_s,sizeof(ad_s));
	ad_s.sin_family=AF_INET;
	ad_s.sin_port=htons(PORT_CLIENT);
	ad_s.sin_addr=*((struct in_addr*)host->h_addr);
 
	if(connect(sock_fd,(struct sockaddr *)&ad_s,sizeof(ad_s))==-1)
	{
		fprintf(stderr,"connect error\n");
		exit(1);
	}
	printf("input something...\n");
	bzero(buffer,1024);
	fgets(buffer,1024,stdin);
	write(sock_fd,buffer,strlen(buffer));
 
	close(sock_fd);
	exit(0);
}

Written by liby

三月 10th, 2012 at 7:51 下午

Posted in 技事巧事

Tagged with ,