STM32
直播中

1652711011.026500

16年用户 11294经验值
私信 关注
[问答]

matlab与stm32是如何利用串口进行通信的

matlabSTM32是如何利用串口进行通信的?有哪些过程?

回帖(1)

徐凤

2021-11-18 11:36:06
  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换行符]。
  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,‘%dn’, data,‘async’ );
  data = fscanf(scom,‘%d’);
  c:
  scanf(“%d”,&data);
  printf(“%drn”,data);
  note1:c中scanf、printf函数默认从终端打印读取数据,这里需要重定向fputc、fgetc函数。
  note2:c中scanf函数在读取到有效数据前会一直运行不退出。
  问题2:在stm32的串口中断函数中调用scanf函数,读取matlab发送的数据,matlab中用fprintf(scom,’%drn’, data,’async’ )发送数据,当stm32进入中断读取数据后,总是会再次进入中断并进入scanf函数出不来。
  处理方法:matlab中发送数据的格式 ‘%drn’,即回车加换行,个人理解是串口发送完一字节后也把‘r’即 回车符也发送出去,所以stm32的串口接收缓存中scanf函数读取数据后,由回车符又引起接收中断。
  将matlab发送函数中数据格式改为‘%dn’ 后,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。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分