Why is webwrite() returning corrupted data?

3 ビュー (過去 30 日間)
Travis J. Clauson
Travis J. Clauson 2023 年 4 月 15 日
コメント済み: Travis J. Clauson 2023 年 5 月 2 日
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

回答 (1 件)

Saffan
Saffan 2023 年 5 月 2 日
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 件のコメント
Travis J. Clauson
Travis J. Clauson 2023 年 5 月 2 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by