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

📄 unit1.~cpp

📁 一个比较好用的串口调试程序,适合自己的学习用.
💻 ~CPP
📖 第 1 页 / 共 2 页
字号:
                    VarArrayRedim((Variant&)temp,1);
                    Rx_Buffer=(unsigned char)temp.GetElement(0);
                    c[1]=Rx_Buffer;
                    c[2]='\0';
                    tStr=DISPLAY->Lines->Strings[DISPLAY->Lines->Count-1]+c;
                }else tStr=DISPLAY->Lines->Strings[DISPLAY->Lines->Count-1]+(char)Rx_Buffer;
                break;
            }
            case 1://十六进制显示
            {
                ltoa((long)Rx_Buffer,c,16);//转换16进制
                if(c[0]>=97&&c[0]<=122)c[0]-=32;//把小写变为大写
                if(c[1]>=97&&c[1]<=122)c[1]-=32;//把小写变为大写
                if(c[1]=='\0')//接收单个HEX转换为标准格式 如B-->0B
                {
                    c[1]=c[0];
                    c[0]='0';
                }
                c[2]='\0';
                tStr=DISPLAY->Lines->Strings[DISPLAY->Lines->Count-1]+c+"  ";
                break;
            }
            case 2://十进制显示
            {
                ltoa((long)Rx_Buffer,c,10);//转换10进制
                if(c[1]=='\0')//接收个位数据
                {
                    c[2]=c[0];
                    c[1]=c[0]='0';
                }else if(c[2]=='\0')//接收双位数据
                {
                    c[2]=c[1];
                    c[1]=c[0];
                    c[0]='0';
                }
                c[3]='\0';//给c添上结束符
                tStr=DISPLAY->Lines->Strings[DISPLAY->Lines->Count-1]+c+"  ";
                break;
            }
        }
        DISPLAY->Lines->Strings[DISPLAY->Lines->Count-1]=tStr;//显示汉字或字符
    }
    if(T_AutoClear->Checked&&DISPLAY->Lines->Count>62)DISPLAY->Clear();//自动清空
    if(DISPLAY->Lines->Count>4096)//太长了,清空
    {
        DISPLAY->Clear();//自动清空
        DISPLAY->Text="串口调试助手提醒您,数据太长,自动清空!";
    }
}
//---------------------------------------------------------------------------
//接收一个字符
void __fastcall TForm1::MSComm1Comm(TObject *Sender)
{
    Timer1->Enabled=true;
    RX_Num++;
    if(RX_Num>65529)RX_Num=0;//溢出,重新填充
    RX_Count++;
    if(RX_Count>9999999)RX_Count=0;//太长了,清掉
    RXCount->Caption="RX:"+IntToStr(RX_Count);
}

//计数器清空
void __fastcall TForm1::ClearTXRXClick(TObject *Sender)
{
    RX_Count=0;
    TX_Count=0;
    RXCount->Caption="RX:0";
    TXCount->Caption="TX:0";
}
//---------------------------------------------------------------------------
//控制显示与否
void __fastcall TForm1::T_StopClick(TObject *Sender)
{
    if(Display_Enable)
    {
        if(MSComm1->PortOpen)
        {
            MSComm1->PortOpen=false;//关闭串口
            MSComm1->PortOpen=true;//再次打开串口
            //利用这个语句把串口的缓存全部清空
        }
        Display_Enable=false;
        T_Stop->Caption="继续显示";
    }else
    {
        Display_Enable=true;
        T_Stop->Caption="停止显示";
    }
}
//---------------------------------------------------------------------------
//显示方式选择
void __fastcall TForm1::T_Type_HexClick(TObject *Sender)
{
    if(T_Type_Hex->Checked)Display_Type=1;//十六进制显示
    else Display_Type=2;//十进制显示
}
//---------------------------------------------------------------------------
//显示方式选择
void __fastcall TForm1::T_Type_DecClick(TObject *Sender)
{
    if(T_Type_Hex->Checked)Display_Type=1;//十六进制显示
    else Display_Type=2;//十进制显示    
}
//---------------------------------------------------------------------------



