Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dropped or swapped characters #9

Closed
forkandspoon opened this issue Jul 24, 2013 · 18 comments
Closed

Dropped or swapped characters #9

forkandspoon opened this issue Jul 24, 2013 · 18 comments

Comments

@forkandspoon
Copy link

Hello,

I am using Phonegap/this plugin/js for the first time to send Arduino serial data to an app.

Using the SimpleSerial example (bluetoothSerial.subscribe), I am seeing some issues with some lines being corrupted (swapped/removed/added characters in the received string).

Below is received data sent to console.log (viewed on comp w/adb logcat)

D/CordovaLog(16839): Received:[1.74, -13.43, 17.5341.82, 5241.82, 521, 23.88,1, 23.88, ]
D/CordovaLog(16839): Received:[1.54, -14.04, 17.94, 51.76, 42.03, 52.48, 26.75, 24.39, ]
D/CordovaLog(16839): Received:[0.20, -14.56, 17.8 41.82, 52.48, 41.2.48, 41.51, 34.13, ]
D/CordovaLog(16839): Received:[1.54, -142, 51.97,2, 51.97, 41.72, 39.77, 42.44, 34.95, ]
D/CordovaLog(16839): Received:[0.20, -15.07, 16.91, 51.56, 413, 42.5.7 34.8544, 34.85, ]
D/CordovaLog(16839): Received:[1.33, -14.04, 17.12, 51.76, 41.72, 344, 34.34, ] 34.34, ]
D/CordovaLog(16839): Received:[4.04, 174.04, 17.94, 51.86, 41.61, 52.48, 42., ] 34.24, ]
D/CordovaLog(16839): Received:[1.13, -14.35, 17.12, 51.86, 40.69, 533, 34.8533, 34.85, ]

This issue doesn't exist when using a Bluetooth app downloaded from the Play store.

Thanks

Built with Cordova 2.7.6
Device Samsung Galaxy S2
Android 4.0.3
Baud rate 9600
BT chipset: http://www.elecfreaks.com/store/download/datasheet/Bluetooth/Bluetooth%20HC-06.pdf

@don
Copy link
Owner

don commented Jul 29, 2013

It is possible the problem is that the plugin is treating the Bluetooth data as a String and some of your data is incompatible or being encoded incorrectly. If you have some sample code I could run that might help troubleshoot the issue.

@forkandspoon
Copy link
Author

Hey Don,

We're interfacing with some hardware that will send as csv (or with any seperating character). Currently my Arduino is just sending dummy data:

/*

 V1 Spine Sensing Interface code
 Based on the SoftwareSerialExample example code from arduino.
 Overall Goal: 
 Bluetooth Serial Comms to mobile device
 Simple extensible protocol (CSV based, but with empahsis on human readability)
 Sense and report state or readings from ADC and digi IO if needed   
 Long run time

 */

#include <SoftwareSerial.h>
#define DEBUG 1

// Preconfiger the software serial interface to the bluetooth serial module
SoftwareSerial btSerial(15,14); // RX, TX (pin 10 and 11 is supported in both arduino leonardo and arduino uno and others)

//
int i = 0;

// Send to all serial devices connected (How do i make this work for integers?)
void sendSerial( String str ){
  Serial.print(str);
  btSerial.print(str);

}

void setup(){
  // Normal Serial interface for debug purpose opened.
  Serial.begin(9600);
  // waiting for serial port to connect. Only required for leonardo  
  // Bluetooth Serial Interface activated
  btSerial.begin(9600);

  //Delay to settle serial interface
  delay(1000) ;
  Serial.println("Serial Console Online");  
  btSerial.println("BTSerial Console Online");  
  delay(1000) ;

}

