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

📄 comm.c

📁 公司产品上的部分程序2
💻 C
字号:
#include <iom128v.h>
#include <control.h>
#include <lcd.h>
#include <macros.h>

extern unsigned char ucMenuLayer,password_ok,reset;
extern unsigned char com_Rxin,com_Txin,com_Txout,com_Rxout,com_tx_empty; 
extern unsigned char com_tx_buf[256],com_rx_buf[256];
extern unsigned char net_rx_buf[256],net_tx_buf[256];
extern unsigned char net_Rxin,net_Txin,net_Txout,net_Rxout,net_tx_empty; 
extern unsigned char send_data[100];
extern unsigned char serv_trl,ask_server_timer,sever_restore,server_start;
extern unsigned char com_tx_empty;
extern unsigned char net_serv_conv;
extern unsigned char net_serv_conv_len;
extern unsigned char net_send_data[256];

//---------------------------------------------------------
// NAME:	com_send_cmd()
// ENTRY:	destination, .
//		len,
// RETURN:	void.
// MODIFY:	2004.2.2
//--------------------------------------------------------
void com_send_cmd(unsigned char destination, unsigned char len )
{
	unsigned char i, temp;
	temp = 0;
	if(destination == 1)
	{
		send_data[0] = 0x47; // start code
		send_data[1] = 0xaa;
		send_data[2] = 2;	   				
		send_data[3] = 0;			   
		send_data[4] = 0xfe;		
		com_server_transmit(5);
	}
	else 
	{
		for(i = 0; i < len; i++)
		{
			temp = net_Rxout + i;
			send_data[i] = net_rx_buf[temp];	  
		}
		com_server_transmit(len);
	}
	for(i = len; i<= 254; i++)
	{
		send_data[i] = 0;	   			
	}
}

//---------------------------------------------------------
// NAME:	com_send_cmd()
// ENTRY:	destination, .
//		len,
// RETURN:	void.
// MODIFY:	2004.2.2
//--------------------------------------------------------
void control_power(void)
{
	unsigned char i,checksum;
	unsigned char temp;
	temp=0;
	checksum=0;
	for(i=2;i<5;i++)
	{
		temp=com_Rxout+i;
		checksum =  checksum+ com_rx_buf[temp];
	}
	checksum=~checksum+1;
	temp=com_Rxout+5;
	if( com_rx_buf[temp]==checksum  )
	{				
		//com_send_cmd(1,5);
		temp=com_Rxout+4;
		if(!com_rx_buf[temp])	      
		{
			//SHOW_LOGO(31);
			ucMenuLayer=0; 
			password_ok=0;
			spk_power_off();
			delayms(200);  
			SHOW_LOGO(57);
			delayms(200);  
			SHOW_LOGO(58);
			delayms(200);  
			SHOW_LOGO(59);
			delayms(200);
			server_power_off();
			net_send_data[0]=0x47;
			net_send_data[0]=0xaa;
			net_send_data[0]=0x03;
			net_send_data[0]=0x09;
			net_send_data[0]=0;
			net_send_data[0]=0xf4;
			net_serv_conv=1;
			net_serv_conv_len =6;
		}				      		   
	}
	com_Rxout += 6;
	com_Rxout &= DATA_LEN;		
}

//---------------------------------------------------------
// NAME:	com_send_cmd()
// ENTRY:	destination, .
//		len,
// RETURN:	void.
// MODIFY:	2004.2.2
//--------------------------------------------------------
void com_server_receive_to_poll(void)
{
	unsigned char checksum,t1,t2;
	checksum=0;
	t1=t2=0;
	t1=com_Rxout+2;
	t2=com_Rxout+3;
	checksum=com_rx_buf[t1]+com_rx_buf[t2];
	checksum=~checksum+1;
	t2=com_Rxout+4;
	if( com_rx_buf[t1]==2 && checksum==com_rx_buf[t2]  )
	{
		if(serv_trl)   
		{
			server_start=0;
			serv_trl=0;
			reset=0;
			ask_server_timer=0;
		}
		server_start=1;
		ask_server_timer=0;
		serv_trl=0;
		sever_restore=0;
		com_Rxout += 5;
		com_Rxout &= 255;
	}
}

