Borrar filtros
Borrar filtros

How to read data from a serial device

2 visualizaciones (últimos 30 días)
eventhorizon01
eventhorizon01 el 26 de Sept. de 2011
Hello, a MATLAB beginner here. I have a weather station which can communicate serial. I tried to communicate with it by Tera Term and it works. I referred to the manual for commands; when I type in C, it gives me the temperature. The device uses the following settings: BAUD: 9600,4800,2400, 1200, 600 or 300 Data Bits: 7 Parity: Even Stop Bits: 2 Flow Control: Hardware
I used 1,200 in Tera Term and it worked. The problem is: how do I do this in MATLAB?
Here's the code I've made so far, I tried it out but I didn't get any output.
Here's my program
%----------------------------------------
s = serial('COM1');
set(s,'BaudRate',1200); %set BAUD
%set(s,'DataBits',7,'Name','WStation')
set(s,'DataBits',7)
set(s,'Parity','none') %set parity to none
set(s,'StopBits',2) %set stop bits to 2
set(s,'FlowControl','Hardware')
set(s,'Terminator','LF') %set the terminator to LF
fopen(s)
%Honestly I have no idea what to put here next.
%I want to read the temperature in every 3 mins or so and to be
%able to plot it against time. Does anyone know which syntax I will %use? Thanks
fclose(s)
clear s
Thanks in advance!

Respuestas (1)

Nuno Martins
Nuno Martins el 26 de Sept. de 2011
Hello, Before you open the serial device you should create a log file:
logFile = fopen(logFileName, 'wt');
The command to read from a serial device is:
serial_string = fscanf(SerialObj);
date_string = datestr(now);
fprintf(logFile,[date_string ' - ' serial_string]);
If you want to read the data each 3 minutes I would recommend you to use a timer ( http://www.mathworks.com/help/techdoc/ref/timer.html )
Hope it helps.

Categorías

Más información sobre Data Acquisition Toolbox Supported Hardware en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by