Need help with Processing Serial Data -- all Numbers

I’m doing some very basic experimentation (Learning really)

So, here is the layout really quick,
Bluetooth module (4 pin)
Arduino Uno R3

Pins:
bluetooth tx → SoftSerial rx (pin 3)
bluetooth rx → SoftSerial tx (pin 2)
Sketch:

#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3

SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
byte pinState = 0;
void setup() {
  // put your setup code here, to run once:
mySerial.begin(115200);   //bluetooth
Serial.begin(9600);          //usb serial
}
void loop() {
  // put your main code here, to run repeatedly: 
  //mySerial.println("hello world");     //group 1
  if (mySerial.read() != -1)               //group 2
    Serial.println(mySerial.read());     //group 2
  delay(2);
}

I’m using RealTerm to send data back and forth.
When I uncomment group 1 and leave group 2 commented it sends “Hello World” across the realterm as expected.
when I comment group 1 and uncomment group 2, and I send data from realterm thru bluetooth into the arduino to be picked up by the serial I get this in my serial monitor:

182
44
203
177
172
237

What I really want is to send “Hello World” from my realterm, and have it show up as “Hello World” on my Serial Monitor.

Any thoughts?
Thanks.

You are reading twice once with each of the mySerial.read()'s change the firat to a call to available()!.

Mark

Okay, changed it up a bit…Mark, thanks for the tip on Available

#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3

SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
byte pinState = 0;
int incomingByte = 0;

void setup() {
  // put your setup code here, to run once:
mySerial.begin(115200);
Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly: 
  //mySerial.println("hello world");
  //if (Serial.available())
  //if (mySerial.read() != -1)
  //  Serial.println(mySerial.read());
  if (mySerial.available() > 0) {
    incomingByte = mySerial.read();
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }
  delay(2);
}

When I send ‘Hello World’, now I get:

I received: 72
I received: 44
I received: 139
I received: 189
I received: 64
I received: 87
I received: 183
I received: 185
I received: 86
I received: 217

What I want to see is:

I received:  Hello World

Incoming byte needs to be a char, not an int. Also most BT modules are set for a baud rate of 9600.

The bluetooth device I got is 115200 baud.. I have that set on all ports (com 7/8)

Getting closer:

     char incomingByte = 0;
and 
    Serial.println(incomingByte);

New Result:

I received: H
I received: ,
I received: ?
I received: õ
I received: 
I received: «
I received: K
I received: ±
I received: d

R

Incoming byte needs to be a char, not an int.

incoming is int not char (you need somthing eg -1 to say no data or in the case of stdio EOF),

Mark

Serial.println(incomingByte, DEC);

If you don't want each character on a separate line, you want to call Serial.print() not println().

The DEC argument causes print() to format the number you supplied as a decimal integer. If you know it's an ascii character and just want to write that character out as-is, then you can just call Serial.write(incomingByte). That won't do any formatting at all. It won't let you insert line feeds, but if your incoming text already has them then you don't need to do anything special - echo them back out just like any other character and the serial monitor (or whatever client you're using) will receive them and display a new line appropriately.

Serial.println(incomingByte, DEC);

Drop the DEC as it tells println to pritn the value as a sequence of digits eg H gets you 72 not H!.

Mark

Okay, lets just ignore storing it in a variable..

Serial.print(mySerial.read());
returns:
724413918914421375177178
Serial.write(mySerial.read());
returns:
H,?½@W·¹^Ù

What I need to figure out is why it's not returning the actual text i'm sending.

Check your setings on Realterm do they match the ones used by serial? (stop bits/parity) and try droping the baud reate strat out at 9600 and work up!.

Mark

Dropped everything to 9600. Didn’t work.
But to prove i’m not crazy… Here’s the settings of all ports… CTS and DSR solid as a rock.

Post a screenshot of the "Port" tab of RealTerm.

Also, you might try the simpler case of just using RealTerm against the programming port to ensure you at least have everything talking properly first and to eliminate the blue tooth serial comms as an issue.

Send it Hello World and have Arduino kick back "OK" or enable the pin 13 LED if it successfully compares what it received to the string it expected.

Here ya go
BTW, I already tested it the other way, doing hello world and painted the terminal in realterm as its supposed to. It’s when I send data from realterm, thru the bluetooth and try to read it thru serial monitor that it’s getting corrupt.

5-25-2013 8-39-44 PM.png

So it looks like your blue-tooth port is COM 8 and I'm guessing the Arduino programming port is on COM 7? Earlier post shows it on COM 6.

When you pair a bluetooth module, it creates 2 ports. You are supposed to use the lower one. Don't ask me why, thats just the way it is.

Try this with RealTerm just using the regular Serial port to at least Validate you have everything working there. I’ve compiled and built this and it works great. I’ve found Serial I/O on the Arduino to be sometimes tough to get right. If this runs fine, then switch back to SoftwareSerial and try it again. At least at that point you know it’s something either in the SoftwareSerial library or your bluetooth module is garbling it up.

#include <String.h>

static String str;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  pinMode(13, OUTPUT);

  // Clear any garbage
  while (Serial.available())
    Serial.read();
}

