⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 网吧电量采集、温湿度采集串口通信实例程序
💻 CPP
📖 第 1 页 / 共 4 页
字号:
  int  i;
  char * CmdStr;

  CmdStr =  StrBCD.c_str();
  if (Count*2-2>StrBCD.Length())
  {
   return char(0);
  }
  for (i=Count*2-2;i<Count*2-1;++i)
  {

    tmp1 = CmdStr[i]-'0';

    if(i+2 > StrBCD.Length())
    {
     tmp2 =0;
    }
    else
    tmp2 = CmdStr[++i]-'0';
    tmp  = tmp1*16+tmp2;
  }

  return char(tmp);

}
//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain:: DataRefresh(char * Buf)         //数据解析
{
  float      D[16];   //采集模拟量存储
  int        DS[8];   //采集数字量存储
  int        tmpSign;
  int        Index;
  Byte       btmp,btmpl,ch0,ch1,ch2,ch3;
  FILE       *LogFile;
  AnsiString  strdata;
  AnsiString  tmp;
  switch (CurrentCommand.CommandCode)
  {

   case 0:             //返回温度数据采集
   try
   {

     if(((Byte(Buf[24])>>7) & 0x01) == 1)
     {
       tmpSign = -1;    //负数
     }
     else
     {
       tmpSign = 1;   //正数
     }

     if(tmpSign == -1)
     {
      tmp = "-";
     }
     else
     {
      tmp = "";
     }
     tmp  = tmp+IntToStr(Byte(Buf[24])& 0x7F)+"."+IntToStr(Byte(Buf[25]))+"℃"; //温度


     btmp  = Byte(Buf[56]);      //数字量采集命令


   }
   catch (Exception & E)
   {
     DisplayData("=====温度数据采集异常=====");
   }

   //----------------------------界面显示数据-----------------------------//

    Label48VXH->Caption = tmp;   //温度

    btmpl = btmp & 0x01;          //烟雾量
    if (btmpl == 0)
    {
     Shape7->Brush->Color = clSilver;
    }
    else
    {
     Shape7->Brush->Color = clRed;
    }

    btmpl = btmp >> 1 & 0x01;          //按钮1
    if (btmpl == 0)
    {
     Shape220VXH->Brush->Color = clSilver;
    }
    else
    {
     Shape220VXH->Brush->Color = clRed;
    }

    btmpl = btmp >> 2 & 0x01;          //按钮2
    if (btmpl == 0)
    {
     Shape1->Brush->Color = clSilver;
    }
    else
    {
     Shape1->Brush->Color = clRed;
    }

    btmpl = btmp >> 3 & 0x01;          //按钮3
    if (btmpl == 0)
    {
     Shape2->Brush->Color = clSilver;
    }
    else
    {
     Shape2->Brush->Color = clRed;
    }

    btmpl = btmp >> 4 & 0x01;          //按钮4
    if (btmpl == 0)
    {
     Shape3->Brush->Color = clSilver;
    }
    else
    {
     Shape3->Brush->Color = clRed;
    }

    btmpl = btmp >> 5 & 0x01;          //按钮5
    if (btmpl == 0)
    {
     Shape4->Brush->Color = clSilver;
    }
    else
    {
     Shape4->Brush->Color = clRed;
    }

    btmpl = btmp >> 6 & 0x01;          //按钮6
    if (btmpl == 0)
    {
     Shape5->Brush->Color = clSilver;
    }
    else
    {
     Shape5->Brush->Color = clRed;
    }

    btmpl = btmp >> 7 & 0x01;          //按钮7
    if (btmpl == 0)
    {
     Shape6->Brush->Color = clSilver;
    }
    else
    {
     Shape6->Brush->Color = clRed;
    }

    break;


   case 1:    //数字量采集命令
   try
   {
     btmp  = Byte(Buf[6]);      //数字量采集命令
   }
   catch (Exception & E)
   {
     DisplayData("=====开关量采集异常=====");
   }

   //----------------------------------界面显示数据--------------------//

    btmpl = btmp & 0x01;          //烟雾量
    if (btmpl == 0)
    {
     Shape7->Brush->Color = clSilver;
    }
    else
    {
     Shape7->Brush->Color = clRed;
    }

    btmpl = btmp >> 1 & 0x01;          //按钮1
    if (btmpl == 0)
    {
     Shape220VXH->Brush->Color = clSilver;
    }
    else
    {
     Shape220VXH->Brush->Color = clRed;
    }

    btmpl = btmp >> 2 & 0x01;          //按钮2
    if (btmpl == 0)
    {
     Shape1->Brush->Color = clSilver;
    }
    else
    {
     Shape1->Brush->Color = clRed;
    }

    btmpl = btmp >> 3 & 0x01;          //按钮3
    if (btmpl == 0)
    {
     Shape2->Brush->Color = clSilver;
    }
    else
    {
     Shape2->Brush->Color = clRed;
    }

    btmpl = btmp >> 4 & 0x01;          //按钮4
    if (btmpl == 0)
    {
     Shape3->Brush->Color = clSilver;
    }
    else
    {
     Shape3->Brush->Color = clRed;
    }

    btmpl = btmp >> 5 & 0x01;          //按钮5
    if (btmpl == 0)
    {
     Shape4->Brush->Color = clSilver;
    }
    else
    {
     Shape4->Brush->Color = clRed;
    }

    btmpl = btmp >> 6 & 0x01;          //按钮6
    if (btmpl == 0)
    {
     Shape5->Brush->Color = clSilver;
    }
    else
    {
     Shape5->Brush->Color = clRed;
    }

    btmpl = btmp >> 7 & 0x01;          //按钮7
    if (btmpl == 0)
    {
     Shape6->Brush->Color = clSilver;
    }
    else
    {
     Shape6->Brush->Color = clRed;
    }
   break;


   case 2:                      // A相电压
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;
   tmp = IntToHex(ch1,1)+ IntToHex(ch0,2);
   Edit1->Text = tmp;
   }
   catch (Exception & E)
   {
     DisplayData("=====A相电压采集异常=====");
   }

   break;


   case 3:                      // B相电压
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;
   tmp = IntToHex(ch1,1)+ IntToHex(ch0,2);
   Edit2->Text = tmp;
   }
   catch (Exception & E)
   {
     DisplayData("=====B相电压采集异常=====");
   }

   break;

   case 4:                      // C相电压
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;
   tmp = IntToHex(ch1,1)+ IntToHex(ch0,2);
   Edit3->Text = tmp;
   }
   catch (Exception & E)
   {
     DisplayData("=====C相电压采集异常=====");
   }

   break;

  case 5:                      // A相电流
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;
   tmp = IntToHex(ch1,1)+ "."+IntToHex(ch0,2);
   Edit4->Text = tmp;
   }
   catch (Exception & E)
   {
     DisplayData("=====A相电流采集异常=====");
   }

   break;

   case 6:                      // B相电流
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;
   tmp = IntToHex(ch1,1)+ "."+IntToHex(ch0,2);
   Edit5->Text = tmp;
   }
   catch (Exception & E)
   {
     DisplayData("=====B相电流采集异常=====");
   }

   break;

   case 7:                      // C相电流
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;
   tmp = IntToHex(ch1,1)+ "."+IntToHex(ch0,2);
   Edit6->Text = tmp;
   }
   catch (Exception & E)
   {
     DisplayData("=====C相电流采集异常=====");
   }

   break;

   case 8:                      // A相功率
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;
   ch2 = Byte(Buf[14])-0x33;
   tmp = IntToHex(ch2,1)+"."+IntToHex(ch1,2)+IntToHex(ch0,2);
   Edit7->Text = tmp;
   }
   catch (Exception & E)
   {
     DisplayData("=====A相功率采集异常=====");
   }

   break;

   case 9:                      // B相功率
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;
   ch2 = Byte(Buf[14])-0x33;
   tmp = IntToHex(ch2,1)+"."+IntToHex(ch1,2)+ IntToHex(ch0,2);
   Edit8->Text = tmp;
   }
   catch (Exception & E)
   {
     DisplayData("=====B相功率采集异常=====");
   }

   break;


   case 10:                      // C相功率
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;
   ch2 = Byte(Buf[14])-0x33;
   tmp = IntToHex(ch2,1)+"."+IntToHex(ch1,2)+IntToHex(ch0,2);
   Edit9->Text = tmp;
   }
   catch (Exception & E)
   {
     DisplayData("=====C相功率采集异常=====");
   }

   break;

   case 11:                      // A相功率因素
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;

   tmp = IntToHex(ch1,2)+IntToHex(ch0,2);
   Edit10->Text = tmp.Insert(".",2);
   }
   catch (Exception & E)
   {
     DisplayData("=====A相功率因数采集异常=====");
   }

   break;

   case 12:                      // B相功率因素
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;

   tmp = IntToHex(ch1,2)+IntToHex(ch0,2);
   Edit11->Text = tmp.Insert(".",2);
   }
   catch (Exception & E)
   {
     DisplayData("=====B相功率因数采集异常=====");
   }

   break;

   case 13:                      // C相功率因素
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;

   tmp = IntToHex(ch1,2)+IntToHex(ch0,2);
   Edit12->Text = tmp.Insert(".",2);
   }
   catch (Exception & E)
   {
     DisplayData("=====C相功率因数采集异常=====");
   }

   break;


   case 14:                      // 电度
   try
   {
   ch0 = Byte(Buf[12])-0x33;
   ch1 = Byte(Buf[13])-0x33;
   ch2 = Byte(Buf[14])-0x33;
   ch3 = Byte(Buf[15])-0x33;

   tmp = IntToHex(ch3,1)+IntToHex(ch2,2)+IntToHex(ch1,2)+"."+IntToHex(ch0,2);
   Edit13->Text = tmp;

   }
   catch (Exception & E)
   {
     DisplayData("=====电度采集异常=====");
   }

   break;


   case 15:                      //地址设置
   case 16:                      //电度清零
   break;

} //end switch

}
//---------------------------------------------------------------------------

int __fastcall TFrmADSLmain::FindFrame(char *FrameStr, int FrameLen, char HeadChr)//寻找特征字符
{
  int  i;
  for( i= 0 ;i<=FrameLen-1;++i)
  {
  if (FrameStr[i] == HeadChr)
   {
      return i;
    }
  }
   return -1;
}
//---------------------------------------------------------------------------

bool __fastcall TFrmADSLmain::ReceiveOk(void)       //接收确认
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -