Why is webwrite() returning corrupted data?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponses (1)
Saffan
le 2 Mai 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.
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!