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

📄 acomm.c

📁 一个使用稳定的串口通信例程。文件格式为51 格式
💻 C
📖 第 1 页 / 共 4 页
字号:
/* 按新协议修改 */

#include "rege58.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "absacc.h"
#define uchar unsigned char
#define uint unsigned int

#define Alert_speed 6			/* 定义断油、断电的最高速度 */
#define HANDTIME 50			/* 定义手动报警的按键时间,1/100 ms */

#define POWERON 0
#define POWEROFF 1
#define WAKEUP 18000l

/* 定义16C554 各口的波特率*/
#define BaudA_h 0				/* 9600 for SIOA PORT for LCD */
#define BaudA_l 0x0C
#define BaudB_h 0				/* 4800 for SIOB PORT for GPS */
#define BaudB_l 0x18
#define BaudC_h 0				/* 9600 for SIOC PORT for VOICE */
#define BaudC_l 0x0C
//#define BaudC_h 0				/* 19200 for SIOD PORT for extport */
//#define BaudC_l 0x06

#define GPSL 0x400				/* GPS 通讯缓冲区 的大小 */
#define GPSP 0x100				/* GPS 处理缓冲区 的大小 */
#define GSML 0x400				/* GSM 通讯缓冲区 的大小 */
#define GSMP 0x300				/* GSM 处理缓冲区 的大小 */
#define LCDL 0x400				/* LCD 通讯缓冲区 的大小 */
#define LCDP 0x100				/* LCD 处理缓冲区 的大小 */
#define TELL 0x100				/* 手柄 通讯缓冲区 的大小 */
#define TELP 0x100				/* 手柄 处理缓冲区 的大小 */
#define TSTP 0x100
#define MESSLEN 10
#define MOUTLEN 25
#define MAXS 361
#define MAXM 121

#define WLNUM 100

#define AUTOSEND 1
#define CENTREWANT 2
#define NOFIRESPEED 3
#define REMOVEALERT 4
#define HALERT 5
#define MISSGPS 6
#define CARSTOP 7
#define CARSTART 8
#define WLOUT 9
#define DOORALERT 10
#define FIREALERT 11
#define WIREALERT 12
#define POWERALERT 13

#define SIOA (( uchar * )0x28000L )	/* 16C554 的地址*/
#define SIOB (( uchar * )0x29000L )
#define SIOC (( uchar * )0x2A000L )
#define SIOD (( uchar * )0x2B000L )
#define PARA (( uchar * )0x2C000L )	/* 扩展I/O口的地址*/
#define P88W (( uchar * )0x2D100L )     /* MT8888的写地址*/
#define P88R (( uchar * )0x2D000L )     /* MT8888的读地址*/

uchar xdata gpsout[GPSL] ;		/* 各串行通讯的接收缓冲区*/
uchar xdata gsmout[GSML] ;
uchar xdata testout[LCDL] ;
uchar xdata telout[TELL] ;

uint data g_head,g_tail ;		/* 各接收缓冲区的头、尾指针*/
uint data s_head,s_tail ;
uint data t_head,t_tail ;
uint data l_head,l_tail ;

uchar xdata gpsproc[GPSP] ;		/* used for received GPS data process */
uchar xdata gsmproc[GSMP] ;		/* used for received GSM data process */
uchar xdata testproc[LCDP] 	;	/* used for received LCD data process */
uchar telproc[TELP] ;			/* used for received HANDSET data process */

uint data gpsp,gsmp,testp,telpp ;	/* 各数据处理缓冲区的尾指针*/

uchar xdata mobile[180] ;		/* 用于生成通讯数据*/
uchar xdata pposition[40] ;		/* 最新位置字符串*/
uchar xdata gpsdata[60] ;		/* 临时GPS数据字符串*/

// Used for save gps data per second, 7424 byte
uchar SGPS[7424];
// Used for save gps data per minute, 3072 byte
uchar MGPS[3072];
uint data sgpshead,sgpstail,mgpshead,mgpstail,mgpstail1 ;

