用串口發送數據,然后用串口助手接收數據,但接收到的數據和程序里寫的數據不一樣,請問這是怎么回事?串口發送的數據:uchar send[8]={0x01,0x03,0x00,0x00,0x00,0x02,0xC4,0x0B}
這是串口助手接收到的數據:跟程序里的數據不一樣,請問大家這是哪里錯了啊?
后來我又把發送的數據全部改成了0X00,結果如下
結果接收到的是這個
這是完整程序:
- #include <stc12c5a.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar rec[9];//接收
- uchar send[8]={0x01,0x03,0x00,0x00,0x00,0x02,0xC4,0x0B};//發送數據
- uchar num=0;
- int i;
- void delay(uint z)//延時函數
- {
- uint x,y;
- for(x=z;x>0;x--);
- for(y=110;y>0;y--);
- }
- void init() //系統初始化
- {
- TMOD |=0X20;//定時器T1,方式2,波特率由PCON寄存器的SMOD決定
- SCON=0x50; //REN RI TI,RI為0,TI為0
- //串行口1方式1 SCON是串行口1的串行控制寄存器,REN為1,允許接收
- PCON=0x00;//各工作方式波特率加倍
- TH1=0xfD;//9600bps@11.0592
- TL1=0xfD;
- TR1=1; //定時器1中斷打開
- EA=1;//cpu總中斷允許位,1為開放中斷
- ES=1;// 1允許串行口中斷
- }
- void main()
- {
- uchar m=0;
- init();
- while(1)
- {
- EA=0;//關中斷
-
- for(i=0;i<8;i++)
- {
- SBUF=send[i];//發送數據
- while(TI==0);
- {
- } //數據發送結束時TI自動置1
- TI=0;
- }
- EA=1;//開中斷
- delay(1000);
- }
- }
- void UART_1() interrupt 4
- {
- RI=0; //RI置0
- rec[num]=SBUF;
- if(rec[0]==0x01)
- {
- num++;
- if(num>=9)
- {
- num=0;
- }
- }
- }
復制代碼 |