From 752e323768d44307279bdbb8947bf1b0787ce015 Mon Sep 17 00:00:00 2001 From: philippd Date: Tue, 14 May 2024 23:03:00 +0200 Subject: [PATCH] added example --- serial_test.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 serial_test.py diff --git a/serial_test.py b/serial_test.py new file mode 100644 index 0000000..c39e692 --- /dev/null +++ b/serial_test.py @@ -0,0 +1,59 @@ +import serial +import time + +# Replace '/dev/ttyUSB1' with your serial port name +ser = serial.Serial('/dev/ttyUSB1', 115200) +time.sleep(2) # Wait for the connection to be established (I don't know why this is necessary, but it won't work without it) + +def send_command(command): + print(f"Sending command: {command}") + ser.write((command + '\n').encode()) + check = 0 + while check == 0: + if ser.in_waiting: + check = 1 + response = ser.readline().decode().strip() + print(f"Received response: {response}") + return response + +def main(): + # Ensure the serial connection is open + if ser.is_open: + print("Serial port is open") + + # Initialize the device + response = send_command('STARTUP') + + # Set parameters + response = send_command('SET_PARAMETER PRESSURE_OFFSET 10') + + response = send_command('SET_PARAMETER PRESSURE_SAMPLING SAMPLING_X4') + + # Get parameters + response = send_command('GET_PARAMETER PRESSURE_OFFSET') + + response = send_command('GET_PARAMETER PRESSURE_SAMPLING') + + # Read sensor data + response = "ERROR" + while response == "ERROR": + response = send_command('GET_SENSOR REQUEST PRESSURE') + + response = "FALSE" + while response == "FALSE": + response = send_command('GET_SENSOR CONFIRM PRESSURE') + + response = "FALSE" + while response == "FALSE": + response = send_command('GET_SENSOR CHECK PRESSURE') + + response = send_command('GET_SENSOR SEND PRESSURE') # binary data will need to be decoded to float + + # Reset sensors (not useful after calling the SEND command) + response = send_command('RESET_SENSORS') + + else: + print("Failed to open serial port") + +if __name__ == "__main__": + main()