Go Down

Topic: Using TX3 and RX3 ports (Read 1 time) previous topic - next topic

chutki

Hello ,


The code below does not work. What is the mistake

Code: [Select]
void setup() {
  // put your setup code here, to run once:
Serial3.begin(9600);

}

char rx_byte = 0;

void loop() {
  // put your main code here, to run repeatedly:
if(Serial3.available())
{
  rx_byte = Serial3.read();
  Serial3.write (rx_byte);
}
}


Instead of using Serial3 , if I use Serial it works. How can I enable the Serial3 ports ?

pert

The code below does not work. What is the mistake
Explain exactly what you mean by "does not work".

chutki

In the serial monitor, In the serial command box if I type anything , it should be echoed . It happens with Serial but echoing does not happen with SErial3

pert

Do you have a USB to serial adapter connected to the RX3 and TX3 pins?

Did you select the COM port of that USB to serial adapter from the Tools > Port menu?

chutki

Oh thank you so much ! I did not know that I have to Open COM3. It now works thanks a lot. I have another question , how can I change the clock frequency in DUE. Its default is 4 mhz. I want it at 16mhz

chutki

Do you have a USB to serial adapter connected to the RX3 and TX3 pins?

Do you have a USB to serial adapter connected to the RX3 and TX3 pins?

Now I have connected DUE to a sensor. The sensor works with UART communication. So i have connected the tx-rx and rx-tx respectively. The sensor values are read on Serial port. For, serial3 port, I donot have a adapter instead I have connected reset and ground . I tried doing the same as serial port, but I cannot read anything from sensor on serial3 .

pert

It's still not clear what you're trying to do. Please provide more details. I know you know what you're talking about but try to read it from the perspective from someone not familiar with your project, who can't see how you have things wired.

chutki

Sorry for the inconvenience. I have attached a picture of my wiring.
1. My sensor and DUE are connected via UART as like in picture and I read out the values of sensor via UART and get it displayed on the serial monitor.
2. Just like I connected with Serial port (Tx0 and Rx0) , I simply want to do the same as for Serial3 port like Tx3 and Rx3.
3. I don not have USB - serial adapter , so I found this https://forum.arduino.cc/index.php?topic=138832.0   , I followed the post #1

pert

OK, so you're actually just using the Due as a USB to serial adapter. For that purpose you should connect sensor RX to Due's Rx0 and sensor Tx to Due's Tx0. I know that seems wrong at a first glance but the pin labels are relative to the SAM microcontroller pins but you're not using the microcontroller, you're using the ATmega16U2 USB-serial chip, which is connected to 0 and 1 opposite to the labels on the pins (because it's connected to the SAM microcontroller RX-TX, TX-RX). So when you make the RX-RX, TX-TX connections between the due and the sensor you're actually doing Rx-Tx, Tx-Rx with the ATmega16U2.

I simply want to do the same as for Serial3 port like Tx3 and Rx3.
Is that because you have two sensors? You won't be able to do the "Due as USB-serial adapter trick with Serial3 because only Serial is connected to the ATmega16U2. For Serial3 you will need to do a standard Rx-Tx, Tx-Rx connection with the SAM microcontroller, don't hold it in reset, and upload a sketch that transfers data between Serial3 and Serial.

chutki

I wanted to know how to use the Serial3 port , there is only one sensor. So, the reset and ground connection should be removed and serial 3 to serial program is required. Ok fine. Thanks fr the clarity.

chutki

#10
Nov 07, 2017, 05:59 pm Last Edit: Nov 07, 2017, 06:08 pm by chutki
OK, so you're actually just using the Due as a USB to serial adapter. For that purpose you should connect sensor RX to Due's Rx0 and sensor Tx to Due's Tx0. I know that seems wrong at a first glance but the pin labels are relative to the SAM microcontroller pins but you're not using the microcontroller, you're using the ATmega16U2 USB-serial chip, which is connected to 0 and 1 opposite to the labels on the pins (because it's connected to the SAM microcontroller RX-TX, TX-RX). So when you make the RX-RX, TX-TX connections between the due and the sensor you're actually doing Rx-Tx, Tx-Rx with the ATmega16U2.
If suppose I want to use the Serial0 port as USB to serial adapter(I would also send some variables from the serial terminal window , that is transmission and reception  to be performed on the serial terminal) , in the sketch will this be enough ?

Code: [Select]

void setup() {
  // initialize serial ports
  Serial.begin(38400);
    // USB serial port 0
}

char read_byte = 0 ;        // stores received byte


void loop() {

   //testread
  // check for data byte on USB serial port
  if(Serial.available()) {
    // get byte from USB serial port
    read_byte = Serial.read();
    // send byte to serial port
    Serial.write(read_byte);
    delay(100);
   
  }


The problem I face is the values that I read fom the sensors are overwrittten. I am pretty confused if I am using the SAM controller or not. The SAM at default works in 4mhz and the sensor is at 16mhz. If I am using the DUE as a USB to serial adapter only , why is that the output on the serial port gets overwritten ? So the SAM's clock frequency affects the ATmega16U2 USB-serial chip and hence SAM must be configured as same as the sensor. Am I right ?

pert

Connect the sensor Tx-Rx3, Rx-Tx3 and then upload the following code

Code: [Select]

void setup() {
  // initialize serial ports
  Serial.begin(38400);  // USB serial port 0
  Serial3.begin(38400);
}

char read_byte = 0 ;        // stores received byte


void loop() {
   //testread
  // check for data byte on USB serial port
  if(Serial3.available()) {
    // get byte from USB serial port
    read_byte = Serial3.read();
    // send byte to serial port
    Serial.write(read_byte);
    delay(100);
  }   
}

chutki

#12
Nov 08, 2017, 09:58 am Last Edit: Nov 08, 2017, 10:03 am by chutki
I tired the code with two DUEs and it worked . When I connected the sensor , it did not work. Is there any method to connect the other serial ports(1,2,3) to USB ? It simply works only with Serial0 port.   

In My  Arduino IDE folder , I cannot find 'sam' library. how can I access the low level registers for the DUE ?

pert

What is the baud rate of the sensor. I just set it to 38400 because that's what you had in your other code but in the original post I see you had it set to 9600. You need to change this line to match the baud rate of the sensor's serial communication:
Code: [Select]
Serial3.begin(38400);
This line should match the baud rate setting in menu in the bottom right corner of Serial Monitor
Code: [Select]
Serial.begin(38400);  // USB serial port 0

Maybe it would be helpful if you told us what this sensor is.

chutki

Code: [Select]

void setup() {
  // initialize serial ports
  Serial.begin(38400);
  Serial3.begin(38400);    // USB serial port 0
}

char rx_byte = 0 ;        // stores received byte


void loop() {

   //testread
  // check for data byte on USB serial port
  if(Serial3.available()) {
    // get byte from USB serial port
    rx_byte = Serial3.read();
    // send byte to serial port
    Serial.write(rx_byte);
    delay(100);
   
  }



1. I connected tx1-rx and rx1-tx. My device is not a sensor but atmega controller. I expected that UART communication between any two devices will be the same and that is why I was asking about clocking of the DUE. My Atmega is working with 16 mhz (I mentioned this earlier as my sensor's working frequency)
2. I did not mention it earlier because I thougt it would be confusing for another person . I apologise.
3. I used 38400 , the code with 9600 is for two DUEs. The above code is the code I use.
 

Go Up