SerPIC = serial('COM10');
set(SerPIC,'BaudRate', 115200, 'DataBits', 8, 'Parity', 'none','StopBits', 1, 'FlowControl', 'software');
fopen(SerPIC); %--open the serial port to the PIC
fprintf(SerPIC, '%s', 'b');
fid = fopen('D:\pipt1.abs');
tline = fgets(fid);
while ischar(tline)
fprintf(SerPIC, '%s',tline )
tline = fgets(fid);
end
fclose(fid);
fclose(SerPIC) %--close the serial port when done
delete(SerPIC)
clear SerPIC
I am using Tms570ls20216 usb. In the board i have bootloader in it. When i send a b to the board it'll flash the board after taking the abs file. Its working properly in hyperterminal but while running in matlab its not flashing. I am new to matlab. Is there anything wrong in my code. I dont know whether this is a proper place to ask the question. Sorry if it is not.
Do you need to send a line-feed at the end of your b command? Like this:
fprintf(SerPIC, 'b\n'); %add line feed, no need for %s from the original code
Related
I have a huge .txt file and parts of which I want to parse (using text scan), say I have 10000 line data and a part which starts at line 300, the part also has a header of 10 lines say,how can I skip the first 300 lines(not using header function of text scan of course as I then wont be able to get my actual 10 line header) or is there a way in which I can jump to line 300 and start text scan from there as if 301 line was the first line.
So, assuming your data is generated by the following (since you have not mentioned how it's formatted in your question):
fid = fopen('datafile.txt', 'w');
for i=1:300
fprintf(fid, 'ignore%d\n', i);
end
for i=301:310
fprintf(fid, 'header%d\n', i);
end
for i=311:10000
fprintf(fid, '%d\n', i);
end
fclose(fid);
You can read the data by first using fgetl to advance to the 300th line, then using textscan twice to get the header info and the data. Basically, the thing to remember is that textscan works starting from the place where fid is pointing. So, if it's pointing to the 301st line, it'll start scanning from there. So, here's the code to read the file above, starting from line 301:
fid = fopen('datafile.txt', 'r');
for i=1:300
fgetl(fid);
end
scannedHeader = textscan(fid, '%s', 10);
scannedData = textscan(fid, '%d');
fclose(fid);
NB: if the data is always the same format, you can use ftell to know where to skip to exactly then use fseek to go to that offset.
I am using a Kitronyx Snowboard (it has an Arduino Leonardo) with the Matrix Sensor 1610. I want to read my data from the serial port directly into Matlab and visualize it.
I have two problems which I think are related:
First of all I can read the Matrix but with a delay. The longer I wait the longer the delay.
Second of all after a while I’ll always get:
Error using serial/fprintf (line 144)
Unexpected error: A timeout occurred during the write operation..
The bigger the InputBufferSize the longer I can plot my data (which doesn’t really matter because of problem one)
How do I know what’s the convenient buffer size? Has anyone any idea why the delay and how I can get rid of it?
Thank you so much in advance
Mike
close all %close all figures
clear all %clear all workspace variables
fclose('all') %close all Files
delete(instrfindall) %Reset Com Port
delete(timerfindall) %Delete Timers
% setup serial
serialPort = serial('COM3');
command = 'A';
nrow = 16;
ncol = 10;
row_index = [9,10,11,12,13,14,15,16,8,7,6,5,4,3,2,1];
col_index = [1,2,3,4,5,6,7,8,9,10];
% 10x16 = 160 bytes
lendata = 160;
BaudRate = 11520;
InputBufferSize = 115200;
Timeout = 5;
set(serialPort , 'BaudRate', BaudRate);
set(serialPort , 'InputBufferSize', InputBufferSize);
set(serialPort , 'Timeout', Timeout);
fopen(serialPort);
while 1
% Request data
fprintf(serialPort, command);
% Get data
%Data is read as string (CSV)
data_string = fgets(serialPort);
data_string_array = strsplit(data_string, ',');
data = str2double(data_string_array);
% Reshape data (1D -> 2D array)
data2d = zeros(nrow, ncol);
k = 1;
for i = 1:nrow
for j = 1:ncol
data2d(row_index(i), col_index(j)) = data(k);
k = k + 1;
end
end
%resize 16x10 image to 160x100 image
data2d_resized = imresize(data2d,50);
imshow(data2d_resized,[0 100]);
end
I don't think we have enough information to give a definitive answer, but there are some considerations.
1) The input buffer is just memory that is set aside to hold data that is received at the serial port. Data fills the buffer until you remove it with a read statement (fgets(), fread(), etc). As long as the buffer size is larger than the amount of data you will receive between read cycles (see below), and it is a small number relative to the RAM available, it will not be a problem.
2) The command fgets() returns the line terminator characters with the string. You don't need these characters; consider using fegtl() or fscanf() instead.
3) The symptoms you describe make it sound like the Arduino is sending a more data with each iteration, or it is sending data faster than your Matlab program can process it. Your code is written in such a way that if more data arrives than you expect, you will not notice this until the input buffer is full and things start crashing.
Try printing the size of the data in the input buffer (disp(serialPort.BytesAvailable)) and the length of the string returned in each iteration.
If this is the problem, you may have to implement some flow control- have the Matlab program send a character back to the Arduino when it is ready for more data. Or read all the data from the input buffer in each read cycle, not just one line.
4) If matlab execution speed is an issue, consider replacing the "Reshape" loop with the reshape command. For loops are always slower than built-in functions.
I'm working on MATLAB, I want to connect only a LED & resistor to a serial port.
When a specific action happens in my program, the LED must turn on.
What I did but nothing had happened:
S = serial( 'com1' );
fid = fopen( s ); # ... 've checked the s vs. S ?
You should be able to control the DTR and RTS lines (on DB-9 connectors on pins 4 and 7, respectively) from MATLAB. According to the reference, serial port objects have the properties DataTerminalReady and RequestToSend, which can be toggled. For example,
S = serial('com1');
for ii = 1:5
set(S, 'RequestToSend', 'on');
pause(1)
set(S, 'RequestToSend', 'off');
pause(1)
end
will flash an LED connected to the RTS pin five times.
I have an array which looks like this:
cloud =
7.5059 51.4406
7.5057 51.4445
7.5048 51.4484
7.5034 51.4522
7.5014 51.4558
7.4989 51.4593
7.4958 51.4627
7.4923 51.4658
7.4884 51.4686
.
.
all i want is to write this array to a text file as it is, in the same format. I tried both fprintf and dlmwritebut i'm able to produce the exact same format. I know its an easy one, but I'm only asking after trying a lot.
Have you looked into string formatting?
fid = fopen( 'myFile.txt', 'w' );
for ii=1:size(cloud,1)
fprintf( fid, '%.5g\t%.5g\r\n', cloud(ii,1), cloud(ii,2) );
end
fclose( fid ); % do not forget to close the file :-)
Have you considered saveing into ascii file?
save( 'myFile.txt', 'cloud', '-ascii', '-tabs' );
EDIT:
End-of-line issue: for text file there are several way of marking the end of line: On windows it is usually required to print \r\n, for Mac and Linux sometimes it is enough to use \r and sometimes \n (I'm not 100% sure). So, you might need to experiment with it a bit to find out what works best for your machine. (Thanks #Rody for correcting me here)
Accuracy: the number in the formatting string %.5g determines the accuracy of the printed number. Again, you can play with it till you are satisfied with the results.
WINDOWS
Here's one way:
fid = fopen('cloud.txt', 'w');
fprintf(fid, '%.4f\t%.4f\r\n', cloud.');
fclose(fid)
Here's the more readable way:
dlmwrite('cloud.txt', cloud, 'delimiter', '\t', 'precision', 4, 'newline', 'pc')
LINUX
Here's one way:
fid = fopen('cloud.txt', 'w');
fprintf(fid, '%.4f\t%.4f\n', cloud.');
fclose(fid)
Here's the more readable way:
dlmwrite('cloud.txt', cloud, 'delimiter', '\t', 'precision', 4);
I have seen already a few articles about this topic, but most of these were from 3-4 years ago and I have to believe someone has done this before. Does anyone know if there is a way to have a MATLAB program that will listen on a specified port number. I am trying to transmit information to MATLAB over a local network, or potentially internet, and have MATLAB do stuff with it? Any suggestions?
My other option is just to send everything to an SQL server, and then have MATLAB poll this server. I however was concerned about the speed of this because MATLAB needs to be spitting out stuff in "real-time" or as close to as possible.
Note information will be taken from an iPhone and transmitted over Wi-Fi
I would use a language which is interfaced through Matlab, such as C over mex-files or Java.
This is my example. Android user sends a file name and the file to matlab TCP server. Then, matlab TCP server receives a line which is file name and file data.
t = tcpip('0.0.0.0', 8000, 'NetworkRole', 'Server');
set(t, 'InputBufferSize', 900000);
fprintf('waiting for client');
fopen(t);
pause(1);
fprintf('client connected');
fid = fopen('temp','w+');
file_name = fgetl(t);
% read first line
while (get(t, 'BytesAvailable') > 0 )
pause(2);
t.BytesAvailable
data = fread(t, t.BytesAvailable);
pause(2);
fwrite(fid, data);
end
copyfile('temp',['../../data/' file_name]);
fclose(t);
fclose(fid);