FreeDATA/test/TEST_TX.py

117 lines
3.9 KiB
Python
Raw Normal View History

2020-12-14 11:44:13 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 13 13:45:58 2020
2020-12-15 12:42:46 +00:00
@author: DJ2LS
2020-12-14 11:44:13 +00:00
"""
import ctypes
from ctypes import *
import pathlib
import binascii #required for string.to_bytes() function
2020-12-15 12:43:43 +00:00
#import crcengine # required for CRC checksum
2020-12-14 11:44:13 +00:00
import sys
#sys.stdout.reconfigure(encoding='utf-8')
def main():
modem = FreeDV() #Load FreeDV Class as "modem"
2020-12-15 12:42:46 +00:00
2020-12-14 11:44:13 +00:00
data = b'TEST' #byte string which will be send
#data = bytes(62) #byte string with XX of zeros
modulated_data = modem.Modulate(data) #Call Modulate function, which returns modulated data
#print(modulated_data)
###################### try: / except necessary beacuse of error 32 - BrokenPipeError while piping data
2020-12-15 12:42:46 +00:00
##try:
2020-12-14 11:44:13 +00:00
#print(bytes(8)) #additional zero bytes before modulated data
#sys.stdout.buffer.write(modulated_data) # the normal way of piping data
#sys.stdout.flush() # flushing data
2020-12-15 12:42:46 +00:00
#print(modulated_data, flush=True) #print modulated data with print function
2020-12-14 11:44:13 +00:00
2020-12-15 12:42:46 +00:00
##except (BrokenPipeError, IOError):
##print ('TEST BrokenPipeError caught', file = sys.stderr)
##sys.stderr.close()
2020-12-14 11:44:13 +00:00
class FreeDV():
def __init__(self):
libname = pathlib.Path().absolute() / "libcodec2.so"
self.c_lib = ctypes.CDLL(libname) # Load FreeDV shared libary
self.freedv = self.c_lib.freedv_open(12) #Set FreeDV waveform ( 10,11,12 --> DATA1-3 )
self.bytes_per_frame = int(self.c_lib.freedv_get_bits_per_modem_frame(self.freedv)/8) #get bytes per frame from selected waveform
self.payload_per_frame = self.bytes_per_frame -2 #get frame payload because of 2byte CRC16 checksum
2020-12-15 12:42:46 +00:00
#print(self.c_lib.freedv_get_n_nom_modem_samples(self.freedv))
2020-12-14 11:44:13 +00:00
# MODULATION-OUT OBJECT
def ModulationOut(self):
#return (c_short * self.c_lib.freedv_get_n_nom_modem_samples(self.freedv)) ## all other modes
2020-12-15 12:42:46 +00:00
#return (c_ubyte * self.c_lib.freedv_get_n_nom_modem_samples(self.freedv)) ## DATA modes ## Fails with n_nom_modem_samples(880) for some reason
return (c_ubyte * 1024) ## DATA modes another test
2020-12-14 11:44:13 +00:00
# Pointer for changing buffer data type
def FrameBytes(self):
return (ctypes.c_ubyte * self.bytes_per_frame)
# Modulate function which returns modulated data
def Modulate(self,data_out):
## #self.freedv_rawdatapreambletx(self.freedv, mod_out) # SEND PREAMBLE
##########################################
mod_out = self.ModulationOut()() # new modulation object and get pointer to it
#buffer = bytearray(self.bytes_per_frame) # use this if no CRC16 checksum is required
2020-12-15 12:42:46 +00:00
buffer = bytearray(self.payload_per_frame) # use this if CRC16 checksum is required ( DATA1-3)
2020-12-14 11:44:13 +00:00
buffer[:len(data_out)] = data_out # set buffersize to length of data which will be send
#buffer = self.scramble(buffer, packet_num)
2020-12-15 12:42:46 +00:00
##crc_algorithm = crcengine.new('crc16-ccitt-false') #load crc16 library
##crc = crc_algorithm(buffer) # get new crc16 from buffer
2020-12-14 11:44:13 +00:00
#print(hex(crc))
2020-12-15 12:42:46 +00:00
crc = c_ushort(self.c_lib.freedv_gen_crc16(bytes(buffer), self.payload_per_frame)) # generate CRC16
crc = crc.value.to_bytes(2, byteorder='big') # convert buffer to 2 byte hex string
2020-12-14 11:44:13 +00:00
buffer += crc # append crc16 to buffer
#print(buffer)
data = self.FrameBytes().from_buffer_copy(buffer) #change data format form bytearray to ctypes.u_byte
#print(len(data))
2020-12-15 12:42:46 +00:00
##return bytes(mod_out) #return modulated data as byte string
#self.c_lib.freedv_rawdatapreambletx(self.freedv, mod_out) # SEND PREAMBLE
#print(bytes(mod_out), flush=True)
self.c_lib.freedv_rawdatatx(self.freedv,mod_out,data) # modulate DATA
print(bytes(mod_out), flush=True)
2020-12-14 11:44:13 +00:00
2020-12-15 11:06:49 +00:00
main()