串口接收不定长字符串

zhongaiying 2009-07-08 08:27:43
void InitSio(void)
{
u16 RELOAD_COUNT = 0;

//使用独立波特率发生器作为波特率发生器
S2CON = 0x50; //0101,0000 8位可变波特率,无奇偶校验位,允许接收

RELOAD_COUNT = (int)(XTAL/BaudRateVal/32/12 + 0.5);

RELOAD_COUNT = 256 - RELOAD_COUNT;
//BRT = 0xFA;//22.118400MHz,12T,SMOD=0,9600bps
BRT = RELOAD_COUNT;
AUXR = 0x11; // T0x12,T1x12,UART_M0x6,BRTR,S2SMOD,BRTx12,EXTRAM,S1BRS
// ES = 1; //允许串口1中断
// ES2 = 1
IE2 = 0x01; //允许串口2中断,ES2=1
EA = 1; //开总中断

}
//----------------------------------------------------------------------------------------------
void WrRiBuf(unsigned char c)
{

disjoin_send(c);
//ribuf[rirear] = c;
//rirear++;
if(rirear >= RIBUFMAX)
{
rirear = 0;
Rs485dislow++;
}
if(ribufcount != RIBUFMAX) ribufcount ++;

}

//-----------------------------------------------------------------------------
unsigned char RdRiBuf()
{
char p;
unsigned char c;
p = rirear - ribufcount;
if(p < 0) p = RIBUFMAX + p;

ribufcount--;
c = ribuf[p];
return c;
}

//----------------------------------------------------------------------------------------------
void IntComm() interrupt 8
{
u16 k = 0;

k = S2CON ;
k = k & 0x01;
if(k==1) //RI
{
IE2 = 0x00; //允许串口2中断,ES2=1
S2CON = S2CON & 0xFE; //1111,1110
WrRiBuf(S2BUF);
IE2 = 0x01; //允许串口2中断,ES2=1
S2CON = S2CON & 0xFE; //1111,1110
}
else
{
S2CON = S2CON&0xfd;
}

}
void handlecomm()
{
char c;
//char i;
if (ribufcount)
c = RdRiBuf();

if(ribuf[0] == 'F'&&ribuf[1] == 'F'){RIBUFMAX = 20;}
if(ribuf[0] == 'A'&&ribuf[1] == '0'){RIBUFMAX = 23;}

manage_key485DataTestdislowSub(); //显示接收到的数据
}

//-----------------------------------------------------
void disjoin_send( u8 hex)//把十六进制数拆开ASCII显示在屏上
{
//把十六进制数拆开ASCII
u8 high;
u8 low;
high = hex>>4;//去掉低四位,直接得到高四位
low = hex&0x0f;
high = CharHex(high);//转换为高位的ASCII码
low = CharHex(low);

ribuf[rirear] = high;
delayAny(2);
rirear++;
ribuf[rirear] = low;
delayAny(2);
rirear++;
ribuf[rirear]= 0x20;
rirear++;
//if(rirear>RIBUFMAX*3-1)rirear1 = 0;
//if(rirear >=20) Rs485dislow = Rs485dislow+1;
}

//把一个十六进制转换为一个ASCII字符
unsigned char CharHex(char ch)
{
if((ch>=0)&&(ch<=9))
return ch+0x30;

else if((ch>=10)&&(ch<=15))

return ch+'A'-10;//即变为从0开始对应字符A

else return (-1);

}
main()
{
handlecomm();
}
我的函数怎么得不到我想要的东西.我想接收一个字符串转一行.
1,我开始接收更短的可以.我变换长字符长度更长的串是.在变回短的.短的后面就增加了几个字符.如果我重新开机.又可以接收到短的长度.不会在后面多字符.
2.我接收时怎么有错码.要不disjoin_send().直接PC发时不会错码.
...全文
312 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhongaiying 2009-07-08
  • 打赏
  • 举报
回复
请高手们帮帮我吧.我都弄了10多天了.好郁闷.

zhongaiying 2009-07-08
  • 打赏
  • 举报
回复
谢谢.
-狙击手- 2009-07-08
  • 打赏
  • 举报
回复
#include "stc12C5A32.h"
#include"serial.h"
#include"common.h"
#include "max7456osd.h"
#include"processmainmenu.h"
#include"key.h"
#include<intrins.h>
#include <string.h>
//idata char txBuffer[TBUF_SIZE];

int RIBUFMAX = 2;
idata unsigned char tibufcount=0,ribufcount=0;
idata unsigned char tirear,rirear;
idata unsigned char tibuf[TIBUFMAX];
idata unsigned char ribuf[10];
idata unsigned char Ribuflength =3;
idata unsigned char ribufdis[30];
idata unsigned char ribufdiscount = 0;
bit ti_wait = 1;

