Pages: [1] 2   Go Down
Author Topic: Garbage using serial functions in Arduino Due  (Read 7735 times)
0 Members and 1 Guest are viewing this topic.
Venezuela
Offline Offline
God Member
*****
Karma: 16
Posts: 515
Ground, ground, always ground
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does anybody know if Arduino Due has some issues with the Serial.print function?

I am running the following sketch taken from http://arduino.cc/en/Serial/Available :

Code:
int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
}

and every time I invoke the serial monitor or any other like Hyperterminal, I am getting garbage in the first two lines like:

I••¥Ù•: 255
I r

I••¥Ù•'ŠÊŠj
I reã

I••¥Ù•ŠÊŠj
I reÿ

I ran the same sketch in my UNO and no garbage came up!

Thanks!
Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1328
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 I forget which board I had the problem with but, I had to put a delay() in setup to give the serial monitor a chance to come up before the board tried to communicate.

 In my case, I think I was printing from the board early in the sketch.
Logged


California
Offline Offline
God Member
*****
Karma: 14
Posts: 629
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


 I forget which board I had the problem with but, I had to put a delay() in setup to give the serial monitor a chance to come up before the board tried to communicate.

 In my case, I think I was printing from the board early in the sketch.

Leonardo?  For that board it was:

Code:
// while the serial stream is not open, do nothing:
   while (!Serial) ;

...or a delay(1) after a serial print.  That said, since this is going through the 16u2, it should not be full speed (FIFO) through the USB and overloading the port buffer.  Curious...
Logged


Venezuela
Offline Offline
God Member
*****
Karma: 16
Posts: 515
Ground, ground, always ground
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried delay(10) after the serial prints but the garbage still. The interesting that the garbage only shows up when the terminal window is open. If I press the reset button, no more garbage. It could be may machine. I will try to test it in another computer.
Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1604
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Edit: after I posted the stuff below, I changed the baud rate on your sketch to 115200 in Serial.begin.  Now the garbage looks like this:

Quote
DDÄI recei‹é 0
I received: 0

I have the same problem with many sketches.  The problem also involves Serial.read(), there is garbage when you use Serial.available().