void __fastcall TForm1::FrontShowClick(TObject *Sender)
{
    static bool Front_Show=false;
    if(Front_Show)
    {
        Front_Show=false;
        FrontShow->Caption="TOP";
        SetWindowPos(Form1->Handle,HWND_NOTOPMOST,0,0,0,0,(SWP_NOSIZE|SWP_NOMOVE));
        SetWindowPos(Form2->Handle,HWND_NOTOPMOST,0,0,0,0,(SWP_NOSIZE|SWP_NOMOVE));
        //非前端显示
    }else
    {
        Front_Show=true;
        FrontShow->Caption="OFF";
        SetWindowPos(Form1->Handle,HWND_TOPMOST,0,0,0,0,(SWP_NOSIZE|SWP_NOMOVE));
        SetWindowPos(Form2->Handle,HWND_TOPMOST,0,0,0,0,(SWP_NOSIZE|SWP_NOMOVE));
        //前端显示
    }
}
//---------------------------------------------------------------------------
//发送文件
void __fastcall TForm1::Send_FilesClick(TObject *Sender)
{
    if(SendFileEn)
    {
        Timer2->Interval=1000;//2ms发送一个数据
        Timer2->Enabled=false;//发送中断关闭
        Send_Files->Caption="发送文件";
        SendFileEn=false;
    }else//开始发送文件
    {
        Timer2->Interval=1;//1ms发送一个数据
        Timer2->Enabled=true;//使能中断
        Send_Files->Caption="终止发送";
        SendFileEn=true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Close_UartClick(TObject *Sender)
{
    Form1->Close();//关闭程序
}
//---------------------------------------------------------------------------
//自动发送开启与否
void __fastcall TForm1::Auto_SendClick(TObject *Sender)
{
    if(Auto_Send->Checked)
    {
        Timer2->Enabled=true;//使能定时器二
        Timer2->Interval=StrToInt(AutoSend_Time->Text);
    }else Timer2->Enabled=false;//关掉定时器二
}
//---------------------------------------------------------------------------
//定时器二 ,自动发送
void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
    static int n=1;
    AnsiString str=Send_Box->Text;//取得发送文本
    OleVariant temp;              //声明变体变量
    uchar c;
    if(SendFileEn&&Uart_Open)//发送文件,串口必须打开
    {
        if(n>str.Length())//发送结束
        {
            n=1;//计数器复位
            Timer2->Enabled=false;//中断关闭
            Timer2->Interval=1000;
            Send_Files->Caption="发送文件";
            SendFileEn=false;//文件发送使能标志清空
            return;
        }
        c=str[n];
        n++;
        if(c=='\r'||c=='\n')return; //忽略换行符
        temp=VarArrayCreate(OPENARRAY(int,(0,0)),varByte);//创造单个变体int,(0,10))发送11个数据
        temp.PutElement(c,0);//字符传入变体变量
        MSComm1->Output=temp;
        TXCount->Caption="TX:"+IntToStr(TX_Count++);//发送计数器计数
    }
    else Send_ButtonClick(Form1);
}
//---------------------------------------------------------------------------
//定时时间设置
void __fastcall TForm1::AutoSend_TimeChange(TObject *Sender)
{
    char *cs;
    unsigned long settime;
    AnsiString c;
    c=AutoSend_Time->Text;
    cs=c.c_str();
    for(int t=0;t<c.Length();t++)
    {
        if(cs[t]>57||cs[t]<48)
        {
            Application->MessageBox("请输入数字!","输入错误",MB_OK);
            AutoSend_Time->Text=1000;//重新设置
            return;
        }
    }
    settime=StrToInt(AutoSend_Time->Text);//得到时间长度(ms)
    if(settime>65535||settime==0)//范围设置
    {
        Application->MessageBox("定时范围:1~65535(ms)","数据溢出",MB_OK);
        AutoSend_Time->Text=1000;//重新设置
        return;
    }
    Timer2->Interval=settime;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Open_FilseClick(TObject *Sender)
{
    if(Form1->OpenDialog1->Execute())//如果执行有效则继续执行
    {
        Send_Files->Enabled=false;//关闭发送按钮,避免触发Send_BoxChange事件
        Send_Box->Lines->LoadFromFile(Form1->OpenDialog1->FileName);
        Send_Button->Enabled=false;//关闭发送按钮
        Auto_Send->Checked=false;//自动发送取消
        Auto_Send->Enabled=false;//自动发送关闭
        SendFileEn=false;//关闭发送使能
        Send_Files->Caption="发送文件";
        Send_Files->Enabled=true;//开启发送按钮
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Send_BoxChange(TObject *Sender)
{
    FileSend_Juder();//发送文件退出处理
}
//---------------------------------------------------------------------------
//打开升级链接
void __fastcall TForm1::Label11Click(TObject *Sender)
{
    ShellExecute(NULL,"OPEN","http://blog.ednchina.com/atom6037/168302/message.aspx",NULL,NULL,SW_SHOW);
}
//---------------------------------------------------------------------------
//打开对话框二
void __fastcall TForm1::Label12Click(TObject *Sender)
{ 
    Form2->Show();
    Form2->Label1->Caption=Label10->Caption;//获得相同版本号
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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