MATLAB串口助手

说明

		上半部分为串口助手,下半部分为雷达测试指令。
		代码目录下有一张名为icon的图片作为图标。
		雷达发波指令
				scan stop\r\n
				ant_calib_new X-30.0Y0.0 1 10 2.5 3.5 0 0\r\n			
				ant_calib_new X-25.0Y0.0 1 10 2.5 3.5 0 0\r\n
				XY:转台角度;
				1/2:高速模式(HV)、低速模式(LV);
				10:每次跑10帧;
				2.5 3.5 0 0:搜索参数;
		接收的数据类似于:

ant_calib_new X0Y-16 1 10 2.5 3.5 0 0

0.000000  0.12086  -49.236392  0.08948  0.000000  0.13483  -16.858400  0.01814  45.000000  0.00049  28.201627  0.09354  121.429557  0.10864  -22.659351  0.01188  -66.929580  0.05257  -173.659790  0.04504  -31.759481  0.08377  175.815109  0.00645  -41.423660  0.00784  -70.484123  0.10719  145.713119  0.04327  -47.862403  0.01224    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
-1.302060  0.11823  -50.194427  0.09308  -1.245369  0.12921  -16.620761  0.01865  45.000000  0.00049  28.072484  0.08930  121.429557  0.10864  -23.600191  0.01205  -67.833656  0.05188  -173.659790  0.04504  -32.592582  0.07999  178.531265  0.00581  -39.805572  0.00838  -71.565048  0.10315  144.462326  0.04065  -48.012786  0.01105    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
0.654915  0.11684  -49.236392  0.08948  0.000000  0.12915  -15.255105  0.01785  45.000000  0.00049  29.419071  0.08894  123.310623  0.10706  -22.858755  0.01169  -66.929580  0.05257  -173.659790  0.04504  -31.097307  0.08260  178.602951  0.00642  -37.874980  0.00793  -70.016899  0.10243  147.528808  0.04150  -48.012786  0.01105    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
-0.658491  0.11551  -49.236392  0.08948  -1.245369  0.12921  -16.534832  0.01700  36.869895  0.00038  28.072484  0.08930  120.963752  0.10168  -25.144783  0.01118  -66.929580  0.05257  -173.659790  0.04504  -31.759481  0.08377  174.427780  0.00647  -41.423660  0.00784  -71.454223  0.10196  146.309936  0.03888  -46.548152  0.01045    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
0.331022  0.11417  -48.179824  0.08926  1.245369  0.12921  -15.478633  0.01735  45.000000  0.00049  30.256433  0.08908  122.152290  0.10431  -21.898197  0.01153  -66.540969  0.05093  -172.476181  0.04361  -29.744878  0.08034  -178.567962  0.00611  -37.874980  0.00793  -69.239028  0.10213  147.528808  0.04150  -46.548152  0.01045    tarRid 15  tarR 3.02  noiP 0.000001  waveT 0 
0.993669  0.11420  -48.179824  0.08926  1.245369  0.12921  -14.237276  0.01823  36.869895  0.00038  30.638578  0.08709  124.077186  0.10284  -23.334047  0.01177  -66.929580  0.05257  -172.476181  0.04361  -30.425777  0.08145  178.636138  0.00673  -37.874980  0.00793  -69.775138  0.10010  149.420761  0.03986  -46.548152  0.01045    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
-0.993669  0.11420  -49.316028  0.08621  0.000000  0.12360  -17.102739  0.01764  36.869895  0.00038  28.943920  0.08680  122.152290  0.10431  -23.198595  0.01084  -67.833656  0.05188  -173.541168  0.04341  -30.827665  0.07954  175.710830  0.00614  -39.472454  0.00740  -70.346168  0.10201  147.528808  0.04150  -46.548152  0.01045    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
-0.658491  0.11551  -48.239696  0.08600  0.000000  0.12360  -15.478633  0.01735  44.999992  0.00027  28.274862  0.08570  122.152290  0.10431  -23.600191  0.01205  -67.833656  0.05188  -173.418045  0.04181  -31.504266  0.08069  178.567962  0.00611  -39.805572  0.00838  -70.559967  0.10042  146.309936  0.03888  -46.548152  0.01045    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
-0.331022  0.11417  -49.236392  0.08948  0.000000  0.12360  -17.102739  0.01764  44.999992  0.00027  28.761159  0.08781  122.905235  0.10010  -22.988710  0.01103  -66.929580  0.05257  -173.418045  0.04181  -31.239213  0.07767  178.567962  0.00611  -39.472454  0.00740  -69.775138  0.10010  147.528808  0.04150  -46.548152  0.01045    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
0.337461  0.11025  -48.239696  0.08600  1.272877  0.12366  -16.782644  0.01652  36.869895  0.00038  30.504354  0.08551  122.905235  0.10010  -21.305780  0.01099  -65.637542  0.05165  -172.476181  0.04361  -29.054605  0.07925  -178.636138  0.00673  -36.384349  0.00850  -68.860176  0.09866  148.240509  0.03723  -45.000000  0.00989    tarRid 15  tarR 3.02  noiP 0.000001  waveT 0 
<EOF>
ant_calib_new X0Y-15 1 10 2.5 3.5 0 0