void InitSio(void)
{
u16 RELOAD_COUNT = 0;

//使用独立波特率发生器作为波特率发生器
S2CON = 0x50; //0101,0000 8位可变波特率,无奇偶校验位,允许接收

RELOAD_COUNT = (int)(XTAL/BaudRateVal/32/12 + 0.5);

RELOAD_COUNT = 256 - RELOAD_COUNT;
//BRT = 0xFA;//22.118400MHz,12T,SMOD=0,9600bps
BRT = RELOAD_COUNT;
AUXR = 0x11; // T0x12,T1x12,UART_M0x6,BRTR,S2SMOD,BRTx12,EXTRAM,S1BRS
// ES = 1; //允许串口1中断
// ES2 = 1
IE2 = 0x01; //允许串口2中断,ES2=1
EA = 1; //开总中断

}
//----------------------------------------------------------------------------------------------
void WrRiBuf(unsigned char c)
{
// disjoin_send(c);
ribuf[rirear] = c;
rirear++;
if(rirear >= RIBUFMAX)
{
rirear = 0;
Rs485dislow++;
}
if(ribufcount != RIBUFMAX) ribufcount ++;

}

//-----------------------------------------------------------------------------
unsigned char RdRiBuf()
{
char p;
unsigned char c;
p = rirear - ribufcount;
if(p < 0) p = RIBUFMAX + p;

ribufcount--;
c = ribuf[p];
return c;
}

//----------------------------------------------------------------------------------------------
void IntComm() interrupt 8
{
u16 k = 0;

k = S2CON ;
k = k & 0x01;
if(k==1) //RI
{
IE2 = 0x00; //允许串口2中断,ES2=1
S2CON = S2CON & 0xFE; //1111,1110
WrRiBuf(S2BUF);
IE2 = 0x01; //允许串口2中断,ES2=1
S2CON = S2CON & 0xFE; //1111,1110

}
else
{
S2CON = S2CON&0xfd;

}
}
void Sio_PutChar(u8 ch)
{
//unsigned char temp = 0;
// WrTiBuf(ch);
unsigned char temp = 0;

// ES = 0; //关串口1中断
IE2 = 0x00; //关串口2中断,es2=0
// TI = 0; //清零串口1发送完成中断请求标志
//S2 Control S2SM0 S2SM1 S2SM2 S2REN S2TB8 S2RB8 S2TI S2RI
S2CON = S2CON & 0xFD; //B'11111101,清零串口2发送完成中断请求标志
// SBUF = i;
S2BUF = ch;
// while(TI ==0); //等待发送完成
do
{
temp = S2CON;
temp = temp & 0x02;
}while(temp==0);

// TI = 0; //清零串口发送完成中断请求标志
S2CON = S2CON & 0xFD; //B'11111101,清零串口2发送完成中断请求标志
// ES = 1; //允许串口1中断
// ES2 = 1
IE2 = 0x01;
}

void Sio_PrintStr(const u8 *msg, u8 len)
{
u8 index = 0;

while(len != index)
{
Sio_PutChar(msg[index]);
index++;
}
}
//-----------------------------------------------------------------------------
//int i = 0;

void handlecomm()
{

unsigned char c;
static unsigned char count = 0;

while(ribufcount)
{
c = RdRiBuf();
if ((c == 0xff) || (c == 0xa0))
{
Rs485dislow++;//转行标记
count = 0;
}
else
count ++;
disjoin_send(c,count);
manage_key485DataTestdislowSub(); //显示接收到的数据

}

}

idata u8 rirear1 = 0;

void disjoin_send( u8 hex)//把十六进制数拆开ASCII显示在屏上
{
//把十六进制数拆开ASCII
u8 high;
u8 low;
high = hex>>4;//去掉低四位,直接得到高四位
low = hex&0x0f;
high = CharHex(high);//转换为高位的ASCII码
low = CharHex(low);
if(rirear1>RIBUFMAX*3-1)rirear1 = 0;
ribufdis[rirear1] = high;
delayAny(2);
rirear1++;
ribufdis[rirear1] = low;
delayAny(2);
rirear1++;
ribufdis[rirear1]= 0x20;
rirear1++;


//if(rirear >=20) Rs485dislow = Rs485dislow+1;
}

//把一个十六进制转换为一个ASCII字符
unsigned char CharHex(char ch)
{
if((ch>=0)&&(ch<=9))
return ch+0x30;


else if((ch>=10)&&(ch<=15))

return ch+'A'-10;//即变为从0开始对应字符A

//else if((ch>='a')&&(ch<='f'))
//return ch+'a'-10;
else return (-1);

}

27,375

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