Arduino Forum

Products => Arduino Due => Topic started by: szekim on Sep 17, 2018, 06:09 pm

Title: Arduino Duo Serial.available not working
Post by: szekim on Sep 17, 2018, 06:09 pm
I got a Duo, which have 4 UARTS port, I connect all these port to TTL to RS232 convertor, and it's working fine for Serial1, Serial2 and Serial3.

Code below is working fine, same to Serial2 and Serial3.
if(Serial1.available() > 0 ){
   Serial.print("from Serial 1");
}

However, the code below is not working
if (Serial.available() > 0 ) {
   Serial1.print("from serial");
}

I'm not sure what wrong is it, I spend like a couple of days for it, but couldn't find an anwer, somebpdy please help me, thank you.
Title: Re: Arduino Duo Serial.available not working
Post by: ard_newbie on Sep 17, 2018, 06:20 pm

Post all your code, between code tags.
Title: Re: Arduino Duo Serial.available not working
Post by: pjrc on Sep 17, 2018, 11:08 pm
"Serial" counts as 1 of the 4 ports.


Quote
I'm not sure what wrong is it, I spend like a couple of days for it, but couldn't find an anwer, somebpdy please help me,
The problem is almost certainly a mistake or misunderstanding in the code or wiring you've used.  But you have shown us only tiny fragments of code and we can't see how you actually connected the hardware.  How is anyone supposed to know what was done incorrectly by only blind guessing?
Title: Re: Arduino Duo Serial.available not working
Post by: szekim on Sep 18, 2018, 02:06 am
Hi all, here is my code

void setup() {
  Serial.begin(9600);
  SerialUSB.begin(9600);
  Serial1.begin(9600);

}

void loop() {                                                                                                                                     
  if (Serial.available() > 0 ) {
    Serial1.print("from serial available");
    SerialUSB.print("from serial available");
  }

  if(SerialUSB.available() > 0 ){
 
    Serial1.print("from serialUSB available");
    Serial.print("from serialUSB available");
  }

  if(Serial1.available() > 0 ){
    SerialUSB.print("from Serial 1 available");
    Serial.print("from Serial 1 available");
  }

  delay(1000);
}

void serialEvent(){
  Serial1.print("from serial event");
    SerialUSB.print("from serial event");
}

At this point, I just wanted to print something whenever "Serial", port 1 is receive something.

I also upload the my wiring connection, basically I use TTL to RS232 convertor and use my laptop to send/receive the data

Title: Re: Arduino Duo Serial.available not working
Post by: ard_newbie on Sep 18, 2018, 06:51 am

A DUE has 5 UART: Serial, Serial1, Serial2, Serial3 and a Serial4 by adding 3 lines of code. You can test your serial connections with this example sketch on a single DUE board:

Code: [Select]

/**********************************************************************/
/******   Hook jumpers between RX1 and A11, and TX1 and pin 52  *******/
/**********************************************************************/

// Use the Arduino core to set-up the unused USART2 on Serial4
RingBuffer rx_buffer5;
RingBuffer tx_buffer5;
USARTClass Serial4(USART2, USART2_IRQn, ID_USART2, &rx_buffer5, &tx_buffer5);
void USART2_Handler(void)   // Interrupt handler for UART2
{
  Serial4.IrqHandler();     // In turn calls on the Serial4 interrupt handler
}

char c = 0;
void setup() {
  PIO_Configure(PIOB, PIO_PERIPH_A, PIO_PB20A_TXD2 /* | PIO_PB21A_RXD2*/, PIO_DEFAULT);
  Serial.begin(250000);
  Serial1.begin(5000000);
  Serial4.begin(5000000);

  Serial4.print("Hello");
}


void loop() {
  String s;
  s = "";

  while (Serial1.available() > 0) {
    c = Serial1.read();
    s += c;
  }
  if (s.length() > 0) {
    Serial.println(s);
    Serial4.print(s);
  }
  delay(1000);
}




Title: Re: Arduino Duo Serial.available not working
Post by: szekim on Sep 18, 2018, 08:10 am
Hello ard_newbie, thanks for your answer, can you advise the wiring for the Serial4?
Title: Re: Arduino Duo Serial.available not working
Post by: szekim on Sep 19, 2018, 04:20 am
Hi all, I use the same code and same wiring on Arduino Mega, everything is working fine, thanks to all who reply my question.