Borrar filtros
Borrar filtros

Why is webwrite() returning corrupted data?

4 visualizaciones (últimos 30 días)
Travis J. Clauson
Travis J. Clauson el 15 de Abr. de 2023
Comentada: Travis J. Clauson el 2 de Mayo de 2023
I am using the webwrite() function to ping my arduino to collect accelerometer data, then reading-in the response with said data. The data is in the form of a string with data points seperated by a space. Then, I use sscanf() to read the data into a matrix of (4,N). However, random chunks of the string are often corrupted like this, "4.16 1300 -9.6ᆳxV?} T?T?2 -0.76 4.09 1313". This screws up the sscanf, and I am left with (at best) chunks of missing data or (at worst) only the data points up until the first chunk of corrupted data. I don't believe its the Arduino's fault, since it prints the string message correctly, but I am not 100% confident.
Below is some of my code:
site_name = <IP>
options = weboptions('Timeout', 10);
response = webwrite(site_name, '1', options);
time = toc;
fprintf('Response: %s Time: %3f\n',response, time)
raw_data = sscanf(response,'%f', [4,N+1]).' %Read string into a matrix, then transpose

Respuestas (1)

Saffan
Saffan el 2 de Mayo de 2023
There can be multiple reasons that can cause this. One of them could be due to the different character encoding of the response received. You can specify the character encoding using “weboptions” in the following way:
options=weboptions('Timeout',10,'CharacterEncoding','UTF-8' );
response = webwrite(site_name, '1', options);
If this does not work, then there must be a problem in the transmission of the data. You can try reducing the data transfer rate by reducing the baud rate or by adding delays.
It can also happen when the data being sent is large. You can use a buffer to break the data into smaller chunks and send them separately.
  1 comentario
Travis J. Clauson
Travis J. Clauson el 2 de Mayo de 2023
Thank you Saffan! I think the messge was too large (approximately 22,000 characters), and so breaking it up into many smaller chunks (1,000 characters) fixed the problem.

Iniciar sesión para comentar.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by