Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 9 of 9

Thread: Teensy3.6 serial communication.. issue ?

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    15

    Teensy3.6 serial communication.. issue ?

    Hello,

    I need to ask how to read serial data on teensy 3.6
    I am working on a task to read GPS data that is available on the serial coming from mini pc. I have to read the data from serial and save it in SD-storage. At this point, I have not purchased GPS module wondering if I could make fake GPS data by some other controller(Arduino UNO maybe) on serial and read that data from teensy 3.6. In doing this effort, I am able to send fake data on serial using Arduino but when I tried to read from serial using teensy3.6 it does not open the com port.

    I believe it's because of the same serial port I'm writing from Arduino and reading through teensy but I'm not sure. any help would be appreciated and if there is any other way of practicing this task to read GPS data from teensy serial please do suggest.

    Thank you

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,556
    If you have a short sample of your code we could spot the problem - maybe this is it.

    Teensy uses Serial exclusively for USB Serial communications, it does not share a UART port like many Arduino boards do as it has native USB hardware.

    For UART Serial - there are multiple numbered ports referenced by Serial1.print() or Serial2.read() - based on the Teensy card with pins labeled Rx1/Tx1 or Rx2/Tx2 …

    If you look up Serial.print() [?] on the Arduino reference pages it will show exceptions for some boards MEGA and others where they have multiple ports - those multiple port notes IIRC generally indicate how Teensy works as well.

    Also since Teensy has multiple UARTS - You can use Serial2.print( " GPS MESSAGE HERE " ); for instance to write to Serial1.read() where you can receive the message by wiring Serial2 Tx to Serial1 Rx and vice versa.
    Last edited by defragster; 05-24-2019 at 08:47 AM.

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Posts
    15
    Serial reading script for teensy

    Code:
    String inputString = "";         // a String to hold incoming data
    bool stringComplete = false;  // whether the string is complete
    
    void setup()
    {
      Serial.begin(9600);
      inputString.reserve(200);
    }
    
    void loop()
    {
      if (stringComplete)
      {
        Serial.println(inputString);
        inputString = "";
        stringComplete = false;
      }
    }
    
    void serialEvent()
    {
      while (Serial.available())
      {
        char inChar = (char)Serial.read();
        inputString += inChar;
        if (inChar == '\n')
        {
          stringComplete = true;
        }
      }
    }
    Fake GPS data generation script
    Code:
    float lat = 37.4980608;
    float lon = 126.9653503;
    char str1[15];
    char str2[15];
    
    void setup()
    {
      Serial.begin(9600);
    }
     
    void loop()
    {
      sprintf(str1, "%.7f", lon);
      sprintf(str2, "%.7f", lat);
      Serial.print(str1);
      Serial.print("\t");
      Serial.println(str2);
    }

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,556
    Quote Originally Posted by Rakeh View Post
    Serial reading script for teensy
    All of those references are to Serial - the USB connection to the PC Serial monitor.

    To use a UART COM port on Teensy starts with Serial1.begin( 115200 ); or Serial2.begin( 2000000 );

    Notes in p#2 suggested that.

    Here is a note from https://www.arduino.cc/en/Serial/Begin - it is called out for MEGA not DUE as was noted in post #2 - this is valid and proper use on Teensy 3.6 where the Teensy 3.6 actually has six ports is the bottom pads are used.
    Arduino Mega example:

    // Arduino Mega using all four of its Serial ports
    // (Serial, Serial1, Serial2, Serial3),
    // with different baud rates:

    void setup(){
    Serial.begin(9600);
    Serial1.begin(38400);
    Serial2.begin(19200);
    Serial3.begin(4800);

    Serial.println("Hello Computer");
    Serial1.println("Hello Serial 1");
    Serial2.println("Hello Serial 2");
    Serial3.println("Hello Serial 3");
    }

    void loop() {}

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,556
    There is an IDE / Example for Serial under EchoBoth that takes input from USB Serial the Serial Monitor and output it to Serial1 and Vice Versa that may give a way to see the code.

    on disk in Arduino install as : "...\examples\Teensy\Serial\EchoBoth\EchoBoth. pde"

    Under IDE Files / Examples ...

  6. #6
    Junior Member
    Join Date
    Mar 2019
    Posts
    15
    Thank you for the suggestion and I'll have a look at the code and see. Hopefully, it works and I'll get back to you then once I'm done some testing the basic scripts

  7. #7
    Junior Member
    Join Date
    Mar 2019
    Posts
    15
    I have tried the mentioned example and another sketch I found for serial comm. between two Teensy. I have used T3.6 for sender and receiver both sides with serial 1 and serial 3 examples I found somewhere on the forum. It is working but I have a question, why do I need a serial1 and serial3 connection to make hardware connection. Why not I simply read the data through serial because my end task is to get GPS coordinates along with other data from the drone through serial comm. connected with a USB cable with the T3.6.If this is the case, is it possible or not and how to achieve this goal.

    Secondly, I was getting a small error while generating fake data as I was writing fake co-ordinates with 7 or above decimal point but the output always gives me right data till 5 decimal point and after that, it has an error. I don't know what is the issue, is this because of T3.6 float precision or something else. And is there any other way around to cope with the error.

    Below is the script for both sender and receiver MCUs
    Looking forward to your kind suggestion.

    Thank you

    Sending code for gps coordinates
    Teensy3.6


    Code:
    double lon = 126.9653503;
    double lat = 137.4980608;
    char str1[21];
    
    void setup()
    {
      Serial3.begin(115200);
      Serial.begin(115200); // Config serial port (USB)
      
      while(!Serial);
      while(!Serial3);
    
      Serial.println("Sending gps data");
    }
    
    void loop()
    {
      sprintf(str1, "%.7f%.7f", lon, lat);
      Serial.println(str1);
      Serial3.write(str1);
      Serial3.flush();
      delay(5000);
    }
    Receiver Code for gps coordinates
    Teensy 3.6


    Code:
    char buf1[11];
    char buf2[10];
    
    void setup()
    {
      Serial.begin(115200);
      Serial1.begin(115200);
    
      while(!Serial);
      while(!Serial1);
    }
    
    void loop()
    {
      if (Serial1.available())
      {
        Serial1.readBytes(buf1, 11);
        Serial1.readBytes(buf2, 10);
        Serial.print(buf1);
        Serial.print("\t");
        Serial.println(buf2);
        }
    }

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,556
    For test purposes you could just store and print the value as a string :: char lat[] = "137.4980608";

    T_3.5 and 3.6 FPU is float so default was made float without override - IIRC using this : double lat = 137.4980608L;

    But then I think sprint() may fail and you have to use the proper dtostr() to convert each to a string.

  9. #9
    Junior Member
    Join Date
    Mar 2019
    Posts
    15
    I believe I have to stick with the most basic approach to store in the array and then send it over serial although sometimes it sends garbage characters once or twice sometimes the other methods did not work.
    I have tried sprintf with float and double data types and I tried itoa and similarly now dtostr too but they all have a limit for 5 digits in decimal and above they give the error.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •