MATLAB put a LED to a serial port - matlab

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.

Related

Reading serial port with matlab fscanf() or fgetl() shows results seconds after actual value change

I'm using the analog pins of an Arduino to measure voltage and print it to the serial port with serial.print(string). This is working, it outputs one value in the form of xxxx.yyyy with varying length, this I checked with the build in serial viewer. To read it, I'm using Matlab with fscanf() or fgetl(). This shows the correct thing, aaaa.bbbb, but with a weird kind of delay. When I change the voltage, the reading lags behind 5-10 seconds, but when I increase the voltage from 0 to 3 to 5, within this timeframe, like 3 seconds between 3 and 5, the reading changes as well. It does not in the same way as I changed the voltage, but it does get these values, as if it were 2 step inputs, even though I changed them by hand not super fast.
When reading and researching, I came across the fact that maybe the BaudRate was too low, so I changed it to the maximum of the Arduino at 115200. I changed the value in the Matlab code as well. I also tried the Arduino Hardware support package, with the readVoltage() function, but this has a too low sampling rate.
The settings for the serial port:
s = serial('com3');
set(s, 'BaudRate', 115200); % set BaudRate to 115200
set(s, 'Parity', 'none'); % set Parity Bit to None
set(s, 'DataBits', 8); % set DataBits to 8
set(s, 'StopBit', 1); % set StopBit to 1
set(s,'Terminator','LF') % set terminating character to LF/new line
fopen(s);
The reading loop:
for i =1:am
val(:,i) = string(fgetl(s)); %retrieve value of serial port, in the form aaaa.bbbb where a and b can vary in length
if mod(i,10) == 0 %Display every 10 iterations
volt1 = floor(val)*5/1023; %Calculating actual voltage for A1, where floor(val) is first value
volt2= (volt1-floor(val))*5/0.1023; %getting only decimals for second voltage
plot(volt1) %plot the values
hold on
plot(volt2)
hold off
ylim([0 5])
xlim([i-1000 i]) %make the plot chug along
drawnow %Live
end
clc
toc
end
There are no error messages and using the other serial port, I had no visible delay. This should also be possible with Matlab, as people get refresh rates of 2-3kHz using this.
The problem is the way you are using plot in a loop. This is very inefficient.
Depending on your Matlab version and the overhead on your serial link you might be able to speed things up calling clf at the beginning of the loop to prevent a wild number of plots stacking on top of each other.
If the problem persist you might want to try animated line and addpoints in the loop.
Finally, there are a couple of things you might want to check: the Arduino connection for Matlab (I never tried it myself but I've read it gives good performance) and the latency timer on your serial port (that might be having an effect at low baud rates).

sending data packet from matlab to watlow F4 temperature controller using Modbus RTU

I am currently using a Tenney Environmental Chamber which uses a Watlow F4 temperature controller to read out the temperature of the chamber. On the back of the Tenney Chamber is a 25 pin connector labelled for Watlow modbus communication. I connected to this terminal using a 25-pin-to-serial-232 adapter and a serial-232-to-usb cable. According to Watlow, to communicate with the temperature controller you must use the modbus RTU protocol. I am trying to read register 100, which is specified by Watlow as getting the actual chamber temperature.
This is very similar to another question that was asked here (Matlab serial Communication with Watlow F4 via Modbus RTU), and when I formatted my code like this it also did not work, so it has me thinking that maybe it has something to do with the 25-pin-to-serial-to-usb connection, but I'm really not sure.
Here is my code:
%create object connected to COM10
s = serial('COM10');
set(s, 'InputBufferSize', 3000);
%open object
fopen(s);
set(s, 'BaudRate', 9600);
set(s, 'Terminator', 'CR');
set(s, 'Timeout', 100);
%format request
request3= uint8(hex2dec(['01'; '03'; '00'; '64'; '00'; '01'; 'C5'; 'D5']))%FROM WATLOW EXAMPLE ONLINE: http://www.testequity.com/documents/chambers/Modbus_Packet.pdf
%write request to modbus
fwrite(s, request3);
%read reply from modbus
outdec3 = fread(s)
However, when I run this code this is the response I get:
Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period.
Am I formatting my request incorrectly? Or setting up the serial object incorrectly? Any help that can be provided would be greatly appreciated.

Plotting all the axes of Accelerometer in MATLAB using XBee+Arduino

I am trying to plot the real time graph of all the axes of the accelerometer wirelessly by XBee through MATLAB.
I have achieved the following until now:
On the transmitter side, I have an Arduino connected with an Xbee shield. I am using a Series 2 Xbees for this purpose.
I have an ADXL335 breakout board accelerometer hooked in to the analog pins Arduino+Shield combo and also the XBee (Co-Ordinator API) on the shield.
On the receiver side, I have an XBee (Router AT) connected to a computer which will record the output and plot it in MATLAB.
I also want to mention that I am using the Arduino library for MATLAB for the Arduino code.
Now, when I wire up the Arduino (i.e. I don't use the Xbee) then I get a perfect output of all the axis of the accelerometer.
But when I hook in the Xbee and try to communicate with the arduino I get nothing. E.g. when use the Xbee the command to plot the graph is:
a = arduino(COM X);
where X is the COM port where the Arduino is connected.
How should I change the command when I connect my Xbee? (Recalling again that my Arduino and accelerometer are not connected to the computer but my receiver side Xbee is.)
Here is my MATLAB code:
a.pinMode(15,'input');
a.pinMode(16,'input');
a.pinMode(17,'input');
a.pinMode(18,'output');
a.pinMode(19,'output');
a.digitalWrite(18,0);
a.digitalWrite(19,1);
xval = zeros();
yval = zeros();
zval = zeros();
xval(1) = a.analogRead(3);
yval(1) = a.analogRead(2);
zval(1) = a.analogRead(2);
pause(.001);
for i = 2:1000
xval(i) = a.analogRead(3);
yval(i) = a.analogRead(2);
zval(i) = a.analogRead(2);
xmod = (xval-496)/300;
ymod = (yval-511)/300;
zmod = (zval-508)/300;
subplot( 3,1,1)
plot(xmod, 'r');
axis([1 1000 -0.5 0.5]);
title( 'x' );
subplot( 3,1,2)
plot(ymod, 'r');
axis([1 1000 -0.5 0.5]);
title( 'y' );
subplot( 3,1,3)
plot(zmod, 'r');
axis([1 1000 -0.5 0.5]);
title( 'z' );
pause(0.001);
end
If I understand your setup correctly, it sounds like the problem is that you're using API firmware on the XBee connected to your Arduino. If you're looking to use XBee modules as a serial cable replacement (i.e., data arrives on serial port of XBee A, and is sent out on serial port of XBee B), then you should use "AT mode" firmware on both, and configure the DH/DL registers with the address of the paired node (with a shortcut of using zeros for DH/DL to indicate the coordinator as the destination node).
The API firmware requires a "smart host" that sends and parses "frames" of data that include header bytes and a checksum footer.
Digi's XBee Examples Site has some nice tutorials on getting XBee modules paired to each other for transparent serial communications.

No ports are available error while trying to read data from COM port in Matlab

I am trying to read data from the serial port of my laptop. The data is coming from the MSP430 through COM13.
When I try to access the data through MATLAB, it says -
??? Error using ==> serial.fopen at 72
Port: COM13 is not available. No ports are available.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.
Error in ==> interfaceplot at 3
fopen(s)
The code I am using is this-
s = serial('COM13'); %assigns the object s to serial port
set(s, 'InputBufferSize', 128); %number of bytes in inout buffer
set(s, 'FlowControl', 'none');
set(s, 'BaudRate', 9600);
set(s, 'Parity', 'none');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout',100);
%clc;
disp(get(s,'Name'));
prop(1)=(get(s,'BaudRate'));
prop(2)=(get(s,'DataBits'));
prop(3)=(get(s, 'StopBit'));
prop(4)=(get(s, 'InputBufferSize'));
disp([num2str(prop)]);
fopen(s); %opens the serial port
data = fscanf(s);
fclose(s); %close the serial port
Usually as pointed out by matlab and the Commenter, instrfind looks for objects to use your serial ports. A radical way to close all of those (unwanted) connections is:
fclose(instrfindall);
delete(instrfindall);
This closes all connections and deletes the objects. Typing instrfind shows you [] afterwards. If this doesn't work try to reconnect the device or restart the computer and then try again. Last thing I can think of is using the terminal/command line whatever (I'm on UNIX) and google how to find out what process uses which COM port. Then terminate the process and try again.
Not needing to say, that you need to close all other programs using that COM port. Be sure of that (it's easily forgotten).

matlab serial communication

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