Creating a Python module that can be imported into other Python modules [closed]

1 day ago 2
ARTICLE AD BOX

I'm trying to create a Python module that reads six channels of a FlySky rc transmitter and configure that script it can be used in other modules. My attempts have yielded the following 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')

I then created a simple script to test the 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)

When I run the test script I get errors.

Read Entire Article