Why do I receive Timeout occurred waiting for the String Terminator error ?

23 vues (au cours des 30 derniers jours)
Hür Doga Kantoglu
Hür Doga Kantoglu le 1 Mar 2022
Réponse apportée : Omega le 26 Déc 2023
I am trying to communicate with a device with serial port interface. Previously, I got true answer for instance when I asked the name of the device but now even though I havent changed anything I get this error .
s = serialport("COM3",115200);
s.Parity = 'Even' ;
configureTerminator(s,"CR") ;
resp = writeread(s,"gname")
Here is my code for asking the name of the device.
Can anyone help me please ?

Réponses (1)

Omega
Omega le 26 Déc 2023
Hi Hür Doga Kantoglu,
I understand that you are facing issues whie communicating with a serial port interface device. The error "Timeout occurred waiting for the String Terminator" suggests that MATLAB is trying to read data from the device over the serial port but is not receiving the expected string terminator within the allotted time frame. This can happen for several reasons:
  1. Device Not Responding: The device might not be responding to your command as expected. It's possible that the device is busy, not powered, or in a state where it's not sending back data.
  2. Wrong Terminator: You've configured the terminator as a carriage return (`"CR"`). Ensure that this is indeed the correct terminator expected by the device after it sends its response. If the device uses a different terminator, MATLAB will wait indefinitely for a carriage return that never comes.
  3. Buffer Issues: There might be unread data in the buffer from previous operations that is not being cleared out, which can confuse subsequent read operations.
  4. Serial Port Settings: Double-check that all serial port settings (baud rate, parity, stop bits, data bits) match the device's requirements.
  5. Timeout Setting: The default timeout might be too short for the device to respond. You can increase the timeout to give the device more time to reply.
Here's how you can modify your code to troubleshoot and potentially fix the issue:
% Create serial port object
s = serialport("COM3", 115200);
% Set serial port settings
s.Parity = 'Even';
s.StopBits = 1; % Set the StopBits if needed
s.DataBits = 8; % Set the DataBits if needed
s.Timeout = 10; % Increase timeout to 10 seconds
% Configure terminator
configureTerminator(s, "CR");
% Flush the input and output buffer
flush(s);
% Write the command and read the response
resp = writeread(s, "gname");
% Close the serial port connection when done
clear s;
Hope this helps!

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by