Index: CN0_Python_Scripts/commands_rev_b.py =================================================================== diff -u -r509657069dd7ee800560a5c0b790077ce4cbd31a -r685d8eb11e2a49f2c8f7a270e379186415594601 --- CN0_Python_Scripts/commands_rev_b.py (.../commands_rev_b.py) (revision 509657069dd7ee800560a5c0b790077ce4cbd31a) +++ CN0_Python_Scripts/commands_rev_b.py (.../commands_rev_b.py) (revision 685d8eb11e2a49f2c8f7a270e379186415594601) @@ -1,150 +1,259 @@ import parser import struct +import time +from datetime import datetime ######################################################################################################################## +# This function reads the sensor data and settings # {"poll", cmd_poll}, def cmd_poll(cond1): cond1.ser.write(b'poll\n') print("Sent command cmd_poll") i = 0 poll = "" - while i <= 21: # decode by number of lines of data messages - current_line_text = str(cond1.ser.readline().decode()) - poll = poll + current_line_text + while i <= 21: + line = cond1.ser.readline() + current_line_text = line.decode('utf-8', errors='replace') + poll += current_line_text i += 1 print(poll) ######################################################################################################################## -# {"poll_bin", cmd_poll_bin}, -def cmd_poll_bin(cond1): - cond1.ser.write(b' poll_bin\n') - print("Sent command cmd_poll_bin") - data = cond1.ser.read(100) - print(data) - parsed = parser.parse_cmd_poll_bin(data) - parser.print_poll_readout(parsed) - -######################################################################################################################## +# This function updates the excitation voltage in the flash. # {"setvolt", cmd_voltage} def cmd_voltage(cond1, value): + cond1.ser.reset_input_buffer() cond1.ser.write(f"setvolt {value}\n".encode()) - print("Sent command cmd_voltage_bin") + print("Sent command cmd_voltage") + # minimal wait until some data arrives + while cond1.ser.in_waiting == 0: + pass + + data = cond1.ser.readline() + print(data) + ######################################################################################################################## -# {"setfreq_bin", cmd_frequency_bin}, -def cmd_frequency_bin(cond1, value): - cond1.ser.write(f"setfreq_bin {value}\n".encode()) - print("Sent command cmd_frequency_bin") - data = cond1.ser.read(1) +# This function updates the excitation frequency in the flash. +# {"setfreq", cmd_frequency}, +def cmd_frequency(cond1, value): + cond1.ser.reset_input_buffer() + cond1.ser.write(f"setfreq {value}\n".encode()) + print("Sent command cmd_frequency") + + # minimal wait until some data arrives + while cond1.ser.in_waiting == 0: + pass + + data = cond1.ser.readline() print(data) ######################################################################################################################## -# {"setk", cmd_cellconstant}, -def cmd_cellconstant(cond1, value): - cond1.ser.write(f"setk {value}\n".encode()) - print("Sent command cmd_cellconstant_bin") +# This function updates the setup time in the flash. +# {"setstm", cmd_setuptime}, +def cmd_setuptime(cond1, value): + cond1.ser.reset_input_buffer() + cond1.ser.write(f"setstm {value}\n".encode()) + print("Sent command cmd_setuptime") + # minimal wait until some data arrives + while cond1.ser.in_waiting == 0: + pass + + data = cond1.ser.readline() + print(data) + ######################################################################################################################## -# {"setcof_bin", cmd_coefficient_bin}, -def cmd_coefficient_bin(cond1, value): - cond1.ser.write(f"setcof_bin {value}\n".encode()) - print("Sent command cmd_coefficient_bin") - data = cond1.ser.read(1) +# This function updates the hold time in the flash. +# {"sethtm", cmd_holdtime}, +def cmd_holdtime(cond1, value): + cond1.ser.reset_input_buffer() + cond1.ser.write(f"sethtm {value}\n".encode()) + print("Sent command cmd_holdtime") + + # minimal wait until some data arrives + while cond1.ser.in_waiting == 0: + pass + + + data = cond1.ser.readline() print(data) ######################################################################################################################## -# {"setstm_bin", cmd_setuptime_bin}, -def cmd_setuptime_bin(cond1, value): - cond1.ser.write(f"setstm_bin {value}\n".encode()) - print("Sent command cmd_setuptime_bin") - data = cond1.ser.read(1) +# This function updates the temperature coefficient in the flash. +# {"setcof", cmd_coefficient}, +def cmd_coefficient(cond1, value): + cond1.ser.reset_input_buffer() + cond1.ser.write(f"setcof {value}\n".encode()) + print("Sent command cmd_coefficient") + + # minimal wait until some data arrives + while cond1.ser.in_waiting == 0: + pass + + data = cond1.ser.readline() print(data) ######################################################################################################################## -# {"sethtm_bin", cmd_holdtime_bin}, -def cmd_holdtime_bin(cond1, value): - cond1.ser.write(f"sethtm_bin {value}\n".encode()) - print("Sent command cmd_holdtime_bin") - data = cond1.ser.read(1) +# This function updates the cell constant in the flash. +# {"setk", cmd_cellconstant}, +def cmd_cellconstant(cond1, value): + cond1.ser.reset_input_buffer() + cond1.ser.write(f"setk {value}\n".encode()) + print("Sent command cmd_cellconstant") + + # minimal wait until some data arrives + while cond1.ser.in_waiting == 0: + pass + + data = cond1.ser.readline() print(data) ######################################################################################################################## -# {"setcal_bin", cmd_setCalData_bin}, +# This function retrieves raw sensor data in ASCII format. +# {"getraw", cmd_tx_raw_poll}, +def cmd_tx_raw_poll(cond1): + cond1.ser.reset_input_buffer() + cond1.ser.write(b'getraw\n') + print("Sent command getraw") + i = 0 + poll = "" + while i <= 6: # decode by number of lines of data messages + current_line_text = str(cond1.ser.readline().decode()) + poll = poll + current_line_text + i += 1 + print(poll) -def cmd_setCalData_bin(cond1): - # Prepare coefficients (floats as plain decimals) - coeffs = [ - 0.016 * 10, - -0.010* 10, - -0.010 * 10, - 0.012 * 10, - 35.832 * 10, - -28.066 * 10, - ] +######################################################################################################################## +# This function retrieves raw conductivity and temperature values in binary format. +def read_autopoll(cond1, duration_sec=10): + fmt = " setcal_bin v1 v2 v3 v4 v5 v6\n" - # Use fixed-point formatting to avoid scientific notation, ensure ASCII - payload = " ".join(f"{v:.10f}" for v in coeffs) - cmd = f"setcal_bin {payload}\n".encode("ascii") - print(cmd) +######################################################################################################################## +# This function enables the auto polling / publishing feature +# {"b", cmd_start_poll_bin} +def cmd_start_poll_bin(cond1): + cond1.ser.reset_input_buffer() + cond1.ser.write(b' b\n') + print("Sent command cmd_start_poll_bin") - # Send command +######################################################################################################################## +# This function disables the auto polling / publishing feature +# {"e", cmd_stop_poll_bin} +def cmd_stop_poll_bin(cond1): + cond1.ser.reset_input_buffer() + cond1.ser.write(b' e\n') + print("Sent command cmd_stop_poll_bin") + +######################################################################################################################## +# This function retrieves the values of 12 coefficients in ASCII format +# {"getcal", cmd_tx_cal_data} +def cmd_get_cal_data(cond1): + cond1.ser.reset_input_buffer() + cond1.ser.write(b' getcal\n') + print("Sent command getcal") + + i = 0 + cal = "" + while i <= 12: # decode by number of lines of data messages + current_line_text = str(cond1.ser.readline().decode()) + cal = cal + current_line_text + i += 1 + print(cal) + +######################################################################################################################## +# This function updates the value of coefficients in the flash. +# idx : Coeffficent index 1 to 12 +# value: New value of the coefficient +# {"setcal", cmd_rx_cal} +def cmd_setcal(cond1, idx, value): + cond1.ser.reset_input_buffer() + cmd = f"setcal{idx} {value}\n".encode() cond1.ser.write(cmd) - print("Sent command cmd_setCalData_bin") + print(f"Sent command cmd_setcal for index {idx} with value {value}") + + # minimal wait until some data arrives + while cond1.ser.in_waiting == 0: + pass + data = cond1.ser.readline() - # Read one byte status (same pattern as your other function) - # data = cond1.ser.read(1) print(data) ######################################################################################################################## -# {"getcal_bin", cmd_getCalData_bin} -def cmd_getCalData_bin(cond1): - cond1.ser.write(b' c\n') - print("Sent command cmd_getCalData_bin") - data = cond1.ser.read(48) - print(data) - d = parser.parse_cal_file_bytes(data) - parser.print_cal_file(d) +# This function retrieves version information in ASCII format +# {"getver", cmd_tx_version} +def cmd_get_version(cond1): + cond1.ser.reset_input_buffer() + cond1.ser.write(b' getver\n') + print("Sent command getver") + i = 0 + ver = "" + while i <= 3: # decode by number of lines of data messages + current_line_text = str(cond1.ser.readline().decode()) + ver = ver + current_line_text + i += 1 + print(ver) + ######################################################################################################################## -# {"setcal_bin", cmd_setCalData_bin}, -def cmd_setSerial_bin(cond1): - value = 0.2 - # cmd = b'setsn 0x002\n' - cmd = f"setsn {value}\n".encode() - cond1.ser.write(cmd) - print("Sent command cmd_setSerial_bin") - data = cond1.ser.read(1) +# This function updates the hardware version number in the flash. +# {"sethw", cmd_set_hardware_ver}, +def cmd_set_hardware_ver(cond1): + value = "EFGH" + cmd = f"sethw {value}\n" + cond1.ser.reset_input_buffer() + cond1.ser.write(cmd.encode("ascii")) + print("Sent command cmd_set_hardware_ver") + + # minimal wait until some data arrives + while cond1.ser.in_waiting == 0: + pass + + data = cond1.ser.readline() print(data) ######################################################################################################################## -# {"getcal_bin", cmd_getSerial_bin} -def cmd_getSerial_bin(cond1): - cmd =b' n\n' - cond1.ser.write(cmd) - print("Sent command cmd_getSerial_bin") - data = cond1.ser.read(4) +# This function updates the serial number in the flash. +# {"setsn", cmd_set_serial_num}, +def cmd_set_serial_num(cond1): + value = "IJKL" + cmd = f"setsn {value}\n" + cond1.ser.reset_input_buffer() + cond1.ser.write(cmd.encode("ascii")) + print("Sent command cmd_set_serial_num") + + # minimal wait until some data arrives + while cond1.ser.in_waiting == 0: + pass + + data = cond1.ser.readline() print(data) - print(struct.unpack('