Arduino Razor IMU NewSoftSerial Problem

macman20:
The first time I ran it the first 1.9 messages came through fine although I played around with it for a while to no avail and then changed it back to its original setup but never got the same results again. These are the results I’m now getting in “bursts”:

“!ANǚ26.7˜–17.6•––41.4’†…!ANGš26.7°”17.6””–41.4—……!ANG:26.74¬17.65¬41.4²„!ANG”

Do you think I should add a 1 or 2 msec delay between chars sent from the IMU?

Thanks.

I assume there was a #include <NewSoftSerial.h> in there somewhere.

I wouldn’t put in a delay, ideally when the IMU message is coming in the Arduino is doing nothing more than receiving it via NSS. I don’t see how a delay would help. I do find it interesting that you’re printing out 25 characters where I had expected 20. That it was consistently 25 (above) characters makes me discount line reflections and such as being the root cause. I am a little concerned about how the IMU message gets properly synched to the Arduino. That is when the Arduino finally gets booted up and running the NSS code, the IMU may have already been sending messages and it’s certainly possible that the first message received by the Arduino was half done but that should only screwup the 1’st message (that I can see). Can I suggest an experiment to absolutely verify that there’s no problem in the comm btw the Arduino and XBee and PC ? Try sending a canned message instead of the IMU data each time the IMU sends a data burst. Something like …

#include <NewSoftSerial.h>
NewSoftSerial mySerial(2, 3);

char test[40];

void setup() 
{
  for (int k = 0; k < 40; k++)
  {
    test(k) = (49+k); //test will hold ASCII chars from 1 to 40
  }
  Serial.begin(57600);
  mySerial.begin(57600);
}

void loop()
{
  int i = 0;
  while (mySerial.available())
  {
    buffer[i++] = mySerial.read(); //read in data storing it in array called buffer
    if(i == BUFF_SIZE) break; //Bail! The buffer is about to explode
  }
  for(int j = 0 ; j < i ; j++)
  {
    Serial.print(test[j]);  //send canned data to serial port-XBee as ASCII chars after all of burst has been rcvd
  }
}
  • I also have run into a similar problem, and thought I’d see if this is of any help or anyone has any thoughts on it:

Running the below code, and sending serial data to the arduino of “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”,

with echoing characters to mySerial, I would rx only “012678CDEJKLPQRVWXcdeijkopqvwz” back over the “Serial” arudino connection, or out the mySerial.

– Commenting out the mySerial echo, I would rx the echo of exactly what I sent over the “Serial”.

Seems to me the mySerial.print() is blocking the UART from reading? during NSS transmit are they possibly using interrupts that will block rx on the main “Serial”? That’s what it’s looking like to me…

#include <NewSoftSerial.h>

NewSoftSerial mySerial(8, 2);

void setup() {
  pinMode(8,INPUT);
  digitalWrite(8, LOW);
  mySerial.begin(9600);
  Serial.begin(57600);
}

void loop()
{
   while(Serial.available()){
     char readChar = (char)Serial.read();
//      mySerial.print(readChar); // comment this out and data echos back correctly below.
      Serial.print(readChar);
   }
}

– Related to the above post, after banging on this it seems it was the mySerial.print() blocking. I put a delay in the Serial.available() / read() loop to keep the arduino from reading ahead of the stream while it was coming in, and now it works correctly. The delay keeps the loop running until all data is received from that transmission, and then tx’s it out the Serial and NSS once completly recieved, so that the NSS (seemingly) blocking print() won’t cause issues. The delay could probably be trimmed based on baud rate to keep you slightly behind each incoming byte, but just ‘1’ was fine for my use. Without the delay, if you send 50 bytes over Serial, it would go into the Serial.available() loop, read the first 3 bytes usually as it was reading faster than the transmission was actually being received, and would go to transmit those 3 bytes out NSS and Serial. while transmitting out NSS any incoming bytes over Serial were lost, creating the echo pattern described above with lost bytes. Good luck!

#include <NewSoftSerial.h>

NewSoftSerial mySerial(8, 2);

void setup() {
  pinMode(8,INPUT);
  digitalWrite(8, LOW);
  mySerial.begin(9600);
  Serial.begin(57600);
}

String buffer = "";
void loop()
{
  if(Serial.available()){
   buffer = "";
   while(Serial.available()){
     delay(1);
     buffer += (char)Serial.read();
     if(buffer.length() > 100)
        break;
   }
     mySerial.print(buffer); 
      Serial.print(buffer);
   }
}