46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
import serial
|
|
import struct
|
|
from google.protobuf.message import DecodeError
|
|
from serial.tools import list_ports
|
|
|
|
from headlight_pb2 import Settings, RequestSettings
|
|
from usb_pb2 import PackageType
|
|
from vcp_driver import *
|
|
|
|
if __name__ == "__main__":
|
|
ser = setup_connection()
|
|
|
|
request = RequestSettings()
|
|
request.deviceId = 1
|
|
print("send request")
|
|
print(request)
|
|
|
|
request_data = request.SerializeToString()
|
|
send_package(PackageType.HEADLIGHT_REQUEST_SETTINGS, request_data, ser)
|
|
|
|
|
|
print("wait for response")
|
|
# Read the header from the serial port
|
|
response_header = ser.read(5) # assuming the header is 5 bytes long
|
|
|
|
# Unpack the header to get the length and type
|
|
length, typeid, check = struct.unpack('<HHB', response_header)
|
|
|
|
print(length, typeid, check)
|
|
|
|
# Check if the type is RESPONSE_DEVICE_LIST
|
|
if typeid == PackageType.HEADLIGHT_SETTINGS:
|
|
# Read the response data from the serial port
|
|
response_data = ser.read(length)
|
|
|
|
# Try to parse the data as a ResponseDeviceList message
|
|
try:
|
|
response = Settings.FromString(response_data)
|
|
|
|
except DecodeError:
|
|
# If we get a DecodeError, it means the data we read is not a valid
|
|
# ResponseDeviceList message. Ignore it and continue reading.
|
|
pass
|
|
|
|
|
|
print(response) |