Serial port Reading

  • Follow


Hello,

I would like to make a program that reads from a serial port continuously and writes the data it takes to a file, i tried scanf, but it gets the character and stops. I want to make it continuously and stop it with another character.

thank u
jacob
0
Reply jacob 10/14/2010 4:47:03 PM

"jacob lalaounis" <lalaounisjacob@gmail.com> wrote in message <i97c67$o7a$1@fred.mathworks.com>...
> Hello,
> 
> I would like to make a program that reads from a serial port continuously and writes the data it takes to a file, i tried scanf, but it gets the character and stops. I want to make it continuously and stop it with another character.
> 
> thank u
> jacob

Hello Jacob,

You might want to look at the following demos:

1.) Serial Port query based continuous plot:
http://www.mathworks.com/matlabcentral/fileexchange/25519-collect-and-plot-data-from-an-instrument-in-real-time

2.) TCPIP based continuous read (without query):
http://www.mathworks.com/matlabcentral/fileexchange/27290-collect-and-plot-data-from-a-tcpip-server-in-real-time

These demos should give you a good idea how you can use your serial port object to do the desired task.

You might also want to read more about the serial object and it's properties at:
http://www.mathworks.com/help/techdoc/matlab_external/f92576.html

Hope it helps,

-Ankit
0
Reply Ankit 10/14/2010 9:10:07 PM


"jacob lalaounis" <lalaounisjacob@gmail.com> wrote in message 
news:i97c67$o7a$1@fred.mathworks.com...
> Hello,
>
> I would like to make a program that reads from a serial port continuously 
> and writes the data it takes to a file, i tried scanf, but it gets the 
> character and stops. I want to make it continuously and stop it with 
> another character.
>
> thank u
> jacob

Hi Jacob

The character that stops the FSCANF is called the terminator character.

s=serial('/dev/ttyS0');
get(s, 'Termintator')

ans =

LF

LF is the 8 bit value of 10.  You can set that any lower ASCII value you 
like.

set(s,'Terminator', 'Z');  % lets stop at the letter Z

The easiest way to continuously read from a port and write to a file is to 
use a callback function that is executed when you receive data.  Given the 
callback function below, the only MATLAB code required is that which sets up 
the file, serial port and callback.

% some file we want to append output to
% C:\TEMP\OUTFILE.TXT on Windows
appenderFile=fopen('/tmp/outfile.txt','a');

% COM1 on Windows, /dev/tty.KeySerial1 (or like device) on Mac
serialConnection=serial('/dev/ttyS0');

% lets setup a helper function thats called everytime a byte is received
set(serialConnection, 'BytesAvailableFcnMode', 'byte');
set(serialConnection, 'BytesAvailableFcnCount', 1);
set(serialConnection, 'BytesAvailableFcn', {@serialEventHandler, 
appenderFile});

fopen(serialConnection);

Data arriving will be put in the file when the helper function is called. 
When you are done, just close the port and file.

fclose(serialConnection);
fclose(appenderFile);

% This helper function should be on your path in a file called
% serialEventHandler.m

function serialEventHandler(serialConnection, ~, appenderFile )
    bytes = get(serialConnection, 'BytesAvailable');
    if(bytes > 0 ) % we may have alread read the data
        % Here I use FREAD and ignore the terminator but FSCANF could be 
used as well.
        data = fread(serialConnection, bytes);
        fwrite(appenderFile, data);
    end
end




0
Reply Trent 10/15/2010 10:59:21 PM

2 Replies
337 Views

(page loaded in 0.128 seconds)

Similiar Articles:













7/23/2012 5:13:45 PM


Reply: