使用 matlab 与华为 E3531 通信时出错

Error while Messaging with Huawei E3531 using matlab

我正在使用华为 E3531 模型通过与 matlab 接口发送消息。但它总是显示错误。我需要解锁我的华为加密狗吗?

如果不是,实际问题是什么?我正在使用以下代码:

clc;
clear all;
global BytesAvail;
global A;
global B;
tx ='ATI';
tx1=char(13);
tx2=char(26);
tx3='AT+CMGS="+919526018418"'; 
tx4= ' This is a test msg ';     
tx5='AT+CMGF=1';

s = serial('COM5'); 

s.baudrate=9600;
fopen(s);
s.Terminator = 'CR';

fprintf(s,'%s', tx); 
fprintf(s,'%s', tx1); 

BytesAvail=s.BytesAvailable;
    if(BytesAvail > 0), A=fread(s,BytesAvail,'char'); end
A;
sprintf('%c', A)

%%%%%%%%%%%%%%%Send SMS%%%%%%%%%%%%
fprintf(s,'%s', tx5);
fprintf(s,'%s', tx1); 
fprintf(s,'%s', tx3);
fprintf(s,'%s', tx1); 
fprintf(s,'%s', tx4);
fprintf(s,'%s', tx2);
BytesAvail=s.BytesAvailable;
    if(BytesAvail > 0), B=fread(s,BytesAvail,'char'); end
B;

fclose(s)

错误是这样

??? Error using ==> serial.fprintf at 144
An error occurred during writing.

Error in ==> message at 20
    fprintf(s,'%s', tx);

>> 

此错误是由于某些虚拟串口驱动程序不支持相当于物理串口的功能,以及 Matlab 处理与虚拟串口通信的方式造成的。

一个可能的解决方案是升级到最新的驱动程序(由串口适配器供应商提供)。

如果即使使用虚拟串行端口适配器的最新驱动程序错误仍然存​​在,则可以通过仪器控制工具箱提供的 VISA 接口使用其他方式与串行 COM 端口进行通信。

Agilent/Keysight (Agilent IO Libraries) 提供的 VISA 软件库独立于 Matlab 串行端口接口处理串行 COM 端口通信。

安捷伦 IO 库可在 the following URL 下载。

将虚拟串口添加为VISA资源名称(也可参考截图):

  1. 打开安捷伦连接专家

  2. 右键单击位于'Instrument I/O on ths PC'列中的所需串口项('COMxx'),然后从弹出窗口中选择select "Add Instrument" -向上上下文菜单。

  3. 在下面的对话框中,取消选中"Auto-identify this instrument"并单击确定。

  4. 验证虚拟串行端口是否已分配 VISA 资源名称(例如 ASRL4::INSTR)

  5. 打开MATLAB,在命令window中运行执行以下命令,显示VISA资源名称对应的VISA对象构造函数名称:

    visaInfo = instrhwinfo('visa', 'agilent'); visaInfo.ObjectConstructorName;

MATLAB Instrument Control Toolbox与VISA接口进行虚拟串口通信的示例代码:

% --- Open and write
v = visa('agilent', 'ASRL4::INSTR');
fopen(v);
fprintf(v, 'abc');
fwrite(v, [97 98 99]);
fclose(v);

% --- Clean up
delete(v);
clear v;

希望这对您有所帮助,