필터 지우기
필터 지우기

Receive serial data in Hex

조회 수: 7 (최근 30일)
Prashant Funde
Prashant Funde 2016년 4월 5일
댓글: Walter Roberson 2016년 4월 6일
I wanted to write data received from serial port in text file. I want received data in the form
40 00 20 00 10 00 08 00 00 0A 0F 0D 10
but i am getting data in the test file as 40 0 20 0 10 0 8 0 0 A F D 10.
Please tell me what should i use to get data as i received.
I am using following code
Rcvd = fread(serConn,45000,'char');
hexpass=sprintf(num2str('%3X', Rcvd));
header1 = '';
file_id=fopen('MyFile.txt','w');
fprintf(file_id,[header1 '\n']);
fprintf(file_id,'%s',hexpass);
Please suggest me to get the data in txt file as
40 00 20 00 10 00 08 00 00 0A 0F 0D 10
Thanks in adavance

채택된 답변

Walter Roberson
Walter Roberson 2016년 4월 5일
hexpass = num2str(Rcvd, '%02X ');
hexpass(end) = ''; %get rid of trailing space
  댓글 수: 2
Prashant Funde
Prashant Funde 2016년 4월 6일
편집: Walter Roberson 2016년 4월 6일
Hello Mr. Walter thanks for your answer but if i use above mentioned functions/steps then data written in txt file is coming like
2C41A7F3CF23402010000000000040201
where as in a text box or display it is seen like
24
CC
43
A4
70
and like this. So if i am using following lines
% hexpass=sprintf(num2str('%03X', Rcvd)); %Convert Data reaceived at COM port to hex value
hexpass = num2str(Rcvd,'%02X ');
hexpass(end) = ''; %get rid of trailing space
assignin('base','Hex',hexpass);
set(handles.Rx,'String',hexpass);
disp(hexpass);
header1 = 'Data in Hex format';
file_id=fopen('MyFile.txt','w');
fprintf(file_id,[header1 '\n']);
fprintf(file_id,'%s',hexpass);
fclose(file_id);
Walter Roberson
Walter Roberson 2016년 4월 6일
hexpass = num2str(Rcvd(:).', '%02X ');
hexpass(end) = ''; %get rid of trailing space

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by