uchar command_serial[10][15] ;		/* 用于保存已执行的命令的序号 */
uchar commpoint ;			/* 已执行的命令序号的存储指针 */
uchar trace_type ;			/* 跟踪类型 单点或多点 */

long PERIOD ;
uchar PERIOD_STR[30] ;

void SendDTMF( uchar * ) ;
void Processtelephone( void ) ;
void ProcessCommand( void ) ;
char *ltoa( long ,char * ) ;
void Rcv_Telecom(void) ;
void ProcessRing( void ) ;
void ProcessNoC( void ) ;
void SendUntimed( void ) ;
uchar Get_view( uchar,uchar * ) ;
void Set_view( uchar,uchar * ) ;
void Rcv_answer( void ) ;
void ProcessDTMF( void ) ;
void Setgsm( uchar * ) ;
void GetLacCid( void ) ;
void DeleteGsmData( void ) ;
void ProcessMessage( void ) ;
void ReadMessage( void ) ;
void ProcessListen( void ) ;
uchar ProcessSend( uchar *, uchar * ) ;
void ProcessSendData( ) ;
void ProcessTest( void ) ;
void ProcessAlert( void ) ;
void AddMout( uchar *,uchar * ) ;
void Text2PDU( uchar *,uchar * ) ;
void PDU2Text( uchar *,uchar * ) ;
void Init554( void ) ;

//void sendgps( uchar ) ;
//void stringsendgps(uchar * ) ;
void sendtelstr(uchar * ) ;
void substr( char *,char *,uint ,uint ) ;
void Setcsca( void ) ;

uchar readtest( uchar * ) ;
uchar readgsm( uchar * ) ;
uchar readgps( uchar * ) ;
uchar readtel( uchar * ) ;
void sendgsm( uchar ) ;
void sendgps( uchar ) ;
void sendtest( uchar ) ;
void sendtel( uchar ) ;
void rtxd(uchar *) ;
void stringsendgsm(uchar * ) ;

uchar xdata regist_state ;		/* GSM 的注册状态, 1 注册中, 0 未注册 */
uint Nowlacn,Nowcidn ;			/* GSM 的 LAC 和 CID 号码 */

uchar xdata ComLac_centre ;		/* 命令所属监控中心 */
uint xdata ComLac_usern ;		/* 命令所属集团用户 */

uchar sendstate,autohand ;		/* 发送性质 */

uchar xdata del_index[30] ;		/* 有待删除的SMS */
uchar xdata smsc_code[30] ;		/* SMSC 号码 */
uchar xdata view_code[30] ;		/* 监控中心号码 */
uchar xdata control_code[30] ;		/* 控制号码 1 */
uchar xdata control_code_bak1[30] ;	/* 控制号码 2 */
uchar xdata control_code_bak2[30] ;	/* 控制号码 3 */
uchar xdata listen_code[30] ;		/* 紧急报警监听号码 */
uchar xdata listen_code1[30] ;		/* 自动报警监听号码 */
struct gpsdt				/* Used for store GPS data */
{
	uchar year,month,date,hour,minute,second,valid ;
	long longitude,latitude ;
	uint speed,direct ;
} xdata Ngps ;				/* Ngps: Current */

struct Message				/* 接收的命令信息 */
{
	uchar year,month,date,hour,minute,second ;
	uchar Source[15] ;
	uchar context[165] ;
} xdata Mold[MESSLEN] ;

struct OMessage				/* 等待发送的命令信息 */
{
	uchar target[15] ;
	uchar context[165] ;
} xdata Mout[MOUTLEN] ;

uint data messph,messpt ;		/* Mold 的头、尾指针 */
uint data moutph,moutpt ;		/* Mout 的头、尾指针 */

uchar data gsmerr ;			/* Count the gsm error */
uint  data gpserr ;			/* Count the gps error */
uint data timer_watchdog ;

uchar xdata t[16] ;			/* 最近一条SMS收到的时间 */
uchar xdata telephone[40] ;
uchar hasphone,needsms ;

uint data timer_0 ;		/* soft timer for general purpose */
long timer_2 ;			/* if timer_2 == 0,it's time to send message */
uint gpstimer ;			/* used for save gps data per minute */
uint timer_4 ;			/* soft timer for reset gsm */
uint in_timer ;			/* soft timer for in call */

