I am working on a project to control a robot using a mobile application. So far, I have been able to send Bluetooth commands to make the robot move as needed. But I was having trouble getting input from the HC-SR04 ultrasonic sensor, which gives the float shaft every second. I wrote code to convert float to bytes, then writes bytes to serial and reads and displays them in textview using java for the application. But I get only a question mark in the textual representation, which, as I suppose, indicates that I can receive data on the channel, or bytes are not filled, or my conversions are incorrect?
Please, help.
########## here is my python script for sending byteswhile True:
a = ser.read()
ser.close()
dist = initio.getDistance()
d = int(a.encode('hex'), 16)
bytSend = struct.pack('f', dist)
ser.open()
ser.write(bytSend)
********** here is the java code for reading data by stream and sending it to the handler in my main code ***************
public void run() {
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
bytes = mmInStream.read(buffer);
mHandler.obtainMessage(initioActivity.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
connectionLost();
break;
}
}
}
*****, then in my main code (inside the handler) I have ***
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
String distance = new String(readBuf, 0, msg.arg1);
myLabel.setText(distance);
Any ideas what I can do wrong?
source
share