# -*- coding: utf-8 -*-
"""

@author: David S. De Lorenzo

This script reads & displays the current measurement from
the Agilent E4418B Power Meter, which is connected to the
Met-Tilt (serial) interface of the Alloy receiver at IP
address 10.1.140.124.

Your E4418B must be configured for serial output:

    System/Inputs > Remote Interface > Select Interface > RS232

And make sure your receiver I/O Configuration is set as follows,
presuming that your serial cable is connected to COM1 or COM2:

    Serialx/COMx   MET-TILT
    Command        :FETCH?
    Schedule        1 Sec.

"""


import time
import requests
import xml.etree.ElementTree as ET


while True:

    # Read the Met-Tilt interface
    request_str = 'http://10.1.140.124/xml/dynamic/metResult.xml'
    resp = requests.get(request_str, auth=('admin', 'password'))

    # Parse the <result> element and cast to float
    x = float(ET.fromstring(resp.text).find('result').text)

    # Print result to screen
    print('Power reading = ' + str(x) + ' dBm')

    # Pause for 1/2 second
    time.sleep(0.5)
