2017년 7월 8일 토요일

Reading data from bluetooth and timing


I'm currently writing and app that connects to an Arduino via bluetooth, and displays sensor readings from the Arduino. The Arduino polls the sensor and transmits the data (0.00 to 5.00 volts) via bluetooth every 100ms.

However, on the Android app, I have some issues with the display. I have a Clock set to 'tick' every 100ms.

When clock1.timer
 -> If (call BluetoothClient1.BytesAvailableToReceive > 0)
    -> then set global Received_Message to (call BluetoothClient1.ReceiveText, numberOfBytes = call BluetoothClient1.BytesAvailableToReceive).
    -> set StatusLabel.text to get global Received_Message

If that makes sense lol.

But, when the App displays the data, I sometimes don't get all of it ex: 1.43 gets displayed as 1, then as .43 until the next refresh it catches the whole 1.43

If I set my Clock too fast, it gets worse. If I set it too slow, I get 1.43.1.43.1. in the message.

Any help appreciated.

--
On the Arduino side, use println() to send your text.

That should end each reading with a \n (newline) character.

On the AI2 side, there is a setting in the Bluetooth 
Receive Text blocks to specify and end of message character,
with \n as the default (I believe.)

Check the docs on the Bluetooth client for more details.

--
Correction - you will need to set your delimiter byte to \n
in the Designer.
(New line, a.k.a. Line Feed, see an Ascii table.)

--
I had a similar issue. I solved my problem by putting the Arduino data in start and end tags. I use < and > but other characters can be used. My data is sent in ascii.

In the app I check to see if BT data is available then add what ever is available to a buffer / variable. I then check the buffer for the tags. If both are present I split the data on the closing tag and leave any extra data in the buffer.

You would then have something like:

Receive <1.43> - add to buffer. Data is complete. Process the data. Clear the <1.43> from the buffer.
Receive <1 - add to buffer. No closing tag, do nothing
Receive .43> - add to buffer. Buffer now = <1.43>. Have complete data so process. Clear the <1.43> from the buffer.
Receive <1.4 - add to buffer. No closing tag, do nothing
Receive 3><1. - add to buffer. Complete data + incomplete data. Process the complete data and remove it from the buffer. The buffer still contains <1.
Receive 43> - add to the buffer. The buffer now contains <1.43>. Process the complete data and remove it from the buffer

I am sure there are many other ways to do this though.

--
This sounds like what I was thinking :)

So it would look something like this:

if (contains text get global received_message), piece "<"
  -> then if (contains text get global received_message), piece ">"
      -> then set global complete_message to split text (get global received_message) at "<"
           -> set global complete_message to split text (select list item list (get global complete_message), 2) at ">"


I think?

--

댓글 없음:

댓글 쓰기