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

📄 l_refine1.~cpp

📁 一个监控系统的上位机界面
💻 ~CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "L_Refine1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "MSCommLib_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 //初始化

        mSend1->Text   ="";
        mSend2->Text   ="";
        mReceive1->Text="";
        mReceive2->Text="";
        mReceive3->Text="";
        MSComm1->CommPort=1;          // COM1端口打开
        MSComm1->InputMode=0;         //设置传入数据的格式,0表示文本形式
        MSComm1->PortOpen=true;       //打开串口
        MSComm1->RThreshold =1;
        MSComm1->Settings=            //设置串口的属性波特率、奇偶校验、数据位和停止位
        ComboBox1->Text+","+
        ComboBox2->Text+","+
        ComboBox3->Text+","+
        ComboBox4->Text;
        Shape1->Brush->Color=clGreen;
        Rb_Com1->Checked=true;        //单选按钮被选中

        //使显示的完整  ???
        Ltest_Status->Caption="当前的端口号是COM1   "+String(" ");

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  //退出系统
        AnsiString msg="是否真的要关闭系统!!!";
        if(Application->MessageBoxA(msg.c_str(),Application->Title.c_str(),MB_OK))
           { if(MSComm1->PortOpen==true)   MSComm1->PortOpen=false;
             exit(EXIT_SUCCESS);
           }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
  //设置波特率,并在Ltest_Status中显示
        MSComm1->Settings=
        ComboBox1->Text+","+
        ComboBox2->Text+","+
        ComboBox3->Text+","+
        ComboBox4->Text;
        //在Ltest_Status中显示串口状态
        Ltest_Status->Caption=ComboBox1->Text+","+
        ComboBox2->Text+","+
        ComboBox3->Text+","+
        ComboBox4->Text;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComboBox2Change(TObject *Sender)
{
 //设置奇偶校验位,并在Ltest_Status中显示
        MSComm1->Settings=
        ComboBox1->Text+","+
        ComboBox2->Text+","+
        ComboBox3->Text+","+
        ComboBox4->Text;// file:设置串口的属性波特率、奇偶校验、数据位和、//停止位。
        //在Label5中显示串口状态
        Ltest_Status->Caption=ComboBox1->Text+","+
        ComboBox2->Text+","+
        ComboBox3->Text+","+
        ComboBox4->Text;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComboBox3Change(TObject *Sender)
{
   //设置数据位,并在Ltest_Status中显示
        MSComm1->Settings=
        ComboBox1->Text+","+
        ComboBox2->Text+","+
        ComboBox3->Text+","+
        ComboBox4->Text;
        //在Ltest_Status中显示串口状态
        Ltest_Status->Caption=ComboBox1->Text+","+
        ComboBox2->Text+","+
        ComboBox3->Text+","+
        ComboBox4->Text;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComboBox4Change(TObject *Sender)
{
 //设置停止位 ,并在Ltest_Status中显示
        MSComm1->Settings=
        ComboBox1->Text+","+
        ComboBox2->Text+","+
        ComboBox3->Text+","+
        ComboBox4->Text;
        //在Ltest_Status中显示串口状态
        Ltest_Status->Caption=ComboBox1->Text+","+
        ComboBox2->Text+","+
        ComboBox3->Text+","+
        ComboBox4->Text;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BtnReceiveClick(TObject *Sender)
{
   //接收数据
        OleVariant safearray_inp;
        long  len;
        int m,n,dot;
        double sum=0.0;
        String strtemp1,strtemp2,strtemp3,Strl;
        if(MSComm1->CommEvent ==comEvReceive) //事件值为2表示接收缓冲区内有字符
        {
           safearray_inp=MSComm1->Input;      //读缓冲区
           Strl= safearray_inp.AsType(varString);    //转换成String型
         //len = sizeof(safearray_inp);


           len= Strl.Length();
       // Ltest_Status->Caption =String(len);

           if(m_ctrlHexDiaplay->Checked )
              for(int i=1,k=1;k<=len;k+=3,i++) //将数组转换为String型变量
                 { //将字符以十六进制方式送入临时变量strtemp(字符串变量)存放,注意这里加入一个空隔
                   strtemp1=IntToHex(Strl[k],2);
                   strtemp2=IntToHex(Strl[k+1],2);
                   strtemp3=IntToHex(Strl[k+2],2);
                   sum=L_Convert( strtemp1,strtemp2,strtemp3);

                   if(i==1)
                     {mReceive1->Text =""; //在接收框mReceive1显示相应字符串
                      mReceive1->Text =String(sum); }
                   else if(i==2)
                     {mReceive2->Text =""; //在接收框mReceive2显示相应字符串
                      mReceive2->Text =String(sum); }
                   else if(i==3)
                     {mReceive3->Text =""; //在接收框mReceive3显示相应字符串
                      mReceive3->Text =String(sum); }

                  // if(i==3)  i=0;
                 }
          else    //待做 (现在是:以十进制显示时接收到的字符全部显示在mReceive1中)
            mReceive1->Text =mReceive1->Text+ Strl+'\r'+'\n';

   //  UpdateData(false); //更新编辑框内容      ?????
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BtnSendClick(TObject *Sender)
{
    //发送数据 ,待修改(因为都发送到同一个目的地)
        AnsiString Str,hexdata;
        int len,pos1 ;
        //判断mSend1 和mSend2  中是否有要发送的字符
        if(mSend1->Text==""&&mSend2->Text=="")
          { AnsiString msg="您没有数据要发送!";
            Application->MessageBoxA(msg.c_str(),"Operate Error",MB_OK);}
        if(mSend1->Text!="")
          { Str=Trim(mSend1->Text);
            pos1=Str.Pos('.');
            if(Judge(pos1,Str))      // 如果待发送的数据合法
               analyze_num(Str);
            else
              {  AnsiString msg="您输入的数据有误!";
                 Application->MessageBoxA(msg.c_str(),"Operate Error!",MB_OK);}
          }
        if(mSend2->Text!="")
          { Str=Trim(mSend2->Text);
            pos1=Str.Pos('.');
            if(Judge(pos1,Str))           analyze_num(Str);
            else
              {  AnsiString msg="您输入的数据有误!";
                 Application->MessageBoxA(msg.c_str(),"Operate Error!",MB_OK);}
          }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Rb_Com2Click(TObject *Sender)
{
  //改变所暂用的串口为串口2
        if( Rb_Com2->Checked ==true)
          {   MSComm1->PortOpen=false;         //关闭串口
              MSComm1->CommPort=2;             //设置端口号

              MSComm1->InputMode=0;            //设置传入数据的格式,0表示文本形式
              MSComm1->PortOpen=true;          //打开串口
              MSComm1->RThreshold =1;
              // 设置串口的属性波特率、奇偶校验、数据位和,停止位
              MSComm1->Settings=
              ComboBox1->Text+","+
              ComboBox2->Text+","+
              ComboBox3->Text+","+
              ComboBox4->Text;
              Shape1->Brush->Color=clGreen;
              Ltest_Status->Caption="当前的端口号是COM2     "+String(" ");
            }

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

void __fastcall TForm1::Rb_Com1Click(TObject *Sender)
{
   //改变所暂用的串口为串口1
        if( Rb_Com1->Checked ==true)

           {  MSComm1->PortOpen=false;         //关闭串口
              MSComm1->CommPort=1;             //设置端口号

              MSComm1->InputMode=0;            //设置传入数据的格式,0表示文本形式
              MSComm1->PortOpen=true;          //打开串口
              MSComm1->RThreshold =1;
              //设置串口的属性波特率、奇偶校验、数据位和,停止位
              MSComm1->Settings=
              ComboBox1->Text+","+
              ComboBox2->Text+","+
              ComboBox3->Text+","+
              ComboBox4->Text;
              Shape1->Brush->Color=clGreen;
              Ltest_Status->Caption="当前的端口号是COM1     "+String(" ");
            }

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



void __fastcall TForm1::analyze_num(String RecNum)
{
  //或以十进制发送,或分解数据以十六进制发送

        String refineStr;
        int pos;//len=RecNum.Length() ;
        int i=RecNum.Pos('.');
        if( m_ctrlHexSend->Checked )     //如果以十六进制发送
        {
         /* if(i==2&&RecNum[1]=='0')

        mini_Str=String(0)+String(i-1);
        Pos=i-1;
        front_Str=RecNum.SubString(1,Pos);
        rear_Str= RecNum.SubString(Pos+2,len-Pos-1);   */
          //把小数点分割出去,并重新组字符串
          if(i==2&&RecNum[1]=='0')
            {refineStr=RecNum.SubString(3,RecNum.Length()-2);
             for(int i=1;i<=(6 - RecNum.Length());i++)
              refineStr=refineStr+String(0);
             pos=0;
             Delete_dot(pos,refineStr);}
          else if(i==2&& RecNum[1]!='0')
            {refineStr=RecNum.SubString(1,1)+RecNum.SubString(3,3);
             pos=1;
             Delete_dot(pos,refineStr);}
          else if(i==3)
            {refineStr=RecNum.SubString(1,2)+RecNum.SubString(4,2);
             pos=2;
             Delete_dot(pos,refineStr);}
          else if(i==4)
            {refineStr=RecNum.SubString(1,3)+RecNum.SubString(5,1);
             pos=3;
             Delete_dot(pos,refineStr);}
          else
            {refineStr=RecNum.SubString(1,4);
             pos=4;
             Delete_dot(pos,refineStr);}
        }
        else //以ASCII(十进制)进制数据发送
             MSComm1->Output=StringToOleStr(RecNum);
}

int __fastcall TForm1::String2Hex(String RecStr)
{
   //按字节把十进制转换成十六进制数
        int len;
        char ch;
        int hexdata,lowhexdata,hexdatalen;
        String toSengStr;
        len=RecStr.Length() ;
        for(int i=1;i<=len;i++)
           {
            char lstr,hstr=RecStr[i];
            //判断hstr/str[i]是否是非法字符(低位)
            if(hstr==' ')    {i++;  continue;  }
            i++;
            if(i>len)      break;
            //把str[i]的合法字符附给lstr(高位)
            lstr=RecStr[i];
            //转换成十六进制
            hexdata=ConvertHexChar(hstr);
            lowhexdata=ConvertHexChar(lstr);
            if((hexdata==16)||(lowhexdata==16))       break;
            else
              { hexdata=hexdata*16+lowhexdata;
                ch=(char)hexdata;
                Ltest_Status->Caption =String(ch);
                MSComm1->Output=StringToOleStr(ch); //发送十六进制数据
              }
            i++;   //因为有空格
                                                     //                    **********可作文章********
            //   toSengStr=toSengStr+" "+String(hexdata);
          }
        hexdatalen=toSengStr.Length();   //可去掉
        //返回长度
        return hexdatalen;
}

int __fastcall TForm1::ConvertHexChar(char ch)
{
   //转换成相应的十六进制数
        if((ch>='0')&&(ch<='9'))
             return ch-0x30;                   //  0x30==48
        else if((ch>='A')&&(ch<='F'))
             return ch-'A'+10;
        else if((ch>='a')&&(ch<='f'))
             return ch-'a'+10;
        else return (-1);

}

double __fastcall TForm1::L_Convert(String dot, String zs, String xs)
{
   // 根据小数点的位置,高位,低位 组合成一个双精度浮点数
        int dot1,Zhengs,Xiaos;
        double sum=0.0;

        dot1=StrToInt(dot);
        Zhengs=StrToInt(zs);
        Xiaos=StrToInt(xs);
        sum=Zhengs*100+Xiaos;
        for(int i=1;i<=(4-dot1);i++)
           sum*=0.1;
        return sum;

}

void __fastcall TForm1::Delete_dot(int Posi, String Recestr)
{
   //把数据分割,重组成String形式为:** ** **
        String final_Str,front_Str,mini_Str,rear_Str;

        front_Str=String(0)+String(Posi);
        mini_Str =Recestr.SubString(1,2);
        rear_Str =Recestr.SubString(3,2);
        final_Str=front_Str+" "+mini_Str+" "+rear_Str;
        String2Hex(final_Str);
}


bool __fastcall TForm1::Judge(int pos, String Str)
{
   // 判断待发送的字符是否合法
        if(pos==0&&Str.Length()>=5)  return 0;
        else if(pos==2&&Str[1]=='0'&&Str.Length()>=7) return 0;
        else if(pos==2&&Str[1]!='0'&&Str.Length()>=6)  return 0;
        else if(3<=pos&&pos<=5&&Str.Length()>=6)         return 0;
        else                             return 1;

}

⌨️ 快捷键说明

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