Read data from ttyUSB port - android

I am trying to read data from a USB TO Serial Adapter. The way I do it is with a FileReader directed to the ttyUSB0 file. The problem is that every time i recieve a byte/char on the android tablet, the android tablet reads the byte/char and then sends the same byte/char back to the Adapter.
The other issue that I am facing, is that until i send the byte "4" on the line, the FileReader refuses to close the connection and to stop recieving data(even though it has already filled up it's buffer)
Any help on this matter will be greatly appreciated.

Related

Bluetooth Packet Payload Missing in WIreshark

I am trying to get at the Bluetooth data needed to control my Bluetooth lamp. I have created the bt_snoop file after successfully controlling my lamp through the app. I've done this many times and I always get the same result.
When I open bt_snoop in Wireshark and filter for my device all of the data has a warning: "[[Expert Info (Warning/Malformed): Length too short]". It also says "Payload: MISSING". I am able to control the light through the app, but the bt_snoop log just shows that error. I'm really lost and would really appreciate help!

Why am I seeing unexpected data at the beginning of the Android app when sending Arduino data via Bluetooth?

I am working on a group project where we are sending serial data over Bluetooth from Arduino to Android. We are all fairly new at both Arduino and Android.
Hardware used include Arduino Uno R3 and HC-05 bluetooth module.
I am sending dummy data for a 3 axis accelerometer packet and successfully read the packet data from Android.
However, we have this blob of data (about 50+ bytes usually and has ranged up to 512 bytes) that always gets sent to the app in the beginning. Its a randomly sized chunk of bytes, which we can't interpret because it doesn't seem to match the packet format we set up for our data. We managed to avoid looking at this byte chunk by checking to see if the packet size is small enough. But this adds a lot of overhead (4 - 5 seconds), so we'd like to figure out what this blob of data is. So, does the HC-05 send some proprietary Bluetooth related data first or is there some thing wrong with my script that's causing the unexpected data to be sent?
This is the Arduino code.
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(10,11);
void setup(){
bluetooth.begin(9600);
}
void loop() {
int x = random(360);
int y = random(360);
int z = random(360);
formAccelerometerPacket(x, y, z);
delay(5000); // wait 5 sec
}
void formAccelerometerPacket(int xVal, int yVal, int zVal) {
printSensorVal('A', xVal);
printSensorVal(':', yVal);
printSensorVal(':', zVal);
}
void printSensorVal(char flag, int sensorVal) {
bluetooth.print(flag);
bluetooth.print(sensorVal);
}
I've looked at it with a Bluetooth terminal app but nothing looks wrong from there. Its LogCat from the app that shows this content received from the app, but I can't interpret it as I said earlier, which is what I need to solve.
I've tried to look at other SO questions but none others could help me.
I don't have the code for the Android app as it is with another teammate, but I know that they followed the BluetoothChat example closely.
The only thought I had was that since Arduino loops the data, if the app starts after the Arduino starts, it might start reading some data part way from what was going on in the serial port before. But it doesn't explain the size difference in the blob of bytes.
Edit on 08/21/2014 at 10:33AM PST
Here is a screenshot of the LogCat. What we did was ran the Android app first and then I started the Arduino to make sure the board didn't have old data. Looking at this makes me think it might be a pairing issue as some one suggested. I am working on trying that fix.
Try Bluetooth SPP on Google Play, then connect to the HC-05. Check the output and then once you get clean data reset the arduino and see what happens. That's how I usually go about checking the output from my HC-05. And no there is nothing sent by the HC-05 when it starts up. I couldn't comment so had to post an answer, sorry.
I am not sure that is your case but may be it's usefull. When you send a data from HC-05(FC-114) to a Slave(HC-06) the first byte(or the first three/four) is sent immediatly and the rest with a delay of 5/10ms. I don't know why, but i see it using oscilloscope. If well managed, you can fix the problem when receive the packet of byte waiting for a while, otherwise, you can get crazy for understand what is happening.

UsbConnection.bulkTransfer returns "-1". What does it mean?

According to the doc negative response means failure.
How to know what's wrong? is there any full doc about error types?
For more curious:
I'm trying to upload arduino sketch via USB. First i'm getting bootloader attention by turning DTR/RTS off/on and then sending data using Stk500 protocol. It's working fine if running on mac/arduino via usb (rxtx serial is used) so i think it should work on android AS-IS (just change serial), but it fails to send the data after DTR/RTS toggling.
Well, after diving into log i was able to handle it - don't close/open usb few times in a row
From the documentation for bulkTransfer():
Returns
length of data transferred (or zero) for success, or negative value for failure
So if you're getting -1, then it means your data transfer has failed. You could look into the logcat, or work with breakpoints to figure out why.

android-serialport-api reading from serial port

I am trying to read data from a serial port in the below given opencv
link, I have no problem with
sending bytes. Everything works fine. There is a problem when i
receive bytes. Every time a read a byte from ttymxc0 something happens
and the byte is not only read for the first time it is sending some
junk data but after pressing the the two enter keys it is able to
read the data correctly but my concern to read the data without
pressing any enter keys in the beginning only.
http://code.google.com/p/android-serialport-api/source/checkout
So could any body provide sme corrected code to get rid of this
annoying serial port read problem,
Am new to this android applications development,
"Android-SerialPort" application works straight forward without any modification, I am not sure what you mean by two key presses required for receiving data. Can you paste code snippet, it is hard to reply to such questions.

Reading Bluetooth input stream in android

I am developing an application that read data from biometric devices using Bluetooth when I send request to biometric device for sending data, biometric device show response with updating its display screen but when I call function for read input stream for getting response the function in_stream.available() return 0. I am not able to trace out the root of problem. I have test same biometric device with some other app it work fine.
Help me if any one have idea about this issue.
Thanks in advance.
Do no use available() method. It is broken in most implementations. You should be constantly reading with read() or read(byte []). If the protocol lets you know the size of the expected data (i.e. some first bytes telling how much data is coming afterwards) you can just read that amount of data.
If the amount of data is unknown or you expect asynchronous data comming then you should manage the writing/reading to/from the streams in a separate thread. This does not only applies to Bluetooth but also to any basic stream handling (network, files, etc.)

Categories

Resources