Add files via upload

This commit is contained in:
DJ2LS 2020-12-15 13:42:46 +01:00 committed by GitHub
parent 3002b3bb89
commit ec0db5f121
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,22 +3,15 @@
"""
Created on Sun Dec 13 13:45:58 2020
@author: parallels
@author: DJ2LS
"""
import ctypes
from ctypes import *
import pathlib
import binascii #required for string.to_bytes() function
#import codecs
import crcengine # required for CRC checksum
#import struct
import sys
#sys.stdout.reconfigure(encoding='utf-8')
@ -29,8 +22,7 @@ def main():
modem = FreeDV() #Load FreeDV Class as "modem"
data = b'TEST' #byte string which will be send
#data = bytes(62) #byte string with XX of zeros
@ -39,17 +31,17 @@ def main():
###################### try: / except necessary beacuse of error 32 - BrokenPipeError while piping data
try:
##try:
#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
print(bytes(32) + modulated_data, flush=True) #print modulated data with print function
#print(modulated_data, flush=True) #print modulated data with print function
except (BrokenPipeError, IOError):
print ('TEST BrokenPipeError caught', file = sys.stderr)
#sys.stderr.close()
##except (BrokenPipeError, IOError):
##print ('TEST BrokenPipeError caught', file = sys.stderr)
##sys.stderr.close()
@ -65,33 +57,14 @@ class FreeDV():
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
###################### CHECKSUM COMPARISON FREEDV VS CRCENGINE ########
#https://crccalc.com
teststring = b'123456789'
# freedv crc16 checksum
crctest2 = c_ushort(self.c_lib.freedv_gen_crc16(teststring, len(teststring)))
print("FREEDV2: " + str(crctest2.value) + " = " + hex(crctest2.value)) #7450
#Python crc16 checksum
crc_algorithm = crcengine.new('crc16-ccitt-false') #load crc16 library
crctest3 = crc_algorithm(teststring)
print("CRCENGINE: " + str(crctest3) + " = " + hex(crctest3)) #8134
#######################################################################
#print(self.c_lib.freedv_get_n_nom_modem_samples(self.freedv))
# MODULATION-OUT OBJECT
def ModulationOut(self):
#return (c_short * self.c_lib.freedv_get_n_nom_modem_samples(self.freedv)) ## all other modes
return (c_ubyte * self.c_lib.freedv_get_n_nom_modem_samples(self.freedv)) ## DATA modes
#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
# Pointer for changing buffer data type
def FrameBytes(self):
return (ctypes.c_ubyte * self.bytes_per_frame)
@ -99,7 +72,6 @@ class FreeDV():
# Modulate function which returns modulated data
def Modulate(self,data_out):
## mod_out = self.ModulationOut()() # new modulation object and get pointer to it
## #self.freedv_rawdatapreambletx(self.freedv, mod_out) # SEND PREAMBLE
@ -108,16 +80,18 @@ class FreeDV():
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
buffer = bytearray(self.payload_per_frame) # puse this if CRC16 checksum is required ( DATA1-3)
buffer = bytearray(self.payload_per_frame) # use this if CRC16 checksum is required ( DATA1-3)
buffer[:len(data_out)] = data_out # set buffersize to length of data which will be send
#buffer = self.scramble(buffer, packet_num)
crc_algorithm = crcengine.new('crc16-ccitt-false') #load crc16 library
crc = crc_algorithm(buffer) # get new crc16 from buffer
##crc_algorithm = crcengine.new('crc16-ccitt-false') #load crc16 library
##crc = crc_algorithm(buffer) # get new crc16 from buffer
#print(hex(crc))
crc = crc.to_bytes(2, byteorder='big') # convert buffer to byte string
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
buffer += crc # append crc16 to buffer
#print(buffer)
@ -126,9 +100,15 @@ class FreeDV():
#print(len(data))
self.c_lib.freedv_rawdatatx(self.freedv,mod_out,data) # SEND DATA
return bytes(mod_out) #return modulated data as byte string
##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)