⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phlayer.c

📁 非常好用的gps数据读写接口
💻 C
字号:
/***************************************************************************                          phlayer.c  -  physical layer                             -------------------    begin                : So M鋜 21 17:50:31 CET 2004    copyright            : (C) 2004 by Dennis Real    email                : dev-null@users.sourceforge.net ***************************************************************************//*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * ***************************************************************************/#include <sys/types.h>#include <sys/stat.h> #include <sys/termios.h>#include <sys/ioctl.h>#include <fcntl.h>#include <termios.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <string.h>       #include "phlayer.h" #include "def.h"/*	init tty port		consider 			"man 3 termios" and			"man 2 open" for more information		returns -1 if error occurs 	*/int InitTTY (void){	int fd;	struct termios mytio;                                     	extern char ttydevice[];	/* try to get serial port from environment variable */	if ( getenv("LGGT_SERIAL_PORT")!=NULL )	{		strncpy(ttydevice, getenv("LGGT_SERIAL_PORT"), 80);	}          	/* open serial port for reading & writing */	fd = open(ttydevice, O_RDWR);  	if (fd < 0)	{    return (-1);	}  bzero(&mytio, sizeof(mytio));	mytio.c_iflag = 0; 	mytio.c_oflag = ONLRET;	mytio.c_cflag = B9600 | CS8 | CREAD | CLOCAL | IXON | IXOFF;	mytio.c_lflag = 0;	/* forget old unread and unwritten data. now. */  tcflush(fd, TCIFLUSH);  tcsetattr(fd, TCSANOW, &mytio);  return fd;}/*	close tty port	*/void CloseTTY(int iohandler){  close (iohandler);}/*	send garminpacket to gps.	*/void SendPacket(int iohandler, char *gp, unsigned int packetsize){  BYTE i;    for ( i=0; i<packetsize; i++ )	{		write (iohandler, &gp[i], 1);	}#ifdef DEBUG		fprintf(stderr, "Sent %i bytes to gps: \"", packetsize);/*  for (i = 0; i<packetsize; i++)  {		fprintf(stderr, "%c", (BYTE)gp[i]);	}	fprintf(stderr, "\"\n");*/	  for (i = 0; i<packetsize; i++)  {		fprintf(stderr, "%02x ", (BYTE)gp[i]);	}	fprintf(stderr, "\n\n");#endif  }/*	read from tty until 0x10 ... !0x10 0x10 0x03 received or timeout	*/char *GetPacket(int iohandler, unsigned int *size){  	static char buffer [READBUFFER];	char ch=0, last=0;	int t=0;	int counted;	int i=0, j;	do	{		/* data available? */		ioctl(iohandler, FIONREAD, &counted);		last=ch;		if ( counted!=0 )		{			/* data available! */			if (read (iohandler, &ch, 1) != 1)  			{				fprintf(stderr, "Error: Reading tty failed. Exiting...\n");				exit(-1);    	}    	else    	{    		/* 1 byte read */				t=0;				/* a valid packet must start with 0x10 */				if ( (i==0) && (ch!=0x10) )				{//fprintf(stderr, "trashed %02x\n", (unsigned char)ch);									}				else 				{					if ( i < (READBUFFER-1) ) 					{						buffer[i++]=ch;					}					else					{						fprintf(stderr, "Error: Receive buffer full. Exiting...\n");						exit(-1);					}				}								if ( (i==2) && ((ch==0x03) || (ch==0x10)) )				{					/* DTE (10) & ETX (3) will never be a packet id. *///fprintf(stderr, "trash! %02x\n", (unsigned char)ch);															i=0;					t=0;					ch=0;					last=0;				}				}			/*	DLE stuffing: A DLE character (0x10) in size, packet data					or checksum will be followed by another DLE. Remove it					in link layer later but take care of complete packet here.	*/ 			if ( (last==0x10) && (ch==0x10) )			{				last=0;			}		}		else		{			/* no data available */						usleep(10000);			if ( t++ >= READTIMEOUT )			{				fprintf(stderr, "Warning: Timeout while reading from tty.\n");				*size=0;				//exit(-1);				return NULL;			}		}  } while ( !((ch==0x03) && (last==0x10)) || (i<5) ); 	buffer[i]='\0';	*size=(unsigned int)i;#ifdef DEBUG    fprintf(stderr, "Received %i bytes from tty: \"", i);/*  for ( j=0; j<i; j++)  {		fprintf(stderr, "%c", (BYTE)buffer[j]);	}	fprintf(stderr, "\"\n");*/	  for ( j=0; j<i; j++)  {		fprintf(stderr, "%02x ", (BYTE)buffer[j]);	}	fprintf(stderr, "\n\n");#endif  return (buffer);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -