60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
|
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()
|