Puzzling STM32F4/F7 chip

/// Insert a message : At the beginning of this year, I recorded a set of systematic introductory single chip microcomputer tutorial , If you want, just ask me for it. It's free , I can send a private message ~ You can also get it by clicking my avatar in black font and adding my earth . I've been relatively idle recently , Lead to finish setting , Take students to provincial or above competitions ///

STM32F4/F7 chip ,matalb Installed is 2021a edition .

The core code is simple , as follows

s=serial('COM3')% Serial port 3 Bestow sfopen(s)% Open serial port 3fprintf(s,'123456 hello
world')% Serial port 3 Send cache write data fscanf(s)% Slave serial port 3 Receive cache read data fclose(s)% Close the serial port 3

As long as the above lines can realize the reading and writing of serial communication . But in the process of practical operation, we should use it at ease , There are many small details to pay attention to , Otherwise, all kinds of error reports will pop up . The essential tips are summarized below

. Before writing communication program , You can use functions instrhwinfo Search for available serial ports :

>> info = instrhwinfo('serial')

info =

  HardwareInfo with properties:

     AvailableSerialPorts: {2x1 cell}

'Version 4.4'

    ObjectConstructorName: {2x1 cell}

{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 =



>> info.AvailableSerialPorts

ans =



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

str =


>> scom=serial(str);

In this way , The serial port can be automatically specified when there is only one serial port device . If there are many serial devices , Other solutions are needed .

tip1: Can use s.status Check the switch status of the serial port .

tip2: Computer default baud rate 9600, Generally, the serial port attribute needs to be set , It can be set at one time , It can also be set separately

The general settings are as follows

s = serial('com3','BaudRate',115200,'BytesAvailableFcnMode','byte');

You can also set it step by step

s = serial(com);

s.BaudRate = 115200;

s.InputBufferSize = 512;

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

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

%s.terminator = CR/LF ; % Note here

Pay attention here , In text processing , CR, LF, CR/LF Is a newline character used on different operating systems .

Dos and windows: Use enter + Line feed CR/LF Indicates the next line . 
UNIX/Linux: Use newline LF Indicates the next line . 
MAC OS: Use carriage return CR Indicates the next line .

CR Use symbols '\r' express , decimal system ASCII The code is 13, Hexadecimal code is 0x0D; 
LF Use symbols '\n' express , decimal system ASCII The code is 10, Sixteen system 0x0A.

therefore Windows Line breaks on the platform are used in text files  0d 0a Two byte representation , and UNIX And apple platform is used for line feed 0a or 0d One byte representation .

Therefore, the output format should be used when sending \n, If used \r\n In the format of ,\r It will also be received as a message .CR Representative enter ,LF Delegate line feed .

About its settings , Also use set instructions , Because it's an internal 1*2 of cell, Saved read and write settings , So simply use s.terminator = CR/LF Will report an error .


tip3: adopt >> s=get(scom) Can pass all the parameters of the serial port and the current value , Among them, you can mainly set :

BaudRate, Baud rate

ByteOrder, Data big end or small end mode , Default segment

DataBits, Data bit , Usually default 8 position

Parity, Check bit , default none

StopBits, Stop bit , default 1

Timeout, matlab Waiting time for serial port to send or read data

ReadAsyncMode, How to read data asynchronously , Continuous or manual , Default continuous continue







Indicates the effective trigger mode of data , amount to c Interrupt trigger event in : Default value terminator, Indicates when the serial port receives a specific terminator , trigger bytes-available
event, Automatically add one to the parameter , And enter the callback function pointed by , amount to c Interrupt function in ; Optional value byte, Indicates when the serial port receives a byte , trigger bytes-available
event, Automatic plus one , Enter the callback function when a byte is received .

The terminator is usually a carriage return or line feed , You can also set it yourself , Customized according to communication protocol ,[Windows,Linux and MAC of CR, LF, CR/LF Line feed ]

Tip4: fwrite and fprint Differences between


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

fwrite and fread Data is sent in binary format , And the one above fprintf and fscanf So ASCII Sending in code format .

for example :data Is a decimal number 123, Its hexadecimal is 0x7b, The underlying binary data stream is 0111
1011, with ASCII Code sending is 0x31,0x32,0x33, The underlying data flow is 0011 0001,0011 0010,0011 0011.

matlab If used fwrite and fread function , The serial port parameters should also be changed to byte.

Tip5: It is usually necessary to delete the serial port after closing the serial port


clear scom;

Tip6:matlab Just opened , The serial port can be successfully opened for the first time , The second time you open it, an error will be reported as follows :

>> s = serial('com3','BaudRate',115200,'BytesAvailableFcnMode','byte');

>> fopen(s)

Error using serial/fopen (line 72)

Open failed: Port: COM3 is not

available. Available ports: COM1.

Use INSTRFIND to determine if other

instrument objects are connected to the

requested device.

Although we use fclose Close the serial port , But the serial port has not been completely cleaned , You need to delete all previous settings for the serial port , as follows :

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

Tip7:STM32 and MATLAB of USB Serial communication ,STM32 Reset will occur , cause MATLAB Cannot read from normally STM32 Serial port data sent . At this time, you need to turn off the sending request