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

📄 uartcomm.c

📁 用于小商品消费的SMS消费机程序
💻 C
📖 第 1 页 / 共 2 页
字号:
//#include <stdio.h>
//#include <string.h>
//#include <stdlib.h>
#include <api.h>
#include <console.h>
//#include <string.h>
//#include <ctype.h>
#include <SMS.h>

//MC35 state
#define GSM_POWER_OFF	0 
#define GSM_POWER_ON	1
#define GSM_CONFIGURED	2

unsigned short GSM_State = GSM_POWER_OFF; 

#define MAX_BUF_SIZE 1024

extern unsigned char gcCommMsgRow;
extern unsigned char CenterNo[12];
extern unsigned char SMSCenterNo[20];


static unsigned short String2Bytes(const unsigned char* pSrc, unsigned char* pDst, unsigned short nSrcLength)
{
        unsigned short i;
 
 for(i=0; i<nSrcLength; i+=2)
 {
  //输出高4位
  if(*pSrc>='0' && *pSrc<='9')
   *pDst = (*pSrc - '0') << 4;
  else
   *pDst = (*pSrc - 'A' + 10) << 4;
  pSrc++;
 
  //输出低4位
  if(*pSrc>='0' && *pSrc<='9')
   *pDst |= *pSrc - '0';
  else
   *pDst |= *pSrc - 'A' + 10;
  pSrc++;
  pDst++;
 }
 
 //返回目标数据长度
 return nSrcLength / 2;
}

//---------------------------------------------------------------------------
// 发送短消息
// pSrc: 源PDU参数指针
//---------------------------------------------------------------------------
unsigned char Send_SMS1(const SMS_Type *SMS)
{
 unsigned short nPduLength;      //PDU串长度
 unsigned char  nSmscLength;     //SMSC串长度
 unsigned char  Cmd[16], Pdu[256];
 
 nPduLength = Encode_PDU(SMS, Pdu); //根据PDU参数,编码PDU串
 strcat(Pdu, "\x01a");  //以Ctrl-Z结束
 
 String2Bytes(Pdu, &nSmscLength, 2); //取PDU串中的SMSC信息长度
 nSmscLength++;  //加上长度字节本身
 
 //命令中的长度,不包括SMSC信息长度,以数据字节计
 sprintf(Cmd, "AT+CMGS=%d\r", nPduLength / 2 - nSmscLength); //生成命令
        Send_AT_Command(Cmd);     //先输出命令串
        if(UART_wait_str("\r\n> ", 3000) == _true)
 {
                Send_AT_Command(Pdu);     //得到肯定回答,继续输出PDU串
                if(UART_wait_str("+CMGS:", 9000) == _true)
   return _true;
 }
 return _false;
}


/*
 * 断开连接
 */
void UART_disconnect()
{
	typ_UART_stat_word Ustat;
	
//	clr_scr();	
//	move_cursor(0,0);
//	puts("UART_receive");
//	getch();

	Ustat.l_word = UART_stat();
	if (Ustat.bits.UART_on)
	{
		UART_init(UART_OFF);
		sys_msg(SM_GOTO_SLEEP);
	}
}

/* 
 * calc checksun as tcp/ip checksun
 */
unsigned short cal_chksum(const void *addr, int len)
{      
       int nleft=len;
       int sum=0;
       unsigned short *w= (unsigned short *)addr;
       unsigned short answer=0;

       while(nleft>1)
       {       sum+=*w++;
               nleft-=2;
       }
       if( nleft==1)
       {       *(unsigned char *)(&answer)=*(unsigned char *)w;
               sum+=answer;
       }
       sum=(sum>>16)+(sum&0xffff);
       sum+=(sum>>16);
       answer=~sum;
       return answer;
}
/*
// delay subroutine:
void delay_n_ms(int mscnt) {
  	for (;mscnt > 0; mscnt--)
    		delay_1ms();
}
*/
// puts via UART
void UART_puts(unsigned char *buf, int len) {
  	for(; len > 0; len--) {
    		while(UART_send_char(*buf));
    		buf++;
  	}
}

