Why is webwrite() returning corrupted data?

3 views (last 30 days)
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

Answers (1)

Saffan
Saffan on 2 May 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 Comment
Travis J. Clauson
Travis J. Clauson on 2 May 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.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!