Serial Object with Callback Function Ignores ASCII values > 127

Hello,
I am using a serial object to communicate with an FPGA. I created a callback function that has its terminator set to '\n\r', or [10 13] in ASCII values. The callback works as expected, up until the time I attempt to send ASCII values that are > 127 from the FPGA to MATLAB, even though the '\n\r' is still present at the end of the packet sent from the FPGA to MATLAB. Is there any reason the serial object would ignore ASCII values > 127 and not call the callback function?
Thanks,
Frank

답변 (2개)

Jan
Jan 2012년 9월 30일

1 개 추천

Please post your code. Perhaps your observation is not correct, e.g. if you store the data in an signed integer and convert it to an unsigned later on.

댓글 수: 1

Hi Jan,
Thanks for the tip--I was using fscanf(serialObject) without specifying the format, so I changed to using fscanf(serialObject,'%u'), but that still isn't working. Below is my code, and I am using a GUI, hence the references to "handles":
-- main GUI code --
% create serial object
serConn = serial('COM2', 'TimeOut', 10,'BaudRate', 115200);
% specify function to handle data received on serial object
serConn.BytesAvailableFcn = {@serial_callback,handles};
% set input buffer size
serConn.InputBufferSize = 1024;
% set "end of serial message" = terminator
serConn.BytesAvailableFcnMode = 'terminator';
% choose terminator to be '\n\r'
serConn.Terminator = 'LF/CR'; % [10 13]
% open serial connection
fopen(serConn);\
--- callback function ---
function serial_callback(serConn, event, handles)
% get data on serial object, read in as unsigned integers
out = fscanf(serConn,'%u');
% get size of data read from serial object
outSize = size(out);
% print output for debugging
for i = 1:outSize(1,2)
fprintf('ASCII value of out(1,%d): %d\n',i,out(1,i));
end
end
I know I am getting the values on the serial object even when the values are > 127, because I can see outputs like this if I explicitly run the "print output for debugging" for loop:
ASCII value of out(1,1): 96
ASCII value of out(1,2): 3
ASCII value of out(1,3): 3
ASCII value of out(1,4): 19
ASCII value of out(1,5): 233
ASCII value of out(1,6): 43
ASCII value of out(1,7): 64
ASCII value of out(1,8): 10
ASCII value of out(1,9): 13
The issue is the callback is not being triggered when a value > 127 is sent to the MATLAB serial object from the external FPGA. So in this case, the 5th byte = 233, so the callback does not occur, even though the terminator is correct with the 8th and 9th bytes = 10 and 13, respectively.
Thanks in advance, Frank

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

per isakson
per isakson 2012년 9월 29일

0 개 추천

  1. CRLF, i.e [13,10] is used more often
  2. do you have 8 databits?

댓글 수: 3

Thanks for the reply. Yes, the serial object is set to 8 databits, no parity, and 1 stop bit, and I was under the impression that this allows transfer of values between 0 and 255. Is this not the case?
I thought so.
Have you ever experienced a similar issue?

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

카테고리

도움말 센터File Exchange에서 Standard File Formats에 대해 자세히 알아보기

질문:

2012년 9월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by