发表于:2005/2/4 10:45:00
#0楼
void Filt(char code[],char c)
{
if(c=='F')
{
if(code[0]>=0X7E)
{
code[1]=code[0]-0X7E;
code[0]=0X7E;
}
else
{
code[1]=0XFF; /*0XFF作为标记code[1]不可能产生0XFF*/
}
}
else if(c=='H')
{
if(code[0]!=0X7E)
{
code[1]=0xFE; /*转换完成标记*/
}
else
{
if(code[1]==0XFE)
{
code[1]=0XFF; /*接收下一个码的标记*/
}
else
{
code[0]=code[0]+code[1];
code[1]=0XFE;
}
}
}
发送时:
char SendChar[2]; /*存储发送的值*/
....
SendChar[0]=c; /*c为待发ASCII码*/
Filt(SendChar,'F');
if(SendChar(1)==0XFF)
{
..... /*发送SendChar[0]*/
}
else
{
...... /*发送SendChar[0],SendChar[1]*/
}
接收时:
char ReceiveChar[2]; /*存储接收的值*/
.....
ReceiveChar[0]=c0; /*c0接收的ASCII码*/
Filt(ReceiveChar,'H');
if(ReceiveChar[1]==0xFF)
{
ReceiveChar[1]=c1; /*c1为下一个*/
Filt(ReceiveChar,'H);
}
else if(ReceiveChar[1]==0xFE)
{
...... /*存储转换后的ReceiveChar[0]*/
}
{
if(c=='F')
{
if(code[0]>=0X7E)
{
code[1]=code[0]-0X7E;
code[0]=0X7E;
}
else
{
code[1]=0XFF; /*0XFF作为标记code[1]不可能产生0XFF*/
}
}
else if(c=='H')
{
if(code[0]!=0X7E)
{
code[1]=0xFE; /*转换完成标记*/
}
else
{
if(code[1]==0XFE)
{
code[1]=0XFF; /*接收下一个码的标记*/
}
else
{
code[0]=code[0]+code[1];
code[1]=0XFE;
}
}
}
发送时:
char SendChar[2]; /*存储发送的值*/
....
SendChar[0]=c; /*c为待发ASCII码*/
Filt(SendChar,'F');
if(SendChar(1)==0XFF)
{
..... /*发送SendChar[0]*/
}
else
{
...... /*发送SendChar[0],SendChar[1]*/
}
接收时:
char ReceiveChar[2]; /*存储接收的值*/
.....
ReceiveChar[0]=c0; /*c0接收的ASCII码*/
Filt(ReceiveChar,'H');
if(ReceiveChar[1]==0xFF)
{
ReceiveChar[1]=c1; /*c1为下一个*/
Filt(ReceiveChar,'H);
}
else if(ReceiveChar[1]==0xFE)
{
...... /*存储转换后的ReceiveChar[0]*/
}