Untitled

mail@pastecode.io avatar
unknown
python
15 days ago
2.1 kB
1
Indexable
Never
import serial
import time

def query_vlf(portname):
    try:
        with serial.Serial(portname, 115200, timeout=1) as ser:
            data_dict = {}
            # Initialize Bluetooth
            ser.write(b'BON')
            time.sleep(0.5)  # Wait for Bluetooth to initialize
            response = ser.readline().decode().strip()
            print(f"Bluetooth initialization response: {response}")
            
            # Proceed with querying data from the sensors
            for x in range(32):
                sensor = f'P{x:02d}'
                ser.write(sensor.encode('utf-8'))
                print(f"Sent command: {sensor}")
                
                time.sleep(0.1)

                if ser.in_waiting > 0:
                    data = ser.readline()
                    data = data.strip().decode("utf-8").replace(';', '')
                    data_dict[sensor] = data
                    print(f"Received data for {sensor}: {data}")
                else:
                    print(f"No data available for {sensor}")
                    data_dict[sensor] = None
            return data_dict
    except serial.SerialException as e:
        print(f"Error: {e}")
        return None

# General query method
def query_board(board_type, board_sn):
    if board_type == "VIZLORE":
        portname = '/dev/serial/by-id/' + board_sn
        data = query_vlf(portname)
    elif board_type == "VIZLORE_BT":
        portname = '/dev/serial/by-id/' + board_sn
        data = query_vlf(portname)
    elif board_type == "BURGE":
        portname = '/dev/serial/by-id/' + board_sn
        data = query_burge(portname)
    else:
        print("Invalid board type.")
        return None
    return data

if __name__ == "__main__":
    board_type = "VIZLORE_BT"  # Change this according to your board type
    board_sn = "B24-3t069"  # Replace with your actual serial port name or ID
    data = query_board(board_type, board_sn)
    if data:
        print("Sensor data:")
        print(data)
    else:
        print("Failed to retrieve sensor data.")
Leave a Comment