How to read data every second in serial communications using matlab

2 ビュー (過去 30 日間)
sushma gunde
sushma gunde 2015 年 8 月 31 日
コメント済み: sushma gunde 2015 年 8 月 31 日
Hello, I would like to read data every one second from the device but i am unable to do it. I have tried with the following code. Here is the code...
s=serial('COM1','BAU',9600,'parity','none','databits',8,'stopbits',1,'terminator','CR/LF')
fopen(s)
fid2=fopen('pressure1.txt','w')
fprintf(fid2,'%s\t\t\t %s\n','time','pressure')
fprintf(s,'%s\r\n','PR1')
while true
out2=fscanf(s)
sr2=datestr(clock,0)
if ~isempty(out2)
fprintf(fid2,'%s: %s\n',sr2,out2)
pause(1)
end
end
fclose(fid2)

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 8 月 31 日
fprintf(s,'PR1')
The default format is '%s\n' . Each \n is replaced with the terminator you configured so you do not need to specifically send \r\n when you have configured CR/LF as the terminator. Notice you did not specifically send \r\n in your later fprintf
  2 件のコメント
sushma gunde
sushma gunde 2015 年 8 月 31 日
編集済み: sushma gunde 2015 年 8 月 31 日
@walter I tried using this also but i dont find any result. whenever i restart my device i will get the output like this:
time pressure measuremnt1
26-Aug-2015 16:51:19 0, 7.5800E-05,5, 2.0000E-02
and when i dont restart my device then my ouput looks like this :
warning: Unsuccessful read: A timeout occurred before the Terminator was reached.
out2 =
''
sr2 =
31-Aug-2015 16:22:26
sushma gunde
sushma gunde 2015 年 8 月 31 日
@walter can you suggest me some ways how to get the output every second. for example:
26-Aug-2015 16:51:19 0, 7.5800E-05,5, 2.0000E-02
26-Aug-2015 16:51:20 0, 7.5800E-05,5, 2.0000E-02
26-Aug-2015 16:51:21 0, 7.5800E-05,5, 2.0000E-02

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by