How does Matlab set serial timeout function work? - matlab

I have a sensor connected to my computer and I downloaded some code that connects it with MATLAB over a serial port.
I have this code which initializes the connection and sets some parameters:
sport = serial(com_port); % assigns the object sport to serial port
set(sport, 'BaudRate', 9600);
... other configs
set(sport, 'Timeout', Timeout);
set(sport, 'ReadAsyncMode', 'continuous');
Where Timeout is a variable set to 5.
What does the set(sport, 'Timeout', Timeout); line actually do? I have looked through MATLAB's Serial Port Devices documentation -Found Here- but I cannot find the documentation on setting a serial port's timeout.
My original idea was that this value was the time my sport object was given to gather data, but the next line, set(sport, 'ReadAsyncMode', 'continuous'); tells the device to continually stream data.
Also, I don't want to go changing too many values without knowing exactly what I'm doing, for fear of breaking anything. Please help!

See the description of the TimeOut property in the matlab documentation.
The property TimeOut sets the "Waiting time to complete a read or write operation"
In the Contents menu on the left you can select other properties, such as ReadAsyncMode and read about them.

Related

How to read Analog Output Holding Registers on Advantech ADAM 6717 through ModBus TCP

I've been exploring the ADAM 6717 from Advantech.
This is the ModBus address table for said device:
At first I wanted to modify the value of the Digital output channel 0(DO0), so, as can be seen from the picture above, such address is the 0x0017.
I succeed at this by using a ModBus tool and the following settings:
Sending either "On" or "Off", turns On and off a LED connected to that output. Everything runs smoothly according to my expectation up to this point.
The problem arises when I want to read the Analog Input channel 6 or equivalently, address 400431~40044.
Since that address lies on the Analog Output Holding Registers part of the address table, I though that the following settings would accomplish the job:
However, as can be seen above, the reading shows 0.0 when there is actually 6V connected to that input (a potentiometer)
It is worth mentioning that I've made sure to enable the AI6 channel as well as setting it to Voltage mode instead of current. Also, the web utility for the device shows the AI6 reading correctly as I change the potentiometer's resistance value.
So the problem doesn't lie in the connection from the potentiometer to the AI6 but somewhere else.
Out of nothing and leaving aside what I think I know on this topic, I though of changing the function from 0x03 to 0x04
However, the response is exactly the same.
It bugs me that I can read and write values to the output coils but not the Analog output holding registers.
Is there any configuration that I might be missing over here?
Thanks in advance.
Device settings:
IP address: 10.0.0.1
Port in which the ModBus service is running: 5020

How to set baudrate on TwinCAT3 PLC for EL6002 or EL6001 comport?

I cannot find how to set baudrate for the Beckhoff EL6002. I got a hint that I should use CoeWrite block for that but as I am bit new to TwinCAT I cannot find the correct function block. Could someone send a code example (on structured text) how to do that?
An alternative to programming it would be to configure it directly via the IO configuration. If you add a Startup value, it will be set every time the IO changes from a specified state to another. In the pic below, PS means when going from Pre-Op to Safety. So it will work, even if you replace the IO.
Another solution is to change it under the IO configuration and COE-online tab. When you update it there, it will always remember the value.
In code, you can update it through CoE (Can over EtherCAT) too. You can find the index number of the setting variable from documentation. For channel 1, it seems to be 8000:11 so index = 8000 and subindex = 11.
Then by using mailbox writer block (FB_EcCoESdoWriteEx) from Tc2_EtherCAT library it is possible to write a value to that parameter. So when your PLC program starts, first run the code that updates the variable to desired baudrate.
For example, something like this:
TargetValue := 1; //WORD, Check documentation for correct value
//MailBoxWriter = Instance of FB_EcCoESdoWriteEx
MailBoxWriter(
sNetId:= **AmsNetId of the EtherCAT master**,
nSlaveAddr:= **Serial interface terminal port**,
nSubIndex:= 11,
nIndex:= 8000,
pSrcBuf:= ADR(TargetValue),
cbBufLen:= SIZEOF(TargetValue),
bExecute:= TRUE,
tTimeout:= T#500MS,
bCompleteAccess:= FALSE,
bBusy=> ,
bError=> ,
nErrId=>
);
The sNetIdis AmsNetId of the EtherCAT bus master. It can be linked from IO configuration, see Master->Infodata->AmsNetId.
The nSlaveAddr is terminal port from EL6002 and it can be linked from IO configuration, see Terminal->InfoData->AdsAddr->port.