/*
 * 握手建立连接(当作发起端)
 */
 /*
short UART_connect()
{
	typ_msg_word msg_buffer;
        typ_UART_stat_word Ustat;
	unsigned char recBuf[MAX_BUF_SIZE];
	unsigned char sendBuf[6];
	unsigned short checksum=0,recLen=0; 
		
	SPT_set(64);
	UART_disconnect();
	
	msg_buffer.s_word = sys_msg(SM_STAY_AWAKE);
		
	UART_init(UART_ON | UART_232_ON | UART_8_DATA_BITS | UART_BAUD_115200);
	UART_fcntl(UART_fcntl(UART_F_INQ) | UART_F_NO_CTS);
	delay_n_ms(3);
	
	//握手数据帧: 一位的起始位:  0xBE 
        //             两位的长度:    0x0006
        //             一位的握手数据:0xAA
        //             两位的校验位
        sendBuf[0] = 0xBE;
        sendBuf[1] = 0x00;
        sendBuf[2] = 0x06;
        sendBuf[3] = 0xAA;
        checksum = cal_chksum((void *)sendBuf, 4);
	sendBuf[4] = checksum >> 8;
	sendBuf[5] = checksum & 0xFF;
	UART_puts(sendBuf,6);
	
        SPT_set(64 );
        while(1)
	{
		msg_buffer.s_word = sys_msg(SM_STAY_AWAKE);
		if (msg_buffer.bits.comm_data)
			break;
		if (msg_buffer.bits.time_out)
		{
			UART_disconnect();
			return 1;
		}
	}
	
        //握手回复帧: 一位的起始位:0xBE
        //             两位的长度:0x0001 
        //             一位的握手数据:0x55
        //             两位的校验位
        memset( (void*)recBuf, 0, sizeof(recBuf) );
	do {
		recBuf[recLen++] = (unsigned char)UART_get_char();
		if( 1 == recLen && recBuf[0] != 0xBE )
			recLen = 0;
		else if ( recLen > 3 && recLen == ( (((unsigned short)recBuf[1])<<8) + recBuf[2] ) )
			break;
		else if ( recLen == 6 )
			break;
		Ustat.l_word = UART_stat();
        } while (Ustat.bits.buff_data_available);
        
        if ( recLen != 6 || recBuf[0] != 0xBE || recBuf[3] != 0x55 || cal_chksum( (unsigned short *)recBuf,recLen) )
        {
 		UART_disconnect();       
        	return 3;
        }
	
	return 0;
}
*/
/*
 * 握手建立连接(当作响应端)
 */
short UART_open()
{
	typ_msg_word msg_buffer;
        typ_UART_stat_word Ustat;
	unsigned char recBuf[MAX_BUF_SIZE];
	unsigned char sendBuf[6];
	unsigned short checksum=0,recLen=0; 
		
	SPT_set(64);
	UART_disconnect();
	
	msg_buffer.s_word = sys_msg(SM_STAY_AWAKE);
	
	UART_init(UART_ON | UART_232_ON | UART_8_DATA_BITS | UART_BAUD_115200);
	UART_fcntl(UART_fcntl(UART_F_INQ) | UART_F_NO_CTS);
	delay_n_ms(3);
	
	SPT_set(64);
/*	
	while(1)
	{
		msg_buffer.s_word = sys_msg(SM_STAY_AWAKE);
		if (msg_buffer.bits.comm_data)
			break;
		if (msg_buffer.bits.time_out)
		{
			UART_disconnect();
			return 1;
		}
	}

        memset( (void*)recBuf,0,sizeof(recBuf) );
	do {
		recBuf[recLen++] = (unsigned char)UART_get_char();
		if( 1 == recLen && recBuf[0] != 0xBE )
			recLen = 0;
		else if ( recLen > 3 && recLen == ( (((unsigned short)recBuf[1])<<8) + recBuf[2] ) )
			break;
		else if (recLen == 6)
			break;
		Ustat.l_word = UART_stat();
        } while (Ustat.bits.buff_data_available);        
        
        if ( recLen != 6 || recBuf[0] != 0xBE || recBuf[3] != 0xAA || cal_chksum((unsigned short *)recBuf,recLen) )
        {
        	sendBuf[0] = 0xBE;
        	sendBuf[1] = 0x00;
        	sendBuf[2] = 0x06;
		sendBuf[3] = 0xFF;
		checksum = cal_chksum((void *)sendBuf, 4);
		sendBuf[4] = checksum >> 8;
		sendBuf[5] = checksum & 0xFF;
		UART_puts(sendBuf,6);
		UART_disconnect();
        	return 2;	
        }
        else
        {
        	sendBuf[0] = 0xBE;
        	sendBuf[1] = 0x00;
        	sendBuf[2] = 0x06;
		sendBuf[3] = 0x00;
		checksum = cal_chksum((void *)sendBuf, 4);
		sendBuf[4] = checksum >> 8;
		sendBuf[5] = checksum & 0xFF;
		UART_puts(sendBuf,6);
		return 0;	
	}
	*/
}

/*
 * 发送数据
 * 参数 in: sendBuf  要发送的数据区(不包括一位开始位、2位长度位和2位校验位)
 *        : sendLen  要发送的数据区的长度(byte)
 *
 * 返回: 0 发送成功  -1 发送失败
 */
short UART_send(const void *sendBuf, unsigned short sendLen)
{
	typ_msg_word msg_buffer;
        typ_UART_stat_word Ustat;	
	unsigned char pBuf[MAX_BUF_SIZE];
	unsigned char recBuf[MAX_BUF_SIZE];
	unsigned short checksum=0,recLen=0; 
	
	if(sendLen + 5 > MAX_BUF_SIZE)
		return -1;
	
	Ustat.l_word = UART_stat();
	if (!Ustat.bits.UART_on)
		return 1;	
	move_cursor(0,gcCommMsgRow);
	puts("发送数据...");
	if(gcCommMsgRow<6)
		gcCommMsgRow += 2;
	else
		gcCommMsgRow = 2;
	
	pBuf[0] = 0xBE;
	pBuf[1] = (sendLen+5) >> 8;
	pBuf[2] = (sendLen+5) & 0xFF;	
	
	memcpy( (void *)&pBuf[3], sendBuf, sendLen );
	
	checksum = cal_chksum( (unsigned short *)pBuf, sendLen+3);
	pBuf[sendLen+3] = checksum >> 8;
	pBuf[sendLen+4] = checksum & 0xFF;
	
	UART_puts( pBuf, sendLen+5);
	
	SPT_set(64);	
	//等到确认数据到来
	while(1)
	{
		msg_buffer.s_word = sys_msg(SM_STAY_AWAKE);
		if (msg_buffer.bits.comm_data)
			break;
		if (msg_buffer.bits.time_out)
			return 2;
	}
	
        memset( (void*)recBuf,0,sizeof(recBuf) );
	do {
		recBuf[recLen++] = (unsigned char)UART_get_char();
		if( 1 == recLen && recBuf[0] != 0xBE )
			recLen = 0;
		else if ( recLen > 3 && recLen == ( (((unsigned short)recBuf[1])<<8) + recBuf[2] ) )
			break;
		else if (recLen == 6)
			break;
		Ustat.l_word = UART_stat();
        } while (Ustat.bits.buff_data_available);
        
        if ( recLen != 6 || recBuf[0] != 0xBE || recBuf[3] != 0x00 || cal_chksum((unsigned short *)recBuf,recLen) )
        	return 3;
        		
	return 0;
}

⌨️ 快捷键说明

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