46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
import serial
|
|
import struct
|
|
from google.protobuf.message import DecodeError
|
|
from serial.tools import list_ports
|
|
|
|
from cls_device_pb2 import RequestDeviceList, Device, ResponseDeviceList
|
|
from usb_pb2 import UsbPackageType
|
|
from vcp_driver import *
|
|
|
|
if __name__ == "__main__":
|
|
ser = setup_connection()
|
|
# Create a RequestDeviceList message
|
|
request = RequestDeviceList()
|
|
request.msg = 1 # or whatever value you want to set
|
|
|
|
# Serialize the request to a bytearray
|
|
request_data = request.SerializeToString()
|
|
|
|
# Send the request
|
|
send_package(UsbPackageType.REQUEST_DEVICE_LIST, request_data, ser)
|
|
|
|
# Now wait for the response
|
|
while True:
|
|
# 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)
|
|
|
|
# Check if the type is RESPONSE_DEVICE_LIST
|
|
if typeid == UsbPackageType.RESPONSE_DEVICE_LIST:
|
|
# Read the response data from the serial port
|
|
response_data = ser.read(length)
|
|
|
|
# Try to parse the data as a ResponseDeviceList message
|
|
try:
|
|
response = ResponseDeviceList.FromString(response_data)
|
|
# If we get here, we successfully parsed the response. Break the loop.
|
|
break
|
|
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
|
|
|
|
# At this point, 'response' contains the ResponseDeviceList message
|
|
print(response) |