按一下触摸屏,会发送很多次同样的指令,如何提取到其中一个完整的数据包?现有程序如下,可以实现串口助手和从机单片机的通讯来启停机,但改成触摸屏作主机就不行了:
单片机源程序如下:
#include "stc81.h"
#include "chuankou.h"
#include "string.h"
extern uchar bufferR[20];
extern uchar bufferS[20];
extern uchar wptr;
void carryout()
{
if(bufferR[7]==0x00&&bufferR[8]==0xaa)//设置/动作
{
if(bufferR[9]==0x00&&bufferR[10]==0x00)//启动
{
RUN11=0;
RUN21=0;
RUN31=0;
RUN41=0;
STOP11=0;
STOP21=0;
STOP31=0;
STOP41=0;
delay(50);
bufferR[9]=0x10;
bufferR[10]=0x10;
wptr=0;
bufferS[0]=0xaa;
bufferS[1]=0x10;
bufferS[2]=0x00;
bufferS[3]=0x00;
bufferS[4]=0x00;
bufferS[5]=0x02;
bufferS[6]=0x58;
bufferS[7]=0x13;
delay(50);
Uart3SendB(bufferS,8);
}
if(bufferR[9]==0x00&&bufferR[10]==0x01)
{
RUN11=1;
RUN21=1;
RUN31=1;
RUN41=1;
STOP11=1;
STOP21=1;
STOP31=1;
STOP41=1;
delay(50);
// memset(bufferR,1,sizeof(bufferR));
bufferR[9]=0x10;
bufferR[10]=0x10;
wptr=0;
bufferS[0]=0xaa;
bufferS[1]=0x10;
bufferS[2]=0x00;
bufferS[3]=0x00;
bufferS[4]=0x00;
bufferS[5]=0x02;
bufferS[6]=0x58;
bufferS[7]=0x14;
delay(50);
Uart3SendB(bufferS,8);
}
}
}
void collectdata()
{
}
void dataprocess()
8{
//CRC校验,符合进入下一步;若不符合,丢掉数据包
if (bufferR[0]==0xaa)//从机地址校验
{
if (bufferR[2]==0x00&&bufferR[3]==0x00)//数据首地址校验
{
if (bufferR[1]==0x10)//主站下发功能
{
carryout();
}
// else if (bufferR[1]=0x03)//从站上传数据
// {
// collectdata();
// }
}
}
}
void main()
{
P5M1 = 0;
P5M0 = 0;
Uart3Init();
IE2 = 0x08; //mabey be set in the T0
EA = 1; //mabey be set in the T0
while(1)
{
RE485 = 1;
dataprocess();
delay(100);
delay(100);
delay(100);
|