Help with reading Serial RX on Arduino Due

Apologies I know this has probably been asked time and time again but I've spent 2 days trawling through google and the various forums and non of the serial code samples work for me.

Here is a sample of the serial data I am receiving (I receive 15 samples a second):

+002362
+002291
+002108
+001986
+001969
+001992
+002028
+002110
+002235
+002298
+002330
+002356
+002339
+002344
+002355
+002373
+002340
+002235

I'm using the Arduino Due and using Serial1.

Simple I thought just call Serial.read and cat 8 bytes together to capture each line.

I print my output to the serial console and receive the following ouput, where am I going wrong?:

yi††gge
yi††ggf
yi††ggc
yi††gg2
yi††gg3
yi††g†2

Here is my code:

char inputData[9];

void setup() {
 // initialize serial:
 Serial.begin(115200);
 Serial1.begin(115200);

}

void loop() {
if (Serial1.available() > 8) {
   for(int j = 0; j < 8; j++){
         inputData[j] = Serial1.read();
      }
  Serial.println(inputData);
   } 
 
}

How many bytes if serial data have to arrive before the code smiles?

  Serial.println(inputData);

inputData is NOT a string. Do not expect functions that deal with strings to handle non-strings properly.

OK, so how do I convert from the char array to a String. Google points to something like this:

String inputString = String(inputData);

However when ouput input String to serial I still get:
††gdyi†
†g2 yi†
†g2yi††
g2fyi††
gdeyi††
g yi††

OK, so how do I convert from the char array to a String.

You don't. You convert from a char array to a string, by adding the necessary NULL terminator.

Of course, your array needs to be large enough to hold the NULL, too.

What is sending the data, and why the hell doesn't it send an end of packet marker?

Its an ADC sending the Mv reading over serial.

The output never stops. Each reading is separated by a new line so I would have thought I could have either read the reading either through number of bytes or looking for the new line character.

The output never stops. Each reading is separated by a new line so I would have thought I could have either read the reading either through number of bytes or looking for the new line character.

Then, the new line IS part of the serial stream. It must ACTUALLY be read to remove it from the buffer.

You will need to read and store the data until the end of record marker arrives. Then you can analyze the packets to make sure that the packet is the right length and starts with the right character.

I suspect that you are loosing serial data because you are not reading it faster (or correctly) than it arrives.

Have a look at the examples in serial input basics. The 2nd example should do what you want.

...R

I'm clearly missing something very obvious.

I'm simplified everything, I just send one character at a time now.

It seems to be the way I have set up serial2 port, it would appear that it is the baud rate but I am 100% sure it should be 9600, so how on earth can it be messing up!?..

Serial port baud is also correct. I've swapped Serial1 to Serial.read and it works perfectly.

Here is my very simple code:

char receivedChar;
boolean newData = false;

void setup() {
Serial.begin(115200);
Serial1.begin(9600);
}

void loop() {
if (Serial1.available() > 0) {
Serial.println((char)(Serial1.read()));
}
}

If I enter the number "3" on serial1 on the pc (Tera Term, baud rate, 9600). The serial monitor prints out "f"

Where am I going wrong!?

Where am I going wrong!?

Posting code that won't even compile.

Serial.println((char)(Serial1.read)());

(Some) (of) (those) (parentheses) (are) (wrong/not needed).

is that better?

Compiles now..

is that better?

No. It is not. Post code in a new reply. Don't change code you posted in an earlier reply.

char receivedChar;
boolean newData = false;

void setup() {
Serial.begin(115200);
Serial1.begin(9600);
}

void loop() {
if (Serial1.available() > 0) {
Serial.println((char)(Serial1.read()));
}
}

We are now at Reply #12 - can you remind us / update us on what the code does, or does not do?

Did you try the code in the link in Reply #7 ?

...R

Hi Robin,

Yes I tried all the examples with the same result as per the text above.

I'm trying to read serial output from a ADC that is outputting over serial.

I've now removed it from the arduino and plugged the RX into the tx on my serial cable plugged into my PC via tera term. The results are detailed in reply 8.

So it seems to be something up with the configuration on my due.

Hi Robin,

Yes I tried all the examples with the same result as per the text above.

I'm trying to read serial output from a ADC that is outputting over serial.

I've now removed it from the arduino and plugged the RX into the tx on my serial cable plugged into my PC via tera term. The results are detailed in reply 8.

So it seems to be something up with the configuration on my due.

edwardo:
The results are detailed in reply 8.

So it seems to be something up with the configuration on my due.

I can't make sense of Reply #8. Can you give us an example of what the code in Reply #12 actually does, and what it should do?

Please draw a diagram showing how everything is connected and post a photo of the drawing.

...R

So after reading all of the replies, I have yet to see anyone suggest this...

Are you sure your SERIAL MONITOR baud rate is set to 115200 so that it matches your arduino due's baud rate of 115200?

Because that is one easy way to get random characters...

There is a screenshot attached of which baud rate button I am speaking of.

Screen Shot 2015-05-25 at 1.47.47 AM.png