25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.0KB

  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. % if nargin < 1
  13. % localhostcellarray = getLocalIP();
  14. % else
  15. % localhostcellarray{1} = localhost;
  16. % end
  17. persistent stored_ipaddress;
  18. if ~isempty(stored_ipaddress)
  19. ipaddress = stored_ipaddress;
  20. return
  21. end
  22. local_port = randi([49152 65535]);
  23. remote_port = 3040;
  24. %% setup dsp to send and recieve udp packets.
  25. udpconnection = udp('255.255.255.255',3040);
  26. %hudpr = dsp.UDPReceiver('LocalIPPort',local_port);
  27. % hudps = dsp.UDPSender('RemoteIPAddress','255.255.255.255','RemoteIPPort',remote_port,'LocalIPPortSource','Property','LocalIPPort',local_port);
  28. % start recieving udp packets
  29. % setup(hudpr);
  30. fopen(udpconnection);
  31. %% Discover prologix.
  32. % magic string to request ipaddress from prologix: ['5a' '00' '5b' 'db' 'ff' 'ff' 'ff' 'ff' 'ff' 'ff' '00' '00']
  33. magic_msg = uint8([90 0 91 219 255 255 255 255 255 255 00 00]);
  34. % Sending packet.
  35. % hudps(magic_msg);
  36. fwrite(udpconnection,magic_msg);
  37. % Recieving packet or resend.
  38. for i = 1:100
  39. pause(0.1);
  40. msg = fread(udpconnection);
  41. if numel(msg)>0
  42. break;
  43. elseif mod(i,10) == 0
  44. %waiting dot and resending of string
  45. fprintf('.')
  46. fwrite(udpconnection,magic_msg);
  47. % step(hudps,magic_msg);
  48. end
  49. end
  50. fclose(udpconnection);
  51. if isempty(msg)
  52. error('No prologix found on network');
  53. end
  54. %ipaddress string is extracted from recieved UDP message. array position 21
  55. %till 24.
  56. ipaddress = num2str(msg(21:24)','%d.%d.%d.%d');
  57. stored_ipaddress = ipaddress;