import time

import serial

# tester_port="/dev/ttyUSB0"
tester_port = "COM3"

conn = serial.Serial(
    tester_port,
    baudrate=115200,
    stopbits=serial.STOPBITS_ONE,
    parity=serial.PARITY_NONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)

cmd_list = [
    #["*IDN?\n", True],  # QUERY ID
    ["FUNC1 \"RES\"\n", False],  # QUERY ID
    ["FUNC1?\n", True],  # QUERY ID
    #["FUNC2 \"FRES\"\n", False],  # QUERY ID
    #["FUNC2?\n", True],  # QUERY ID
    #["RATE S\n", True],  # QUERY ID
    #["RATE?\n", True],  # QUERY ID
    ["AUTO 0\n", False],  # SET RESISTANCE MEASURE, 500 Ohm range
    #["AUTO?\n", True],  # SET RESISTANCE MEASURE, 500 Ohm range
    ["CONF:RES 500\n", False],  # SET RESISTANCE MEASURE, 500 Ohm range
    # ["AUTO?\n", True],  # SET RESISTANCE MEASURE, 500 Ohm range
    #["RANGE:RES?\n", True],  # SET RESISTANCE MEASURE, 500 Ohm range
    #["MEAS?\n", True],  # GET MEASURE
    ["MEAS1?\n", True],  # GET MEASURE
    ["MEAS1?\n", True],  # GET MEASURE
    ["MEAS1?\n", True],  # GET MEASURE
    ["CALC:NULL:OFFS?\n", True],  # GET MEASURE
    ["CALC:NULL:OFFS 1E+02\n", True],  # GET MEASURE
    ["CALC:STAT:OFFS 1E+02\n", True],  # GET MEASURE
    ["CALC:NULL:OFFS?\n", True],  # GET MEASURE
    ["MEAS1?\n", True],  # GET MEASURE
    ["MEAS1?\n", True],  # GET MEASURE
    ["MEAS1?\n", True],  # GET MEASURE
    #["MEAS2?\n", True],  # GET MEASURE
]

for [cmd, expect_response] in cmd_list:
    conn.write(bytes(cmd, encoding="ascii"))
    if expect_response:
        response = conn.readline()
        print(f"{cmd} RESPONSE: " + response.decode("ascii", errors="backslashreplace"))
    else:
        print(f"{cmd}")
    time.sleep(1)
conn.close()
