Serial Hardware and Serial Software do not work

HSEY

Member
Code:
I have two Teensy 4.0 microcontrollers. The digital and analog pins can be read and written,

but the serial hardware and serial software are not working.

The programs I'm using, e.g., Tiny GPS, run perfectly on an Arduino Mega.

What am I doing wrong?
 
Code:
I have two Teensy 4.0 microcontrollers. The digital and analog pins can be read and written,

but the serial hardware and serial software are not working.

The programs I'm using, e.g., Tiny GPS, run perfectly on an Arduino Mega.

What am I doing wrong?
This is what you need to do: Show us your code.
 
On Teensy 4.x, Serial is the USB serial port
Hardware serials are Serial1, Serial2, ....
Which one is used in your code ?????
 
This is what you need to do: Show us your code.
Arduino Exambel
Code:
```cpp
/*
 * TimeGPS.pde
 * example code illustrating time synced from a GPS
 *
 */

#include <TimeLib.h>
#include <TinyGPS.h>       // http://arduiniana.org/libraries/TinyGPS/
//#include <SoftwareSerial.h>
// TinyGPS and SoftwareSerial libraries are the work of Mikal Hart

//SoftwareSerial SerialGPS = SoftwareSerial(10, 11);  // receive on pin 10
TinyGPS gps;

// To use a hardware serial port, which is far more efficient than
// SoftwareSerial, uncomment this line and remove SoftwareSerial
#define SerialGPS Serial1

// Offset hours from gps time (UTC)
const int offset = 1;   // Central European Time
//const int offset = -5;  // Eastern Standard Time (USA)
//const int offset = -4;  // Eastern Daylight Time (USA)
//const int offset = -8;  // Pacific Standard Time (USA)
//const int offset = -7;  // Pacific Daylight Time (USA)

// Ideally, it should be possible to learn the time zone
// based on the GPS position data.  However, that would
// require a complex library, probably incorporating some
// sort of database using Eric Muller's time zone shape
// maps, at http://efele.net/maps/tz/

time_t prevDisplay = 0; // when the digital clock was displayed

void setup()
{
  Serial.begin(9600);
  while (!Serial) ; // Needed for Leonardo only
  SerialGPS.begin(4800);
  Serial.println("Waiting for GPS time ... ");
}

void loop()
{
  while (SerialGPS.available()) {
    if (gps.encode(SerialGPS.read())) { // process gps messages
      // when TinyGPS reports new data...
      unsigned long age;
      int Year;
      byte Month, Day, Hour, Minute, Second;
      gps.crack_datetime(&Year, &Month, &Day, &Hour, &Minute, &Second, NULL, &age);
      if (age < 500) {
        // set the Time to the latest GPS reading
        setTime(Hour, Minute, Second, Day, Month, Year);
        adjustTime(offset * SECS_PER_HOUR);
      }
    }
  }
  if (timeStatus()!= timeNotSet) {
    if (now() != prevDisplay) { //update the display only if the time has changed
      prevDisplay = now();
      digitalClockDisplay(); 
    }
  }
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits) {
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


```
The program doesn't work with Serial1...

nor with SoftwareSerial on the Teensy.
 
A test program for serial communication between two SEEED XIAO devices works, but not with XIAO and Teensy!
 
Try this code.

Code:
#define GPS_SERIAL Serial1

// Very basic sketch to monitor GPS data from GPS_SERIAL
void setup() {
  GPS_SERIAL.begin(9600);
  Serial.begin(9600);
}

void loop() {
  // while there's data from the GPS, send it to the computer
  while(GPS_SERIAL.available() > 0) {
    Serial.write(GPS_SERIAL.read());
  }
  // while there's data from the computer, send it to the GPS
  while(Serial.available() > 0) {
    GPS_SERIAL.write(Serial.read());
  } 
}

With an Adafruit Ultimate GPS on Serial1 (T4), it continuously prints output from the GPS, such as this:
Code:
$GPGGA,163847.000,52xx.5645,N,106xx.7871,W,1,9,0.90,513.5,M,-20.6,M,,*62
$GPGSA,A,3,04,29,09,26,03,31,07,16,11,,,,1.19,0.90,0.78*0E
$GPRMC,163847.000,A,52xx.5645,N,106.7871,W,0.19,350.12,171225,,,A*74
$GPVTG,350.12,T,,M,0.19,N,0.36,K,A*35
$PGTOP,11,3*6F

Pete
 
Your code works on my GPS+T4 when I change the baud rate to 9600 for my GPS.
Are you sure that your GPS is set to 4800?
Maybe try:
Code:
  SerialGPS.begin(9600);

Pete
 
Code looks like it should work. Serial1 on Teensy definitely does work at 4800 baud.

My guess is a problem with wiring. Please show us photos of how you've connected the hardware. If there's a mistake or misunderstanding, maybe we'll be able to spot the problem when we're able to see the wiring.
 
SoftwareSerial however, only really works in 1.60-beta.

On version 1.59 and earlier, SoftwareSerial tries to use regular hardware serial if you choose 2 pins which are a real serial port. Otherwise, the software-only emulation basically did not work on Teensy 4.x. Since we have 7 real serial ports on Teensy 4.0 and 8 ports on Teensy 4.1, fixing this was never really a priority.

However, a working software emulation was finally added for 1.60. It's still in beta testing today. The beta versions should work with all programs that use Arduino's SoftwareSerial library, regardless of which pins you choose. But just like regular Arduino SoftwareSerial, it really is the software emulation that hogs a lot of CPU time and tends to interfere with other libraries needing interrupts.

Recent versions also try to make FlexIO_t FlexSerial compatible with libraries that use hardware serial. FlexIO serial ports really are hardware. They don't have FIFOs, so not as efficient as Teensy's regular hardware serial. But most Arduino boards have non-FIFO serial, so with FlexIO you get serial ports of similar capability to traditional Arduino. The one gotcha with FlexIO is a special config is needed for slower baud rates (less than 115200 baud). That library's examples show how to do it.
 
The problem is solved, thank you to all members for your help.

It's likely that the serial ports with their low transmission speeds are causing problems (and you shouldn't really be using them anyway!).

Everything works perfectly with 9600.

Thanks again!
 
Back
Top