我想在我的緩衝區中存儲超過1024個字節。其實我試圖從python
腳本中獲得價值。從我python
腳本緩衝區java阻塞到1024?
33值的工作原理:
33*15(digits)*2(2 types of values)=990 bytes
。
如果我嘗試發送超過33個值,則不起作用。我的程序說收到了29個字節。
我覺得問題是從緩衝區。我試圖修復在65536緩衝區,但它沒有改變。它是否存在任何解決方案像我的緩衝區一樣儲存更多數據?
Python代碼:
from pylab import*
from rtlsdr import*
from bluetooth import*
import sys
#configure device
sdr= RtlSdr()
sdr.sample_rate=double(sys.argv[3])
sdr.gain=double(sys.argv[2])
sdr.center_freq=double(sys.argv[1])
NFFT=34
#Bluetooth connection
server_sock=BluetoothSocket(RFCOMM)
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port=server_sock.getsockname()[1]
uuid="94f39d29-7d6d-437d-973b-fba39e49d4ee"
client_sock,client_info=server_sock.accept()
while(1):
samples=sdr.read_samples(256*1024)
result=psd(samples,NFFT, Fs=sdr.sample_rate/1e6, Fc=sdr.center_freq*1e6/1e6)
tab_freq=(result[1]/1e6)
value_freq=str(tab_freq)[1:-1]
value_list=[format(float(v), ".10f") for v in value_freq.split()]
value_freq2= "\n".join(value_list)
tab_pxx=result[0]
value_pxx=str(tab_pxx)[1:-1]
client_sock.send(value_freq2+'\n'+'\n'.join(value_pxx.split()))
java代碼:
public void run() {
byte[] buffer = new byte[1048576]; // 20 bits
int bytes;
String strRx = "";
while (running) {
try {
bytes = connectedInputStream.read(buffer);
final String strReceived_freq = new String(buffer,0, bytes);
final String strReceived_pxx = new String(buffer,(bytes/2)+1, bytes);
//final int samples_sdr=new Integer(buffer,0,bytes);
final String strByteCnt = String.valueOf(bytes) + " bytes received.\n";
runOnUiThread(new Runnable(){
@Override
public void run() {
Pxx_value.setText(strReceived_pxx+"\n"); // get data PXX
freq_value.setText(strReceived_freq+"\n"); // get data freq
// plot value
/* for (int i=0; i<nb_points; i++)
{
freq[i]=Double.parseDouble(strReceived_freq);
pxx[i]=Double.parseDouble(strReceived_pxx);
series.appendData(new DataPoint(freq[i],pxx[i]), true,500);
}*/
}});
感謝您的幫助。
connectedInputStream連接到了什麼? 'new String(buffer,(bytes/2)+1,bytes)'中計算的意義究竟是什麼? – EJP
使用connectedInputStream,我可以通過藍牙從Raspberry PI 3獲取數據。我只需編輯我的問題即可添加我的Python腳本。 'new String(buffer,0,bytes/2)'得到所有freq值和'new String(buffer,(bytes/2)+ 1,bytes)得到pxx值(dB/Hz)' – azzerty2017
你不能得到來自輸入流的內容比可讀取的內容更多。你可以嘗試添加一個'BufferedInputStream'。但是如果你確實需要一次2048字節的話,你需要'DataInputStream.readFully()'。 – EJP