|
|
|
@@ -369,6 +369,7 @@ classdef EquipmentClass < handle |
|
|
|
end |
|
|
|
|
|
|
|
function ipaddress = getInterfaceIP() |
|
|
|
%GETINTERFACEIP gets the local ipaddresses on this machine. |
|
|
|
if ispc |
|
|
|
% Code to run on Windows plaform |
|
|
|
[status,result]=system('ipconfig'); |
|
|
|
@@ -396,14 +397,38 @@ classdef EquipmentClass < handle |
|
|
|
end |
|
|
|
|
|
|
|
function prologixIP = discoverPrologix() |
|
|
|
persistent stored_ipaddress; |
|
|
|
if ~isempty(stored_ipaddress) |
|
|
|
prologixIP = stored_ipaddress; |
|
|
|
%DISCOVERPROLOGIX attemts to find the prologix on the network. |
|
|
|
%It will broadcast on all interfaces and return the ipaddress |
|
|
|
%of the prologix. Not build for multiple prologixs on a single |
|
|
|
%network. |
|
|
|
persistent storedipaddress; %store the ipaddress of the prologix |
|
|
|
if ~isempty(storedipaddress) |
|
|
|
prologixIP = storedipaddress; |
|
|
|
return |
|
|
|
end |
|
|
|
ipaddress = getInterfaceIP() |
|
|
|
udpconnection = udp('255.255.255.255',3040); |
|
|
|
udpconnection.LocalHost = localhost; |
|
|
|
remotehost = '255.255.255.255'; %set broadcast address |
|
|
|
remoteport = 3040; %set remoteport that is used for the prologix discoverprotocol |
|
|
|
timeout = 0.2; %set timeout on package. |
|
|
|
% magic string to request ipaddress from prologix: ['5a' '00' '5b' 'db' 'ff' 'ff' 'ff' 'ff' 'ff' 'ff' '00' '00'] |
|
|
|
magic_msg = uint8([90 0 91 219 255 255 255 255 255 255 00 00]); |
|
|
|
ipaddress = EquipmentClass.getInterfaceIP(); %get all interface addresses from getInterfaceIP. |
|
|
|
%create an UDPconnection-object for each interface. |
|
|
|
udpconnection = cellfun(@(ip) udp(remotehost,remoteport,'LocalHost',ip,'Timeout',timeout),ipaddress,'UniformOutput',0); |
|
|
|
cellfun(@fopen,udpconnection); %open all udpconnections |
|
|
|
cellfun(@(udpconnection) fwrite(udpconnection,magic_msg),udpconnection); %broadcast the magic_msg via all udpconnections |
|
|
|
for i = 1:10 |
|
|
|
answer = cellfun(@fread,udpconnection,'UniformOutput',0); %read all udpInputBuffers for response from prologix |
|
|
|
ack = ~cellfun(@isempty,answer); %check if prologix responded on one of the interfaces. |
|
|
|
if sum(ack) > 0 %true if recieved response |
|
|
|
msg = answer{ack}; %put message from answer cell in to msg variable |
|
|
|
prologixIP = num2str(msg(21:24)','%d.%d.%d.%d'); %get ipaddress from msg. |
|
|
|
cellfun(@fclose,udpconnection); %close all connections |
|
|
|
storedipaddress = prologixIP; %store ip-address for next functioncall |
|
|
|
return |
|
|
|
end |
|
|
|
end |
|
|
|
cellfun(@fclose,udpconnection); %close all connections |
|
|
|
prologixIP = ''; %nothing found, return empty chararray. |
|
|
|
end |
|
|
|
end |
|
|
|
end |