void loop(){

  sendSerial("0.1,-0.1,0.1,-0.1,0.1,-0.1,0.1,-0.1");
  sendSerial("\n");

  delay(100);

  sendSerial("0.15,-0.15,0.15,-0.15,0.15,-0.15,0.15,-0.15");
  sendSerial("\n");

  delay(100);

  sendSerial("0.2,-0.2,0.2,-0.2,0.2,-0.2,0.2,-0.2");
  sendSerial("\n");

  delay(100);

  sendSerial("0.25,-0.25,0.25,-0.25,0.25,-0.25,0.25,-0.25");
  sendSerial("\n");

  delay(100);

  sendSerial("0.2,-0.2,0.2,-0.2,0.2,-0.2,0.2,-0.2");
  sendSerial("\n");

  delay(100); 

    sendSerial("0.15,-0.15,0.15,-0.15,0.15,-0.15,0.15,-0.15");
  sendSerial("\n");

  delay(100);

  sendSerial("0.01,-0.01,0.01,-0.01,0.01,-0.01,0.01,-0.01");
  sendSerial("\n");

  delay(100);


}

I've just made minor adjustments to your current code (specifically, the subscribe part)

        bluetoothSerial.subscribe('\n', function (data) {
            app.clear();
            app.display(data);
            console.log("Raw string: " + data);
            var array = app.convertArray(data);
            console.log("Converted array: " + array);
        });

Where convertArray just returns into array via str.split(","); I'm reading console.log via adb logcat -s "CordovaLog"

Sample output:

D/CordovaLog(17087): Raw string: 0.15,-00.150.15,-0.15,0.15,-0.15,0.15,-0.15
D/CordovaLog(17087): Converted array: 0.15,-00.150.15,-0.15,0.15,-0.15,0.15,-0.15
D/CordovaLog(17087): Raw string: 0.2,-0.2,0.2,-0.2,0.2,-0.
D/CordovaLog(17087): Converted array: 0.2,-0.2,0.2,-0.2,0.2,-0.
D/CordovaLog(17087): Raw string: ,0.2,-0.2
D/CordovaLog(17087): Converted array: ,0.2,-0.2
D/CordovaLog(17087): Raw string: 0.25,-0.25,0.25,-0.25,0.25,-0.25,0.25,-0.25
D/CordovaLog(17087): Converted array: 0.25,-0.25,0.25,-0.25,0.25,-0.25,0.25,-0.25
D/CordovaLog(17087): Raw string: 0.2,-0.2,0.2,-0.2,0.2,-0.2,0.2,-0.2

Here are the files:
https://github.com/jiandu/BTSerialSpine/tree/master/simpleserialdroppedchar

You'll need to edit with your BT MAC address.

Any help would be appreciated.

@forkandspoon
Copy link
Author

Hi don,

We are still experiencing problems with your plugin. For the record, the arduino is sending out "ASCII" encoded strings, and the javascript app is just reading whatever your plugin sent as a "string" via the subscribe method.