uint speed ;			/* 当前的速度 */
long send_time ;		/* 发送时间间隔 */

uint count ;			/* 剩余的发送次 */
bit in_call,at_talk,out_call ;
//bit hsent,asent ;

bit stop_car, lock_door ;	/*中心要求锁车*/  /*遥控器要求锁车*/
bit stop_carm, lock_doorm ;	/*报警器要求锁车*/
bit stop_carp, lock_doorp ;	/*电源要求锁车*/

bit alm1,alm2,alm3,alm4,alm5,alm6,alm7,alm,s_send,del_mark ;
bit hand_alm,listen_alm ;
bit firep ;
bit located ;
bit wlvalid ;
bit IN1 , IN2 , IN3 , IN4 , HA , DES , CHK ;
bit f_init554 ;
bit f_fire = 0 ;
bit sample = 0 ;
bit timer101 = 0 ;
bit timer102 = 0 ;
bit P10Enable ;
uchar data o2 ;
sbit CS=0x90 ;
sbit SI=0x91 ;
sbit SCL=0x92 ;
sbit SO=0x93 ;

uchar ReadStatus( );
void WriteStatus(uchar );
uint data hand_alerm,door_state,v12_state,fire_state,remove_state,wire_state,alermed ;
long stop_state ;
uint nogps,wloutt ;
long speedup ;
uchar sendnum ;

uchar terminalID[15] ;
uchar password[15] ;
uchar SetState[15] ;            /* [0]:漫游标志 1:漫游  [1]:进入GPS盲区报警标志 1:报警 */
                                /* [2]:发送标志 1:发送  [3]:进入GPS盲区报警标志 1:报警 */
uchar wltype,wlnumber ;
long wlwidth ;
struct wl
{
	long longti,lati ;
} xdata wl[WLNUM] ;

code uchar Lac_centre = 'A' ;	/* 终端所属监控中心 */
code uint Lac_usern = 1;	/* 终端所属集团用户 */

code uint  send_etime = 3000 ;
code uchar Pdu_head1[] = "0891683108200005F2110006810400100000AA" ;
code uchar Pdu_head2[] = "0891683108200105F211000B813106091145F80000AA" ;
code uchar qczd[] ={0x6c,0x7d,0x8f,0x66,0x63,0x2f,0x52,0xa8,0x00,0x00} ;/*汽车振动*/

struct MYtype
{
	uint LAC;
	char QH[5];
};

code int latidegree[] =
{
	0,5,10,15,20,25,30,35,40,45,50,55,60
} ;
code int mmperdegree[] =
{
	186,185,183,179,174,168,161,152,142,131,119,106,93
} ;
code int mmperdegreelati = 185 ;

code uint number_LAC = 51 ;

//code uchar atcmgs[] = "AT+CMGS=140" ; /* 11 */
code uchar atcmgs[] = "AT+CMGS=" ;      /* 8 */
code uchar atcmgl[] = "AT+CMGL=4" ;     /* 9 */
code uchar atcmgd[] = "AT+CMGD=" ;      /* 8 */
//code uchar atcsca[] = "AT+CSCA?" ;    /* 8 */
//code uchar atcsca1[] = "AT+CSCA=" ;   /* 8 */
code uchar atcpbw[] = "AT+CPBW=1," ;    /* 10 */
code uchar atcpbr[] = "AT+CPBR=1" ;     /* 9 */
code uchar atcreg[] = "AT+CREG?" ;      /* 8 */

