![](/img/trans.png)
[英]Sending a file from android application to raspberry pi using SSH
[英]sending bytes from Raspberry Pi (using python) to a java android application over bluetooth
我正在開發一個使用移動應用程序控制機器人的項目。 到目前為止,我已設法發送藍牙命令,使機器人按需要移動。 但是我無法接收來自超聲波傳感器HC-SR04的輸入,它每秒都會產生一個浮動谷。 我已編寫代碼將float轉換為字節然后將字節寫入串行並讀取並使用java為應用程序在textview上顯示它們。 但我只在textview上得到一個問號,我假設顯示我能夠在通道上接收數據,或者字節不完整或我的轉換是錯誤的?
請幫忙。
##########這是我發送字節的python腳本public void run() {
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(initioActivity.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
//
connectionLost();
break;
}
}
}
**********這里是java代碼,用於讀取流上的數據並發送到我的主代碼上的處理程序***************
case MESSAGE_READ:
//byte[] readBuf = (byte[]) msg.obj;
byte[] readBuf = (byte[]) msg.obj;
//construct a string from valid bytes in buffer
String distance = new String(readBuf, 0, msg.arg1);
myLabel.setText(distance);
*****然后在我的主要代碼(在處理程序內)我有***
case MESSAGE_READ: //byte[] readBuf = (byte[]) msg.obj; byte[] readBuf = (byte[]) msg.obj; //construct a string from valid bytes in buffer String distance = new String(readBuf, 0, msg.arg1); myLabel.setText(distance);
我有什么想法可能做錯了嗎?
假設距離為d
d = 23.45454
編碼后的消息是
encoded_d = struct.pack("f",d)
print repr(encoded_d) # '\xe6\xa2\xbbA'
所以,當你在java中讀取字符串時,你得到了...因為那些不是有效的字符?
對於任何無效的角色
你需要弄清楚如何將它decode
回java中的浮點數
要解決這個問題,最簡單的方法就是將其作為ascii發送
d = 23.45454
ser.write(str(d))
這將使您的消息傳遞速度略慢但除非它實際上是一個問題,它應該足夠了
這篇文章中的所有代碼都是python
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.