void loop() {
  char b = 0;
  while (Serial.available())
  {
    b = Serial.read();
    if (b != NULL)
    {
      str.concat(b);
      if (str.equalsIgnoreCase("Hello World"))
      {
        Serial.println("OK");
        digitalWrite(13, LOW);
        str = "";
      }    
    }
  }
}

So, the way this is written is if I go into the Serial Monitor and type in 'Hello World', it prints out OK and my LED blinks, but I understand what you're saying about using an LED to test the output as it shows up in the board, instead of using a serial write.

I have USB hooked up to Serial right now, so I have no choice but to use SoftwareSerial to test it.

Hmm, not sure we are clearly understanding each other. Yes, with the serial monitor that is true that you can type in "Hello World" and get OK back and the LED does its thing. However, the same should be true with RealTerm as well. So, instead of running the Serial Monitor, do NOT run the serial monitor and instead run RealTerm and connect to the same COM port as the Arduino IDE uses to program the board.

You dang well better get OK there as well. Note, that in order for what I did to work correctly, I had to check for nulls on read else I'd get garbage at the front of the string.

If you do get OK you're real-term settings should be fine and you can be confident it's doing what it should be. After that, I'd say it's time to validate the communication between the bluetooth module/library and Arduino.

Check the byte that gets read against what you expect it to be as it gets read. It really looks to me like you have either stop bit, parity bit, or flow control issue or you are losing bits as the bytes are read.

You might also be able to eliminate SoftwareSerial as the cause by specifying pins 0 and 1 as your rx and tx pins respectively and then using SoftwareSerial to read from the lines otherwise used by Serial. If that works through the Serial Monitor at all and you get what you expect then you can probably safely eliminate that from your causes and focus on the Blue Tooth module.

exedor,

While it may not have seemed as if I was understanding you, I was actually a little bit ahead of you as far as trouble shooting goes. I understood what you were getting at, and it got me poking around a bit more. Thank you for your code and help. The code helped me more than anything to try and figure out what was going on. It made me realize that I needed to test it beyond a blinking light, and start printing it back out to both windows to get a full test.

So I’ve concluded that the communications on the BT module are all jacked up. The internal communications on the bluetooth were what was jacked up. This prompted me to just go in and reset it to run 57600. I’m not 100% sure of the baud rate on the module.
The code I’m using now is:

#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3


SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
byte pinState = 0;
char incomingByte = 0;
static String str;

void setup() {
  // put your setup code here, to run once:
mySerial.begin(57600);
Serial.begin(9600);
}
void loop() {
char b = 0;
  while (mySerial.available())
  {
    b = mySerial.read();
    if (b != NULL)
    {
      str.concat(b);
      if (str.equalsIgnoreCase("Hello World"))
      {
        Serial.println("OK");
        digitalWrite(13, LOW);
        str = "";
      }    
      mySerial.print(str);
      Serial.print(str);
    }
  }
}

So, the light doesn’t blink yet, and when I type in ‘Hello World’ I get:

JustDoc:
exedor,

While it may not have seemed as if I was understanding you, I was actually a little bit ahead of you as far as trouble shooting goes. I understood what you were getting at, and it got me poking around a bit more. Thank you for your code and help. The code helped me more than anything to try and figure out what was going on. It made me realize that I needed to test it beyond a blinking light, and start printing it back out to both windows to get a full test.

So I’ve concluded that the communications on the BT module are all jacked up. The internal communications on the bluetooth were what was jacked up. This prompted me to just go in and reset it to run 57600. I’m not 100% sure of the baud rate on the module.
The code I’m using now is:

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3

SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
byte pinState = 0;
char incomingByte = 0;
static String str;

void setup() {
  // put your setup code here, to run once:
mySerial.begin(57600);
Serial.begin(9600);
}
void loop() {
char b = 0;
  while (mySerial.available())
  {
    b = mySerial.read();
    if (b != NULL)
    {
      str.concat(b);
      if (str.equalsIgnoreCase(“Hello World”))
      {
        Serial.println(“OK”);
        digitalWrite(13, LOW);
        str = “”;
      }   
      mySerial.print(str);
      Serial.print(str);
    }
  }
}




So, the light doesn't blink yet, and when I type in 'Hello World' I get:

Well, looking at this code and the description of the results, I think you got what you asked the Arduino to do. No where do you turn on the LED, so once you get the full string you turn off an already off LED. (oops). Also, with the serial port you are printing (printing w/o line feed) your buffer as each character is added. And then once you have the full string you flush your buffer right before you get a chance to print the result. That is why it seems to stutter and then never complete.