Usisng Two sensos on the Teensy 3.6 with Two TX and Rx Pins

Status
Not open for further replies.

turbo

New member
I am trying to read data from two sensors connected to rx1,tx1 and rx2,tx2. In the setu function I use Serial1 and Serial2. The problem I am facing is that sensor data of 1 sensor changes whereas the sensor data of the other sensor does not change.It keeps on printing the same data again and again. The sensor works well when tested individuallly. The problem arises only when I use two rx and tx pins and use two sensors.
Should I introduce a wait time to print the data from second sensor or should I enable the second serial port?


Code:
#include "Wire.h"

#define XSERIAL Serial1
#define YESERIAL Serial2


void setup() {
Serial.begin(115200);
XSERIAL.begin(115200, SERIAL_8N1);
YSERIAL.begin(115200, SERIAL_8N1);

}





void loop() {

  
  int RxMsg;
  unsigned int bytec = 0;
  unsigned char Data[246];

  int RxMsg1;
  unsigned int byte1 = 0;
  unsigned char Data1[246];

  Serial.println();
  Serial.print("NEW DATA");
  Serial.println();

  
  delay(1);

  while(XSERIAL.available() && bytec < 544)
  {

    RxMsg = XSERIAL.read();
    Data[bytec] = RxMsg;

    bytec++;
    
    
    delayMicroseconds(90);
  
  }

  delay(100);

  Serial.println();

  Serial.clear();
  XSERIAL.clear();




  
  for(int i = 0; i < 121; i++)
  {
  
    Serial.print("data 1:  ");
    Serial.print(Data[i]);
    Serial.print(", ");
 
    Serial.print('\n'); 
  }


 
  delay(1); 

  while(YSERIAL.available() && byte1 < 544)
  {

    RxMsg1 = YSERIAL.read();
  
    Data1[byte1] = RxMsg1;

    byte1++;
    
  
  }

  delay(100);

  Serial.println();

  Serial.clear();
  YSERIAL.clear();


  for(int i = 0; i < 121; i++)
  {
    Serial.print("data2:  ");
    Serial.print(Data1[i]);
    Serial.print(", ");
    }
  
  Serial.println();
  Serial.println();
  
    

}
 
It is sometimes hard to know what is happening without understanding more about your setup.

For example how much data is coming from these sensors?

There is also some issues in your example code:
Code:
unsigned int bytec = 0;
  unsigned char Data[246];

   while(XSERIAL.available() && bytec < 544)
So you have an array of 246, but you store up to 544 which will run over in memory.
And you print 121 elements of it?

And similar things for the second one.

Another issue. The software FIFO RX queues on T3.6 is setup to default to store a maximum of 64 bytes of data. So if your two parts are sending more than 62 and you are waiting for all of the first one to come in before doing anything with the second one, you will lose data... Which maybe what you are seeing...

There are several different ways to handle this. One way (personally don't recommend) is to edit the core files teensy3/serial1.c (serial2.c) and change the SERIAL1_RX_BUFFER_SIZE to something big enough to hold your message...

Another is to process bot sets of data at the same time. That is you check for data available and store it at the same time... Your code code be similar to what you have or could be done totally different...

Here is a hacked up version of yours....
Code:
#include "Wire.h"

#define XSERIAL Serial1
#define YSERIAL Serial2
#define MAX_DELTA_TIME_PER_CHAR 100
int RxMsg;
unsigned int bytec = 0;
unsigned char Data[246];

int RxMsg1;
unsigned int byte1 = 0;
unsigned char Data1[246];

elapsedMicros emX;  // elapsed time in micros
elapsedMicros emY;

void setup() {
  Serial.begin(115200);
  XSERIAL.begin(115200, SERIAL_8N1);
  YSERIAL.begin(115200, SERIAL_8N1);

}

void loop() {

  while (XSERIAL.available() && bytec < sizeof(Data))
  {
    RxMsg = XSERIAL.read();
    Data[bytec] = RxMsg;
    bytec++;
    emX = 0;
  }

  while (YSERIAL.available() && byte1 < sizeof(Data1))
  {
    RxMsg1 = YSERIAL.read();
    Data1[byte1] = RxMsg1;
    byte1++;
    emY = 0;
  }

  if (bytec &&  (emX > MAX_DELTA_TIME_PER_CHAR))
  {
    Serial.println();
    Serial.print("NEW X DATA");
    Serial.println();

    for (int i = 0; i < 121; i++)
    {
      Serial.print("data 1:  ");
      Serial.print(Data[i]);
      Serial.print(", ");
      Serial.print('\n');
    }
    XSERIAL.clear();
    bytec = 0;
  }

  if (byte1 &&  (emY > MAX_DELTA_TIME_PER_CHAR))
  {
    Serial.println();
    Serial.print("NEW Y DATA");
    Serial.println();

    for (int i = 0; i < 121; i++)
    {
      Serial.print("data 2:  ");
      Serial.print(Data1[i]);
      Serial.print(", ");
      Serial.print('\n');
    }
    YSERIAL.clear();
    byte1 = 0;
  }
}
WARNING: this is a hacked up version of yours, that I have not run or double checked I got all of the right variables... But hopefully gives you some clues.

Good luck
 
Not sure if this helps but your defines don't match your code.

You have:
#define XSERIAL Serial1
#define YESERIAL Serial2

But your following code refers to YSERIAL


Code:
#include "Wire.h"

#define XSERIAL Serial1
#define YESERIAL Serial2


void setup() {
Serial.begin(115200);
XSERIAL.begin(115200, SERIAL_8N1);
YSERIAL.begin(115200, SERIAL_8N1);

}





void loop() {

  
  int RxMsg;
  unsigned int bytec = 0;
  unsigned char Data[246];

  int RxMsg1;
  unsigned int byte1 = 0;
  unsigned char Data1[246];

  Serial.println();
  Serial.print("NEW DATA");
  Serial.println();

  
  delay(1);

  while(XSERIAL.available() && bytec < 544)
  {

    RxMsg = XSERIAL.read();
    Data[bytec] = RxMsg;

    bytec++;
    
    
    delayMicroseconds(90);
  
  }

  delay(100);

  Serial.println();

  Serial.clear();
  XSERIAL.clear();




  
  for(int i = 0; i < 121; i++)
  {
  
    Serial.print("data 1:  ");
    Serial.print(Data[i]);
    Serial.print(", ");
 
    Serial.print('\n'); 
  }


 
  delay(1); 

  while(YSERIAL.available() && byte1 < 544)
  {

    RxMsg1 = YSERIAL.read();
  
    Data1[byte1] = RxMsg1;

    byte1++;
    
  
  }

  delay(100);

  Serial.println();

  Serial.clear();
  YSERIAL.clear();


  for(int i = 0; i < 121; i++)
  {
    Serial.print("data2:  ");
    Serial.print(Data1[i]);
    Serial.print(", ");
    }
  
  Serial.println();
  Serial.println();
  
    

}
 
Hey @KurtE

Thanks for the reply. I edited the core file of Serial 1 and Serial2, it works fine. A total of 242 data come from the sensor each time.
Also I wanted to know if it is possible to use 4 sensors on the Teensy 3.6 using the Serial1,2,3,4 ports.
 
Last edited:
Status
Not open for further replies.
Back
Top