您的位置 首页 动态

组态王与单片机通信协议3

此浮点数格式的转换:1)ASCII码到浮点数:floatC4toD(char*c){BYTEHd[30],Jiema[30];floatDTc[30];floatDecimal=0;memset(Hd

浮点数格局的转化:

1)ASCII码到浮点数:

float C4toD(char * c)

{

BYTEHd[30], Jiema[30];

floatDTc[30];

floatDecimal = 0;

memset(Hd, 0, sizeof(Hd));

memset(Jiema, 0, sizeof(Jiema));

memset(DTc, 0, sizeof(DTc));

floatreturnflo = 0;

BOOLShuFU = FALSE, JieFU = FALSE;

if((c[7] > 0x40) && (c[7] < 0x47))

Hd[7] = ((c[7]– 0x37) & 0x0f);

else if((c[7] > 0x60) && (c[7] < 0x67))

Hd[7] = ((c[7]– 0x57) & 0x0f);

else

Hd[7] = ((c[7]– 0x30) & 0x0f);

if((c[6] > 0x40) && (c[6] < 0x47))

Hd[6] = ((c[6]– 0x37) & 0x0f);

else if((c[6] > 0x60) && (c[6] < 0x67))

Hd[6] = ((c[6]– 0x57) & 0x0f);

else

Hd[6] = ((c[6]– 0x30) & 0x0f);

DTc[2] = (float)(((float)(Hd[6] * 16.0) + (float)(Hd[7])) / 256.0);

if((c[5] > 0x40) && (c[5] < 0x47))

Hd[5] = ((c[5]– 0x37) & 0x0f);

else if((c[5] > 0x60) && (c[5] < 0x67))

Hd[5] = ((c[5]– 0x57) & 0x0f);

else

Hd[5] = ((c[5]– 0x30) & 0x0f);

if((c[4] > 0x40) && (c[4] < 0x47))

Hd[4] = ((c[4]– 0x37) & 0x0f);

else if((c[4] > 0x60) && (c[4] < 0x67))

Hd[4] = ((c[4]– 0x57) & 0x0f);

else

Hd[4] = ((c[4]– 0x30) & 0x0f);

DTc[1] = (float)((((float)(Hd[4] * 16.0) + (float)Hd[5]) + DTc[2]) / 256.0);

if((c[3] > 0x40) && (c[3] < 0x47))

Hd[3] = ((c[3]– 0x37) & 0x0f);

else if((c[3] > 0x60) && (c[3] < 0x67))

Hd[3] = ((c[3]– 0x57) & 0x0f);

else

Hd[3] = ((c[3]– 0x30) & 0x0f);

if((c[2] > 0x40) && (c[2] < 0x47))

Hd[2] = ((c[2]– 0x37) & 0x0f);

else if((c[2] > 0x60) && (c[2] < 0x67))

Hd[2] = ((c[2]– 0x57) & 0x0f);

else

Hd[2] = ((c[2]– 0x30) & 0x0f);

Decimal = (float)(((float)(Hd[2] * 16) + (float)(Hd[3]) + DTc[1])/ 256.0);

if((c[1] > 0x40) && (c[1] < 0x47))

Jiema[1] = ((c[1]– 0x37) & 0x0f);

else if((c[1] > 0x60) && (c[1] < 0x67))

Jiema[1] = ((c[1]– 0x57) & 0x0f);

else

Jiema[1] = ((c[1]– 0x30) & 0x0f);

if((c[0] > 0x40) && (c[0] < 0x47))

Jiema[0] = ((c[0]– 0x37) & 0x0f);

else if((c[0] > 0x60) && (c[0] < 0x67))

Jiema[0] = ((c[0]– 0x57) & 0x0f);

else

Jiema[0] = ((c[0]– 0x30) & 0x0f);

ShuFU = ((Jiema[0] & 0x08) >> 3) > 0;

JieFU = ((Jiema[0] & 0x04) >> 2) > 0;

Jiema[2] = (Jiema[0] & 0x03) * 16 + Jiema[1];

if(JieFU)

returnflo = (float)pow(2, (-1) * Jiema[2]) * Decimal;

else

returnflo = (float)pow(2, Jiema[2]) * Decimal;

if(ShuFU)

returnflo = (-1) * returnflo;

return returnflo;

}

2)浮点数到ASCII码:

