How to clear serial port buffers from matlab to arduino?

91 views (last 30 days)
Hi there, I tried to use 'flush' to clear the serial buffer sent from MATLAB to Arduino, but I can't make it. Here is a simple code in MATLAB:
device = serialport("/dev/cu.usbmodem2101",9600);
write(device, 1:5, "uint8");
The device.NumBytesAvailable = 0, and device.NumBytesWritten = 5, and after I run this:
flush(device);
the device.NumBytesWritten is still 5.
I've tried different MATLAB versions (2022b,and 2019b, and tried to clear this in arduino, but neither works.
Please help me. I don't know what's going wrong and I've been stuck in this for several days. Thanks.

Accepted Answer

Hassaan
Hassaan on 3 Jan 2024
Edited: Hassaan on 4 Jan 2024
MATLAB Script:
This script sends a new piece of data to the Arduino every second. It assumes the Arduino is set up to read incoming data and act upon it.
% Define the serial port object connected to the Arduino
device = serialport("/dev/cu.usbmodem2101", 9600);
% Continuously send new data every second
for i = 1:100 % Send 100 updates as an example
% Convert the loop index to uint8 and write it to the serial port
write(device, uint8(i), "uint8");
% Wait for 1 second before sending the next piece of data
pause(1);
end
% Close the serial port connection
delete(device);
clear device;
Arduino Code:
This Arduino code is set up to read the incoming data and perform some action with it. It checks for new data, reads it, and then clears the buffer.
void setup() {
// Initialize serial communication at 9600 bits per second
Serial.begin(9600);
}
void loop() {
// If there's any serial available, read it
while (Serial.available() > 0) {
int incomingByte = Serial.read(); // Read the incoming byte
// TODO: Do something with the incomingByte, like controlling an LED
// Print the incoming byte to the serial monitor
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
How it Works:
  • The MATLAB script sends a new byte to the Arduino every second.
  • The Arduino continuously checks for new data. When new data is received, it reads the data, acts upon it (this part is up to you to define based on your needs), and prints a confirmation message.
Important Notes:
  • Make sure that the serial port (/dev/cu.usbmodem2101) specified in the MATLAB script matches the one assigned to your Arduino. This can vary and should be confirmed in the Arduino IDE.
  • The Serial.begin(9600); baud rate in the Arduino code must match the 9600 in the MATLAB script.
  • The pause(1); in the MATLAB script waits for 1 second between sending data. Adjust this timing based on your needs.
  • The example action in the Arduino code is merely printing the received byte. Replace the TODO comment with actual actions you want the Arduino to perform.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Development | Simulations
  • Electrical and Electronics Engineering

More Answers (1)

Hassaan
Hassaan on 3 Jan 2024
% Create a serial port object connected to the Arduino
device = serialport("/dev/cu.usbmodem2101", 9600);
% Write an array of uint8 values to the serial port
write(device, 1:5, "uint8");
% The user checks the number of bytes available to read from the Arduino
% and the number of bytes written to the Arduino
numBytesAvailable = device.NumBytesAvailable;
numBytesWritten = device.NumBytesWritten;
% Display the number of bytes available and written before flush
fprintf('Bytes available before flush: %d\n', numBytesAvailable);
fprintf('Bytes written before flush: %d\n', numBytesWritten);
% Flush the input and output buffers of the serial port
flush(device);
% Check the number of bytes available and written after flush
numBytesAvailableAfterFlush = device.NumBytesAvailable;
numBytesWrittenAfterFlush = device.NumBytesWritten;
% Display the number of bytes available and written after flush
fprintf('Bytes available after flush: %d\n', numBytesAvailableAfterFlush);
fprintf('Bytes written after flush: %d\n', numBytesWrittenAfterFlush);
% Close the serial port connection
delete(device);
clear device;
This script does the following:
  1. Opens a serial port connection to an Arduino.
  2. Writes a sequence of numbers (from 1 to 5) to the Arduino.
  3. Flushes the serial port buffers (both input and output).
  4. Prints out the number of bytes available (to read) and bytes written (to write) before and after the flush operation.
  5. Closes the serial port connection to clean up.
Remember that device.NumBytesWritten does not reflect the content of the output buffer; it's a cumulative count of all bytes that have been written. The flush command clears the buffers but does not reset this counter.
Also, this code will not work if the Arduino is not connected or if the serial port (/dev/cu.usbmodem2101) is not the correct port for your setup. Always confirm the correct port name using the Arduino IDE or your computer's device manager.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems
  • Electrical and Electronics Engineering
  2 Comments
颖 韩
颖 韩 on 3 Jan 2024
Thanks. The output of the script is:
Bytes available before flush: 0
Bytes written before flush: 5
Bytes available after flush: 0
Bytes written after flush: 5
I need to send to arduino updating data. My Arduino can respond to the first data I sent, but I want to delete the first one and send a new one.
So is device.NumBytesWritten just a counter and does arduino read from device.NumBytesWritten or device.NumBytesAvailable? How can I update the buffer it read? Thanks.
Hassaan
Hassaan on 3 Jan 2024
@颖 韩 The device.NumBytesWritten property in your MATLAB script is indeed just a counter indicating the total number of bytes you've sent to the Arduino up to that point. It doesn't represent the actual data stored in the buffer or the data that the Arduino reads. The Arduino doesn't read from device.NumBytesWritten; it reads from its own serial buffer, which stores the incoming data.
On the other hand, device.NumBytesAvailable indicates how many bytes are available to read from the Arduino's perspective. This means it's the number of bytes that have been sent to the Arduino and are waiting to be read.
To update the data that the Arduino reads, you don't need to manipulate device.NumBytesWritten or device.NumBytesAvailable directly. Instead, you should focus on how you manage the sending of data and how the Arduino processes it

Sign in to comment.

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!