15.018359  0.10999  -16.927505  0.03528  21.425298  0.10989  7.594632  0.01398  33.690063  0.00079  34.592289  0.05799  147.955307  0.09111  -16.699249  0.02037  -42.337001  0.05655  -167.005371  0.01086  -23.782026  0.06343  147.380752  0.03025  -0.954701  0.01374  -76.883567  0.04267  159.537719  0.07803  -53.130100  0.00153    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
14.691963  0.10965  -16.927505  0.03528  22.306201  0.10846  6.441594  0.01485  38.659812  0.00063  34.359378  0.05376  148.781600  0.09088  -15.945407  0.02022  -41.633541  0.05531  -167.905242  0.01251  -24.590116  0.06424  147.188629  0.03119  0.939203  0.01420  -73.979705  0.04212  160.559967  0.07935  -53.130100  0.00153    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
15.106301  0.10874  -16.927505  0.03528  23.198595  0.10709  5.617577  0.01433  38.659812  0.00063  36.869895  0.05724  149.036239  0.08768  -16.389541  0.01917  -41.633541  0.05531  -167.905242  0.01251  -24.962789  0.06245  147.578933  0.02932  1.877948  0.01421  -74.649864  0.04268  160.426864  0.07831  -52.125019  0.00198    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
16.135580  0.10451  -16.927505  0.03528  23.749494  0.10245  5.710622  0.01387  30.963750  0.00052  37.539432  0.05476  150.068481  0.08850  -14.036246  0.01874  -40.292148  0.05550  -167.905242  0.01251  -23.782026  0.06343  149.369995  0.02976  1.877948  0.01421  -73.827842  0.04136  161.565048  0.07725  -50.906139  0.00162    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
16.566074  0.10366  -14.620860  0.03448  23.749494  0.10245  7.471573  0.01444  30.963750  0.00052  37.060405  0.05521  150.908813  0.08837  -15.037812  0.01836  -40.292148  0.05550  -164.054595  0.01294  -23.782026  0.06343  150.255111  0.03000  2.815568  0.01423  -73.827842  0.04136  162.194656  0.07898  -48.814071  0.00172    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
14.620886  0.10561  -16.927505  0.03528  22.456941  0.10049  6.546283  0.01438  33.690063  0.00079  36.280254  0.05187  149.688781  0.08650  -14.826472  0.01888  -40.914382  0.05411  -167.905242  0.01251  -24.145540  0.06165  147.783447  0.02840  0.954701  0.01374  -74.207595  0.04038  160.292007  0.07729  -48.814071  0.00172    tarRid 15  tarR 3.02  noiP 0.000001  waveT 0 
16.868654  0.10007  -14.620860  0.03448  23.470991  0.10475  8.820379  0.01314  26.565052  0.00069  37.064449  0.05294  151.056076  0.08680  -15.037812  0.01836  -39.559673  0.05432  -167.905242  0.01251  -22.963777  0.06265  150.018356  0.03093  2.815568  0.01423  -73.141601  0.04082  162.718490  0.07625  -49.085613  0.00150    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
14.886263  0.10196  -16.927505  0.03528  22.190288  0.10280  6.881735  0.01302  30.963750  0.00052  36.098281  0.05385  149.167053  0.08610  -16.389541  0.01917  -40.179233  0.05293  -167.905242  0.01251  -24.962789  0.06245  148.477813  0.02953  1.877948  0.01421  -73.515541  0.03984  162.208786  0.07555  -48.814071  0.00172    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
16.665760  0.10246  -14.620860  0.03448  24.676868  0.10120  6.654402  0.01392  30.963750  0.00052  38.585395  0.05516  150.871170  0.08579  -13.431014  0.01810  -40.179233  0.05293  -167.005371  0.01086  -24.145540  0.06165  148.815017  0.03011  3.751758  0.01426  -72.979476  0.04007  162.454269  0.07761  -48.814071  0.00172    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
16.665760  0.10246  -14.620860  0.03448  24.325569  0.09793  8.530760  0.01404  33.690063  0.00079  38.047039  0.05206  151.205993  0.08524  -14.826472  0.01888  -39.559673  0.05432  -165.465530  0.01187  -22.963777  0.06265  151.132827  0.03026  3.751758  0.01426  -72.103866  0.03882  163.657119  0.07328  -47.290611  0.00119    tarRid 15  tarR 3.02  noiP 0.000002  waveT 0 
<EOF>

		K:帧数
		M:配置波形数
		N:接收通道数(虚拟后)
		P_((k,m,n)):接收通道的相位(单位,deg)
		A_((k,m,n)):接收通道的幅度(2D-FFT后目标幅度值平方)
		tarRid((k,m)):目标的距离单元(从0开始)
		tarR((k,m)):目标的距离(单位:米)
		noiR(k,m)):噪声幅度(2D-FFT后噪声幅度值平方)
		waveT((k,m)):波形类型

		打印日志:
		--------log--------
		时间:31-Aug-2023 17:23:55
		SingleTarget  起始角-75  终止角75  步进角30  角度差0  rcs1:10  rcs2:15
		FlagINFO SingleTarget -75 75 30 0 10 15
		--------log--------
		
		参考代码:https://gitee.com/xsy754384522/matlab_-xsy
		推荐继续学习app designer:https://github.com/wdther/APPdesignerPractice

代码

