Differentiate Serial Port in Arduino Uno

Hi all. Good day to everyone
I have this mini project which the Arduino Uno need to receive print command from Raspberry Pi and then print out the text on serial printer via serial port
So the connection is
Raspberry Pi connect to Uno via USB Port
Serial Printer connect to Uno Pin 0 (Rx) and Pin 1(Tx)

However, in my source how do i differentiate these 2 serial.

Below is the sample Arduino code for reading command from Raspberry Pi

// USB - USB.ino

//
// Description:
// Executes commands retrieved from serial (USB) port.
//
// Created by John Woolsey on 12/17/2019.
// Copyright (c) 2019 Woolsey Workshop. All rights reserved.

void setup() {
Serial.begin(9600); // start serial communication at 9600 baud
}

void loop() {
// Read and execute commands from serial port
if (Serial.available()) { // check for incoming serial data
String command = Serial.readString(); // read command from serial port
if (command == "led_on") { // turn on LED
digitalWrite(LED_BUILTIN, HIGH);
} else if (command == "led_off") { // turn off LED
digitalWrite(LED_BUILTIN, LOW);
} else if (command == "read_a0") { // read and send A0 analog value
Serial.println(analogRead(A0));
}
}
}

Below is the sample Arduino code for printing via serial

[color=#333333][b]void loop() {[/b]

is_switch_press = digitalRead(SW); // Reading the Switch press status

if (is_switch_press == HIGH){
delay(debounce_delay); // debounce delay for button press
if(is_switch_press == HIGH){
digitalWrite(led, HIGH);
Serial.println("Hello");
delay(100);
Serial.println("This is a Thermal printer interface");
Serial.println("with Arduino UNO.");
delay(100);
Serial.println("Circuitdigest.com");
Serial.println ("\n\r");
Serial.println ("\n\r");
Serial.println ("\n\r");
Serial.println ("---------------------------- \n \r");
Serial.println ("Thank You.");
Serial.println ("\n\r");
Serial.println ("\n\r");
Serial.println ("\n\r");
digitalWrite(led, LOW);
}
}

else{
digitalWrite(led, LOW);
}
}[/color]

if i am combining these 2 code, it will be calling the same serial port. In this case how do i specify which serial is read (USB in this case) , which serial is to send print command (Tx and Rx Pin)

The two serials are the same on the Uno. RX and TX are connected to the ttl-to-usb converter.

So your approach will not work.

Look at SoftwareSerial to create a second serial port on an Uno.

PS
Please edit your post and replace the quote tags by code tags.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.