• TI xWR系列毫米波雷达如何使用MATLAB自动连接串口?


    本文编辑:调皮哥的小助理

    在这里插入图片描述

    今天给大家分享的是,如何使用MATLAB(版本2021a)自动连接串口?之前使用MATLAB做雷达实验都是手动配置串口,一来二去的很麻烦,今天给大家研究了自动配置的方法。可参照如下:

    以前的方法:

    %调皮连续波
    controlSerialPort = ‘COM14’;% 预先设定USER COM
    dataSerialPort = ‘COM13’;% 预先设定DATA COM
    hDataSerialPort = configureDataSport(dataSerialPort, 65536); %链接 DATA COM串口
    hControlSerialPort = configureControlPort(controlSerialPort);%链接 USER COM串口

    现在的方法:

    %调皮连续波
    [controlSerialPort,dataSerialPort] = serial_port(); %自动读取串口
    hDataSerialPort = configureDataSport(dataSerialPort, 65536); %链接 DATA COM串口
    hControlSerialPort = configureControlPort(controlSerialPort);%链接 USER COM串口

    一句话搞定,不需要去计算机设备管理器里查看,换电脑、换雷达都没问题。

    但是还需要两个功能函数支持,如下:

    % 调皮连续波
    function [userport, dataport] = serial_port()
    port =IdentifySerialComs();
    user_flag = 1;
    data_flag = 1;
    for i = 1:length(port)
    if(user_flag)
    userportID = find(strcmp(‘XDS110 Class Application/User UART’, port(i,1)));
    if(userportID)
    user_flag = 0;
    userport = port(i,2);
    end
    end
    if(data_flag)
    dataportID = find(strcmp(‘XDS110 Class Auxiliary Data Port’, port(i,1)));
    if(dataportID)
    data_flag = 0;
    dataport = port(i,2);
    end
    end
    end
    userport = [‘COM’,num2str(cell2mat(userport))];
    dataport = [‘COM’,num2str(cell2mat(dataport))];
    end

    另外一个功能函数由CSDN作者:【雨寒sgg】创作。

    function devices = IdentifySerialComs()
    devices = [];
    Skey = ‘HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM’;
    [~, list] = dos([‘REG QUERY ’ Skey]);
    if ischar(list) && strcmp(‘ERROR’,list(1:5)) %% strcmp 两个字符串相同返回1
    disp(‘Error: EnumSerialComs - No SERIALCOMM registry entry’)
    return;
    end
    list = strread(list,’%s’,‘delimiter’,’ ‘); %#ok requires strread()
    coms = 0;
    for i = 1:numel(list) %%numel 返回元素个数
    if strcmp(list{i}(1:3),‘COM’)
    if ~iscell(coms)
    coms = list(i);
    else
    coms{end+1} = list{i}; %#ok Loop size is always small
    end
    end
    end
    out = 0;
    outK = 0;
    for j=1:2
    switch j
    case 1
    key = ‘HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB’;
    case 2
    key = ‘HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\FTDIBUS’;
    end
    [~, vals] = dos([‘REG QUERY ’ key ’ /s /f “FriendlyName” /t “REG_SZ”’]);
    if ischar(vals) && strcmp(‘ERROR’,vals(1:5))
    disp(‘Error: EnumSerialComs - No Enumerated USB registry entry’)
    return;
    end
    vals = textscan(vals,’%s’,‘delimiter’,’\t’);
    vals = cat(1,vals{:});
    for i = 1:numel(vals)
    if strcmp(vals{i}(1:min(12,end)),‘FriendlyName’)
    if ~iscell(out)
    out = vals(i);
    else
    out{end+1} = vals{i}; %#ok Loop size is always small
    end
    if ~iscell(outK)
    outK = vals(i-1);
    else
    outK{end+1} = vals{i-1}; %#ok Loop size is always small
    end
    end
    end
    end
    i_dev=1;Sservices=[];
    for i = 1:numel(coms)
    match = strfind(out,[coms{i},’)’]);
    ind = 0;
    for j = 1:numel(match)
    if ~isempty(match{j})
    ind = j;
    [~, sers] = dos([‘REG QUERY "’ outK{ind} ‘" /f “Service” /t “REG_SZ”’]);
    sers = textscan(sers,’%s’,‘delimiter’,’\t’);
    sers = cat(1,sers{:});
    if (numel(sers)>1)
    sers=strread(sers{2},’%s’,‘delimiter’,’ ‘);
    Sservices{i_dev} = sers{3};
    i_dev=i_dev+1;
    end
    end
    end
    end
    Sservices=unique(Sservices);
    i_dev=1;
    for ss=1:numel(Sservices)
    key = [‘HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services’ Sservices{ss} ‘\Enum’];
    [~, vals] = dos([‘REG QUERY ’ key ’ /f “Count”’]);
    if ischar(vals) && strcmp(‘ERROR’,vals(1:5))
    % disp(‘Error: EnumSerialComs - No Enumerated services USB registry entry’)
    % return
    end
    vals = textscan(vals,’%s’,‘delimiter’,’\t’);
    vals = cat(1,vals{:});
    if (numel(vals)>1)
    vals=strread(vals{2},’%s’,‘delimiter’,’ ‘);
    Count=hex2dec(vals{3}(3:end));
    if Count>0
    [~, vals] = dos([‘REG QUERY ’ key]);
    vals = textscan(vals,’%s’,‘delimiter’,’\t’);
    vals = cat(1,vals{:});
    out=0;
    j=0;
    for i = 1:numel(vals)
    Enums=strread(vals{i},’%s’,‘delimiter’,’ ‘);
    try nums=hex2dec(Enums{1});
    catch
    nums=-1;
    end
    if(nums==j)
    out=[‘HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum’ Enums{3}];
    [~, listC] = dos([‘REG QUERY "’ out ‘" /s /f “PortName” /t “REG_SZ”’]);
    listC = textscan(listC,’%s’,‘delimiter’,’\t’);
    listC = cat(1,listC{:});
    if (numel(listC)>1)
    listC=strread(listC{2},’%s’,‘delimiter’,’ ‘);
    for i = 1:numel(coms)
    if strcmp(listC{3},coms{i})
    [~, NameF] = dos([‘REG QUERY "’ out ‘" /s /f “FriendlyName” /t “REG_SZ”’]);
    NameF = textscan(NameF,’%s’,‘delimiter’,’\t’);
    NameF = cat(1,NameF{:});
    com = str2double(coms{i}(4:end));
    if com > 9
    length = 8;
    else
    length = 7;
    end
    devices{i_dev,1} = NameF{2}(27:end-length); %#ok
    devices{i_dev,2} = com; %#ok Loop size is always small
    i_dev=i_dev+1;
    end
    end
    end
    j=j+1;
    end
    end
    end
    end
    end
    end

    把上面的代码,放入MATLAB即可自动链接串口。

    感谢大家关注,最近有很多新关注我的粉丝,感谢你们!

  • 相关阅读:
    全局异常拦截和Spring Security认证异常的拦截的顺序
    HGH30CA、HGH30HA、HGH35CA、HGH35HA、HGH45HA等可以替换吗?
    【李宏毅】机器学习——作业1-PM2.5预测
    Linux内核源码分析 (B.x)Linux页表的映射
    基于ElasticSearch存储海量AIS数据:AIS数据索引机制篇
    【11.1】【VP】Codeforces Round #730 (Div. 2)
    鸿蒙4.0开发笔记之DevEco Studio之配置代码片段快速生成(三)
    蓝桥杯算法题汇总
    初识 RabbitMQ
    字节大佬打造1137页【数据结构与算法】神册!太香了
  • 原文地址:https://blog.csdn.net/qq_35844208/article/details/127657467