Re: Problem in establishing RS232 Communication
- From: Joseph <nono@xxxxxxxx>
- Date: Tue, 8 May 2007 10:15:51 -0400
I wouldn't name your serial port object 'Com1', that may lead to
confusion later.
You may need to use a 'async' read with your fprintf command
Dominic Suter wrote:
.
Hi Forum
I wrote a function where I connect an external measurement device
to
get the measurement datas. Therefore I read several samples and
tested them extensively but one last problem is still occuring:
Everytime when I boot up my system and starting Matlab for aquiring
the measurement datas I can't establish the RS232 interface. The
code
I use is the following:
<code>
function [SpecimenVal] = tep
%%
%Creating and Opening the RS232 Serial Port (Com1)
Com1 = serial('Com1');
set(Com1, 'BaudRate', 9600, 'Parity', 'none', 'DataBit', 8,
'StopBit', 1, 'Terminator','CR/LF');
fopen(Com1);
disp('Connection to TEP Device through RS232 interface
established');
%%
--More code--
</code>
After starting the fuction from the command window, the only state
I
can recognice is a "busy" bessage from Matlab and nothing happends
anymore.
When typing the command straight to the command windwow, the system
is getting "busy" right after tiping "Com1 = serial('Com1').
However, I'm wondering that this error won't occour when I'm
opening
the function file in the source editor and use the "run" button.
Has anyone an Idea what's causing this error and what I'll have to
chance that the function will run properly out of the commandline
or
even .m-files?
Thank you very much!
My System: Matlab Version 7.3.0.267 (R2006b)
Windows XP Prof with all the latest updates
All the code:
<code>
%%
function [SpecimenVal] = tep
%%
%Creating and Opening the RS232 Serial Port (Com1)
Com1 = serial('Com1');
set(Com1, 'BaudRate', 9600, 'Parity', 'none', 'DataBit', 8,
'StopBit', 1, 'Terminator','CR/LF');
fopen(Com1);
disp('Connection to TEP Device through RS232 interface
established');
%%
%Communicating via COM1
fprintf(Com1, 'E'); %Request Echo form TEP
pause(1); %break for 1 sec to setup the TEP device
TepStatus = fscanf(Com1); %Answer from TEP. * == ACK, ! == ERR
pause(1);
fprintf(Com1, 'P'); %Get measurement values
disp('Getting measurement values');
pause(25);
fprintf(Com1, 'F'); %Finish measurement
disp('Finishing measurement');
pause(1);
for buffer=0:5
SpecimenVal = fscanf(Com1);%Seebeckcoefficient of Specimen
end;
SpecimenVal = regexprep(SpecimenVal, 'P', '');
SpecimenVal = regexprep(SpecimenVal, ' nV./£', '');
SpecimenVal = {str2num(SpecimenVal) 'nV/°C'};
clear buffer; clear TepStatus;
disp('End of procedure');
%%
%%Closing COM1 (it is important to close the COM Port for emtying
the
%%Buffer!
fclose(Com1);
delete(Com1);
clear('Com1');
disp('Connection to COM1 succesfully closed');
</code>
- Follow-Ups:
- References:
- Problem in establishing RS232 Communication
- From: Dominic Suter
- Problem in establishing RS232 Communication
- Prev by Date: Read Hdf5 data subset
- Next by Date: Does xmlread work with schema?
- Previous by thread: Problem in establishing RS232 Communication
- Next by thread: Re: Problem in establishing RS232 Communication
- Index(es):