receive date from PIC to matlab via serial port

2 views (last 30 days)
esra
esra on 6 Sep 2013
Edited: Eslam gomaa on 2 Apr 2015
hi
i have some problem i connect matlab code with PIC16F874A micro_control , i need to sent 2 hex number from PIC to matlab (becouse PIc only work with hex number),
this number will active the matlab code in matlab code I open connect between PIC and computer via serial port
_______________________
if true
%%open connect
ess=serial('COM3');
set(ess,'BaudRate',9600);
fopen(ess);
fread(ess)%to read data came from PIc
x=hex2dec(fread(ess))
________________________________
above code (fread)to read data from PIC
but when run the matlab code output ====>
Warning: The specified amount of data was not returned within the Timeout period.
ans =
Empty matrix: 1-by-0
Warning: The specified amount of data was not returned within the Timeout period.
x =
[]
please i need help
sorry for bad English
  3 Comments
esra
esra on 6 Sep 2013
thanks i search in help of matlab about this function but the output was
Use the Help browser Search tab to search the documentation,
or type "help help" for help command options, such as help for methods.
Muthu Annamalai
Muthu Annamalai on 6 Sep 2013
Sorry, fflush is not available on MATLAB, right now.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 6 Sep 2013
Set the terminator mode to byte. Unless, that is, the PIC is sending a CR or LF after the two bytes, in which case leave the fcn mode as terminator and set the serial port Terminator property as appropriate.
Do not just fread() against the port: that is going to try to keep reading until end of file or the buffer is full or terminator is encountered. You didn't set a buffer size and you didn't mention there being any terminator so you would be reading until the port closed or until the buffer of default size was reached.
[h, count] = fread(ess, 2, 'uint8=>char');
if count < 2
%uh oh, end of file or timeout
else
x = hex2dec(h);
end
  1 Comment
Eslam gomaa
Eslam gomaa on 2 Apr 2015
Edited: Eslam gomaa on 2 Apr 2015
hi
I try to recieve data from pic to matlab but I send character in pic code I know i will Set the terminator mode to byte.but which fn to get that data, is fread or fscan or else?? it showed me there is a problem in timeout what about time out??!!

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!