def parseUnit(formatString, undefined="NaN"):
    """Returns the unit of data item from MXElectric data message enclosed in [].
       Returns empty string as parse error. If the unit is not defined, string parameter
       undefined can be used to set it (defaults to NaN)."""
    opening = formatString.find('[')
    closing = formatString.find(']')
    if closing < 0 or opening < 0:
        # did not find both brackets, returning empty string
        return ""
    elif closing - opening <= 1:
        # brackets in the wrong order or no unit defined, returning undefined
        return undefined
    else:
        return formatString[opening+1:closing]