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

📄 unit1.cpp

📁 很不错的ADSL串口测试程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
  return  strDetail;

}

//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain::SB48VPSetOrder(void)             //电压系数设置命令
{
  PADSLCommand CommandNode;
  int          Index;
  float  FD,FS;    //目标值,显示值
  int  ISX,IDX;        //显示电压系数,目标电压系数
try
{
  for (Index = 0 ; Index<=PCommandList->Count - 1 ;++Index)
  {
    delete(PCommandList->Items[Index]);
  }
  PCommandList->Clear();


  CommandNode = new TADSLCommand;
  CommandNode->CommandCode = 2;       //命令代号
  if(FAuto48VSet)
  {
    FD  = StrToFloat(Edit1->Text.Trim());  //目标电压值
    FS  = StrToFloat(LMDLEDLab48V->Caption.Trim());  //当前电压值
    ISX = StrToInt(LB48VShow->Caption.Trim());   //当前电压系数
    if(LMDLEDLab48V->Caption.Trim()=="00.00")
    {

      //SpeedButton2->Click();   //关闭自动调试
      FAuto48VSet = false;

    }
    else
    {
      //FAuto48VSet = true;
      IDX = int((FD/FS)*ISX);
    }  
    CommandNode->Value = IDX;
    Edit48VPSet->Text = IntToStr(IDX);
  }
  else
  {
    CommandNode->Value = StrToInt(Edit48VPSet->Text.Trim());
  }
  CommandNode->AnswerLen   = 9;                           //命令返回字节数

  PCommandList->Add(CommandNode);

  IsTransmit = true;
  FIsClearBuf = true;
 // ShowMessage("操作命令提交!");
}
catch(Exception & E)
{
 //ShowMessage("请输入有效数据!!!");
}
}
//---------------------------------------------------------------------------
void __fastcall TFrmADSLmain::SBAddressSet(void)      //地址设置命令
{
  PADSLCommand CommandNode;
  int          Index;
try
{
  for (Index = 0 ; Index<=PCommandList->Count - 1 ;++Index)
  {
    delete(PCommandList->Items[Index]);
  }
  PCommandList->Clear();

  CommandNode = new TADSLCommand;
  CommandNode->CommandCode =6;   //命令代号

  CommandNode->Value = StrToInt("0x"+CBoxAdrSET->Text);
  CommandNode->AnswerLen   = 9;                   //命令返回字节数

  //增加命令
  PCommandList->Add(CommandNode);

  IsTransmit = true;
  FIsClearBuf = true;

  //ShowMessage("操作命令提交!");
}
catch(Exception & E)
{
 //ShowMessage("请输入整数!!!");
}
}
//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain::YbCommDevice1CommNotify(TObject *Sender,
      int NotifyType)
{
   ShowPortSetInfo();         //显示端口配置信息

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

void __fastcall TFrmADSLmain::ShowPortSetInfo(void)
{
   StatusBar1->Panels->Items[1]->Text = "当前连接方式:串口"
                                          +IntToStr(YbCommDevice1->PortNo)+"COM"
                                          +"  波特率:"+ArrayBound[YbCommDevice1->Baud];

}
//---------------------------------------------------------------------------
void __fastcall TFrmADSLmain::SB48VOrderXH(void)           //登陆设置命令
{
  PADSLCommand CommandNode;
  int          Index;
try
{
  for (Index = 0 ; Index<=PCommandList->Count - 1 ;++Index)
  {
    delete(PCommandList->Items[Index]);
  }
  PCommandList->Clear();

  CommandNode = new TADSLCommand;
  CommandNode->CommandCode =4;   //命令代号
  CommandNode->Value1 = Edit48VOrder->Text.Trim();
  CommandNode->AnswerLen   = 9;                   //命令返回字节数

  //增加命令
  PCommandList->Add(CommandNode);

  IsTransmit = true;
  FIsClearBuf = true;

  //ShowMessage("操作命令提交!");
}
catch(Exception & E)
{
 //ShowMessage("请输入整数!!!");
}
}
//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain::N2Click(TObject *Sender)
{
  Memo1->Lines->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain::N4Click(TObject *Sender)
{

 SBStartTask->Click();
}
//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain::CB_SaveClick(TObject *Sender)
{
    if (CB_Save->Checked)     //  若需要保存通讯记录
       {
         FLogComFileName = FLogPath + "\\Com" +FormatDateTime("yyyymmddhhnnss",Now()) + ".txt";
         FILE *LogFile = fopen(FLogComFileName.c_str(),"a+");
         AnsiString strdata = "****************START LOGING********************\n";
         fputs(strdata.c_str(),LogFile);   //写入通讯数据
         fclose(LogFile);
        }
    else
    {
         FILE *LogFile = fopen(FLogComFileName.c_str(),"a+");
         AnsiString strdata = "****************END LOGING********************\n";
         fputs(strdata.c_str(),LogFile);   //写入通讯数据
         fclose(LogFile);
    }

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

void __fastcall TFrmADSLmain::CBAddressChange(TObject *Sender)
{
try
{
   //DevAdd =StrToInt("0x"+CBAddress->Items->Strings[CBAddress->ItemIndex]);  //获取设备号
   DevAdd =StrToInt("0x"+CBAddress->Text);  //获取设备号

 }
 catch(Exception &E)
 {
  ShowMessage("请输入有效数据!!!");
 }

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

void __fastcall TFrmADSLmain::L1Click(TObject *Sender) //打开日志文件
{
 if (!FLogComFileName.IsEmpty())
 {
   WinExec(AnsiString("explorer "+FLogComFileName).c_str(),SW_SHOWNORMAL);
 }
}
//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain::P2Click(TObject *Sender)
{
  SBStopTask->Click();        
}
//---------------------------------------------------------------------------
void __fastcall TFrmADSLmain::CreateLogDir(void)  //创建log目录
{
  if(!DirectoryExists(FLogPath))
 {
   if(!CreateDir(FLogPath))
   {
     ShowMessage("创建日志log目录失败!!!");
   }
 }

}
//---------------------------------------------------------------------------
void __fastcall TFrmADSLmain::FormClose(TObject *Sender,
      TCloseAction &Action)
{
 if (PortIsOpen)
 {
  YbCommDevice1->Active = false;
 }
  if (ClientSocket1->Active)
  {
    ClientSocket1->Active= false;
  }

  //清除命令
  for (int Index = 0 ; Index<=PCommandList->Count - 1 ;++Index)
  {
    delete(PCommandList->Items[Index]);
  }
  PCommandList->Clear();
        
}
//---------------------------------------------------------------------------

__fastcall TFrmADSLmain::TFrmADSLmain(TComponent* Owner)
        : TForm(Owner)
{
    PortIsOpen = false;     //初始化系统变量
    IsTransmit = false;
    FAuto48VSet = false;  //自动调试标志
    Timer1->Interval = 400;
    DevAdd =StrToInt("0x"+CBAddress->Text);  //获取设备号
    FLogPath = ExtractFileDir(Application->ExeName); //存储目录初始化
    FLogPath = FLogPath + "\\log";
    CreateLogDir();                      //创建日志log目录
    for (int Index = 0 ;Index<2; ++Index)       //模拟量存储阀值数组初始化
    {
      FValve_V[Index] = 0;
    }
    CircleIndex = 0 ;            //命令代码轮寻初始化
    FIsClearBuf = false;        //设置清空缓存标志
    FIsRecvError = false;       //接收数据解析与校验正常

    //统计信息初始化
    FStatistics.SendCount        = 0;
    FStatistics.RecvCount        = 0;
    FStatistics.SendByteCount    = 0;
    FStatistics.RecvByteCount    = 0;
    FStatistics.RecvCountTimeOut = 0;
    FStatistics.RecvCountError   = 0;

    
    N1->Enabled = true;         //系统初始化时为串口连接方式
    SBPortSet->Enabled = true;
    EditTCP->Enabled = false;
    TCP1->Enabled = false;
    PortIsOpen = true;         //串口打开标志置位
    ShowPortSetInfo();         //显示端口配置信息
  try
   {
     YbCommDevice1->Active = true;
   }
  catch(Exception &e)
   {
     ShowMessage("YbCommDevice1: "+e.Message);
     if(!YbCommDevice1->SettingsDialog(this,true))
       Application->Terminate();
   }

  YbCommDevice1->PackageSize = 4096; //最大可发送 4096 个字节的数据包
  YbCommDevice1->PackageType = cptFrameTimeout; //用判断超时的方法接收数据包
  YbCommDevice1->UsePackage  = true; //启动数据包 (可以随时启动和停止, 与 Active 属性无关)

  PCommandList->Clear();
}
//---------------------------------------------------------------------------


void __fastcall TFrmADSLmain::SBAddrSETClick(TObject *Sender)
{
 SBAddressSet();//设置地址
}
//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain::Edit48VPSetChange(TObject *Sender)
{
   SB48VOrderXH();//登陆命令设置
}
//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain::UpDown2Click(TObject *Sender,
      TUDBtnType Button)
{
  SB48VOrderXH();//登陆命令设置
}
//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain::EditTCPChange(TObject *Sender)         //TCP连接
{
try
  {
    ClientSocket1->Active = false;
    AnsiString StrIP = EditTCP->Text.Trim();
    ClientSocket1->Address = StrIP.SubString(1,StrIP.Pos(":")-1);   //IP
    ClientSocket1->Port = StrToInt((StrIP.SubString(StrIP.Pos(":")+1,StrIP.Length())).Trim()); //端口号
    ClientSocket1->Active = true;
    PortIsOpen = false;
    EditTCP->Enabled = true;
    TCP1->Enabled = true;
    N1->Enabled = false;
    SBPortSet->Enabled = false;
    StatusBar1->Panels->Items[1]->Text = "当前连接方式:TCP "+StrIP;
  }
  catch(Exception &E)
  {
     ShowMessage(E.Message);
  }
}
//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain::ClearShow(void)
{
try
  {
   if((CurrentCommand.CommandCode != 4)&&(CurrentCommand.CommandCode != 2))
   {                                      //如果是登陆失败,则不清除显示
      LMDLEDLab48V->Caption = "00.00";   // 48V直流电压
      Shape220VXH->Brush->Color = clSilver ;
      Label220VXH->Caption = "无电";
      LB48VShow->Caption = "0000";
   }
  }
  catch(Exception &E)
  {
     ShowMessage(E.Message);
  }
}
//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain::SpeedButton1Click(TObject *Sender)   //自动调整电压系数
{
  SpeedButton1->Enabled = false;
  SpeedButton2->Enabled = true;
  FAuto48VSet = true;   //设置自动调整电压系数标志
  SB48VOrderXH();//登陆命令设置
  UpDown2->Enabled = false;
          
}
//---------------------------------------------------------------------------

void __fastcall TFrmADSLmain::SpeedButton2Click(TObject *Sender)   //取消自动调整
{
  SpeedButton1->Enabled = true;
  SpeedButton2->Enabled = false;
  FAuto48VSet = false;   //取消自动调整电压系数标志
  UpDown2->Enabled = true;
}
//---------------------------------------------------------------------------

 void __fastcall TFrmADSLmain::AllCollect(void)   //全部采集命令
{
  PADSLCommand CommandNode;
  int          Index;
try
{
  for (Index = 0 ; Index<=PCommandList->Count - 1 ;++Index)
  {
    delete(PCommandList->Items[Index]);
  }
  PCommandList->Clear();

  CommandNode = new TADSLCommand;
  CommandNode->CommandCode =0;     //命令代号
  CommandNode->AnswerLen   = 11;                   //命令返回字节数

  //增加命令
  PCommandList->Add(CommandNode);

  IsTransmit = true;
  FIsClearBuf = true;

  //ShowMessage("操作命令提交!");
}
catch(Exception & E)
{
 //ShowMessage("请输入整数!!!");
}

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

void __fastcall TFrmADSLmain::A48VCollect(void)   //48V电压系数查询命令
{
  PADSLCommand CommandNode;
  int          Index;
try
{
  for (Index = 0 ; Index<=PCommandList->Count - 1 ;++Index)
  {
    delete(PCommandList->Items[Index]);
  }
  PCommandList->Clear();

  CommandNode = new TADSLCommand;
  CommandNode->CommandCode =3;     //命令代号
  CommandNode->AnswerLen   = 10;             //命令返回字节数

  //增加命令
  PCommandList->Add(CommandNode);

  IsTransmit = true;
  FIsClearBuf = true;

  //ShowMessage("操作命令提交!");
}
catch(Exception & E)
{
 //ShowMessage("请输入整数!!!");
}

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

⌨️ 快捷键说明

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