搜索
bottom↓
回复: 4

pic,串口怎样发送和接收连续字符?

[复制链接]

出0入0汤圆

发表于 2018-1-19 18:05:55 | 显示全部楼层 |阅读模式
单片机怎样发送和接收连续的字符?比如发送AT命令:“AT+????”,返回的字符又怎么处理?

出0入0汤圆

 楼主| 发表于 2018-1-22 09:52:48 | 显示全部楼层
        unsigned char chr[]="AT+CGMI";
        unsigned char i;//, j;
//        j=sizeof(chr);
        if(Comm_Receive_OK)//接收完成
       { Comm_Receive_OK = 0;
                EN485=1;
                asm("nop"); asm("nop");//发送
                for(i=0;chr[i]!='\0';i++)
        //        for(i=0;i<j;i++)
                        {TXREG1 = chr[i];//发送  
                         }   
               
                asm("nop"); asm("nop");
                EN485=0;  
                }
         }

出10入61汤圆

发表于 2018-1-22 09:58:20 | 显示全部楼层
中断接收放到缓存,主程序里判断是否有期望的返回字符串或错误返回,同时要作超时出错处理

出0入20汤圆

发表于 2018-2-28 09:16:34 | 显示全部楼层
平时很少扫盲的,今天发2个代码:
#include "Use.h"
#include "Ver.H"
/**************************************************************
UART接收程序在这里完成P0.4接收!   
协议:
正常使用后,GPIO3=RXD;GPIO5=TXD!!GPIO4=0 进入下载模式!
波特率:9600 8 n 1
**************************************************************/
#define Uart_IN_IO  GPIO3                   //GPIO3=接收!
//********************************************************************
#define MAX_RS232_CNT                6                //通讯的字节数
//-----------------------------------------------------------------------
static unsigned char getBuf[MAX_RS232_CNT],cnt=0;         //接收缓存!
static unsigned char tRSok=0;
//********************************************************************
// 通讯格式: 05 H  L SUM
unsigned char Get_Uart_Rece_Dat (void ){
        unsigned char i,bt;

        BPS_Delay();
        //************************************************************
        bt=8;
        i=0;
        while(bt--){
                i>>=1;
                if(Uart_IN_IO) i |=0x80;
                //*************************************************************
                //延时等待下一个位
                BPS_Delay();
        }
        //************************************************************
        //等待结束位 (1)
        //BPS_Delay();
        return i;
}
//***********************************************************
//产生中断,开始接收!查询!
void Uart_Rece (void ){
        getBuf[cnt]=Get_Uart_Rece_Dat();
        tRSok=5;       
        if(++cnt >= MAX_RS232_CNT)        cnt=0;
}
//***********************************************************
//查询中断数据,是否接收完毕!!
unsigned char Get_Receive_DATA_COM0(void){
        unsigned char i;
        if(tRSok==0) return 0;
        if(tRSok--==1){
                i=cnt;
                cnt=0;
                return i;       
        }
        return 0;
}
//***********************************************************
//返回合成ASC的BCC
unsigned char Get_BCC(unsigned char *p,unsigned char Long){

        unsigned char Bcc;
        Bcc=0;
        while(Long--)        Bcc +=*p++;               
        return (Bcc);               
}
//***********************************************************
//查询中断数据
unsigned char  GetPLC_Do(void){

        unsigned char i,bcc;

        i=Get_Receive_DATA_COM0();
        if(i==0)        return 0;        //返回错误,未收到数据!!
        //****************************************************
        //02 05 46 4D
        bcc=Get_BCC(getBuf,3);
        //----------------------------------------------------
        if        (bcc != getBuf[3])                 return 2;         //校验码出错
        if         (getBuf[0] !=0x02)                 return 3;        //特征字不对!
        //*****************************************************************
        PRM.PRM0 =(getBuf[1]<<8 | getBuf[2]);
        //*****************************************************************
        SendToPC();
        return 1; //数据OK!!
}
/***********************************************************************
通讯的数据
02 03 20 25  //800
02 03 52 57  //850
02 03 84 89  //900
02 03 e8 ed  //1000
02 04 b0 b6  //1200
02 05 46 4D  //1350
02 05 dc e3  //1500
02 06 40 48  //1600
02 07 08 11  //1800
*************************************************************************/


出0入20汤圆

发表于 2018-2-28 09:17:37 | 显示全部楼层
#include "Use.h"
#include "Ver.H"
/********************************************************************************
正常使用后,GPIO3=RXD;GPIO5=TXD!!GPIO4=0 进入下载模式!
波特率:9600 8 n 1
********************************************************************************/
#define  UART_SEND_Pin GPIO4        //GPIO4=TXD
//********************************************************************************
/*
void BPS_Delay(void){
        TMR2IF=0;
        TMR2ON=1;
        while(!TMR2IF);
        TMR2ON=0;
        PR2=TtK;
}
*/
//********************************************************************************
/*
void UART_Out(unsigned char Dat){

        unsigned char i;          

        i=8;                                                  //1200 8.n 1        P d8 S
        UART_SEND_Pin=0;
        PR2=TtK;
        BPS_Delay();
        while(i--){
                UART_SEND_Pin=(bit)(Dat & 0x01);
                Dat>>=1;       
                BPS_Delay();
        }
        UART_SEND_Pin=1;
        BPS_Delay();
}
*/
//*********************************************************************************
//发送一串数据
//*********************************************************************************
void SendOut(unsigned char *p,unsigned char Long){
        unsigned char i,Dat;          
        while(Long--){
                CLRWDT();                                //定时喂狗.
                i=8;                                                  //9600 8.n 1        P d8 S
                Dat=*p++;
                BPS_Delay();
                UART_SEND_Pin=0;                        //开始位
                BPS_Delay();
                while(i--){
                        UART_SEND_Pin=(bit)(Dat & 0x01);
                        Dat>>=1;       
                        BPS_Delay();
                }
                UART_SEND_Pin=1;
                BPS_Delay();
        }
}
//*************************************************************
//发送频率到PC机
void SendToPC(void){
        unsigned char i,buf[20];
        static unsigned int delay=0;

        //if(delay++ >100){
                //发送到PC机
                i=sprintf(buf,"F=%u  U=%u \r\n",(unsigned int)PRM.PRM,(unsigned int)(PRM.Ua*1000));
                SendOut(buf,i);
                //delay=0;
//        }
}


回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-19 18:39

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表