Код: Выделить всё
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)
AttributeError: объект «функция» не имеет атрибута «ch1»
Если я заменю вторую строку тестового сценария просто «импортировать FlySky» и запущу его, я получаю следующую ошибку:
NameError: имя «transmit» не определено
Надеюсь, эта информация будет полезна.
Спасибо, Брайан
Подробнее здесь: https://stackoverflow.com/questions/798 ... on-modules
Мобильная версия