×
单片机 > 单片机程序设计 > 详情

matlab与stm32之间利用串口通信记录

发布时间:2020-07-08 发布时间:
|

1、涉及到的函数


serial,fopen,fclose,

instrfindall,instrhwinfo,

fprintf,fscanf,fwrite,fread,isempty


1.1 串口函数


scom = serial('com6','BaudRate',115200,'BytesAvailableFcnMode','byte'); 

fopen(scom); 

.

fclose(scom);


或者对串口属性单独设置,如下:


scom = serial(com);  

scom.BaudRate = 115200;

scom.InputBufferSize = 512;

scom.BytesAvailableFcnMode = 'terminator';  % 'byte'

scom.terminator = CR/LF ;

scom.Timeout = 50; %read or write wait time

fopen(scom);

.

fclose(scom);


通常会在关闭串口后删除串口,在matlab工作空间中清除串口数据:


delete(scom);

clear scom;


问题1.:matlab刚打开后,第一次可以成功打开串口,第二次再打开就会报错如下:


>> scom = serial('com6','BaudRate',115200,'BytesAvailableFcnMode','byte');

>> fopen(scom)

Error using serial/fopen (line 72)

Open failed: Port: COM6 is not

available. Available ports: COM1.

Use INSTRFIND to determine if other

instrument objects are connected to the

requested device.


处理方法:个人理解是关闭串口后,串口并没有被完全清理,就像有些软件卸载后第二次再安装就会失败,所以需要在重新打开串口之前删除之前对串口的所有设置,如下:


>> scom = serial('com6','BaudRate',115200,'BytesAvailableFcnMode','byte');

>> fopen(scom);

Error using serial/fopen (line 72)

Open failed: Port: COM6 is not

available. Available ports: COM1.

Use INSTRFIND to determine if other

instrument objects are connected to the

requested device.


>> delete(instrfindall('Type','serial'));

>> scom = serial('com6','BaudRate',115200,'BytesAvailableFcnMode','byte');

>> fopen(scom);

>> fclose(scom)


>> help instrfindall

 instrfindall Find all communication interface objects with specified

 property values.


instrfindall函数可以找出所有与matlab通信的接口,也可以找出符合特定参数的接口。


1.2 serial 参数理解 

通过>> s=get(scom) 可以过得串口的所有参数以及当前的值,其中主要设置的有:


BaudRate,波特率

ByteOrder,数据大端或者小端模式,默认小段

DataBits,数据位,通常默认8位

Parity,校验位,默认none

StopBits,停止位,默认1

Timeout,matlab串口发送或者读取数据等待时长

ReadAsyncMode,异步读取数据的方式,连续或者手动,默认连续continue


----------

BytesAvailableFcnMode

BytesAvailableFcnCount

BytesAvailableFcn

Terminator

BytesAvailable

表示数据有效的触发方式,相当于c中的中断触发事件:默认值terminator,表示串口接收到特定的终止符时,触发bytes-available event,参数自动加一,并进入由指向的回调函数,相当于c中的中断函数;可选值byte,表示串口收到一个字节时,触发bytes-available event,自动加一,当收到

个字节时进入回调函数。

终止符通常是回车或者换行符,也可以自己设定,根据通信协议自定义,[Windows、Linux和MAC的CR, LF, CR/LF换行符](http://blog.csdn.net/cckit/article/details/41604771)。


matlab搜索可用串口端口函数instrhwinfo :


>> info = instrhwinfo('serial')

info = 

  HardwareInfo with properties:

     AvailableSerialPorts: {2x1 cell}

           JarFileVersion: 'Version 3.7'

    ObjectConstructorName: {2x1 cell}

              SerialPorts: {2x1 cell}

Access to your hardware may be provided by a support package. Go to the Support Package Installer to learn more.


>> info.SerialPorts

ans = 

    'COM1'

    'COM6'

>> info.AvailableSerialPorts

ans = 

    'COM1'

    'COM6'

>> str = char(info.SerialPorts(2))

str =

COM6

>> scom=serial(str);


我在电脑设备管理器查看,用的是串口’COM6’, ‘COM1’还不知道时接的是什么,所以这里想用matlab自动选择串口的功能还没有实现。


1.3 数据读写函数


matlab:

fprintf(scom,'%d\n', data,'async' );

data = fscanf(scom,'%d');

c:

scanf("%d",&data);

printf("%d\r\n",data);


note1:c中scanf、printf函数默认从终端打印读取数据,这里需要重定向fputc、fgetc函数。 

note2:c中scanf函数在读取到有效数据前会一直运行不退出。


问题2:在stm32的串口中断函数中调用scanf函数,读取matlab发送的数据,matlab中用fprintf(scom,’%d\r\n’, data,’async’ )发送数据,当stm32进入中断读取数据后,总是会再次进入中断并进入scanf函数出不来。 

处理方法:matlab中发送数据的格式 '%d\r\n',即回车加换行,个人理解是串口发送完一字节后也把'\r'即 回车符也发送出去,所以stm32的串口接收缓存中scanf函数读取数据后,由回车符又引起接收中断。 

将matlab发送函数中数据格式改为'%d\n' 后,stm32可以正常读取数据。


matlab:

fwrite(scom,data,'uint8','async');

cmd_ack = fread(scom,1,'uint8');

c:

rec = USART_ReceiveData( DEBUG_USART );

Usart_SendByte(DEBUG_USART,data);


note1:fwrite和fread是以二进制的格式发送数据,而上面的fprintf和fscanf是以ASCII码的格式发送。 

例如:data为十进制数123,其十六进制为0x7b,底层二进制数据流就是0111 1011,以ASCII码发送是0x31,0x32,0x33,底层数据流是0011 0001,0011 0010,0011 0011。 

matlab如果用fwrite和fread函数,串口参数也要改为byte。



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
基于arm的指纹识别门禁系统是如何设计的