void main( )
{
	uint i ;
        uchar a,b ;

	i=sizeof( command_serial[1] ) ;

	P10Enable = 1 ;
	in_call = 0 ;
	at_talk = 0 ;
	listen_alm = 0 ;
	out_call = 0 ;
	hasphone = 0 ;
	PERIOD = 360000 ;
	autohand = 0 ;

	P15 = POWERON ;
	P41 = 0 ;
	P42 = 0 ;

	AUXR = 0x01 ;
	moutph = 0 ;
	moutpt = 0 ;
	wloutt = 0 ;
	messph = 0 ;
	messpt = 0 ;
	commpoint = 0 ;

	Get_view( 'W',PERIOD_STR ) ;
	rtxd(PERIOD_STR);
	PERIOD = atol( PERIOD_STR )*100 ;
	if( PERIOD == 0 )
		PERIOD = 360000 ;
	if( ( PCON & 0x10 )==0x10 )
	{
		PCON &= 0xef ;
		located = 0 ;
		sgpshead = 0 ;
		sgpstail = 0 ;
		mgpshead = 0 ;
		mgpstail = 0 ;
		mgpstail1 = 0 ;
		count = 0 ;
		timer_2 = PERIOD ;
		send_time = PERIOD ;
		trace_type = '1' ;
		wlvalid = 0 ;
	}
	needsms = 0 ;
	regist_state = '0' ;
	IN1 = ( bit )( PARA[0] & 0x01 ) ;
	IN2 = ( bit )( PARA[0] & 0x02 ) ;
	IN3 = ( bit )( PARA[0] & 0x04 ) ;
	IN4 = ( bit )( PARA[0] & 0x08 ) ;
	HA  = ( bit )( PARA[0] & 0x10 ) ;
	DES = ( bit )( PARA[0] & 0x20 ) ;
	CHK = ( bit )( PARA[0] & 0x40 ) ;
	if( IN3 == 0 )
		firep = 1 ;
	else
		firep = 0 ;
	f_init554 = 0 ;
	sendstate = AUTOSEND ;
	ComLac_centre = Lac_centre ;
	ComLac_usern = Lac_usern ;
//	hsent = 1 ;
//	asent = 1 ;
	s_send = 0 ;
	del_mark = 0 ;
	alm1 = 0 ;
	alm2 = 0 ;
	alm3 = 0 ;
	alm4 = 0 ;
	alm5 = 0 ;
	alm6 = 0 ;
	alm7 = 0 ;
	hand_alm = 0 ;
	hand_alerm = 0 ;
	wire_state = 0;
	door_state = 0 ;
	remove_state = 0 ;
	alermed = 0 ;
	fire_state = 0 ;
	stop_state = WAKEUP ;
	firep = 0 ;
	v12_state = 0 ;
	stop_car = 0 ;
	lock_door = 0 ;
	stop_carm = 0 ;
	lock_doorm = 0 ;
	stop_carp = 0 ;
	lock_doorp = 0 ;
	speed = 0 ;
	gsmerr = 0 ;
	gpserr = 0 ;

	sendnum = 0 ;
	nogps = 0 ;
        speedup = 0 ;
	gpsp = 0 ;
	gsmp = 0 ;
	testp = 0 ;
	telpp = 0 ;
	timer_0 = 0 ;
	gpstimer = 0 ;
	timer_watchdog = 0 ;
        in_timer = 0 ;

	SCON = 0x50 ;
	TMOD = 0x21 ;
	TL1 = 0xfd ;            /* fa:9600  fd:19200  f4:4800 */
	TH1 = 0xfd ;            /*19200 for gsm baud rate*/
//	TL0 = 0xe4 ;            /* 10 ms */
//	TH0 = 0xdb ;
	TL0 = 0x00 ;            /* 2.5 ms */
	TH0 = 0xf7 ;
	PCON = 0x80 ;
	TR1 = 0 ;
	TR0 = 0 ;
	IP = 0x15 ;
	IT0 = 1 ;
	IT1 = 1 ;
	ET0 = 1 ;               /* Enable timer0 interrupt */
	ES = 1 ;                /* Enable sio interrupt */
	TR0 = 1 ;
	TR1 = 1 ;
	EA = 1 ;                /* Enable interrupt */
	Init554( ) ;
	EX0 = 1 ;               /* Enable extern int0 interrupt */
	EX1 = 1 ;

	gpsdata[0] = 'P' ;
	gpsdata[1] = 'P' ;

	for (i=2 ; i<58 ; i++)
		gpsdata[i] = '0' ;
	gpsdata[58] = 0 ;

	P40 = 0 ;
	timer_0 = 120 ;
	while ( timer_0 != 0 ) ;
	P40 = 1 ;
	timer_0 = 120 ;
	while ( timer_0 != 0 ) ;
	P40 = 0 ;

	g_head = 0 ;
	g_tail = 0 ;
	s_head = 0 ;
	s_tail = 0 ;
	t_head = 0 ;
	t_tail = 0 ;
	l_head = 0 ;
	l_tail = 0 ;


/*      初始化MT8888      */
        P88W[0] = 0x00 ;
        P88W[0] = 0x08 ;
        P88W[0] = 0x02 ;
        P88W[0] = 0x0c ;

	for( i=0 ; i<30 ; i++ )
		del_index[i] = 0 ;
	strcpy( pposition,"AAAAAA^~lMATkgS@@@sA" ) ;

/************************************初始化GPS板,可以不要***********************************/
/*	timer_0 = 20 ;
	while ( timer_0 != 0 ) ;
	InitGPS( ) ;  */

	WriteStatus(ReadStatus( )&0xcf);/*1.4s*/
	SCL=0;
//	WriteStatus(ReadStatus( )|0x30);/*disable the watchdog( );*/
	WriteStatus(ReadStatus( )&0xf3);/*do this for every block unprotected*/
        Get_view( 'Z',SetState ) ;
	if( ( SetState[6] != '9' )||( SetState[7] != '2' )||( SetState[8] != '3' )||( SetState[9] != '4' ) )
	{
		Set_view( 'A',"13609011548" ) ;
		Set_view( 'B',"555542" ) ;
		Set_view( 'C',"13609011548" ) ;
		Set_view( 'D',"555513" ) ;
		Set_view( 'E',"8613800200500" ) ;
		Set_view( 'F',"13609011548" ) ;
		Set_view( 'G',"96111" ) ;
		Set_view( 'H',"96110" ) ;
		Set_view( 'I',"96111" ) ;
		Set_view( 'J',"96110" ) ;
		Set_view( 'K',"96111" ) ;
		Set_view( 'L',"96110" ) ;
		Set_view( 'M',"96111" ) ;
		Set_view( 'N',"96110" ) ;
		Set_view( 'O',"96111" ) ;
		Set_view( 'P',"96110" ) ;
		Set_view( 'Q',"13609011548" ) ;
		Set_view( 'W',"14400" ) ;
		Set_view( 'X',"1111111111" ) ;
		Set_view( 'Y',"2222222222" ) ;
		Set_view( 'Z',"1100009234" ) ;
	}
	Get_view( 'A',view_code ) ;
	Get_view( 'B',control_code ) ;
	Get_view( 'C',control_code_bak1 ) ;
	Get_view( 'D',control_code_bak2 ) ;
	Get_view( 'E',smsc_code ) ;
	Get_view( 'F',listen_code ) ;
	Get_view( 'Q',listen_code1 ) ;
	Get_view( 'W',PERIOD_STR ) ;
	Get_view( 'X',password ) ;
	Get_view( 'Y',terminalID ) ;
	Get_view( 'Z',SetState ) ;

	rtxd(view_code);
	rtxd(control_code ) ;
	rtxd(control_code_bak1 ) ;
	rtxd(control_code_bak2 ) ;
	rtxd(smsc_code);
	rtxd(listen_code);
	rtxd(listen_code1);
	rtxd(PERIOD_STR);
	rtxd(password);
	rtxd(terminalID);
	rtxd(SetState);

	PERIOD = atol( PERIOD_STR )*100 ;
	if( PERIOD == 0 )
		PERIOD = 360000 ;

	while( 1 )
	{
		timer_watchdog = 0 ;
		strcpy( mobile,"" ) ;

	Get_view( 'A',view_code ) ;
	Get_view( 'B',control_code ) ;
	Get_view( 'C',control_code_bak1 ) ;
	Get_view( 'D',control_code_bak2 ) ;
	Get_view( 'E',smsc_code ) ;
	Get_view( 'F',listen_code ) ;
	Get_view( 'Q',listen_code1 ) ;
	Get_view( 'W',PERIOD_STR ) ;
	Get_view( 'X',password ) ;
	Get_view( 'Y',terminalID ) ;
	Get_view( 'Z',SetState ) ;

		if( in_timer == 0 )
			in_call=0 ;
		if( IN2 == 0 )
			stop_state=WAKEUP ;

		ReadMessage( ) ;
		ProcessMessage( ) ;
		DeleteGsmData( ) ;
		Setgsm( "AT+CLIP=1" ) ;
		Setgsm( "AT+CREG=2" ) ;
		Setgsm( "AT^SNFS=1" ) ;
		ProcessAlert( ) ;
		SendUntimed( ) ;
		ProcessListen( ) ;
		Rcv_answer( ) ;
		ProcessDTMF( ) ;
		Rcv_Telecom( ) ;
		ProcessCommand( ) ;
		Processtelephone( ) ;

/*		if( stop_state == 0 )
		{
			P15 = POWEROFF ;
			f_init554 = 0 ;
			P14=1;
		}
		else*/
		{
			P15 = POWERON ;                 /* 省电模式结束*/
			timer_0 = 20 ;
			while ( timer_0 != 0 ) ;

			if ( f_init554==0 )
				Init554() ;
		}
		if( gsmerr > 10 )
		{
			P42 = 1 ;
			timer_0 = 100 ;
			while ( timer_0 !=0 ) ;
			P42 = 0 ;
			gsmerr = 0 ;
		}
		P40 = !P40 ;

		if( listen_alm )
		{
			if( alm2 == 1 )
				SendDTMF( "1313" ) ;
			if( alm3 == 1 )
				SendDTMF( "1111" ) ;
			if( hand_alm == 1 )
				SendDTMF( "33333333" ) ;
		}

			if( ( in_call == 1 )&&( autohand == 1 ) )
			{
				stringsendgsm( "ATA" ) ;
				while(1)
				{   timer_0 = 100 ;
					gsmp = 0;
					while( timer_0 )
					{       a = readgsm( &b ) ;
						if( a )
						{       gsmproc[gsmp] = b ;
							if( gsmp < (GSMP-1) )
								gsmp++ ;
							if( b == 0x0a )
								break ;
						}
					}
					if( !timer_0 )
						break ;
					else if( ( gsmproc[0] == 'O' ) && ( gsmproc[1] == 'K' ) )
					{
                                        	at_talk = 1 ;
						break ;
					}
					else if( ( gsmproc[0] == 'E' ) && ( gsmproc[1] == 'R' ) )
						break ;
					else if( ( gsmproc[0] == 'R' ) && ( gsmproc[1] == 'I' ) && ( gsmproc[2] == 'N' ) && ( gsmproc[3] == 'G' ) )
						ProcessRing( ) ;
					else if( ( gsmproc[0] == 'N' ) && ( gsmproc[1] == 'O' ) && ( gsmproc[2] == ' ' ) && ( gsmproc[3] == 'C' ) )
						ProcessNoC( ) ;
				}
				in_call = 0 ;
			}


	}
}

void sendgsm( uchar a )
{
	s_send = 1 ;
	SBUF = a ;
	while( TI==0 ) ;
	TI = 0 ;
	s_send = 0 ;
}

void sendtest( uchar a )
{
	uchar data x,y ;
	while( 1 )
	{
		y = SIOA[5] ;
		x = y&0x20 ;
		if( x==0x20 )
		{
			SIOA[0] = a ;
			return  ;
		}
	}
}
/**********
void sendgps( uchar a )
{
	uchar data x,y ;
	while( 1 )
	{
		y = SIOB[5] ;
		x = y&0x20 ;
		if( x==0x20 )
		{
			SIOB[0] = a ;
			return  ;
		}
	}
}
**************/
void sendtel( uchar a )
{
	uchar x,y ;
	while( 1 )
	{
		y = SIOC[5] ;
		x = y&0x20 ;
		if( x==0x20 )
		{
			SIOC[0] = a ;
			return  ;
		}
	}
}

⌨️ 快捷键说明

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