📄 sci.c
字号:
default:{re1_temp=0;re1_temp1=0;re1_temp2=0;}break;}
} break;
case 1:
{
re1_buf[re1_no++]=temp; //如果re1_temp=1表示已经收到同步字,对控制字进行处理
/***************判断6个数据是否收完********************/
if(re1_no==6)
{
re1_no=0;
if(!((re1_buf[0]^0x01)&0x8e)) //控制字节低四位是否为0001,E=0? if1
{
L1=(re1_buf[0]&0x40);
S1=(re1_buf[0]&0x20);
D1=(re1_buf[0]&0x10);
if(D1)
{
if(re1_buf[4]!=0x00)
{re1_data();md1_temp=0; }
}
if(md1_temp)
{
if(S1) re1_yuan=re1_buf[3]; //有源地址,反馈信息以此为目的地址
else re1_yuan=0x00; //目的地址为re1_yuan
temp=crc(re1_buf);
if(re1_buf[5]==crc(re1_buf)) //收到一组6个数据以后crc校验
{
if(L1) {re1_xinxi=re1_buf[2];re1_temp=2;xinxi1_no=1;} //本帧有信息字
else re1_xinxi=0x00;
re1_zlb=re1_buf[1];
switch(re1_zlb)
{
case 0x20: {re1_data(); //数据设置成功
o_v[0]=set_ov[0];
o_v[1]=set_ov[1];
if(sci_set1) //串口设置
{
get_ovbuf();
tx_data(0x20,re_yuan);}
if(key_set==1)
{key_set=0;
ov_temp[1]=ov_buf[1];
ov_temp[2]=ov_buf[2];
ov_temp[3]=ov_buf[3];
ovv=(uint)(ov_buf[1]-0x30)*100+(uint)(ov_buf[2]-0x30)*10+(uint)(ov_buf[3]-0x30);
ovv_val=ovv;
}
}break;
case 0x22: {re1_data(); //数据设置成功
l_v[0]=set_lv[0];
l_v[1]=set_lv[1];
if(sci_set2) //串口设置
{
get_lvbuf();
tx_data(0x22,re_yuan);}
if(key_set==2) //键盘设置
{key_set=0;
lv_temp[1]=lv_buf[1];
lv_temp[2]=lv_buf[2];
lv_temp[3]=lv_buf[3];
lv=(uint)(lv_buf[1]-0x30)*100+(uint)(lv_buf[2]-0x30)*10+(uint)(lv_buf[3]-0x30);
lv_val=lv;} }break;
case 0x24: {re1_data(); //数据设置成功
ov_v[0]=set_ovv[0];
ov_v[1]=set_ovv[1];
if(sci_set3) //串口设置
{
get_ovvbuf();
tx_data(0x24,re_yuan);}
if(key_set==3) //键盘设置
{key_set=0;
vot_temp[1]=vot_buf[1];
vot_temp[2]=vot_buf[2];
vot_temp[3]=vot_buf[3];
vot=(uint)(vot_buf[1]-0x30)*100+(uint)(vot_buf[2]-0x30)*10+(uint)(vot_buf[3]-0x30);
vot_val=vot;} }break;
case 0xe0: {re1_data();
if(sci_set1)
tx_data(0xE0,re_yuan);
else if(++key_err1>=3)
{
ov_buf[1]=ov_temp[1];
ov_buf[2]=ov_temp[2];
ov_buf[3]=ov_temp[3];
}
else {tx1_data(0x20,tx_mudi);t_temp3=0;}
}break;
case 0xe2: {re1_data();
if(sci_set2)
tx_data(0xE2,re_yuan);
else if(++key_err2>=3)
{
lv_buf[1]=lv_temp[1];
lv_buf[2]=lv_temp[2];
lv_buf[3]=lv_temp[3];
}
else {tx1_data(0x22,tx_mudi);t_temp3=0;}
}break;
case 0xe4: {re1_data();
if(sci_set3)
tx_data(0xE4,re_yuan);
else if(++key_err3>=3)
{
vot_buf[1]=vot_temp[1];
vot_buf[2]=vot_temp[2];
vot_buf[3]=vot_temp[3];
}
else {tx1_data(0x24,tx_mudi);t_temp3=0;}
}break;;
case 0xf0:{tx1_data(tx1_zlb_end,tx1_add_end);re1_data();}break;
default:break;
}
}
else
{ //校验错误,反馈信息给上位机
re1_data();
tx1_data(0xf0,re1_yuan);
}
}
}
else re1_data(); //不符合控制字节规则,else1
}
} break;
//*****************判断6个数据收完,何种命令结束***************************//
case 2: //如果re_temp=2表示帧中包含信息字,对信息字处理
{
re1_buf[re1_no++]=temp;
/***************判断6个数据是否收完********************/
if(re1_no==6)
{
re1_no=0;
if(re1_buf[5]==crc(re1_buf)) //收到一组6个数据以后crc校验
{
switch(re1_zlb)
{
case 0x01:
{
if(re1_buf[0]==xinxi1_no)
{
temp2=(xinxi1_no-1)*4;
for(i=1;i<5;i++)
data_buf[temp2+i-1]=re1_buf[i];
if (xinxi1_no==re1_xinxi)
{re1_data();init_flag=1;} //接收完毕
else xinxi1_no++;
}
else re1_data();
} break;
case 0x30: if((re1_buf==0xf0)&&(re1_buf[1]==re1_buf[3])&&(re1_buf[2]==re1_buf[4])&&(re1_xinxi==0x01))
{o_v[0]=re1_buf[1];
o_v[1]=re1_buf[2];
tx_data(0x30,re_yuan);}
else re1_data();break;
case 0x32: if((re1_buf==0xf2)&&(re1_buf[1]==re1_buf[3])&&(re1_buf[2]==re1_buf[4])&&(re1_xinxi==0x01))
{l_v[0]=re1_buf[1];
l_v[1]=re1_buf[2];
tx_data(0x32,re_yuan);}
else re1_data(); break;
case 0x34: if((re1_buf==0xf4)&&(re1_buf[1]==re1_buf[3])&&(re1_buf[2]==re1_buf[4])&&(re1_xinxi==0x01))
{ov_v[0]=re1_buf[1];
ov_v[1]=re1_buf[2];
tx_data(0x34,re_yuan);}
else re1_data(); break;
default:break;
}
}
else //校验错误
{
re1_data();
tx1_data(0xf0,re1_yuan);
}
}
} break;
default: break;
}
}
else //发送中断
{
TI1=0;
switch(tx1_temp)
{case 0: {
if(tx1_temp1==0) {SBUF1=0xD7;tx1_temp1=1;}
else{
SBUF1=0x09;tx1_temp1=0;tx1_temp2++;
if(tx1_temp2==3)
{
tx1_no=0;
tx1_temp=1;
tx1_buf[0]=0x31;
tx1_buf[1]=tx1_zlb;
switch(tx1_buf[1])
{
case 0x10: {tx1_buf[0]=(tx1_buf[0]|0x40);tx1_buf[2]=0x01;tx1_xinxi=0x01;}break;
case 0x12: {tx1_buf[0]=(tx1_buf[0]|0x40);tx1_buf[2]=0x01;tx1_xinxi=0x01;}break;
case 0x14: {tx1_buf[0]=(tx1_buf[0]|0x40);tx1_buf[2]=0x01;tx1_xinxi=0x01;}break;
default:tx1_buf[2]=0x00;
}
tx1_buf[3]=0x00;
tx1_buf[4]=tx1_add;
tx1_buf[5]=crc(tx1_buf);
}
}
} break;
case 1: {
SBUF1=tx1_buf[tx1_no++];
if(tx1_no==6)
{
tx1_no=0;
switch(tx1_zlb)
{
case 0x10:
{
tx1_temp=2;
tx1_buf[0]=0xf0;
tx1_buf[1]=set_ov[0];
tx1_buf[2]=set_ov[1];
tx1_buf[3]=set_ov[0];
tx1_buf[4]=set_ov[1];
tx1_buf[5]=crc(tx1_buf);
} break;
case 0x12:
{
tx1_temp=2;
tx1_buf[0]=0xf2;
tx1_buf[1]=set_lv[0];
tx1_buf[2]=set_lv[1];
tx1_buf[3]=set_lv[0];
tx1_buf[4]=set_lv[1];
tx1_buf[5]=crc(tx1_buf);
} break;
case 0x14:
{
tx1_temp=2;
tx1_buf[0]=0xf4;
tx1_buf[1]=set_ovv[0];
tx1_buf[2]=set_ovv[1];
tx1_buf[3]=set_ovv[0];
tx1_buf[4]=set_ovv[1];
tx1_buf[5]=crc(tx1_buf);
} break;
default:tx1_temp=3;break;
}
}
} break;
case 2: {
SBUF1=tx1_buf[tx1_no++];
if(tx1_no==6)
{
tx1_xinxi--;tx1_no=0;
if(tx1_xinxi)
{}
else tx1_temp=3;
}
} break;
case 3: {
tx1_flag=0;
tx1_temp=0;
tx1_temp1=1;
tx1_temp2=0;
if(tx1_zlb!=0xf0) //如果帧类别不为0xf0,则将最终发送的帧类别代码暂存tx_zlb_end
{
tx1_zlb_end=tx1_zlb; //如果收到校验错误帧类别,以该帧类别发送数据
tx1_add_end=tx1_add;
}
if(tx1_fifo) //缓冲区是否有新的帧需要发送?
{tx1_fifo=0;tx1_data(tx1_zlb_temp,tx1_add_temp);}
} break;
default: break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -