Go Down

Topic: Using Serial1 to read on the serial monitor. (Read 140 times) previous topic - next topic

edrickp

Hi guys.

Can anyone help me with the code to read Serial1 on the serial monitor?
I am using an Adafruit Ultimate GPS to read the data on Serial1.
The GPS module can be used for direct computer wiring but it's using serial port digital 0 and 1 for RX and TX, but how can I use the code for direct computer wiring to read on the serial monitor?


void setup() {
  // put your setup code here, to run once:
  //initialize both serial ports
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  //read from port 1, send to port 0
  if (Serial1.available()){
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  //read from port 0, send to port 1
  if (Serial.available()){
    int inByte = Serial.read();
    Serial1.write(inByte);
  }
}

lesept

#1
Apr 24, 2019, 10:43 am Last Edit: Apr 24, 2019, 10:44 am by lesept
What Arduino board are you using? A Mega?
Usually, digital pins 0 and 1 are connected to the USB port so that your board talks to the computer using Serial. If you want to connect Serial1 to the serial monitor, read this

But I beleive you can connect your GPS onto another port than Serial :
GPS -- Serial1
USB -- Serial
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

edrickp

What Arduino board are you using? A Mega?
Yes I am using A Mega.
The A Mega can connect the GPS to another serial using SoftwareSerial, but is there any other way to connect without using SoftwareSerial?

edrickp

Here.

In this pdf there's a way to connect using a direct wiring, but on A Uno, there's only 1 serial connection which is connected directly to the USB.

I think that to connect to Serial1 on A Mega, I will need USB to TTL serial cable. Is that true?

lesept

I don't know which GPS module you're using. But if it has RX and TX connectors, then I guess you can connect it directly to Serial1. On an Arduino Mega, there are several hardware serial ports:
  • Serial 1: 19 (RX) and 18 (TX)
  • Serial 2: 17 (RX) and 16 (TX)
  • Serial 3: 15 (RX) and 14 (TX)
You can use whichever you want to connect your GPS module. Connect like this
  • Arduino RX <--> GPS TX
  • Arduino TX <--> GPS RX

I found this here, try to connect your GPS to port 1 (pins 18 & 19) and upload this sketch: does it display anything ?
Code: [Select]
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.print("Test");
}

void loop() {
  if (Serial1.available()) {
    byte inByte = Serial1.read();
    Serial.print(inByte, BYTE);
  }
  delay(100);
}

Also look at the answers #11 (points to a website with GPS code for Mega) and #14 (points to a GPS library suited for Mega).
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

sterretje

I think that to connect to Serial1 on A Mega, I will need USB to TTL serial cable. Is that true?
Basically yes. But you might run into issues. A RX/TX swap is needed.

Maybe start with what you want to achieve?

Do you want to feed the gps data to both the mega and the pc? Or only to the pc (no Arduino required).

Do you want to be able to send data from the pc to the gps?
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

edrickp

does it display anything ?
Yes it displays the incoming bytes:
Test367180717165445051 and so on.

The GPS module I am using is Adafruit Ultimate GPS which I posted on the link.

If I connect it directly to computer wiring which is on pin 0 (RX) and 1 (TX) it would be like:
$GPGGA, 000014.799, , , , , 0, 00, , , M, , M, , *7A
$GPGSA, A, 1, , , , , , , , , , , , , , , *1E
$GPRMC, 000014.799, V, , , , , 0.00, 0.00, 060180, , , N*40
$GPVTG, 0.00, T, , M, 0.00, N, 0.00, K, N*32


So this type of data is what I want to get on Serial Monitor using the Serial1 pin which is pin 18(TX1) and pin 19 (RX1). Is that possible?

Thank you for your help Lesept.

edrickp

Basically yes. But you might run into issues. A RX/TX swap is needed.

Maybe start with what you want to achieve?

Do you want to feed the gps data to both the mega and the pc? Or only to the pc (no Arduino required).

Do you want to be able to send data from the pc to the gps?
What I want to achieve is to get the GPS data such as GPRMC, GPGGA, and the others from the GPS to my serial monitor using Serial1 pins.

sterretje

So you don't need an Arduino; just a USB-to-TTL converter.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

edrickp

I need the arduino because I want to use the Serial1 and plot it to the serial monitor just like the Serial (pins 0 and 1).

lesept

I don't have such a GPS module, so I don't know what those GP*** mean, but the library NeoGPS seems to handle them. Have a look at the Readme file and the NMEAsimple sketch
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

Go Up