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: Serial port Reading - comp.soft-sys.matlabHello, 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... Help Reading Continuous Serial Port Data Stream? - comp.soft-sys ...I haven't done too much in the past using the serial port functions within Matlab so I'm trying to figure out what I'll need to do to read in my data ... serial port communication problem with null modem - comp.sys.hp ...It's more difficult to read "x or more" data bytes from a serial port than it is to read "x or fewer". For example, if the send port is being given 16 bytes max per ... Random NULL chars from serial port? - comp.unix.programmer ...Hi I'm working on an application which involves reading and writing from a serial port. The port has a GSM modem (actually a Nokia mobile phone) atta... Very slow serial port objects - comp.soft-sys.matlabHi there A month ago I wrote a very simple code to read from a COM-port. I have a device, which just transmits random numbers via COM3, and then I ... Serial port comunication in GUI - comp.soft-sys.matlabI am tring to read 3 byte data from serial port and display it on 3 edit boxes (GUI). I use ByteAvailable Function to generate an interrupt. When numb... Reading Serial data through Simulink Input Packet - comp.soft-sys ...Matlab and Simulink Data Input as bit ... serial signal through a RS232 serial port on my computer via simulink. ... Read data from workspace - Simulink - MathWorks ... Reading packets into a buffer - comp.os.linux.networking ...Windows OS Answers - SQL Server, .Net, Sharepoint, Windows Serial Port Communication problem reading bytes into packets ... PacketArray = Buffer.ToArray(); // Turn current ... RS232 data into MATLAB workspace - comp.soft-sys.matlabHi all, I need to read data into the MATLAB workspace from the serial port. I have tried using the RS232 block in simulink without success. I actua... serial communication problem with fread function - comp.soft-sys ...Help Reading Continuous Serial Port Data Stream? - comp.soft-sys ..... much in the past using the serial port functions ... Reading GPS data using serial port Hi, I have ... Reading from a Serial Port - Microsoft Corporation: Software ...An application calls the ReadFile function to receive data from a device at the other end of a serial connection. ReadFile takes the same parameters as the WriteFile ... Reading from serial port (using rxtx) « Embedded Freaks..Once you’ve got the serial InputStream, reading would be easy. Here’s an example of code to read the serial port using InputStream: /** * Buffer to ... 7/23/2012 5:13:45 PM
|