void D4toC(char * c,float d)

{

BYTEi = 0, Jiema = 0;

charinbyte1[30];

BOOLShuFu = FALSE, JieFu = FALSE;

intinbyte2 = 0, inbyte3 = 0, inbyte4 = 0;

charafterbyte2[30], afterbyte3[30], afterbyte4[30];

floatF_afterbyte2 = 0, F_afterbyte3 = 0, F_afterbyte4 = 0;

memset(inbyte1, 0x30, sizeof(inbyte1));

memset(afterbyte2, 0x30, sizeof(afterbyte2));

memset(afterbyte3, 0x30, sizeof(afterbyte3));

memset(afterbyte4, 0x30, sizeof(afterbyte4));

inbyte1[10] = 0x0;

afterbyte2[10] = 0x0;

afterbyte3[10] = 0x0;

afterbyte4[10] = 0x0;

if(d == 0)

{

for(int j = 0; j < 8; j++)

c[j] = 0x30;

return;

}

if(d < 0)

{

ShuFu = TRUE;

d = (-1) * d;

}

while(d > 1)

{

d =(float)(d / 2.0);

i ++;

}

while(d <= 0.5)

{

JieFu = TRUE;

d = (float)(d * 2.0);

i ++;

}

if(d == 1)

{

for(int j = 2; j < 8; j++)

c[j] = 0x46;

}

else

{

inbyte2 = (int)(d * 256);

F_afterbyte2 = (d * 256) – (int)(d * 256);

inbyte3 = (int)(F_afterbyte2 * 256);

F_afterbyte3 = (F_afterbyte2 * 256) – (int)(F_afterbyte2 * 256);

inbyte4 = (int)(F_afterbyte3 * 256);

F_afterbyte4 = (F_afterbyte3 * 256) – (int)(F_afterbyte3 * 256);

itoa(inbyte2, afterbyte2, 16);

itoa(inbyte3, afterbyte3, 16);

itoa(inbyte4, afterbyte4, 16);

if(inbyte2 == 0)

{

c[2] = 0x30;

c[3] = 0x30;

}

else if(inbyte2 < 16)

{

c[2] = 0x30;

c[3] = afterbyte2[0];

}

else

{

c[2] = afterbyte2[0];

c[3] = afterbyte2[1];

}

if(inbyte3 == 0)

{

c[4] = 0x30;

c[5] = 0x30;

}

else if(inbyte3 < 16)

{

c[4] = 0x30;

c[5] = afterbyte3[0];

}

else

{

c[4] = afterbyte3[0];

c[5] = afterbyte3[1];

}

if(inbyte4 == 0)

{

c[6] = 0x30;

c[7] = 0x30;

}

else if(inbyte4 < 16)

{

c[6] = 0x30;

c[7] = afterbyte4[0];

}

else

{

c[6] = afterbyte4[0];

c[7] = afterbyte4[1];

}

}

if(JieFu)

{

if(i > 0x3f)

i = 0x3f;

}

else if(i > 0x32)

i = 32;

if(ShuFu)

i = i | 0x80;

if(JieFu)

i = i | 0x40;

itoa(i, inbyte1, 16);

if(inbyte1 == 0)

{

c[0] = 0x30;

c[1] = 0x30;

}

else if(i < 16)

{

c[0] = 0x30;

c[1] = inbyte1[0];

}

else

{

c[0] = inbyte1[0];

c[1] = inbyte1[1];

}

for(i = 0; i < 8; i ++)

{

if((c[i] > 0x60) && (c[i] < 0x67))

c[i] = c[i] – 0x20;

}

c[8] = 0x00;

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/dongtai/256788.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部