But this only have 1 serial port to connect your serial data devices, Serial1.
But I found a way around it, I got Serial2 to work, Serial3 and Serial4 is not possible right now but I will find out if its possible.
Here is a simple code for sending Serial2 data to serial port and make it possible to send data to serial2 from serial port.
I used an Adafruit Ultimate GPS for this testing.
Code: Select all
// Serial2 pin and pad definitions (in Arduino files Variant.h & Variant.cpp)
#define PIN_SERIAL2_RX (34ul) // Pin description number for PIO_SERCOM on D12
#define PIN_SERIAL2_TX (36ul) // Pin description number for PIO_SERCOM on D10
#define PAD_SERIAL2_TX (UART_TX_PAD_2) // SERCOM pad 2
#define PAD_SERIAL2_RX (SERCOM_RX_PAD_3) // SERCOM pad 3
// Instantiate the Serial2 class
Uart Serial2(&sercom1, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX);
void setup()
{
Serial.begin(115200);
Serial2.begin(9600); // Begin Serial2
}
void loop()
{
if (Serial.available())
{
byte byteRead2 = Serial.read();
Serial2.write(byteRead2);
}
if (Serial2.available()) // Check if incoming data is available
{
byte byteRead = Serial2.read(); // Read the most recent byte
Serial.write(byteRead); // Echo the byte back out on the serial port
}
}
void SERCOM1_Handler() // Interrupt handler for SERCOM1
{
Serial2.IrqHandler();
}
It also sets the GPS to 1Hz update and RMC +GGA data.
Code: Select all
// Serial2 pin and pad definitions (in Arduino files Variant.h & Variant.cpp)
#define PIN_SERIAL2_RX (34ul) // Pin description number for PIO_SERCOM on D12
#define PIN_SERIAL2_TX (36ul) // Pin description number for PIO_SERCOM on D10
#define PAD_SERIAL2_TX (UART_TX_PAD_2) // SERCOM pad 2
#define PAD_SERIAL2_RX (SERCOM_RX_PAD_3) // SERCOM pad 3
// Instantiate the Serial2 class
Uart Serial2(&sercom1, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX);
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(115200);
Serial2.begin(9600);
delay(1000);
Serial2.println("$PMTK220,1000*1F"); // 1 Hz
delay(100);
Serial2.println("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28"); // RMCGGA
delay(100);
Serial2.println("$PMTK286,1*23");
delay(100);
Serial2.println("$PGCMD,33,0*6D");
delay(100);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
serialEvent();
// print the string when a newline arrives:
if (stringComplete) {
Serial.print(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
void SERCOM1_Handler() // Interrupt handler for SERCOM1
{
Serial2.IrqHandler();
}
void serialEvent() {
while (Serial2.available()) {
//SerialUSB.println("Serial1 available");
// get the new byte:
char inChar = (char)Serial2.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
//SerialUSB.println("String is complete");
}
}
}