使用HackRF传输TXT文本文件
本文的内容部分参考了别的博主
链接: 【Gnuradio结合hackrf 通过FSK调制实现文本文件的发送与接收】
一、 实验目的
通过HackRF设备实现对于电脑中txt文件的收发。
二、 实验设备
带Ubuntu 20.4系统的电脑一台,HackRF两台
三、 实验过程
- 首先,使用编码软件,对要发送的文本文档send.txt进行二进制编码(运行data_send.py文件),得到code.txt
- 之后搭建发射端的gnuradio文件,将code.txt二进制文件传输出去
- 在接收端搭建宽带调频解调模块,还原二进制信号
- 在电脑上运行demod.py文件解码二进制流文件receive.txt为res.txt
四、 实验结果
- 接收端流程图:
- 发射端流程图像:
-
发射端图像(右图)和接收端图像
-
最终还原的文本:
There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.þ³³ There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.þ³³ There are moments in life when you miss someone so much that you just want to pick them f
可以看到黄色部分为发送的原文,100%的正确传输到了接收端并成功解码。
*发射前的转二进制编码程序data_send.py:
def intTo2Str( X , K ):
try:
X= long(X)
except:
X=0
try:
K=int(K)
except:
K=0
if K<1 :
K =1
if X<0 :
FH =1 ; X=-X
else:
FH= 0
A =[0 for J in xrange(0, K) ]
J=K -1
while(J>=0) and (X>0):
Y = X%2
X=X/2
A[J]=Y
J=J-1
if FH==1:
for J in xrange(0,K):
if A[J] ==1:
A[J]=0
else:
A[J]=1
J=K-1
while J>=0:
A[J]=A[J]+1
if A[J]<=1:
break;
A[J]=0
J=J-1
return "".join([chr(J+48) for J in A])
f = open(r'/home/tiger/code.txt','wb')
f1 = open(r'/home/tiger/send.txt')
send =f1.readlines()
print send
send = send[0][0:-1]
print str(send)
f1.close()
length=len(send)
serial =[1,0,1,1,0,1,0,0]
serial=serial*2
for i in serial:
f.write(chr(i))
str1 =intTo2Str(length,8)
for i in range(8):
temp = str1[i]
temp = int(temp)
f.write(chr(temp))
for i in range(length):
str1 = intTo2Str(ord(send[i]),8)
for i in range(8):
temp = str1[i]
temp = int(temp)
f.write(chr(temp))
serial =[1,1,1,1,1,1,1,1]
for i in serial:
f.write(chr(i))
f.close()
*接收端收到recieve.txt后对其进行解码的程序demod.py:
def process(str1):
result=''
for i in range(len(str1)):
if(str1[i]=='b'or str1[i]=="'" or str1[i]=="x" or str1[i]=="\\"):
pass
else:
result=result+str1[i]
return result
def byte2int(str):
result=''
for i in range(int(len(str)//4)):
if(str[i*4]=='0' and str[i*4+1]=='0' and str[i*4+2]=='0' and str[i*4+3]=='0'):
result=result+'0'
elif((str[i*4]=='0' and str[i*4+1]=='0' and str[i*4+2]=='0' and str[i*4+3]!='0')or (str[i*4]=='0' and str[i*4+1]=='0' and str[i*4+2]!='0' and str[i*4+3]=='0')or (str[i*4]!='0' and str[i*4+1]=='0' and str[i*4+2]=='0' and str[i*4+3]=='0')or (str[i*4]=='0' and str[i*4+1]!='0' and str[i*4+2]=='0' and str[i*4+3]=='0')):
result=result+'1'
elif ((str[i*4]=='f' or str[i*4]=='e') and (str[i*4+1]=='f' or str[i*4+1]=='e') and (str[i*4+2]=='f' or str[i*4+2]=='e') and (str[i*4+3]=='f' or str[i*4+3]=='e')):
result = result + 'f'
return result
def byte2int2(str):
result=''
for i in range(int(len(str)//2)):
if(str[i*2]=='0' and str[i*2+1]=='0'):
result=result+'0'
elif((str[i*2]=='0' and str[i*2+1]=='1')or (str[i*2]=='1' and str[i*2+1]=='0')):
result=result+'1'
elif ((str[i*2]=='f') and (str[i*2+1]=='f' )):
result = result + 'f'
return result
def output(str):
text=''
for i in range(1,len(str)):
if(str[i-1]==str[i]):
pass
else:
if(str[i]=='1'):
text=text+'1'
elif(str[i]=='0'):
pass
elif(str[i]=='f'):
text=text+'0'
return text
def byte2char(str):
r=0
for i in range(len(str)):
if(str[i]=='0'):
r=r*2+0
elif(str[i]=='1'):
r = r * 2 + 1
if(r==0):return ('')
return chr(r-1)
with open("receive.txt","rb") as f:
tmp=''
for i in range(500):
strb = f.read(1024)
if strb == b"":
break
tmp=tmp+process(str(strb))
#print(tmp)
txt=byte2int(tmp)
txt=byte2int2(txt)
print(txt)
r1=output(txt)
print(r1)
start=r1.find('1011010010110100', 0, len(r1))+16
end=r1.find('11111111',start)
result=''
for i in range(start,len(r1),8):
str_tmp=r1[i:i+8]
if(byte2char(str_tmp)=='end'):break
result=result+byte2char(str_tmp)
print(result)
fh = open('res.txt', 'w', encoding='utf-8')
fh.write(result)
fh.close()