Is it possible to use the serialport object to connect exactly like Putty is able to?

6 visualizzazioni (ultimi 30 giorni)
I am using a MATLAB GUI app I wrote to connect via serial over USB to an Arduino. Unfortunately whenever I open the connection, the Arduino resets. This is expected behaviour, and there are hardware workarounds (including cutting the reset line, or using a 22uF cap like discussed here https://electronics.stackexchange.com/questions/24743/arduino-resetting-while-reconnecting-the-serial-terminal).
However, it IS also possible to prevent this behaviour by controlling the DTR line on the connection. For example, in Putty, if I select DSR/DTR mode, then the Arduino does not reset, so I know it is possible. I have not however been able to work out how to do it with MATLAB, and don't know whether it is possible. I have explored the SerialPort object help, and am aware of the DTR option. However, I have exhausted all attempts at getting this to work and am now not sure whether it is even possible.
Has anyone ever done this or got it working?
Many thanks,

Risposta accettata

Rushikesh
Rushikesh il 4 Giu 2025
Hello Robyn,
In certain hardware platforms like the Arduino UNO, configuring the DTR (Data Terminal Ready) signal to false before opening the serial port allows communication without triggering a reset. As per my knowledge, in MATLAB, the serialport object does not currently support setting the DTR line prior to opening the port. The setDTR function is available, but it can only be used after the port is opened—by which time the Arduino has already reset.
To work around this limitation, you can use MATLAB’s Python integration to control the DTR line before opening the port. Here’s how you can achieve this using Python’s serial module within MATLAB:
% Set up the port and baudrate
port = 'COM3';
baud = int32(9600); % Needs to be int32 for Python
% Import Python serial module
py.importlib.import_module('serial');
% Create a Python serial object with DTR disabled before opening
ser = py.serial.Serial();
ser.port = port;
ser.baudrate = baud;
ser.dtr = false; % Prevent auto-reset
ser.open();
disp('Opened port without DTR reset');
%other code to read-write to Arduino
% Close port
ser.close();
To verify that the Arduino does not reset, you can modify the Arduino sketch to blink the built-in LED in the "setup" function. If the LED does not blink when the MATLAB code runs, it confirms that the board did not reset.
  1 Commento
Robyn Galliers
Robyn Galliers il 21 Lug 2025
Just a note for any future users. It is not possible to use this Python method with a GUI app using the callback function (as you can with the serialport object), you would need to constantly poll for any new data.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by