From 51f5c6325e2b0077ca3bdf4f302306790b86418c Mon Sep 17 00:00:00 2001 From: Wouter Horlings Date: Sun, 25 Mar 2018 17:51:30 +0200 Subject: [PATCH] discoverprologix nu static method in equipmentclass --- OOequipment/debug/discovery.m | 4 +-- OOequipment/subfiles/EquipmentClass.m | 37 ++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/OOequipment/debug/discovery.m b/OOequipment/debug/discovery.m index 7fba8b8..6558794 100644 --- a/OOequipment/debug/discovery.m +++ b/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 diff --git a/OOequipment/subfiles/EquipmentClass.m b/OOequipment/subfiles/EquipmentClass.m index ed2d506..6c70068 100644 --- a/OOequipment/subfiles/EquipmentClass.m +++ b/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 \ No newline at end of file