Using fscanf, won't print out second row of data

조회 수: 1 (최근 30일)
Brandon
Brandon 2013년 2월 9일
I'm having trouble using the fscanf command to show the two different data points that are being sent over a com port. The data that is coming from the com port looks like this:
X,Y
1,6
2,5
3,4
4,3
5,2
6,1
Where each set of 0,0 is in a single cell.
I would like the data to look like this
X: 1, 2, 3, 4, 5, 6
Y: 6, 5, 4, 3, 2, 1
Please help if you can! I need to get this working soon. Thank you in advance!
%% Clearing COM PORTS
delete(instrfindall)
%% Create the serial object
serialPort = 'COM4';
serialObject = serial(serialPort,'BaudRate',38400, 'DataBits',8);
fopen(serialObject);
%% Set the instrument in remote mode
fprintf(serialObject,'%f,%f');
%% Set the time span and interval for data collection
stopTime = '14:31';
timeInterval = 0.0025;
%% Collect data
count = [1,1];
while ~isequal(datestr(now,'MM:SS'),stopTime)
time(count) = datenum(clock);
position(count) = fscanf(serialObject,'%f,%f');
pause(timeInterval);
count = count +1;
end
fclose(serialObject);
delete(serialObject);
clear serialObject;

채택된 답변

Walter Roberson
Walter Roberson 2013년 2월 9일
편집: Walter Roberson 2013년 2월 9일
You have
fprintf(serialObject,'%f,%f');
fprintf() is to send data, but you have not included any data to send. Perhaps the instrument is not in remote mode because of this problem.
Also, I suspect that for that instrument you will need to set the serial port Terminator property; I suspect it sends CR or CR+LF

추가 답변 (2개)

Brandon
Brandon 2013년 2월 11일
Ok, some of that makes sense to me.
I commented out the fprintf( and the code still works the same. Maybe the code I am trying to adapt to my device isn't exactly right.
I am using an arduino that has already been programmed. Basically what the arduino is doing is sending dx and dy position through a usb cable. I need to open the com port to save the data in matlab. The dx and dy originally is received in one cell. Like this dx,dy dx,dy ... Right now only dy is showing up. Let me know if you need anything else and thank you again for your help.
  댓글 수: 3
Brandon
Brandon 2013년 2월 11일
So how would I be able to store two values?
Walter Roberson
Walter Roberson 2013년 2월 11일
position(count,:) = fscanf(serialObject,'%d,%d');

댓글을 달려면 로그인하십시오.


Brandon
Brandon 2013년 2월 11일
What if I add two different fscanf( lines, like this:
position_x(count) = fscanf(serialObject,'%d,%d*');
position_y(count) = fscanf(serialObject,'%d*,%d');
  댓글 수: 1
Brandon
Brandon 2013년 2월 11일
I messed up on the location of the * the code should be:
position_x(count) = fscanf(serialObject,'%d,%*d');
position_y(count) = fscanf(serialObject,'%*d,%d');
This worked for me. Thanks for the help.
I just need to speed up the refresh rate now. If you have any ideas on that let me know. Thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by