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

Thread: SoftwareSerial Serial1 Errors

  1. #1

    SoftwareSerial Serial1 Errors

    Hello,
    I am trying to use the TF03 LiDAR module with the Teensy 4.1 board. The TF03 uses UART to communicate, so I was wanting to use SoftwareSerial Serial1. After reading some post on here, I changed the following code which works perfectly on an Arduino UNO board:

    Code:
    //Works with UNO board
    #include <SoftwareSerial.h>
    
    SoftwareSerial Serial1(2, 3);
    int dist;
    int strength;
    int check;
    int uart[9];
    int i;
    const int HEADER=0x59;
    
    void setup(){
        Serial.begin(115200);
        Serial1.begin(115200);
       
    }
    
    void loop(){
      if(Serial1.available()){
        if(Serial1.read()==HEADER){
          uart[0]=HEADER;
          if(Serial1.read()==HEADER){
            uart[1]=HEADER;
            for(i=2;i<9;i++){
              uart[i]=Serial1.read();
            }
            
            check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];
            if(uart[8]==(check&0xff)){
              dist = uart[2]+uart[3]*256;
              strength = uart[4]+uart[5]*256;
              Serial.print("Distance: ");
              Serial.print(dist);
              Serial.print('\t');
              Serial.print("Strength: ");
              Serial.print(strength);
              Serial.print('\n');
              delay(100);
            }
          }
        }
      }
    }
    And I modified it using the tips found in THIS post. Below is that attempt, but nothing comes out of the serial monitor with this code:

    Code:
    #include <SoftwareSerial.h>
    #define txPin 1
    #define rxPin 0
    SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
    
    
    int dist;
    int strength;
    int check;
    int uart[9];
    int i;
    const int HEADER=0x59;
    
    void setup(){
      pinMode(rxPin, INPUT);
      pinMode(txPin, OUTPUT);
        Serial.begin(115200);
        mySerial.begin(115200);
       
    }
    
    void loop(){
      if(mySerial.available()){
        if(mySerial.read()==HEADER){
          uart[0]=HEADER;
          if(mySerial.read()==HEADER){
            uart[1]=HEADER;
            for(i=2;i<9;i++){
              uart[i]=mySerial.read();
            }
            
            check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];
            if(uart[8]==(check&0xff)){
              dist = uart[2]+uart[3]*256;
              strength = uart[4]+uart[5]*256;
              Serial.print("Distance: ");
              Serial.print(dist);
              Serial.print('\t');
              Serial.print("Strength: ");
              Serial.print(strength);
              Serial.print('\n');
              delay(100);
            }
          }
        }
      }
    }
    Any help on how to achieve UART communication with the Teensy 4.1 board would be greatly appreciated,

    Thank you very much
    ~RandoRkt

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,553
    Software serial only works on tensy pins which are actually hardware serial pins… which pins 2 and 3 are not.

    Also Serial1 is already defined as a hardwareserial device.

    If possible usually best to use normal serial pins.

    May also have options with FlexIO but not at desk to check

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,022
    The first program looks like it should work if you just delete the SoftwareSerial stuff and connect the signals to the Serial1 RX1 and TX1 pins (digital pins 0 and 1).

  4. #4
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    9,244
    I would be surprised if it works.
    There is nowhere a check if really all characters were received.
    The only check is if something was received.
    But not how much.
    Of course, it could be that it works by chance. The unnecessary delay contributes to this, and the fact that read() returns -1 (0xff) if the buffer is empty. The delay kicks in if a single bit is set...

  5. #5
    Quote Originally Posted by KurtE View Post
    Software serial only works on tensy pins which are actually hardware serial pins… which pins 2 and 3 are not.
    Hey Kurt, thank you for replying, the first block of code is what works on the Arduino UNO board. I changed that sketch to work on the Teensy board, that's why it's pins 0 and 1 in that code block.

    I tried to remove the "SoftwareSerial stuff" as Paul suggested, but still there is nothing coming out of the serial monitor. Plus the logic breaks if I don't have "mySerial" defined somewhere to check for any incoming bits.

    And lastly, Frank, I got the original base code from HERE and have been trying to modify it ever since. I am new to this stuff, first time interfacing with UART, I'm trying to learn. Thank you
    ~RandoRkt

Posting Permissions

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