classdef Serial_app < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        MySerialUIFigure       matlab.ui.Figure
        Serial_Lamp            matlab.ui.control.Lamp
        Serial_Switch          matlab.ui.control.Switch
        Label_10               matlab.ui.control.Label
        Signal_select          matlab.ui.control.DropDown
        Label_5                matlab.ui.control.Label
        Serial_check_bits      matlab.ui.control.DropDown
        Label_4                matlab.ui.control.Label
        Serial_stop_bits       matlab.ui.control.DropDown
        Label_3                matlab.ui.control.Label
        Serial_data_bits       matlab.ui.control.DropDown
        Label_2                matlab.ui.control.Label
        Serial_baud_rate       matlab.ui.control.DropDown
        Label                  matlab.ui.control.Label
        Serial_port_number     matlab.ui.control.DropDown
        filename               matlab.ui.control.EditField
        Label_11               matlab.ui.control.Label
        Angle_difference       matlab.ui.control.Spinner
        SpinnerLabel           matlab.ui.control.Label
        step_angle             matlab.ui.control.Spinner
        Label_8                matlab.ui.control.Label
        end_angle              matlab.ui.control.Spinner
        Label_9                matlab.ui.control.Label
        Start_angle            matlab.ui.control.Spinner
        Label_7                matlab.ui.control.Label
        RCS2Spinner            matlab.ui.control.Spinner
        RCS2SpinnerLabel       matlab.ui.control.Label
        RCS1Spinner            matlab.ui.control.Spinner
        RCS1SpinnerLabel       matlab.ui.control.Label
        Clockwise              matlab.ui.control.DropDown
        Single_Multi           matlab.ui.control.DropDown
        list_of_messages       matlab.ui.control.TextArea
        FilePathButton         matlab.ui.control.Button
        FilePath               matlab.ui.control.EditField
        over_button            matlab.ui.control.Button
        radar_pause            matlab.ui.control.Button
        Clear_message_display  matlab.ui.control.Button
        radar_start            matlab.ui.control.Button
        TX_Text_hex            matlab.ui.control.TextArea
        TextArea               matlab.ui.control.TextArea
        Hex_send               matlab.ui.control.CheckBox
        Hex_disp               matlab.ui.control.CheckBox
        Button_clr_receive     matlab.ui.control.Button
        Button_save            matlab.ui.control.Button
        Label_6                matlab.ui.control.Label
        RX_Text                matlab.ui.control.TextArea
        Button_clr_send        matlab.ui.control.Button
        Button_send            matlab.ui.control.Button
        TextArea_2Label        matlab.ui.control.Label
        TX_Text                matlab.ui.control.TextArea
    end

    %assignin("base","led_color",app.Serial_Lamp.Color);
    %观察控件状态保存至工作区,class查看数据类型,disp();

    properties (Access = private)
        SerialObject % Description
        Str_Data
        Hex_Data
        %%%%%%%%%%雷达控制部分
        Message_display
        StartAngle
        StopAngle
        StepAngle
        realStepAngle   %串口发出的步进角,带正负
        NowAngle        %转台现在的角度
        SendQuit        %结束按钮标识符
        SignalFlag      %选择接受方式,串口助手or雷达存储
        FileName        %文件名
        RadarFid        %雷达数据存储文件
        pause_TIME      %每度延时
    end


    methods (Access = private)

        function SerialConfig(app)
            app.Serial_port_number.Items=seriallist("all");     %读取存在的串口
            if(strcmp(app.Serial_Switch.Value,'开启'))
                delete(app.SerialObject);   %关闭
                app.Serial_Lamp.Color=[0,1,0];
                try
                    %配置串口
                    app.SerialObject=serialport(app.Serial_port_number.Value , str2double(app.Serial_baud_rate.Value) , ...
                        "DataBits",str2double(app.Serial_data_bits.Value) , ...
                        "StopBits",str2double(app.Serial_stop_bits.Value) , ...
                        "Parity",app.Serial_check_bits.Value);

                    configureTerminator(app.SerialObject,'LF'); %换行结束
                    configureCallback(app.SerialObject,"terminator",@SerialCallback);   %终止符时触发
                catch
                    msgbox('串口打开失败');
                    delete(app.SerialObject);   %关闭
                    app.Serial_Lamp.Color=[0.65,0.65,0.65];
                    app.Serial_Switch.Value = '关闭';
                end

            elseif(strcmp(app.Serial_Switch.Value,'关闭'))
                delete(app.SerialObject);   %关闭
                app.Serial_Lamp.Color=[0.65,0.65,0.65];
            end

            function SerialCallback(src,event)
                if(app.SignalFlag)
                    radar_receive_func(app)
                else
                    Serial_receive_func(app);
                end
            end
        end



        function Serial_receive_func(app)   %串口助手接收函数
            app.TextArea.Value=[app.TextArea.Value;readline(app.SerialObject)]; %拼接
            app.Str_Data=app.TextArea.Value;
            app.Str_Data(1)=[];     %删除第一空行
            app.Hex_Data='';    %字符16进制转换
            num_cell=length(app.Str_Data);  %已接收的数据行数
            for i=1:num_cell
                temp=cellstr(dec2hex(abs(app.Str_Data{i})));
                len=length(temp);
                for j=1:len
                    if(length(temp{j})==2)  %补上空格和0;
                        app.Hex_Data=strcat(app.Hex_Data,32,temp{j});
                    else
                        app.Hex_Data=strcat(app.Hex_Data,32,'0',temp{j});
                    end
                end
                app.Hex_Data=strcat(app.Hex_Data,32,'0A');  %补上换行符
            end

            %选择HEX和16进制输出
            if(app.Hex_disp.Value==1)
                app.RX_Text.Value=app.Hex_Data;
            else
                app.RX_Text.Value=app.Str_Data;
            end
        end


        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        function radar_receive_func(app)    %雷达接收函数

            radar_receive_data1=readline(app.SerialObject);

            app.RadarFid=fopen(app.FileName,'a');
            fprintf(app.RadarFid,'%s\n',radar_receive_data1); %换行\r\n
            fclose(app.RadarFid);

        end
    end


    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            app.Serial_port_number.Items=seriallist("all");     %读取存在的串口
            app.StartAngle = app.Start_angle.Value;     %更新角度
            app.StepAngle = app.step_angle.Value;
            app.StopAngle = app.end_angle.Value;
            app.pause_TIME=0.35;            %每度延时
            if(strcmp(app.Signal_select.Value,'雷达信号'))
                app.SignalFlag=1;
            else
                app.SignalFlag=0;
            end
        end

        % Drop down opening function: Serial_port_number
        function Serial_port_numberDropDownOpening(app, event)
            %串口号
            SerialConfig(app);
        end

        % Value changed function: Serial_port_number
        function Serial_port_numberValueChanged(app, event)
            %串口号
            SerialConfig(app);
        end

        % Value changed function: Serial_baud_rate
        function Serial_baud_rateValueChanged(app, event)
            %波特率
            SerialConfig(app);
        end

        % Value changed function: Serial_data_bits
        function Serial_data_bitsValueChanged(app, event)
            %数据位
            SerialConfig(app);
        end

        % Value changed function: Serial_stop_bits
        function Serial_stop_bitsValueChanged(app, event)
            %停止位
            SerialConfig(app);
        end

        % Value changed function: Serial_check_bits
        function Serial_check_bitsValueChanged(app, event)
            %校验位
            SerialConfig(app);
        end

        % Value changed function: Serial_Switch
        function Serial_SwitchValueChanged(app, event)
            %开关
            SerialConfig(app);
        end

        % Value changed function: Hex_disp
        function Hex_dispValueChanged(app, event)
            %选择HEX和16进制输出
            if(app.Hex_disp.Value==1)
                app.RX_Text.Value=app.Hex_Data;
            else
                app.RX_Text.Value=app.Str_Data;
            end
        end

        % Button pushed function: Button_clr_receive
        function Button_clr_receivePushed(app, event)
            %清除接收
            app.RX_Text.Value='';
            app.TextArea.Value='';
            app.Str_Data='';
            app.Hex_Data='';
        end

        % Button pushed function: Button_save
        function Button_savePushed(app, event)
            context=app.RX_Text.Value;%接收数据显示区
            arr=length(context);%数据行数
            if(app.Hex_disp.Value==0)%保存字符串数据
                fid=fopen('data.txt','w+');
                for i=1:arr
                    fprintf(fid,'%s\r\n',context{i}); %换行
                end
                fclose(fid);
            elseif(app.Hex_disp.Value==1)%保存16进制数据
                fid=fopen('data_hex.txt','w+');
                fprintf(fid,'%s\r\n',context{1}); %换行
                fclose(fid);
            end
        end

        % Button pushed function: Button_clr_send
        function Button_clr_sendPushed(app, event)
            app.TX_Text.Value='';
            app.TX_Text_hex.Value='';
        end

        % Button pushed function: Button_send
        function Button_sendPushed(app, event)
            if(strcmp(app.Serial_Switch.Value,'开启'))
                if(app.Hex_send.Value==0)  %字符串发送
                    str=app.TX_Text.Value;
                    cell_num=length(str);
                    if(cell_num==1)
                        if((length(abs(str{1})))~=0)%判断是否有可以发送的数据
                            write(app.SerialObject,abs(str{1}),"uint8");
                        end
                    else
                        for i=1:cell_num-1
                            writeline(app.SerialObject,sprintf('%s\r',str{i})); % %s 0D 0A
                        end
                        if(~isempty(abs(str{cell_num})))%最后一行是否是回车
                            write(app.SerialObject,abs(str{cell_num}),"uint8");
                        end
                    end
                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                else    %16进制发送
                    n=length(app.TX_Text_hex.Value);     %16进制行数为1
                    if(n==1)
                        context=hex2dec(strsplit(app.TX_Text_hex.Value{1}));%16进制字符串转uint8数组
                        len=length(context);
                        uint8 send_flag;
                        send_flag=1;
                        if(len==0)
                            msgbox('发送内容不能为空')
                        else
                            for i=1:len
                                if(context(i)>255)
                                    send_flag=0;
                                    break;
                                end
                            end
                            if(send_flag)
                                write(app.SerialObject,context,"uint8");
                            else
                                msgbox('请输入正确的16进制数据');
                            end
                        end
                    else
                        msgbox('16进制发送请用 0D 0A 代替回车换行');
                    end

                end
            end
        end

        % Value changed function: Hex_send
        function Hex_sendValueChanged(app, event)
            if app.Hex_send.Value==1    %普通字符转16进制
                app.TX_Text_hex.Visible=1;
                app.TX_Text.Visible=0;
                temp=app.TX_Text.Value;
                string s;
                s ='';
                m=length(temp);
                if(m>1)     %不止一行数据
                    for i=1:m-1
                        x=cellstr(dec2hex(abs(temp{i})));%字符串转为16进制字符元胞数组
                        n=length(x);
                        if(n~=0)%这一行字符不为空字符
                            for j=1:n %将每行的数据转成16进制并进行拼接
                                s=strcat(s,32,x{j});
                            end
                            s=strcat(s,32,'0D',32,'0A');%每行的数据末尾补0D 0A
                        end
                    end
                    %最后一行为空,不补0D 0A
                    x=cellstr(dec2hex(abs(temp{m})));
                    n=length(x);%最后一行的hex字符个数
                    %当最后一行不为回车时进行这一步
                    flag=length(dec2hex(abs(temp{m})));%flag为0时最后一行有数据
                    if(flag)%最后一行为空字符
                        for j=1:n
                            s=strcat(s,32,x{j});
                        end
                    end
                    s(1)=[];
                elseif(m==1)%只有1行字符串,切换十六进制
                    if(~isempty(abs(temp{1})))%数据不为空
                        x=cellstr(dec2hex(abs(temp{1})));%字符
                        n=length(x);%字符长度
                        for j=1:n
                            s=strcat(s,32,x{j});
                        end
                        s(1)=[];
                    end
                end
                app.TX_Text_hex.Value=s;
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            else
                app.TX_Text_hex.Visible=0;
                app.TX_Text.Visible=1;
                hex_temp=app.TX_Text_hex.Value;
                hex_temp_length=length(hex_temp);%l为数据行数
                if(hex_temp_length==1)
                    try
                        temp=(char(hex2dec(strsplit(hex_temp{1}))))';%类型为字符串,但会损失一个回车0D 0A
                        ugrp=abs(temp);
                        v=length(ugrp);
                        if((v>=2)&&(ugrp(v)==10)&&(ugrp(v-1)==13))%末尾监测到0D0A
                            app.TX_Text.Value=temp;
                            app.TX_Text.Value{end+1}='';
                        else
                            app.TX_Text.Value=temp;
                        end
                    catch
                        msgbox('请输入正确的16进制数据');
                    end

                elseif(hex_temp_length>1)
                    msgbox('16进制发送请用 0D 0A 代替回车换行');
                    app.Hex_send.Value=1;
                    app.TX_Text_hex.Visible=0;
                    app.TX_Text.Visible=0;
                end
            end

        end

        % Value changed function: Start_angle
        function Start_angleValueChanged(app, event)
            app.StartAngle = app.Start_angle.Value;

        end

        % Value changed function: step_angle
        function step_angleValueChanged(app, event)
            app.StepAngle = app.step_angle.Value;

        end

        % Value changed function: end_angle
        function end_angleValueChanged(app, event)
            app.StopAngle = app.end_angle.Value;

        end

        % Button pushed function: FilePathButton
        function FilePathButtonPushed(app, event)
            FilePathtemp = uigetdir;
            if(FilePathtemp)
                app.FilePath.Value = FilePathtemp ;
            end
        end

        % Button pushed function: radar_start
        function radar_startButtonPushed(app, event)
            if(strcmp(app.Serial_Switch.Value,'开启'))
                app.SendQuit=0;
                try
                    app.Single_Multi.Enable=0;
                    app.Clockwise.Enable=0;
                    app.RCS1Spinner.Enable=0;
                    app.RCS2Spinner.Enable=0;
                    app.Start_angle.Enable=0;
                    app.step_angle.Enable=0;
                    app.end_angle.Enable=0;
                    app.Angle_difference.Enable=0;

                    app.FileName=fullfile(app.FilePath.Value,strcat(app.filename.Value,'.txt'));
                    if ~isempty(dir(app.FileName))      %查找是否已存在该文件
                        delete(app.FileName);   %存在则删除
                    end

                    app.RadarFid=fopen(app.FileName,'a');
                    %写入相关信息
                    fprintf(app.RadarFid,'--------log--------\n');
                    fprintf(app.RadarFid,'时间:%s\n',datestr(now));
                    fprintf(app.RadarFid,'%s  起始角%d  终止角%d  步进角%d  角度差%d  rcs1:%d  rcs2:%d\r\n',...
                        app.Single_Multi.Value,...
                        app.Start_angle.Value,...
                        app.end_angle.Value,...
                        app.step_angle.Value,...
                        app.Angle_difference.Value,...
                        app.RCS1Spinner.Value,...
                        app.RCS2Spinner.Value);
                    fprintf(app.RadarFid,'FlagINFO %s %d %d %d %d %d %d\r\n',...
                        app.Single_Multi.Value,...
                        app.Start_angle.Value,...
                        app.end_angle.Value,...
                        app.step_angle.Value,...
                        app.Angle_difference.Value,...
                        app.RCS1Spinner.Value,...
                        app.RCS2Spinner.Value);
                    fprintf(app.RadarFid,'--------log--------\n\n');
                    fclose(app.RadarFid);

                    write(app.SerialObject,sprintf('scan stop\r\n'),"string");
                    if(strcmp(app.Clockwise.Value,'负坐标系')) %转到起始角方向判断
                        if(app.StartAngle<0)    %默认原本为0
                            direction = 0 ;  %0为顺时针
                        else
                            direction = 1 ;  %1为逆时针
                        end
                    else
                        if(app.StartAngle<0)
                            direction = 1 ;  %1为逆时针
                        else
                            direction = 0 ;  %0为顺时针
                        end
                    end
                    write(app.SerialObject,sprintf('rotary_cailb %d %d \r\n',direction,app.StartAngle),"string");    %转到雷达起始角度
                    app.Message_display=strcat(app.Message_display,'----开始----');
                    app.list_of_messages.Value=app.Message_display;
                    pause_time=round(app.pause_TIME*abs(app.StartAngle));   %延时时间,一度0.35秒
                    while((pause_time>0)&(strcmp(app.Serial_Switch.Value,'开启')))   %为啥用&&有问题呢
                        app.Message_display=strcat(app.Message_display,'延时还剩',num2str(pause_time),'秒。。。');
                        app.list_of_messages.Value=app.Message_display;
                        pause(1);
                        pause_time=pause_time-1;
                        if(app.SendQuit)
                            return;
                        end
                    end
                    app.NowAngle=app.StartAngle;
                    app.Message_display=strcat(app.Message_display,'!!!延时结束!!!');
                    app.Message_display=strcat(app.Message_display,'----当前角度',num2str(app.NowAngle),'----');
                    app.list_of_messages.Value=app.Message_display;
                    write(app.SerialObject,sprintf('ant_calib_new X%d.0Y0.0 1 50 2.5 3.5 0 0\r\n',app.NowAngle),"string");
                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                    if(strcmp(app.Clockwise.Value,'负坐标系')) %单步旋转方向判断
                        if(app.StartAngle>app.StopAngle)
                            direction = 0 ;  %0为顺时针
                            app.realStepAngle=-app.StepAngle;
                        else
                            direction = 1 ;  %1为逆时针
                            app.realStepAngle=app.StepAngle;
                        end
                    else
                        if(app.StartAngle>app.StopAngle)
                            direction = 1 ;  %1为逆时针
                            app.realStepAngle=-app.StepAngle;
                        else
                            direction = 0 ;  %0为顺时针
                            app.realStepAngle=app.StepAngle;
                        end
                    end
                    pause_time=app.pause_TIME*abs(app.StepAngle);   %单步延时时间
                    while((abs(app.NowAngle-app.StopAngle)>=abs(app.StepAngle))&(strcmp(app.Serial_Switch.Value,'开启')))
                        write(app.SerialObject,sprintf('rotary_cailb %d %d \r\n',direction,app.realStepAngle),"string");
                        pause(pause_time);
                        app.NowAngle=app.NowAngle+app.realStepAngle;
                        app.Message_display=strcat(app.Message_display,'----当前角度',num2str(app.NowAngle),'----');
                        app.list_of_messages.Value=app.Message_display;
                        write(app.SerialObject,sprintf('ant_calib_new X%d.0Y0.0 1 50 2.5 3.5 0 0\r\n',app.NowAngle),"string");
                        if(app.SendQuit)
                            return;
                        end
                    end
