你好!我也碰到和你一样的问题,
我用C语言写得接受上位机发送的数据,按照MODbusRTU格式写得帧头帧尾,如果接受到数据P2的指示灯会亮,可是我从上位机发送数据时,P2的指示灯并没有亮,也就是没有执行到这不!郁闷中!
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar tx_count,rx_temp,tx_number;
uchar rx_count;
uchar rxbuf[];
uchar txbuf[];
bit rx_ok;
void inittimer()
{
TMOD= 0x21;
TL1 = 0xfd;
TH1 = 0xfd;
TH0 = 0xf2;
TL0 = 0xdf;
TR0 = 1;
TR1 = 1;
ET0 = 1;
}
void uartcomm()
{
SCON =0x50;
ES = 1;
}
/***************************************************************************
定时器中断
***************************************************************************/
void timer1() interrupt 3
{
TH0=0xf2; //3.646ms interrupt
TL0=0xdf;
if(rx_count>=5) //超时后,若接收缓冲区有数则判断为收到一帧
{
rx_ok=1;
}
}
/***************************************************************************
modbus RTU模式
***************************************************************************/
void scomm() interrupt 4
{
if(TI)
{
TI = 0;
if(tx_count < tx_number) //是否发送结束
{
SBUF = txbuf[tx_count];
}
tx_count++;
}
if(RI)
{
rx_temp=SBUF;
if(rx_ok==0) //已接收到一帧数据,在未处理之前收到的数舍弃
{
//if(rx_count)
rxbuf[rx_count]=rx_temp;
rx_count++;
}
TH0=0xf2; //timer1 reset,count again
TL0=0xdf;
RI=0;
}
}
void main ()
{
inittimer();
uartcomm();
if (rx_ok)
{
P2=0x00;
}
}