Telemetry Forwarding with Mavproxy

This is what I have so far for my script for my step 2. I was planning on running this script as a background process. Although, I wasn’t sure how to handle the flags and new fields in mavlink2 frame.

#This is a generic script for logging sensor data from a sensor connected to a raspberry pi.
#To run this as a background process on the CLI, type 'python3 file_write_TSM.py &'
#type 'kill -9' followed by PID number to terminate process

#The script does the following.
#1)Creates a file for the sensor data onboard the computer companion
#2)Reads the incoming Serial data and stores it in a variable
#3)Creates a mavlink packet to forward to the ardupilot flight controller.

#!/usr/bin/env python
import time
import serial
import datetime
from os.path import exists

###########Variables for converting to mavlink###################
stx='0xFD' #protocol specific start of text
length=100 #payload length 0-255
incF= #incompatibility flag
cmpF= #compatibility flag
sq= 0 #sequence - detects apcket loss
sysID= 1#ID of vehicle sending the message 1-255
cmpID= 0#ID of component sending the message. zero broadcast to ALL
msgID=251#ID of message type used in payload used to decode data back in msg object.251 is a NAMED_VALUE_FLOAT
payload=' '#Stores the sensor data
chksm=
sig=0#13 byte signature. THIS IS OPTIONAL

########Naming and creating file for Sensor Log################
dt =  str(datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S"))
filename = 'SENSOR_SAMPLE' + dt +'.txt'
f = open('PATH/TO/FILE'+filename , 'w')
########Naming and creating file for mavlink converted packets####
filename = 'mavlink_SENSOR_SAMPLE' + dt +'.txt'
f2 = open('PATH/TO/FILE'+filename , 'w')


#Set Up Serial Port for the sensor
#Ensure you are using the correct header name in '/dev' which the component is plugged in to. If the ETHUSB Hub Hat is attached, the ttyUSB number sometimes changes between zero and one.
ser = serial.Serial(
        '/dev/ttyUSB0',
        baudrate = 115200,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1
)

#The program will continue to write into the file until it is terminated.
while 1:
    data = str(ser.read(100))
    #debugs printer
    print(data)
    #writes into the log
    f.write(data)
    f.write('/n')
    #convert the data into mavlink
    sq++ #increment sequence
    if sq = 256:
        sq = 0
    payload = data
    packet= stx + len + seq + sysID + comID + msgID + payload + checksum
    f2.write(packet)
    f2.write('/n')

f.close()
f2.close()