def register_to_signed_int(x, base=16):
    """
    Modbus uses 16-bit integers, which can sometimes be treated as a signed 15-bit number.
    This handles converting a signed 16-bit number to a proper int
    Optionally can also work on other bases, but that's not normally needed
    """
    if x & (1 << (base-1)):
        y = x - (1 << base)
    else:
        y = x
    return y