//---------------------------------------------------------
// NAME:	com_server_receive()
// ENTRY:	void.
// RETURN:	void.
// MODIFY:	2004.2.2
//--------------------------------------------------------
void com_server_receive(void)
{
	unsigned char comm, l, i;
	unsigned char checksum;	
	unsigned char len, pack_len, j;
	pack_len = 0;
	if(com_Rxin == 0x20)   
		i = 0;
	//no data received 
	if(com_Rxin == com_Rxout )	
		return;
	if(com_Rxin > com_Rxout )
		len = com_Rxin - com_Rxout;		
	else				
		len = 256 - (com_Rxout - com_Rxin);
	//receive data is  enough?
	if(len < 5)	
		return;
	//search the start char of the information
	while(len > 0)			
	{
		if(com_rx_buf[com_Rxout] == 0X47)	
			break;
		com_Rxout++;
		com_Rxout &= 255;
		len--;
	}
	//receive data is  enough?
	if(len < 5 )	
		return;		
	j = com_Rxout + 1;
	//read the command uchar
	comm = com_rx_buf[j];	
	checksum = 0;
	if(comm == 0xaa)   
	{
		l = com_Rxout + 3;
		if(com_rx_buf[l] == 1)
			com_server_receive_to_poll();
		else   if(com_rx_buf[l] == 9)
			control_power();
	}
	else  if(comm == 0x55)
	{
		j = com_Rxout + 2;
		pack_len = com_rx_buf[j] + 3;
		if(pack_len>len)  return;
		j = com_Rxout + 2;
		//count checksum
		for(i = 0; i < com_rx_buf[j]; i++)    
		{
			l = j + i;
			checksum =  checksum + com_rx_buf[l];
		}
		checksum = ~checksum + 1;
		i = com_Rxout + com_rx_buf[j] + 2;
		//commpare   checksum
		if(com_rx_buf[i] == checksum )     
		{	  	       
			l=com_rx_buf[j]+3;
			//com_send_cmd(1,5);     //no ack needed
			for(i=0;i<l;i++)
			{
				j=com_Rxout+i;
				net_send_data[i]=com_rx_buf[j];
			}
			net_serv_conv=1;
			net_serv_conv_len =l;
			/*
			W3100A_connect();
			//SREG&=~0X80;
			net_send_cmd(2,com_rx_buf[com_Rxout+2]+3);
			//SREG|=0X80;
			*/
			i=com_Rxout+2;
			l=com_rx_buf[i]+3;
			for(j=0;j<l;j++)
			{
				if(com_Rxout==255)    com_Rxout=0;
				else
					com_Rxout ++;
			}
			//com_Rxout +=com_rx_buf[i]+3;
			//com_Rxout &= DATA_LEN;
		}
	}
}

//---------------------------------------------------------
// NAME:	com_server_transmit()
// ENTRY:	leng, .
// RETURN:	void.
// MODIFY:	2004.2.2
//--------------------------------------------------------
void com_server_transmit(unsigned char leng )
{
	unsigned char i, num;
	// judge the send array is emtpy 
	if(com_Txout == com_Txin)					
	{
		// the send data in the send array is full
		if(!com_tx_empty)	
			return;
		// the most send array
		num = 255;						
	}
	// there is data in the send array
	else 								
	{
		// count the length of send array spare space
		if(com_Txout < com_Txin)				
			num = 256 - (com_Txin - com_Txout);
		else	
			num = com_Txout - com_Txin;
	}
	// the space of the serial send array is not enough
	if(num < 5)		
		return;		
	for(i = 0; i < leng; i++)
	{
		// put the data to be send into the send array
		com_tx_buf[com_Txin] = send_data[i];	
		com_Txin++;
		com_Txin &= 255;
	}
	// serial send array have empty
	if(com_tx_empty)	
	{
		UDR1 = send_data[0];								  // start the send interrupt to send the data					
		com_Txout++;	
		com_Txout &= 255;
	}
	return;
}


⌨️ 快捷键说明

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