Serial Output

Hi, Im working on a personal project to add a display to a serial device. I've been able to connect to it Via-PuttY and I can get the output fine from putty. it basically starts in burst mode displaying

** 1PROBE ** ** 1PROBE ** ** 1PROBE ** ** 1PROBE ** ** 1PROBE ** (etc...)

Basically it outputs the number probe (can either be 1 or 2) and then it ends each line with ASCII char 13 (carriage return) and then ASCII char 10 (new line)

After that you can send commands to the device via some ASCII (200 - 210)

Baud Rate is 57600 8 data bits 1 stop bit no parity

From what I've read these are also the default values for the serial in Arduino (except the baud rate), so I dont believe I have to modify these.

I'm going over various examples of Serial communications and I've tried many of them but I keep getting weird outputs. I am confused as whether to use Serial. or SoftwareSerial.

I tried changing it to CHAR instead of ASCII but It still doesnt display what it should. The current output is some weird stuff

I tried the following: http://www.arduino.cc/en/Tutorial/SoftwareSerialExample (modified baud rate)

Most of the other examples are slight variations of the link above. I tried changing Serial.write(mySerial.read()); to Serial.println(char(mySerial.read()));

and it did output different characters, however it didnt output ** 1PROBE **. Additionally it only outputted 1 character per line.

I wanted help on the following:

  • First and most important, how do I get it to output the correct information? (** 1PROBE** )

  • How to set it to read up to ASCII char 10 and then print a new line (or ASCII 13?), I am thinking this should be a serial event? (I wanted help with this)

  • is there a way to make this some more effective void() loop? (every time I run it arduino seems to freeze for a moment before opening the serial monitor). I tried adding delays but it still seems to freeze up the software a little. I intend to add a display so I am not sure if the processor will be able to handle this loop and the display management.

Thank you for your help!

Post your code.

Mark

Please post your code.

How to use this forum

See this video: MIDI Decoder demonstration

In that I read from a MIDI keyboard at 31250 baud using SoftwareSerial, and write to hardware serial at 115200 baud.

I am not sure if the processor will be able to handle this loop and the display management.

Yes, it can.

http://www.gammon.com.au/serial

Thank you for the replies, as mentioned I used the code in the example just slightly modified as follows:

my code:
/*
Software serial multple serial test

Receives from the hardware serial, sends to software serial.
Receives from software serial, sends to hardware serial.

The circuit:

  • RX is digital pin 10 (connect to TX of other device)
  • TX is digital pin 11 (connect to RX of other device)

Note:
Not all pins on the Mega and Mega 2560 support change interrupts,
so only the following can be used for RX:
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

Not all pins on the Leonardo support change interrupts,
so only the following can be used for RX:
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

created back in the mists of time
modified 25 May 2012
by Tom Igoe
based on Mikal Hart’s example

This example code is in the public domain.

*/
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

Serial.println(“This message prints”);

// set the data rate for the SoftwareSerial port
mySerial.begin(57600);
mySerial.println(“This message does not print”);
}

void loop() // run over and over
{
if (mySerial.available())
Serial.println(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}

THIS OUTPUTS:

This message prints
84
168
157
168
183
178
75
76
40
45
149
82
72
72
137
69
101
130
189
145
117
97
134
208
84
42
13
138
73
169
90
160
222
100
45
145
161
180
149
149
67
33
37
37
85
9
67
213
20
26
161
169
41
29
20
255
73
137
213
130
145
181
69
42
42
42
42
84
173
168
183
178
150
152
40
45
37
85
72
248
42
42
84
173
168
183
178
150
152
40

====================

Additionally I tried:

my alternate code:
void loop() // run over and over
{
if (mySerial.available())
Serial.println(char(mySerial.read()));
if (Serial.available())
mySerial.write(Serial.read());
}

THIS OUTPUTS:

This message prints
*
*
¤
–
¨
·
²
K
¤
(
•
’
R
H
È
¥
D
Õ
B
½
Ñ
Y
b
†
Ð
*
ª
…
…
©
Q
µ
A
Ý
É
Z
b
†
h
*
’
†
…
•
•
ª

ª
V
)
…
ª
5
)
ÿ
I
‰
Õ
u
‘
µ
E
…
‘
*
*
T

¨
·
*
*
T
­
¨
·
²
–
˜
(

%
U
H
ø
*
*
T
­
¨
·
²
–
L
(

%
U
H
ø
*
*
T
­

Which device is connected to Serial and which device to mySerial. You code does not seem to correspond to what you said in your first post.

void loop() // run over and over { if (mySerial.available()) Serial.println(mySerial.read()); if (Serial.available()) mySerial.write(Serial.read()); }

THIS OUTPUTS:

This message prints 84 168 157 168

This code should print each character received from mySerial, onto a separate line on Serial. So, how are you getting 2 or 3 characters per line ?

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

I'm not even sure what char( ) does, but it appears to do the same thing as

(char) mySerial.read( ) , which would be the normal way to cast the result of read( ) as a char, and which is unnecessary anyway.

So, I am very doubtful that this "alternative code" in that line only, is the only thing you changed between those two example outputs.

The second output sample looks very much like a speed mismatch.

If your device is spontaneously emitting 1PROBE[CR][LF] then the correct code to display this on the serial monitor would be

void loop()
{
    while( mySerial.available() )
    {
          Serial.print( mySerial.read() );
    }
}

You don't want to println( ) every char that you receive, you will get one character per line. Use Serial.print()

Does you device start outputting these "1PROBE" messages as soon as it is powered on, or does it require some configuration commands in the other direction, first ?

Thank you for the replies,

indeed the only change between codes is the char part added to the code.

What I said in the first post is that I am confused as I do not know if I should use Serial or SoftwareSerial.

I have only used SoftwareSerial (i havent tried regular serial).

I will try the code michinyon suggested, thank you.

from what I've read char converts from ASCII table codes to characters.

The device starts on burst mode as soon as it gets poewred on. no commands needed, although it does take in commands.

About which is serial and which is myserial I am not sure how to answer, as mentioned previously this code is copy-pasted from the Arduino examples of SoftwareSerial. The only thing I modified is the baud rate

Sorry for the double post, but after trying michinyon's code I only got the same as the first try (just numbers)

i tried doing it as a println instead of a print and i get just numbers but with carriage returns in between. Please see attached pictures. I tried all other options (No line, New line, carriage return, both NL and CR) and they all display the same output.

Also attaching a PuTTY screenshot.

Please note that this is a different device however it works the same way (burst mode broadcasting its name) |396x500 |500x313

I tried all other options (No line, New line, carriage return, both NL and CR) and they all display the same output.

The options you refer to affect what the Serial Monitor application appends to data that it SENDS. They have NO affect on the data that it receives.

The Serial.read() function returns an int. If you KNOW that there is data to read, because you called available() first, do NOT use an int to store the returned value (implicitly). Use a char. Store the data; print the stored data. DO NOT PRINT DATA WITHOUT STORING IT!

Got it, I finally did it using regular serial, not Softwareserial. I am not sure as what the difference is

/*
Reading a serial ASCII-encoded string.

This sketch demonstrates the Serial parseInt() function.
It looks for an ASCII string of comma-separated values.
It parses them into ints, and uses those to fade an RGB LED.

Circuit: Common-anode RGB LED wired like so:

  • Red cathode: digital pin 3
  • Green cathode: digital pin 5
  • blue cathode: digital pin 6
  • anode: +5V

created 13 Apr 2012
by Tom Igoe

This example code is in the public domain.
*/

// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

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

}

void loop() {
// if there’s any serial available, read it:
while (Serial.available() > 0) {
Serial.print(char(Serial.read()));

if (Serial.read() == 13) {
Serial.print(’\n’);
}
}
}

This sketch demonstrates the Serial parseInt() function.

No, it doesn't.

while (Serial.available() > 0) {
   Serial.print(char(Serial.read()));

    if (Serial.read() == 13) {
    Serial.print('\n');
    }

If there a byte of serial data to be read, read and print it. Then, regardless of whether or not there is any data to read, read a byte, and see if it is a \n. Fail!

Hi PaulS, I dont believe I understand your reply. I do apologize for the comments, I forgot to edit them (I worked off an example and started by copy-pasting the code).

The second part of your post I dont understand, can you elaborate? The code I posted currently outputs what I was looking for, although it does it at a very very high rate and I dont know how to slow it down (if I add a delay function it messes everything up)

The second part of your post I dont understand, can you elaborate?

What does Serial.available() tell you? It tells you how many bytes there are in the input buffer to be read. Suppose is says, as usual, that there is one byte. How many can you read? How many DO you read?

The real question is why you won't store the value that you read so that you can print it AND see if it, not the next character, is a carriage return.

After some more internet reading I have modified the code significantly. Please see below

/*
Communicating with tPod system. 
 */


void setup() {
  // initialize serial:
  Serial.begin(57600);
}


void loop() {
  
  char buffer[13];
  while (Serial.available() > 0) {
   Serial.readBytesUntil(10,buffer,15); 
   Serial.println(buffer);

  }
}

This code outputs:

**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**


**T-Pod-1Ch**

I made it store the whole thing in a char buffer however I am having some issues as I am limiting this buffer to what I know is the output. So the current device I'm running outputs in burst mode T-Pod-1CH (Carriage Return) (New Line)

I assumed this were 15 characters however if I use a buffer of 15 char I get

T-Pod-1CH** T-Pod-1CH*T- Pod-1CHT-Po d-1CH***T-Pod-

(ETC)

which is not what I wanted it to get. So I modified the buffer to 13 which now outputs T-Pod-1CH

So I am currently getting the values I wanted to get, however I know this is not a correct solution since I am not really reading a line regardless of its size just delimiting by the fact that its a line.

I want the buffer to not be set, I just want it to:

  • Print line T-Pod-1CH (or whatever it might be)
  • Flush the buffer
  • Read a new line

Another problem I am having is the speed at which it reads which is much much greater than the speed at which the device outputs. I tried adding a delay function but that just messes everything up.

If I change the void part to this:

void loop() {
  
  char buffer[13];
  while (Serial.available() > 0) {
   Serial.readBytesUntil(10,buffer,15); 
   Serial.println(buffer);
delay(120);
  }

I get this:

**T-Pod-1Ch**

**T-Pod-1Ch**

**T-Pod-1Ch**

**T**T-Pod-1Ch*
*
T**T-Pod-1Ch*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T**T-Pod-1Ch*
*
T**T-Pod-1Ch*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T**T-Pod-1Ch*

Please note that some lines are more than 13 characters in lenght, I dont know why. I am also very curious as to why when i copy paste there are 2 new lines (in the first output) instead of 1 (in the serial monitor it only has 1 carriage return)

 char buffer[13];
  while (Serial.available() > 0) {
   Serial.readBytesUntil(10,buffer,15);

Why are you LYING to the function? The array can NOT hold 15 characters!

You can avoid these issues by using sizeof:

   Serial.readBytesUntil(10,buffer,sizeof buffer);
while (Serial.available() > 0) {
   Serial.print(char(Serial.read()));

    if (Serial.read() == 13) {

Your debugging print has consumed the character (you read it). You can't just re-read it.

Slow down and read what PaulS is saying. He knows what he is doing.

Hi, Thank you for the replies, they've been really helpful to analyze my code. To answer your comment PaulS:

Current code output, as you mentioned "lies" to the software by asking it to read 15 characters into a 13 character size variable. I ended up with those numbers via trial-and-error

/*
Communicating with tPod system. 
 */


void setup() {
  // initialize serial:
  Serial.begin(57600);
}

void loop() {
  
  char buffer[15];
  while (Serial.available() > 0) {
   Serial.readBytesUntil(10,buffer,sizeof buffer); 
   Serial.println(buffer);
delay(125);
  }
}

that particular code (char buffer of 15 and reading 15 characters or as Nick suggested sizeof buffer) produces this output: (identical if I type 15 or if I use sizeof buffer)

**T-Pod-1Ch**
Ã
**T-Pod-1Ch**
Ã
**T-Pod-1Ch**
Ã
**T-Pod-1Ch**
Ã
**T-Pod-1Ch**
Ã
**T-Pod-1Ch**
Ã
**T**T-Pod-1Ch*Ã
*
T**T-Pod-1Ch*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T**T-Pod-1Ch*Ã
*
T**T-Pod-1Ch*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T**T-Pod-1Ch*Ã
*
T**T-Pod-1Ch*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T**T-Pod-1Ch*Ã
*
T**T-Pod-1Ch*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T**T-Pod-1Ch*Ã
*
T**T-Pod-1Ch*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T**T-Pod-1Ch*Ã
*
T**T-Pod-1Ch*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã
**T-Pod-1Ch**
*Ã

If i edit the buffer size to 13:

void setup() {
  // initialize serial:
  Serial.begin(57600);
}

void loop() {
  
  char buffer[13];
  while (Serial.available() > 0) {
   Serial.readBytesUntil(10,buffer,sizeof buffer); 
   Serial.println(buffer);
delay(125);
  }
}

I get this:

**T-Pod-1Ch**Ã

*T-Pod-1Ch**Ã
**T-Pod-1Ch**Ã

*T-Pod-1Ch**Ã
**T-Pod-1Ch**Ã

*T-Pod-1Ch**Ã
**T-Pod-1Ch**Ã

*T-Pod-1Ch**Ã
**T-Pod-1Ch**Ã

*T-Pod-1Ch**Ã
**T**T-Pod-1CÃ
h******T-Pod-Ã
1Ch******T-PoÃ
d-1Ch******T-Ã
Pod-1Ch******Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã
T-Pod-1Ch****Ã

and if I lie to it:

void setup() {
  // initialize serial:
  Serial.begin(57600);
}

void loop() {
  
  char buffer[13];
  while (Serial.available() > 0) {
   Serial.readBytesUntil(10,buffer,15); 
   Serial.println(buffer);
delay(125);
  }
}

I get the clean output I expect:

**T-Pod-1Ch**

**T-Pod-1Ch**

**T-Pod-1Ch**

**T-Pod-1Ch**

**T-Pod-1Ch**

**T-Pod-1Ch**

**T**T-Pod-1Ch*
*
T**T-Pod-1Ch*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T**T-Pod-1Ch*
*
T**T-Pod-1Ch*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T**T-Pod-1Ch*
*
T**T-Pod-1Ch*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*
**T-Pod-1Ch**
*

(NOTE: I am still having the issue with the delay function messing up the output)

To address one of your previous concerns on whether I was just reading without storing, I believe that has been addressed by storing the read output in the char array buffer.

That being said however, does not produce that if statement that I originally created that you mentioned was wrong.

From what I've read in the arduino page readBytesUntil should perform the task of that if statement (if the character is 10 stop reading). Maybe this is where I am confused and there is another method of reading until?

Again the current device outputs as follows:

T-Pod-1CH (Carriage Return) (New Line)

Carriage return is byte 13 and new line is byte 10