%                     if(abs(app.NowAngle-app.StopAngle)>0)   %最后无法整除的剩余部分,直接归位
%                         if(app.realStepAngle<0)
%                             tempstep=-abs(app.NowAngle-app.StopAngle);
%                         else
%                             tempstep=abs(app.NowAngle-app.StopAngle);
%                         end
%                         write(app.SerialObject,sprintf('rotary_cailb %d %d \r\n',direction,tempstep),"string");
%                         pause(pause_time);
%                         app.NowAngle=app.StopAngle;
%                         app.Message_display=strcat(app.Message_display,'----当前角度',num2str(app.NowAngle),'----');
%                         app.list_of_messages.Value=app.Message_display;
%                         write(app.SerialObject,sprintf('ant_calib_new X%d.0Y0.0 1 50 2.5 3.5 0 0\r\n',app.NowAngle),"string");
%                     end
                    app.Message_display=strcat(app.Message_display,'----结束----');
                    app.list_of_messages.Value=app.Message_display;
                catch
                    %                                         msgbox('提示:意外中断!');
                end
                try
                    app.Single_Multi.Enable=1;
                    app.Clockwise.Enable=1;
                    app.RCS1Spinner.Enable=1;
                    app.RCS2Spinner.Enable=1;
                    app.Start_angle.Enable=1;
                    app.step_angle.Enable=1;
                    app.end_angle.Enable=1;
                    app.Angle_difference.Enable=1;
                catch
                end
            end
        end

        % Button pushed function: radar_pause
        function radar_pauseButtonPushed(app, event)
            str=app.radar_pause.Text;
            if(strcmp(str,'暂停'))    %会有弹窗?
                app.radar_pause.Text='继续';
                uiwait;
            else
                app.radar_pause.Text='暂停';
                uiresume;
            end
        end

        % Button pushed function: over_button
        function over_buttonButtonPushed(app, event)
            app.SendQuit=1;
            try
                app.Single_Multi.Enable=1;
                app.Clockwise.Enable=1;
                app.RCS1Spinner.Enable=1;
                app.RCS2Spinner.Enable=1;
                app.Start_angle.Enable=1;
                app.step_angle.Enable=1;
                app.end_angle.Enable=1;
                app.Angle_difference.Enable=1;
            catch
            end
        end

        % Button pushed function: Clear_message_display
        function Clear_message_displayButtonPushed(app, event)
            app.Message_display='';
            app.list_of_messages.Value='';
        end

        % Value changed function: Signal_select
        function Signal_selectValueChanged(app, event)
            value = app.Signal_select.Value;
            if(strcmp(value,'雷达信号'))
                app.SignalFlag=1;
            else
                app.SignalFlag=0;
            end
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create MySerialUIFigure and hide until all components are created
            app.MySerialUIFigure = uifigure('Visible', 'off');
            app.MySerialUIFigure.Color = [0.8 0.902 0.949];
            app.MySerialUIFigure.Position = [100 100 700 700];
            app.MySerialUIFigure.Name = 'MySerial';
            app.MySerialUIFigure.Icon = 'icon.jpg';
            app.MySerialUIFigure.Scrollable = 'on';

            % Create TX_Text
            app.TX_Text = uitextarea(app.MySerialUIFigure);
            app.TX_Text.Position = [330 375 350 125];

            % Create TextArea_2Label
            app.TextArea_2Label = uilabel(app.MySerialUIFigure);
            app.TextArea_2Label.HorizontalAlignment = 'center';
            app.TextArea_2Label.Position = [253 475 75 25];
            app.TextArea_2Label.Text = '发送数据';

            % Create Button_send
            app.Button_send = uibutton(app.MySerialUIFigure, 'push');
            app.Button_send.ButtonPushedFcn = createCallbackFcn(app, @Button_sendPushed, true);
            app.Button_send.Position = [475 340 75 25];
            app.Button_send.Text = '发送数据';

            % Create Button_clr_send
            app.Button_clr_send = uibutton(app.MySerialUIFigure, 'push');
            app.Button_clr_send.ButtonPushedFcn = createCallbackFcn(app, @Button_clr_sendPushed, true);
            app.Button_clr_send.Position = [605 340 75 25];
            app.Button_clr_send.Text = '清除发送';

            % Create RX_Text
            app.RX_Text = uitextarea(app.MySerialUIFigure);
            app.RX_Text.Position = [330 556 350 125];

            % Create Label_6
            app.Label_6 = uilabel(app.MySerialUIFigure);
            app.Label_6.HorizontalAlignment = 'center';
            app.Label_6.Position = [253 656 75 25];
            app.Label_6.Text = '接收数据';

            % Create Button_save
            app.Button_save = uibutton(app.MySerialUIFigure, 'push');
            app.Button_save.ButtonPushedFcn = createCallbackFcn(app, @Button_savePushed, true);
            app.Button_save.Position = [475 525 75 25];
            app.Button_save.Text = '保存数据';

            % Create Button_clr_receive
            app.Button_clr_receive = uibutton(app.MySerialUIFigure, 'push');
            app.Button_clr_receive.ButtonPushedFcn = createCallbackFcn(app, @Button_clr_receivePushed, true);
            app.Button_clr_receive.Position = [605 525 75 25];
            app.Button_clr_receive.Text = '清除接收';

            % Create Hex_disp
            app.Hex_disp = uicheckbox(app.MySerialUIFigure);
            app.Hex_disp.ValueChangedFcn = createCallbackFcn(app, @Hex_dispValueChanged, true);
            app.Hex_disp.Text = 'Hex显示';
            app.Hex_disp.Position = [263 556 75 50];

            % Create Hex_send
            app.Hex_send = uicheckbox(app.MySerialUIFigure);
            app.Hex_send.ValueChangedFcn = createCallbackFcn(app, @Hex_sendValueChanged, true);
            app.Hex_send.Text = 'Hex显示';
            app.Hex_send.Position = [263 374 75 50];

            % Create TextArea
            app.TextArea = uitextarea(app.MySerialUIFigure);
            app.TextArea.Visible = 'off';
            app.TextArea.Position = [330 556 350 125];

            % Create TX_Text_hex
            app.TX_Text_hex = uitextarea(app.MySerialUIFigure);
            app.TX_Text_hex.Position = [330 374 350 126];

            % Create radar_start
            app.radar_start = uibutton(app.MySerialUIFigure, 'push');
            app.radar_start.ButtonPushedFcn = createCallbackFcn(app, @radar_startButtonPushed, true);
            app.radar_start.Position = [40 58 100 30];
            app.radar_start.Text = '开始';

            % Create Clear_message_display
            app.Clear_message_display = uibutton(app.MySerialUIFigure, 'push');
            app.Clear_message_display.ButtonPushedFcn = createCallbackFcn(app, @Clear_message_displayButtonPushed, true);
            app.Clear_message_display.Position = [180 58 100 30];
            app.Clear_message_display.Text = '清除显示';

            % Create radar_pause
            app.radar_pause = uibutton(app.MySerialUIFigure, 'push');
            app.radar_pause.ButtonPushedFcn = createCallbackFcn(app, @radar_pauseButtonPushed, true);
            app.radar_pause.Position = [40 20 100 30];
            app.radar_pause.Text = '暂停';

            % Create over_button
            app.over_button = uibutton(app.MySerialUIFigure, 'push');
            app.over_button.ButtonPushedFcn = createCallbackFcn(app, @over_buttonButtonPushed, true);
            app.over_button.Position = [180 20 100 30];
            app.over_button.Text = '结束';

            % Create FilePath
            app.FilePath = uieditfield(app.MySerialUIFigure, 'text');
            app.FilePath.Position = [330 20 240 30];
            app.FilePath.Value = 'C:\Users\Public\Downloads';

            % Create FilePathButton
            app.FilePathButton = uibutton(app.MySerialUIFigure, 'push');
            app.FilePathButton.ButtonPushedFcn = createCallbackFcn(app, @FilePathButtonPushed, true);
            app.FilePathButton.Position = [600 20 80 30];
            app.FilePathButton.Text = '保存路径';

            % Create list_of_messages
            app.list_of_messages = uitextarea(app.MySerialUIFigure);
            app.list_of_messages.Position = [330 65 350 250];

            % Create Single_Multi
            app.Single_Multi = uidropdown(app.MySerialUIFigure);
            app.Single_Multi.Items = {'SingleTarget', 'MultiTarget'};
            app.Single_Multi.Position = [40 289 100 25];
            app.Single_Multi.Value = 'SingleTarget';

            % Create Clockwise
            app.Clockwise = uidropdown(app.MySerialUIFigure);
            app.Clockwise.Items = {'负坐标系', '正坐标系'};
            app.Clockwise.Position = [180 289 100 25];
            app.Clockwise.Value = '负坐标系';

            % Create RCS1SpinnerLabel
            app.RCS1SpinnerLabel = uilabel(app.MySerialUIFigure);
            app.RCS1SpinnerLabel.HorizontalAlignment = 'right';
            app.RCS1SpinnerLabel.Position = [19 246 33 26];
            app.RCS1SpinnerLabel.Text = 'RCS1';

            % Create RCS1Spinner
            app.RCS1Spinner = uispinner(app.MySerialUIFigure);
            app.RCS1Spinner.Limits = [-100 100];
            app.RCS1Spinner.Position = [65 246 75 25];

            % Create RCS2SpinnerLabel
            app.RCS2SpinnerLabel = uilabel(app.MySerialUIFigure);
            app.RCS2SpinnerLabel.HorizontalAlignment = 'right';
            app.RCS2SpinnerLabel.Position = [157 245 38 26];
            app.RCS2SpinnerLabel.Text = 'RCS2';

            % Create RCS2Spinner
            app.RCS2Spinner = uispinner(app.MySerialUIFigure);
            app.RCS2Spinner.Limits = [-100 100];
            app.RCS2Spinner.Position = [205 246 75 25];

            % Create Label_7
            app.Label_7 = uilabel(app.MySerialUIFigure);
            app.Label_7.HorizontalAlignment = 'right';
            app.Label_7.Position = [13 198 38 26];
            app.Label_7.Text = '起始角';

            % Create Start_angle
            app.Start_angle = uispinner(app.MySerialUIFigure);
            app.Start_angle.Limits = [-180 180];
            app.Start_angle.ValueChangedFcn = createCallbackFcn(app, @Start_angleValueChanged, true);
            app.Start_angle.Position = [65 197 75 25];

            % Create Label_9
            app.Label_9 = uilabel(app.MySerialUIFigure);
            app.Label_9.HorizontalAlignment = 'right';
            app.Label_9.Position = [152 198 43 26];
            app.Label_9.Text = '终止角';

            % Create end_angle
            app.end_angle = uispinner(app.MySerialUIFigure);
            app.end_angle.Limits = [-180 180];
            app.end_angle.ValueChangedFcn = createCallbackFcn(app, @end_angleValueChanged, true);
            app.end_angle.Position = [205 197 75 25];

            % Create Label_8
            app.Label_8 = uilabel(app.MySerialUIFigure);
            app.Label_8.HorizontalAlignment = 'right';
            app.Label_8.Position = [0 147 51 26];
            app.Label_8.Text = '步进角';

            % Create step_angle
            app.step_angle = uispinner(app.MySerialUIFigure);
            app.step_angle.Limits = [1 180];
            app.step_angle.ValueChangedFcn = createCallbackFcn(app, @step_angleValueChanged, true);
            app.step_angle.Position = [65 148 75 25];
            app.step_angle.Value = 5;

            % Create SpinnerLabel
            app.SpinnerLabel = uilabel(app.MySerialUIFigure);
            app.SpinnerLabel.HorizontalAlignment = 'right';
            app.SpinnerLabel.Position = [152 147 43 26];
            app.SpinnerLabel.Text = '角度差';

            % Create Angle_difference
            app.Angle_difference = uispinner(app.MySerialUIFigure);
            app.Angle_difference.Limits = [-90 90];
            app.Angle_difference.Position = [205 148 75 25];

            % Create Label_11
            app.Label_11 = uilabel(app.MySerialUIFigure);
            app.Label_11.HorizontalAlignment = 'right';
            app.Label_11.Position = [5 99 46 26];
            app.Label_11.Text = '文件名';

            % Create filename
            app.filename = uieditfield(app.MySerialUIFigure, 'text');
            app.filename.Position = [65 100 75 25];
            app.filename.Value = 'group1';

            % Create Serial_port_number
            app.Serial_port_number = uidropdown(app.MySerialUIFigure);
            app.Serial_port_number.Items = {'com1', 'com2', 'com3', 'com4'};
            app.Serial_port_number.DropDownOpeningFcn = createCallbackFcn(app, @Serial_port_numberDropDownOpening, true);
            app.Serial_port_number.ValueChangedFcn = createCallbackFcn(app, @Serial_port_numberValueChanged, true);
            app.Serial_port_number.Position = [100 656 100 25];
            app.Serial_port_number.Value = 'com1';

            % Create Label
            app.Label = uilabel(app.MySerialUIFigure);
            app.Label.HorizontalAlignment = 'right';
            app.Label.Position = [13 656 68 28];
            app.Label.Text = '串口号';

            % Create Serial_baud_rate
            app.Serial_baud_rate = uidropdown(app.MySerialUIFigure);
            app.Serial_baud_rate.Items = {'9600', '115200', '3000000'};
            app.Serial_baud_rate.ValueChangedFcn = createCallbackFcn(app, @Serial_baud_rateValueChanged, true);
            app.Serial_baud_rate.Position = [100 609 100 25];
            app.Serial_baud_rate.Value = '9600';

            % Create Label_2
            app.Label_2 = uilabel(app.MySerialUIFigure);
            app.Label_2.HorizontalAlignment = 'right';
            app.Label_2.Position = [13 608 68 28];
            app.Label_2.Text = '波特率';

            % Create Serial_data_bits
            app.Serial_data_bits = uidropdown(app.MySerialUIFigure);
            app.Serial_data_bits.Items = {'8', '7', '6', '5'};
            app.Serial_data_bits.ValueChangedFcn = createCallbackFcn(app, @Serial_data_bitsValueChanged, true);
            app.Serial_data_bits.Position = [100 563 100 25];
            app.Serial_data_bits.Value = '8';

            % Create Label_3
            app.Label_3 = uilabel(app.MySerialUIFigure);
            app.Label_3.HorizontalAlignment = 'right';
            app.Label_3.Position = [13 562 68 28];
            app.Label_3.Text = '数据位';

            % Create Serial_stop_bits
            app.Serial_stop_bits = uidropdown(app.MySerialUIFigure);
            app.Serial_stop_bits.Items = {'1', '1.5', '2'};
            app.Serial_stop_bits.ValueChangedFcn = createCallbackFcn(app, @Serial_stop_bitsValueChanged, true);
            app.Serial_stop_bits.Position = [100 517 100 25];
            app.Serial_stop_bits.Value = '1';

            % Create Label_4
            app.Label_4 = uilabel(app.MySerialUIFigure);
            app.Label_4.HorizontalAlignment = 'right';
            app.Label_4.Position = [13 515 68 28];
            app.Label_4.Text = '停止位';

            % Create Serial_check_bits
            app.Serial_check_bits = uidropdown(app.MySerialUIFigure);
            app.Serial_check_bits.Items = {'none', 'even', 'odd'};
            app.Serial_check_bits.ValueChangedFcn = createCallbackFcn(app, @Serial_check_bitsValueChanged, true);
            app.Serial_check_bits.Position = [100 471 100 25];
            app.Serial_check_bits.Value = 'none';

            % Create Label_5
            app.Label_5 = uilabel(app.MySerialUIFigure);
            app.Label_5.HorizontalAlignment = 'right';
            app.Label_5.Position = [13 469 68 28];
            app.Label_5.Text = '校验位';

            % Create Signal_select
            app.Signal_select = uidropdown(app.MySerialUIFigure);
            app.Signal_select.Items = {'串口助手', '雷达信号'};
            app.Signal_select.ValueChangedFcn = createCallbackFcn(app, @Signal_selectValueChanged, true);
            app.Signal_select.Position = [100 425 100 25];
            app.Signal_select.Value = '雷达信号';

            % Create Label_10
            app.Label_10 = uilabel(app.MySerialUIFigure);
            app.Label_10.HorizontalAlignment = 'right';
            app.Label_10.Position = [13 424 68 28];
            app.Label_10.Text = '接收选择';

            % Create Serial_Switch
            app.Serial_Switch = uiswitch(app.MySerialUIFigure, 'slider');
            app.Serial_Switch.Items = {'关闭', '开启'};
            app.Serial_Switch.ValueChangedFcn = createCallbackFcn(app, @Serial_SwitchValueChanged, true);
            app.Serial_Switch.Position = [130 371 56 25];
            app.Serial_Switch.Value = '关闭';

            % Create Serial_Lamp
            app.Serial_Lamp = uilamp(app.MySerialUIFigure);
            app.Serial_Lamp.Position = [51 371 25 25];
            app.Serial_Lamp.Color = [0.651 0.651 0.651];

            % Show the figure after all components are created
            app.MySerialUIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = Serial_app

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.MySerialUIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.MySerialUIFigure)
        end
    end
end
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值