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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by