Does the subscribe method use the buffer or not? This is because we used your chat example, of which we sent less than 4 char strings per 100ms, but yet we still got swapped characters (If the buffer has been filled completely, shouldn't we be getting dropped characters only, not swapped? what about when I sent "1234" and got "2234"... weird).

This is since we are sending data at a rate of 9600 baud rate, and on going though your java code on the buffer ( stringbuffer() specifically), we found that you by default use a buffer of 16 characters. I don't know if this would be a solution to us, would it be theoretically be possible to solve this issue by changing "stringbuffer()" to increase buffer size to something larger? (according to http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuffer.html , you can change buffer to 100 by "stringbuffer(100)")

Oh and apologies if the cause or solution is obvious, we are newbies to javascript and phonegap.

-BK

@don
Copy link
Owner

don commented Aug 1, 2013

I haven't had a chance to try your code and debug the problem yet. I'll try
and look into this over the weekend.
On Aug 1, 2013 2:59 AM, "jiandu" notifications@github.com wrote:

Hi don,

We are still experiencing problems with your plugin. For the record, the
arduino is sending out "ASCII" encoded strings, and the javascript app is
just reading whatever your plugin sent as a "string" via the subscribe
method.

Does the subscribe method use the buffer or not? This is because we used
your chat example, of which we sent less than 4 char strings per 100ms, but
yet we still got swapped characters.

This is since we are sending data at a rate of 9600 baud rate, and on
going though your java code on the buffer ( stringbuffer() specifically),
we found that you by default use a buffer of 16 characters. I don't know if
this would be a solution to us, would it be theoretically be possible to
solve this issue by changing "stringbuffer()" to increase buffer size to
something larger. (according to
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuffer.html, you can change buffer to 100 by "stringbuffer(100)")

-BK


Reply to this email directly or view it on GitHubhttps://github.com//issues/9#issuecomment-21918457
.

@manorius
Copy link

manorius commented Sep 7, 2013

I'm having exactly the same problem.
I send the following string "EBBAi" with arduino over bluetooth and the first letter switches randomly between E and A when received with Bluetooth serial. @jiandu Did you manage to find a solution?

cheers,
m

@forkandspoon
Copy link
Author

I have been putting this off so far, working on other parts of our code.

I think it could be a buffer size problem. Going to try clearing the buffer after each received string or increase the buffer size next week. We were using the subscribe function for the first test code, perhaps other code is somehow stuttering the app, causing the bluetooth library to run irregularly.

@manorius how fast were you sending your data? Baud rate and OS? If you could send me your code I could give it a test?

@don any luck with taking a look at this? Also, could you provide some sample code for read/readUntil/available and buffer?

@forkandspoon
Copy link
Author

Other than that, our team was planning on incorporating a CRC within each line for error detection. Our app is proof of concept (before we go native) but it would be great if this corruption was resolved, as Phonegap works great for us otherwise.

We're also moving from a HC05 bluetooth to RN42 by Roving Networks

@manorius
Copy link

manorius commented Sep 7, 2013

@jiandu baud is 9600 and I just found a workaround to my problem.
Using the following Arduino code, the first character of my data gets swapped randomly :

mySerial.print("EBBAi"); 
mySerial.print("\n");

But if I add a line feed before my data I don't get any errors and no character swapping

mySerial.print("\n");
mySerial.print("EBBAi"); 
mySerial.print("\n");

cheers,
m

@forkandspoon
Copy link
Author

Does it also work if your data is longer? say, 32 characters long?

@manorius
Copy link

manorius commented Sep 7, 2013

@jiandu I tried, and I've noticed errors occurring more frequently... even with the line feed....

@don don closed this as completed in 5592d1d Sep 18, 2013
@don
Copy link
Owner

don commented Sep 18, 2013

@jiandu @manorius sorry this fix took so long

The data was being corrupted while it was being passed in the android.os.Message from BluetoothSerialService to BluetoothSerial. I guess passing a byte[] in a Message is a bad idea. Maybe the original was being overwritten?

I updated the code to create the String in the Service and pass the String in the message. It appears to be working without corruption now. Hopefully this fixes the problem for you too.

@forkandspoon
Copy link
Author

Hi Don,

I will try this out and report back ASAP.

Thanks for this.

JD

don added a commit that referenced this issue Sep 18, 2013
Fixes #8 - add connectInsecure
Fixes #9 - fix logic
Update docs
@forkandspoon
Copy link
Author

I can confirm this issue is fixed, thanks Don!

@McSodbrenner
Copy link

Hi, the fix seems not to work for me.
I'm using Phonegap Build (phonegap-version 3.0.0) with BluetoothSerial 0.2.1
and an Arduino with a HC-06 Bluetooth module.

Just sending the strings "left" and "right". This is the output:
image

This is the javascript of the subscribe function:

    bluetoothSerial.subscribe('\n', function(data){
        if (data == 'left\r\n') adjustScore('.score-1', +1 );
        if (data == 'right\r\n') adjustScore('.score-2', +1 );
        console.log(data);//, data.length);
    });

If I use Blueterm on Android to check the data, everything is ok.

@don
Copy link
Owner

don commented Dec 19, 2013

@McSodbrenner This might be a problem with PhoneGap build. Unfortunately it's really painful to get updates pushed to build. Any chance you could try using Cordova locally?

I'm going to release 0.2.3. momentarily. This includes additional fixes that might help.

@don don reopened this Dec 19, 2013
@McSodbrenner
Copy link

Sorry, I am a beginner at Phonegap and don't want to have the hassle with Phonegap and the SDKs. But it is not urgent. I can wait for a couple of weeks and have a look then. :)
So is the 0.2.1 at build not the correct 0.2.1 from the repository?

Thanks,
Christoph

@don
Copy link
Owner

don commented Jan 17, 2014

phonegap build has 0.2.2

@don don closed this as completed Jan 17, 2014
@McSodbrenner
Copy link

Does not work. :) Hoping for .0.2.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants