瀏覽代碼

discoverprologix nu static method in equipmentclass

master^2
Wouter Horlings 7 年之前
父節點
當前提交
51f5c6325e
共有 2 個文件被更改,包括 33 次插入8 次删除
  1. +2
    -2
      OOequipment/debug/discovery.m
  2. +31
    -6
      OOequipment/subfiles/EquipmentClass.m

+ 2
- 2
OOequipment/debug/discovery.m 查看文件

@@ -4,11 +4,11 @@ ipPrefixRegex = '10\.0\.0';
if ispc()
system(['for /L %a in (1,1,254) do @start /b ping ' ipPrefix '.%a -w 100 -n 2 >nul']);
[~,arptable] = system('arp -a');
ipaddresses = regexp(arptable,[ipPrefixRegex '\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])(?=[-a-f0-9\s]*dynamic)'],'match');
ipaddresses = regexp(arptable,[ipPrefixRegex '\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])(?=[-a-f0-9\s]*?dynamic)'],'match');
elseif isunix()
[~,ipaddresses] = system(['echo $(seq 254) | xargs -P255 -I% -d" " ping -W 1 -c 1 ' ipPrefix '.% | grep -P -o "[0-1].*?(?=:)"']);
elseif ismac()
[~,ipaddresses] = system(['seq 254 | xargs -n1 -P255 -I% -- sh -c "ping -W1 -c1 ' ipPrefix '.% &>/dev/null && echo ' ipPrefix '.%"']);
else
end


+ 31
- 6
OOequipment/subfiles/EquipmentClass.m 查看文件

@@ -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

Loading…
取消
儲存