Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

53 řádky
1.7KB

  1. function ipaddress = prologix_discovery()
  2. %% PROLOGIX_DISCOVERY Find your prologix GPIB device.
  3. % ipaddress = PROLOGIX_DISCOVERY() finds prologix on your
  4. % local network. When your prologix device is detected
  5. % string with ip address is returned.
  6. %
  7. % Function uses persistent ipaddress: when ipaddress is
  8. % changed use CLEAR ALL to find the current ipaddress.
  9. % See also CLEAR
  10. %% setup correct variables.
  11. % if location of prologix is known: just return stored ip address
  12. persistent stored_ipaddress;
  13. if ~isempty(stored_ipaddress)
  14. ipaddress = stored_ipaddress;
  15. return
  16. end
  17. local_port = randi([49152 65535]);
  18. remote_port = 3040;
  19. %% setup dsp to send and recieve udp packets.
  20. hudpr = dsp.UDPReceiver('LocalIPPort',local_port);
  21. hudps = dsp.UDPSender('RemoteIPAddress','255.255.255.255','RemoteIPPort',remote_port,'LocalIPPortSource','Property','LocalIPPort',local_port);
  22. % start recieving udp packets
  23. setup(hudpr);
  24. %% Discover prologix.
  25. % magic string to request ipaddress from prologix: ['5a' '00' '5b' 'db' 'ff' 'ff' 'ff' 'ff' 'ff' 'ff' '00' '00']
  26. magic_msg = uint8([90 0 91 219 255 255 255 255 255 255 00 00]);
  27. % Sending packet.
  28. step(hudps,magic_msg);
  29. % Recieving packet or resend.
  30. for i = 1:100
  31. pause(0.1);
  32. msg = step(hudpr);
  33. if numel(msg)>0
  34. break;
  35. elseif mod(i,10) == 0
  36. %waiting dot and resending of string
  37. fprintf('.')
  38. step(hudps,magic_msg);
  39. end
  40. end
  41. if isempty(msg)
  42. error('No prologix found on network');
  43. end
  44. %ipaddress string is extracted from recieved UDP message. array position 21
  45. %till 24.
  46. ipaddress = num2str(msg(21:24)','%d.%d.%d.%d');
  47. stored_ipaddress = ipaddress;
  48. %% releasing connection.
  49. release(hudps);
  50. release(hudpr);