Serial.read problems with Arduino Due

the following line was perfectly working in Arduino Uno.

...
byte LVcmd[11]; // 11 Bytes of command from host
...
for (int i=0; i<11; i++)
{
LVcmd = Serial.read(); // Build Command From Serial Buffer
}
...
Now, with the Arduino Due, it seems not working (checked the content of Lvcmd...).
Have I to take care of something new ?

Add code tags to your code. Try this:

...
byte LVcmd [11];
...
for(int i=0;i<11;i++)
{
LVcmd[i]=Serial.read();
}
...

..sorry...

it was already with the tags, just made the error in the post.
In the sketch is already programmed correctly, but still does not work.

Absolutely not working.

I have also some doubts regarding the setting of the port.

9600, 8, n, 1 or 9600, 8, n, 2 ?

Really annoying.
May be the Due has still to many problems and I have to wait for new releases....

During these last tests it looks that the transmission of data to the Arduinoi Due resets the hardware.

I have in fact a confirmation character sent back only from void stup() to check the initialization and every many time I send a string of data to the USB port this character is sent (not always, only sometime).

I am thinking that for some unknown reasons the serial port is resetting the hardware.

Is there any interrupt or timer involved in Serial ?
My sketch uses the TC1 Ch 0 and TC3_IRQn

Any help is welcome. Thanks.

Possibly something that is not correct on either the Due or the PC
side of the USB connection.

Does the Native USB port get recognized by the PC?
As what, a virtual Comm Port?
How are you setting up the PC side, with a dumb terminal program?

Can you post a short setup and loop so others can test,
and examine your initialization of the Serial I/O in the Due?

For example, what library are you using?

The port is recognized by the PC, in fact I am using it to program the Due.
The sketch is really complex (but working in Arduino Uno) and I am adapting it for the Due.
I am using the standard serial library that comes with the IDE 1.5.2

I will simplify the sketch to isolate the problem, but for the moment I am suspecting an hardware reset when the USB of the Due receives data from the PC.

Notice that the Serial.print("K"); is written only in the setup loop, but sporadically it arrives also in the interfaced PC.

The PC runs with a standard LabView application from the National Instruments examples and it works properly.

// This is the setup:

void setup(void) {
Serial.begin(9600); // Baud Rate 1 Mbps
while (!Serial) {};
delay(2000);
Serial.print("K");
Serial.flush();
pinMode(4,OUTPUT); // Pin 4 - DCC Signal
startTimer(TC1, 0, TC3_IRQn, 19200); // TC1 channel 0, IRQ and 58 us
}

// This is the interrupt handler:

void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency)
{
pmc_set_writeprotect(false);
pmc_enable_periph_clk((uint32_t)irq);
TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK1);
uint32_t rc = VARIANT_MCK/2/frequency;
TC_SetRA(tc, channel, rc/2);
TC_SetRC(tc, channel, rc);
TC_Start(tc, channel);
tc->TC_CHANNEL[channel].TC_IER=TC_IER_CPCS;
tc->TC_CHANNEL[channel].TC_IDR=~TC_IER_CPCS;
NVIC_EnableIRQ(irq);
}

The problem is not related only to the Serial.Read, but in the serial communication at whole.

Neither this simple program works, or, it works with the IDE monitor, but not with the interfaced PC.
In the interfaced PC runs a very simple program from the examples of LabView, test many and may times...it works with everything, but not with Arduiino Due.
Tried to set in many way the port, such as 9600, 1, n, 1 etc, but never found the way to communicate.
What I receive from Arduino Due is alway a "K" or a "W" and sometime some trash.
Notice that the "K" is sent only at the setup loop.
The Led in pin 13 goes ON automatically (never set ON in the sketch...) every time I send a byte

Definitively, every time I send a byte, the Arduino Due resets it self !

What a hell is happening ?

//******************************************************************************************
// Setup (executed only one time
//******************************************************************************************
void setup(void)
{
Serial.begin(9600); // Baud Rate test may values as 1200, 115200, 9600 etc..., no one works
while (!Serial);
pinMode(13,OUTPUT); // This pin is ON at power on (?) and it goes on every time I send a byte with the PC, I can reset it via pin input 10
pinMode(10,INPUT); // Just for test, used to resetpin output 13 (led) that is on by it self (?)
Serial.print("K"); // This print is in setup loop, then sent only one time, but it appears time by time when I send bytes from the PC
Serial.flush();
}

//******************************************************************************************
// Main Loop
//******************************************************************************************
void loop(void)
{
if(Serial.available() >0)
{
Serial.print("W");
}
if (digitalRead(10) == 0) {digitalWrite(13,0);}
delay(100);
}

Have you read about R23 wrong value causing resets?
Maybe this is the problem?? :open_mouth:

http://forum.arduino.cc/index.php?topic=167492.msg1366523#msg1366523

My Due can upload the sketch, but has problems during the communication with the PC.
May be this is the problem, but I do not know how to find the R23 in the layout.
My Arduino Due is Rev3.

Where can I find the layout and schematic of the Due ?

found the drawings and located the R23.
jumped the R23, but still the problem remains.

I am definitively convinced: the Arduino Due resets it self when a byte arrives in the serial port.
Remark: downloading the sketch is not a problem, only the communications with the PC generates the reset.

May be there is one way to disable the master reset...?...

Hello giubio51,
Normally, when you wait for serial data is to read it, but if you only want to write 'W' each time a character is sent via serial, a good way to empty Serial.available() is using Serial.read()as follows:

//******************************************************************************************
// Setup (executed only one time
//******************************************************************************************
void setup(void)
{
  Serial.begin(9600);         // Baud Rate test may values as 1200, 115200, 9600 etc..., no one works
  while (!Serial);
  pinMode(13,OUTPUT);    // This pin is ON at power on (?) and it goes on every time I send a byte with the PC, I can reset it via pin input 10
  pinMode(10,INPUT);       // Just for test, used to resetpin output 13 (led) that is on by it self (?)
  Serial.print("K");            // This print is in setup loop, then sent only one time, but it appears time by time when I send bytes from the PC
  Serial.flush();        
}

//******************************************************************************************
// Main Loop
//******************************************************************************************
void loop(void)
{
  if(Serial.available() >0)
  {
    Serial.read();
    Serial.print("W");
    delay(200);
  }
  if (digitalRead(10) == 0) {digitalWrite(13,0);}
  delay(100);
}

Regards!

Solved the problem !

...and I have to admit it was a my big mistake in the LabView side.
Instead of looping with the read and write operations, I was sending also a reset of the port (VISA port clear) at every loop in the LabView side.
This instruction was resetting also the Arduino Due.
By clearing the port at the first loop and then operating with serial read and write only, the problem disappeared.

Now the communications are working at full speed without problems.

Sorry for the wrong considerations regarding the Arduino Due.
This time the fault was mine.

Thanks to everybody has helped me.