In your sketch, it appears to execute the code after this if statement:
Code:
if (Serial.available() > 0) {

Your output is garbled stuff from these print calls:
Code:
 Serial.print("I received: ");
  Serial.println(incomingByte, DEC);

Note how often you get "I re" as part of the garbage.  The 255 suggests you read 0XFF.
« Last Edit: December 01, 2012, 10:23:12 am by fat16lib » Logged

France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 266
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm almost convinced there is something wrong with DUE's implementation of Serial.available() Serial.read() and so

As I've explained on some other threads this morning, I've got code running for years on MEGA, it does compile the download fine on DUE but will not work, in my case a Serial protocol called SerPro using HDLC on top of USB link. There are constant CRC errors hence the frames are rejected then re-sent, my core program
Code:
// Start of SERPRO implementation
struct SerialWrapper
{
public:
static inline void write(uint8_t v){Serial.write(v);}
static inline void write(const uint8_t *buf,int size){
int z;
for(z=0;z<size;z++) Serial.write(buf[z]);
}
static inline void flush() {}
};

struct SerProConfig {
static unsigned int const maxFunctions = MAX_SERPRO_FUNCTIONS;
static unsigned int const maxPacketSize = 32;
static unsigned int const stationId = 3;
static SerProImplementationType const implementationType = Slave;
};

DECLARE_SERPRO(SerProConfig,SerialWrapper,SerProHDLC,SerPro);

void setup()
{
// protocol initialization
  Serial.begin(BAUD_RATE);

// Testing leds
  pinMode(ledLinkUp,OUTPUT);
  pinMode(ledCRCerror,OUTPUT);
  pinMode(ledRcv,OUTPUT);
  pinMode(ledXmit,OUTPUT);
}

void loop()
{
  if (Serial.available() > 0) SerPro::processData(Serial.read() & 0xff);
}

// End of SERPRO implementation
IMPLEMENT_SERPRO(MAX_SERPRO_FUNCTIONS,SerPro,SerProHDLC);
Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1604
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a sketch with a counter.  Looks like Serial has several, maybe 6, bytes of garbage input when a reset from opening a Serial monitor happens.

Load this sketch into the Due:
Code:
int n = 0;
void setup() {
  Serial.begin(115200);
}
void loop() {
  if (Serial.available()) {
    Serial.read();
    Serial.write('N');
    Serial.println(++n);
  }
}

 then open a serial monitor and you get this:
Quote
þþN5
N6
Close and open the serial monitor again and you get a similar result.  Almost always ending with the "N6" line.

Adding a delay(2000) after Serial.begin seems to fix it after reload but not on a reopen of the monitor.

I tried this but still get the problem.  
Code:
while (!Serial);
So if you write for Uno, Leonardo, and Due what is the correct answer to avoid this?
« Last Edit: December 01, 2012, 11:09:23 am by fat16lib » Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1604
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did more testing and the junk you get is sent to serial before the reset done by opening the serial monitor.

If you run this sketch and open a serial monitor at 115200, you will see some junk printed from loop() before the HI printed in setup().
Code:
void setup () {
  Serial.begin(115200);
  Serial.println();
  Serial.println("HI");
}
int n = 0;
void loop() {
  if (Serial.available()) {
    Serial.println();
    Serial.print(++n);
    Serial.write("c=");
    Serial.print(Serial.read(), HEX);
  }
}
output:
Quote
öþ
5c=0
6c=0
HI
This seems to indicate that when the serial monitor is opened, some junk is sent to serial, and then the reset restarts the program which prints "HI".

This can cause big problems with a sketch that tries to wait for input like this:
Code:
void setup() {
  Serial.begin(115200);
  Serial.println("Type any character to start");
  while (Serial.available() == 0) {}
}
int n = 0;
void loop() {
  Serial.println(++n);
}
The output is:
Quote
Êa
F’š
1
42
j
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
ÿType any character to start
Logged

California
Offline Offline
God Member
*****
Karma: 14
Posts: 629
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this happen at 9600?  Does "while (!Serial)" do anything?
Logged


France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 266
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In my case, it happens at 112500 bauds. I could change easily my software which consist to make dialog a Java GUI on MacBook Air with my Mega or my Due but i don't feel loosing anymore time on this since it is obvious in this thread or other recent related threads the Serial.libs function in 1.5 IDE are the root cause of this issue... Sorry !
Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1604
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It happens at 9600.

"while (!Serial)" does nothing since the Due library always return true:

Code:
    operator bool() { return true; }; // UART always active

I spent many hours because of this bug.  Many of my test examples for SdFat have somthing like this:
Code:
  Serial.println("Type any character to start");
  while (!Serial.available());
Examples that write to the SD were getting a corrupt file system.  This happens because opening the serial monitor causes some junk input.  The program then executes for about 30 ms before a reset happens.  This is just enough time to have the reset happen during a critical sequence of writes to the SD.

I thought I had a bug in the new Due version of SdFat that caused the problem.
Logged

France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 266
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

P.S. Another thing that I've noticed, maybe i'm not doing something right with IDE but i've noticed two differences between same code compile/load on MEGA versus on DUE:
  • both compilation times seem similar
  • downloading into DUE takes more than 10 or 20 times than downloading into MEGA

Do you think this could be related to the same issue we're discussing here ?
Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1604
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The only connection between download time and this problem is the ATmega16U2.  It handles the download and the USB/serial communication to the serial monitor.

I tried adding some error checking to the UART driver.  I rejected characters with framing or overrun errors but it didn't help my problem.  I still get at least one junk character when I bring up the serial monitor.

I think I have a work around for what I need.  This seems to prevent the problem of my examples executing on junk input:

Code:
void setup() {
  Serial.begin(9600);
  while (!Serial) {}  // wait for Leonardo
  Serial.println("Type any character to start");
  while (Serial.read() <= 0) {}
  delay(200);  // Catch Due reset problem
  // assume the user typed a valid character and no reset happened
}
Logged

Forum Administrator
Milano, Italy
Offline Offline
Sr. Member
*****
Karma: 23
Posts: 292
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It seems a bug in the firmware of the 16u2 that does USB-2-serial conversion.

https://github.com/arduino/Arduino/pull/1267

Can you check if the updated firmware above solves the issues?
Logged

C.

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1328
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 What method is the best way to load that hex file? I have the program called Flip but, I am not sure if I am using it correctly because, it does seem to want to connect to the 16u2.
Logged


Pages: [1] 2   Go Up
Jump to: