Anonymous
Создание модуля Python, который можно импортировать в другие модули Python.
Сообщение
Anonymous » 11 фев 2026, 02:37
Я пытаюсь создать модуль Python, который считывает шесть каналов радиоуправляемого передатчика FlySky, и настроить этот сценарий, чтобы его можно было использовать в других модулях. Мои попытки привели к следующему коду:
Код: Выделить всё
import serial
import os
fly = serial.Serial("/dev/serial0", 115200)
fly.parity = serial.PARITY_NONE
fly.stopbits = serial.STOPBITS_ONE
def transmit(ch1, ch2, ch3, ch4, ch5, ch6):
while True:
frame = bytearray()
received_data = None
received_data = fly.read() # read serial port
intReceived = int.from_bytes(received_data, byteorder='little')
if intReceived == 32:
frame.extend(received_data) # add the header
# read the next 31 bytes of the frame (to make a 32 byte frame size)
nextBytes = fly.read(31)
# add the readed 31 bytes to the frame bytearray
frame.extend(nextBytes)
ch1byte = bytearray()
ch1byte.append(frame[2])
ch1byte.append(frame[3])
ch1 = int.from_bytes(ch1byte, byteorder='little')
ch2byte = bytearray()
ch2byte.append(frame[4])
ch2byte.append(frame[5])
ch2 = int.from_bytes(ch2byte, byteorder='little')
ch3byte = bytearray()
ch3byte.append(frame[6])
ch3byte.append(frame[7])
ch3 = int.from_bytes(ch3byte, byteorder='little')
ch4byte = bytearray()
ch4byte.append(frame[8])
ch4byte.append(frame[9])
ch4 = int.from_bytes(ch4byte, byteorder='little')
ch5byte = bytearray()
ch5byte.append(frame[10])
ch5byte.append(frame[11])
ch5 = int.from_bytes(ch5byte, byteorder='little')
ch6byte = bytearray()
ch6byte.append(frame[12])
ch6byte.append(frame[13])
ch6 = int.from_bytes(ch6byte, byteorder='little')
Затем я создал простой скрипт для проверки кода:
Код: Выделить всё
import time
from FlySky import transmit
while True:
print("ch1=", transmit.ch1, "ch2=", transmit.ch2, "ch3=", transmit.ch3, "ch4=", transmit.ch4, "ch5=", transmit.ch5, "ch6=", transmit.ch6)
Когда я запускаю тестовый скрипт, я получаю ошибки.
Подробнее здесь:
https://stackoverflow.com/questions/798 ... on-modules
1770766642
Anonymous
Я пытаюсь создать модуль Python, который считывает шесть каналов радиоуправляемого передатчика FlySky, и настроить этот сценарий, чтобы его можно было использовать в других модулях. Мои попытки привели к следующему коду: [code]import serial import os fly = serial.Serial("/dev/serial0", 115200) fly.parity = serial.PARITY_NONE fly.stopbits = serial.STOPBITS_ONE def transmit(ch1, ch2, ch3, ch4, ch5, ch6): while True: frame = bytearray() received_data = None received_data = fly.read() # read serial port intReceived = int.from_bytes(received_data, byteorder='little') if intReceived == 32: frame.extend(received_data) # add the header # read the next 31 bytes of the frame (to make a 32 byte frame size) nextBytes = fly.read(31) # add the readed 31 bytes to the frame bytearray frame.extend(nextBytes) ch1byte = bytearray() ch1byte.append(frame[2]) ch1byte.append(frame[3]) ch1 = int.from_bytes(ch1byte, byteorder='little') ch2byte = bytearray() ch2byte.append(frame[4]) ch2byte.append(frame[5]) ch2 = int.from_bytes(ch2byte, byteorder='little') ch3byte = bytearray() ch3byte.append(frame[6]) ch3byte.append(frame[7]) ch3 = int.from_bytes(ch3byte, byteorder='little') ch4byte = bytearray() ch4byte.append(frame[8]) ch4byte.append(frame[9]) ch4 = int.from_bytes(ch4byte, byteorder='little') ch5byte = bytearray() ch5byte.append(frame[10]) ch5byte.append(frame[11]) ch5 = int.from_bytes(ch5byte, byteorder='little') ch6byte = bytearray() ch6byte.append(frame[12]) ch6byte.append(frame[13]) ch6 = int.from_bytes(ch6byte, byteorder='little') [/code] Затем я создал простой скрипт для проверки кода: [code]import time from FlySky import transmit while True: print("ch1=", transmit.ch1, "ch2=", transmit.ch2, "ch3=", transmit.ch3, "ch4=", transmit.ch4, "ch5=", transmit.ch5, "ch6=", transmit.ch6) [/code] Когда я запускаю тестовый скрипт, я получаю ошибки. Подробнее здесь: [url]https://stackoverflow.com/questions/79887034/creating-a-python-module-that-can-be-imported-into-other-python-modules[/url]