打印
[485通信]

串口通信

[复制链接]
1116|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
花儿一样ha|  楼主 | 2019-7-6 08:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stc15.h"
#include "UART.h"
#include "queue.h"
#define uchar unsigned char
#define uint  unsigned int
#define uint32   unsigned long
sbit LED1=P1^5;
sbit LED2=P1^4;
extern QUEUE que;

unsigned int pos =0;

void Uart_IRQHendler() interrupt 8 using 1
{
     unsigned char recv_data[13]={0};
     unsigned char j;
     unsigned char ucTemp=0;
          if(S2CON & 0x01)
          {
              LED2=0;
              S2CON &=0xFE;
              ucTemp=S2BUF;
           /* if(pos<14)
                 {
                     recv_data[pos++] = ucTemp;//将队列中的数据均取出到这个数组中                 
                     aa = ((aa<<8)|ucTemp);
                 }
                 else                 
                      pos =0;  
             if(aa==0xFFCFFFF)
                 {
                    size = pos;
                    pos = 0;   //读出后复位
                    aa =0;
                 }
                 else
                    size = 0;
               if(size>0)
             {   
                 LED1=0;
                 for(j=0;j<size;j++)
                 {
                     Uart1Data_SendByte(recv_data[j]);
                 }
                 size =0;
             }  */               
                 
                 Uart1Data_SendByte(ucTemp);
              /*S2BUF=ucTemp;
              while(!(S2CON & 0x02));
                  S2CON &= 0xFD;*/
         
          // Uart1Data_SendByte(ucTemp);


             if((que._head+1)%QUEUE_MAX_SIZE!=que._tail)
              {
                que._data[que._head]=ucTemp;
                que._head=(que._head+1)%QUEUE_MAX_SIZE;
              }
   }
   }
  


   
   int main(void)
  {
    unsigned char recv_data[20]={0};
    unsigned char size =0;
    unsigned char i,j,temp;
    uint16 aa=0;
    uint32 aa=0;
    LED1=1;
    LED2=1;


    Uart2Init();
    Uart1Init();
    EA =1;
    IE2=0x01;
//    while(1){}

// 20190624      
//    while(1)
//    {
//        while(!RI);
//        RI=0;
//        temp=SBUF;
//        SBUF=temp;
//        while(!TI);
//        TI=0;
//    }
//  end

    queue_reset();
    //delayms(10);
  
   while(1)
   {      
      if(queue_size(que)>0)
         {
             for(i=0;i<queue_size(que);i++)
              {
                 if(que._tail!=que._head)//非空
                 {
                     temp = que._data[que._tail];
                     que._tail= (que._tail+1)%QUEUE_MAX_SIZE;
                 }//此函数是从队列中取一个数据,而且从队尾开始取
                  
                 if(pos==0&&temp!=0xEE)//指令第一个字节必须是帧头,否则跳过
                     continue;
                                 
                 if(pos<QUEUE_MAX_SIZE)
                 {
                     recv_data[pos++] = temp;//将队列中的数据均取出到这个数组中                 
                     aa = ((aa<<8)|temp);
                 }
                 else                 
                      pos =0;      //溢出后复位
                 
                 //if(temp==0x0A)
                 if(aa==0xFFFFFCFF)
                 {
                    size = pos;
                    pos = 0;   //读出后复位
                    aa =0;
                 }
                 else
                    size = 0;
                // Uart2Data_SendByte(temp);
             }
            
             if(size>0)
             {   
                 LED1=0;
                 for(j=0;j<size;j++)
                 {
                     Uart1Data_SendByte(recv_data[j]);
                 }
                 size =0;
             }
         }
     
    }
}   
这个不定长的串口接收程序,出现的问题是用电脑的串口助手,发送一些列是数据比如EE 13 14等,都可准确接收并且返回电脑,但是用大彩屏就是另一个相当于上位机的发送一样的数据,就出现了乱码,而且只接收到一个数据,不知道原因是什么,谢谢大家可以帮忙看一下

使用特权

评论回复

相关帖子

沙发
airwill| | 2019-7-13 10:37 | 只看该作者
检查一下信号波形, 也可以两个对比一下, 可能不是软件的问题

使用特权

评论回复
板凳
牧天尊| | 2019-7-15 22:16 | 只看该作者
两者发送数据有多大差异?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

31

帖子

0

粉丝