<P>我这里有一个程序,实现了PC与单片机串口通信,看看对你是否有帮助</P>
<P>function instrcallback(obj, event)<BR>%INSTRCALLBACK Display event information for the event.<BR>%<BR>% INSTRCALLBACK(OBJ, EVENT) displays a message which contains the <BR>% type of the event, the time of the event and the name of the<BR>% object which caused the event to occur. <BR>%<BR>% If an error event occurs, the error message is also displayed. <BR>% If a pin event occurs, the pin that changed value and the pin <BR>% value is also displayed. <BR>%<BR>% INSTRCALLBACK is an example callback function. Use this callback <BR>% function as a template for writing your own callback function.<BR>%<BR>% Example:<BR>% s = serial(\'COM1\');<BR>% set(s, \'OutputEmptyFcn\', {\'instrcallback\'});<BR>% fopen(s);<BR>% fprintf(s, \'*IDN?\', \'async\');<BR>% idn = fscanf(s);<BR>% fclose(s);<BR>% delete(s);<BR>%</P>
<P>% MP 2-24-00<BR>% Copyright 1999-2001 The MathWorks, Inc. <BR>% $Revision: 1.2 $ $Date: 2001/03/19 15:26:54 $</P>
<P><BR>% Define error message.<BR>error1 = \'Type \'\'help instrument\\instrcallback\'\' for an example using INSTRCALLBACK.\';</P>
<P><PTCH nargin<br>case 0<BR> error(sprintf([\'This function may not be called with 0 inputs.\\n\',...<BR> \'Type \'\'help instrument\\instrcallback\'\' for an example using INSTRCALLBACK.\']));<BR>case 1<BR> error(error1);<BR>case 2<BR> if ~isa(obj, \'instrument\') | ~isa(event, \'struct\')<BR> error(error1);<BR> end <BR> if ~(isfield(event, \'Type\') & isfield(event, \'Data\'))<BR> error(error1);<BR> end<BR>end </P>
<br>
<P>% Determine the type of event.<BR>EventType = event.Type;</P>
<P>% Determine the time of the error event.<BR>EventData = event.Data;<BR>EventDataTime = EventData.AbsTime;<BR> <BR>% Convert the clock time to a datenum so that it can be converted to the<BR>% display string.<BR>EventDataTime = num2cell(EventDataTime);<BR>EventDataTime = datenum(EventDataTime{:});</P>
<P>% Create a display indicating the type of event, the time of the event and<BR>% the name of the object.<BR>name = get(obj, \'Name\');<BR>fprintf([EventType \' event occurred at \' datestr(EventDataTime,13),...<BR>\' for the object: \' name \'.\\n\']);</P>
<P>% Display the error string.<BR>if strcmp(lower(EventType), \'error\')<BR>fprintf([EventData.Message \'\\n\']);<BR>end</P>
<P>% Display the pinstatus information.<BR>if strcmp(lower(EventType), \'pinstatus\')<BR> fprintf([EventData.Pin \' is \'\'\' EventData.PinValue \'\'\'.\\n\']);<BR>end</P>
<P>%Define the serial buffer array.<BR>out=zeros(200,1);</P>
<P>%Function of serial event operation.<BR>out=fread(obj, 200, \'uint8\') %Recieve 200 bytes from MCU.<BR>end</P>
<P>% Function of drawing a waveform. <BR>time1=1:1.8:360;<BR>plot(time1, out(:),\'.r\');<BR>%plot(time1, out_filter (:),\'--.b\');<BR>legend(\'距离定位\',1); <BR> grid on; <BR>title(\'测量信号波形图\');<BR>% end</P> |