Non blocking UDP receiver in Matlab GUI

I'm creating a MATLAB GUI using the app designer (very similar to, but better than, GUIDE) which I want to use to monitor the data output of my simulink model in real time.
In other words, I have a simulink model and a GUI, both running in the same MATLAB instance and I want to send packets over UDP from the simulink model and use that data in my GUI to update plots. However, I don't know how to read the data from the UDP packet without blocking.
Is there a way to bind a handler when a packet is received so that I can execute a function to update plots/fields?
Of course, beside BytesAvailableFcn matlab has the datagramreceivedfcn to call your custom function on new dagatrams, which is nonblocking while fread/fscanf are blocking (temporarily). Regarding callbacks in matlab read events and cbs
Compilable standalone could look like:
%% standalone main
%{
see docs/*
%}
function exitcode = main(port, remotePort)
% sanitize input
if(~exist('port','var') || ~exist('remotePort','var'))
disp(['args: port remotePort']);
exit(1);
end
if ischar(port)
port=str2num(port);
end
if ischar(remotePort)
remotePort=str2num(remotePort);
end
% create udp object
u = udp('127.0.0.1',remotePort, 'LocalPort', port);
% connect the UDP object to the host
fopen(u);
exitcode = 0;
% since we poll, suppress warning
warning('off','instrument:fscanf:unsuccessfulRead');
warning VERBOSE
% specify callback on datagram
while true
msgEnc= fscanf(u);
% timed out without datagram
if isempty(msgEnc)
continue
end
% do sth with msgEnc (which is a readable string)
fprintf(u, 'heard sth'); %answer
end
end
If you would like to use simulink block use udpreceive
which has a non-blokcking capability
A First In First Out (FIFO) buffer receives the data. At every time
step, the Data port outputs the requested values from the buffer. In a
nonblocking mode, the Status port indicates if the block has received
new data.

Trouble with communication with usb B type machine with Matlab

I am using matlab to communicate with several machines.
I am trying to connect with LCC25 (Liquid crystal retarder controller made by Thorlabs) using usb b to usb a cable.
I made a code like this.
clear all; clc;
%%
ss=serial('COM7','BaudRate',9600,'DataBits',8);
set(ss,'Parity','none');
set(ss,'Terminator','LF');
fopen(ss);
fprintf(ss,'*idn?');
aa=fscanf(ss)
fclose(ss)
Then I get "Warning : Unsuccessful read : A timeout occurred before the Terminator was reached aa=="
Is there any problem in my code?
I am also interested in buying the LCC25 and controlling it with MATLAB, so this is very interesting for me and I would love to find out whether it works...
To debug your code, I am wondering what happens when you comment out everything but:
ss=serial('COM7','BaudRate',9600,'DataBits',8);
set(ss,'Parity','none');
set(ss,'Terminator','LF');
fopen(ss);
Since then we can now if the problem is in establishing the connection itself (which you should not run every time btw!), or in trying to send a command to the device...
If the object creation is succesful, you should see something like this:
Serial Port Object : Serial-COM4
Communication Settings
Port: COM7
BaudRate: 9600
Terminator: 'LF'
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
Then you can try to add run
fopen(ss)
fscanf(ss)
in a seperate file, and see what the output is. If all of this works, you can start to try sending commands using the 'fprintf' command, but make sure not to run the 'serial' and 'fopen' command every
I am wondering where you obtained the command string '*idn?', did you find this in the help file? The same for the terminator 'LF', are you sure this is the correct terminator to use for the LCC25? When reading the error message you received, I suspect the problem to be that you might need to use other terminators, such as 'CR'.

Terra Term Macro Baud Rate Adjustment

I'm trying to make a macro for adjusting the baud rate of radios but I've run into some trouble and I haven't been able to find a solution through google search. I'm using the application terra term.
'Link macro first. Use 'connect macro' is the first error that is given when I try to run the macro. Here is the macro.
connect ttpmacro
initBaudRate=115200 ;
endBaudRate=9600 ;
setbaud initBaudRate
send 'at+ipr=' endBautRate
setbaud endBaudRate
send 'at&w'
setbaud initBaudRate
Of course there are a few other errors after this regarding syntax but I'm most interested in fixing the first error first, its my first time using ttl so I'm bound to have syntax errors that I'll pick up on.
The goal of the code is to set the serial port Baud rate to 115200 so it can talk to the radio, then change the radio to a baud rate of 9600, then change the serial port tob9600 and finish with at&w command.