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

Thread: Serial communication between 2 teensy

  1. #1
    Junior Member
    Join Date
    Nov 2017
    Posts
    6

    Serial communication between 2 teensy

    Hi,
    I am new in the teensy world. I am trying to communicating a teensy 3.6 with e teensy 3.2 via serial port. For teensy 3.6 I am using Serial3 port and for teensy 3.2 I am using Serial1 port. Here is the code:

    Code:
    //code for teensy 3.6
    #define Ts 20000
    boolean timerFlag;
    IntervalTimer myTimer;
    
    void timerISR(void) {
      timerFlag = true;
    }
    
    void setup() {
      Serial3.begin(38400);
      Serial.begin(38400); // Config serial port (USB)
      
      while(!Serial);
      while(!Serial3);
    
      myTimer.begin(timerISR,Ts);
    }
    
    void loop() {
      Serial.println("master");
      if(timerFlag) {
        timerFlag = false;
        Serial3.write('a');
        Serial3.flush();
    	delayMicroseconds(100);
        if (Serial3.available()) //teensy 3.2 detected
        {
          if (Serial3.read() == 'a') //accept response from teensy 3.2,connection stablished
          {
    		Serial.println("response received: ");
    	  }
    	}
      }
    }
    Code:
    //Receiver Code
    //code for teensy 3.2
    char command;
    
    void setup() {
      Serial.begin(38400);
      Serial1.begin(38400);
    
      while(!Serial);
      while(!Serial1);
    }
    
    void loop() {
      if (Serial1.available()) //wait for teensy 3.6
      {
        Serial.println("command received");
        command = Serial1.read();
        if(command == 'a')
    	{
          Serial.println("Hello");
          Serial1.write('a'); //send response to teensy 3.6
          Serial1.flush();
        }
      }
    }
    What I am trying to do, I am sending letter 'a' from teensy 3.6 to teensy 3.2 with a timer generated interrupt and again I am sending it back from teensy 3.2 to teensy 3.6.

    Another question, what should be maximum baud rate for this particular case (teensy to teensy)?
    Let me know what I am doing wrong!

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,458
    Your timerFlag variable needs to be volatile.

    Double check your hardware. 3 wires are needed. Connecting same-named signals together, as you would do with SPI or I2C, is a common mistake with serial. Each TX needs to connect to the other side's RX pin. Don't forget a GND to GND wire.

    On both these boards, Serial1 & Serial2 clock from F_CPU and Serial 3+ clock from F_BUS. The max baud rate is their clock divided by 16. So you might actually have a higher maximum if you underclock Teensy 3.6, and use Serial1 or Serial2, possibly all the way up to 6 Mbit/sec. At such high speeds, usually RTS/CTS flow control is needed. Using Serial3 on Teensy 3.6 limits you to 3.75 Mbit/sec, if using the default 60 MHz F_BUS.

  3. #3
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Quote Originally Posted by PaulStoffregen View Post
    Your timerFlag variable needs to be volatile.

    Double check your hardware. 3 wires are needed. Connecting same-named signals together, as you would do with SPI or I2C, is a common mistake with serial. Each TX needs to connect to the other side's RX pin. Don't forget a GND to GND wire.

    On both these boards, Serial1 & Serial2 clock from F_CPU and Serial 3+ clock from F_BUS. The max baud rate is their clock divided by 16. So you might actually have a higher maximum if you underclock Teensy 3.6, and use Serial1 or Serial2, possibly all the way up to 6 Mbit/sec. At such high speeds, usually RTS/CTS flow control is needed. Using Serial3 on Teensy 3.6 limits you to 3.75 Mbit/sec, if using the default 60 MHz F_BUS.
    Hi,
    Thank you for your cordial reply. I have just checked all connections. They are okay... Any other possible bug that may cause communication failure?

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    3,496
    Did you add the volatile to both programs and reload them?

    Are both Teensy board plugged into a computer? As they both wait on while (!Serial) ;
    Not too worried about the whiile (!Serial1) as these always return true.
    With apps like this I would put in something like: Serial.println("Setup Complete");

    At the end of Setup just to make sure you get there...

    What exactly are you seeing? Does your T3.2 program ever receive anything?

    Again check wiring... Also send pictures of setup.

    Make sure you actually have connections between the two boards. Have seen more than once
    where some one puts the Teensy on a breadboard and does not solder the pins to the Teensy.

  5. #5
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,444
    your sketches worked for me. I added volatile to timerFlag, and increased timer to 200000, and i removed the Serial.println("master'") to keep from overrunning the monitor output. I hooked T3.1 pin 0 to T3.6 pin 8, and pin 1 to pin 7, and GND to GND. You need a monitor running on both boards, or get rid of while(!Serial); I also blinked pin 13 with digitalWrite(13,!digitalRead(13)); in if (timerFlag) block to verify timer was firing.

    Click image for larger version. 

Name:	tmp.png 
Views:	18 
Size:	262.5 KB 
ID:	12007
    Last edited by manitou; 11-06-2017 at 07:28 PM.

  6. #6
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thank you all for your kind support. Your support helped me to make successful communication but now got stuck in another part. Let me clear waht I am intended to do here:
    I am reading capacitance from total 15 pins (9 from teensy 3.6 and 6 from teensy 3.2). Then I am sending back 6 reading from teensy 3.2 to teensy 3.6. Here is the scheme:
    1. First teensy 3.6 will send 'a' to establish the connection with teensy 3.2 (teensy 3.2 will send 'a' back as an akoknowledgement).
    2. both will read the value from capacitor
    3. Teensy 3.6 will send a request 's' to teensy 3.2 to send back the 6 capacitor value.
    4. Then Teensy 3.6 will print all the value in terminal (currently, I am printing value from teensy 3.2 for debugging purpose only).

    But somehow now Teensy 3.2 is failing to receive the sending request 's'. Let me know, what is my fault. As teensy 3.2 has initially received the connection request 'a', then the data send request 's' should also be received in the later part of the code as I followed the same configuration.

    Here is the code:

    Code:
    //For Teensy 3.6
    #define Ts 20000 //50Hz communication
    #define SlaveCommunicator Serial3
    #define baudRate 115200
    #define TotalMasterCapSensor 9
    #define TotalSlaveCapSensor 6
    
    IntervalTimer myTimer;
    
    const char CapPin[] = { 23, 22, 19, 18, 17, 16, 15, 0, 1 }; //define capacitive input
    
    volatile boolean timerFlag = false;
    uint16_t cap[TotalMasterCapSensor+TotalSlaveCapSensor];
    float timeStamp;
    volatile unsigned long tickCounter = 0;
    char i;
    
    uint16_t uint16SlaveReceiver(void)
    {
      uint16_t receivedVal;
      receivedVal = SlaveCommunicator.read(); //read higher nibble
      receivedVal = receivedVal << 8; //shifted to right place
      while(!SlaveCommunicator.available());
      receivedVal |= SlaveCommunicator.read(); //read lower nibble
      return receivedVal;
    }
    
    void uint16SlaveSender(uint16_t sendingValue)
    {
      SlaveCommunicator.write((char)((sendingValue>>8)&0xff)); //send higher nibble
      SlaveCommunicator.write((char)(sendingValue & 0xff)); //send lower nibble
      SlaveCommunicator.flush();
    }
    
    void timerISR(void) {
      timerFlag = true;
    }
    
    void setup() {
      SlaveCommunicator.begin(baudRate);
      Serial.begin(baudRate); // Config serial port (USB)
      //delay(1000);
      while(!Serial);
      
      myTimer.begin(timerISR,Ts);
    
      Serial.println("t,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12.c13,c14,c15");
    }
    
    void loop() 
    {
      if(timerFlag)
      {
        Serial.println("Hi"); //for debugging purposes
        timerFlag = false;
        SlaveCommunicator.write('a'); //take initiative to communicate with slave
        SlaveCommunicator.flush();
        //delayMicroseconds(100); //wait for the response from slave
        if (SlaveCommunicator.available()) //slave detected
        {
          if (SlaveCommunicator.read() == 'a') //accept response from slave,connection stablished
          {
            Serial.println("got it!"); //for debugging purposes
            for(i = 0; i < TotalMasterCapSensor ; i++)
            {
              cap[i] = touchRead(CapPin[i]);
              Serial.println(cap[i]);
            }
            Serial.println("done reading");
            SlaveCommunicator.write('s'); //request slave to send data
            SlaveCommunicator.flush();
            while(!SlaveCommunicator.available()); //wait for the data from slave
        
            do {
              cap[i++] = uint16SlaveReceiver();
              while(!SlaveCommunicator.available());
            } while(i < TotalMasterCapSensor+TotalSlaveCapSensor);
        
            timeStamp = tickCounter*Ts/1000000.00;
            
            Serial.print(timeStamp,4);Serial.print(',');
            for (i = 0; i < TotalMasterCapSensor+TotalSlaveCapSensor-1; i++)
            {
              Serial.print(cap[i]);Serial.print(',');
            }
            Serial.println(cap[i]);
          }
        }
        tickCounter++;
      }
    }
    Code:
    //For Teensy 3.2
    #define Ts 20000 //Cycle period for 50Hz communication
    #define MasterCommunicator Serial1
    #define baudRate 115200
    #define TotalSlaveCapSensor 6
    
    const char CapPin[] = { 16, 17, 18, 19, 1, 0 }; //define capacitive input
    
    uint16_t cap[TotalSlaveCapSensor];
    char i;
    
    uint16_t uint16MasterReceiver(void)
    {
      uint16_t receivedVal;
      receivedVal = MasterCommunicator.read(); //read higher nibble
      receivedVal = receivedVal << 8; //shifted to right place
      while(!MasterCommunicator.available());
      receivedVal |= MasterCommunicator.read(); //read lower nibble
      return receivedVal;
    }
    
    void uint16MasterSender(uint16_t sendingValue)
    {
      MasterCommunicator.write((char)((sendingValue>>8)&0xff)); //send higher nibble
      MasterCommunicator.write((char)(sendingValue & 0xff)); //send lower nibble
      MasterCommunicator.flush();
    }
    
    void setup() {
      MasterCommunicator.begin(baudRate);
      Serial.begin(baudRate); //for debugging purposes
    }
    
    void loop() 
    {
      if (MasterCommunicator.available()) //wait for the master
      {
        if (MasterCommunicator.read() == 'a')
        {
          Serial.println("command received"); //for debugging purposes
          MasterCommunicator.write('a'); //send response
          MasterCommunicator.flush();
          for(i = 0; i < TotalSlaveCapSensor ; i++)
          {
            cap[i] = touchRead(CapPin[i]);
            Serial.println(cap[i]);
          }
          
          Serial.println("done reading");
          while(!MasterCommunicator.available());
          if(MasterCommunicator.read() == 's')
          {
            Serial.println("command2 received");
            for (i = 0; i < TotalSlaveCapSensor; i++)
            {
              uint16MasterSender(cap[i]);
            }
            MasterCommunicator.flush();
          }
        }
      }
    }
    Thanks in advance. (I am really surprised with the quick response!)

    Regards,
    Sabquat

  7. #7
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,444
    Your T3.2 sketch is using Serial1 (pins 0 and 1), but your CapPin[] is also trying to use pins 0 and 1. There may be other problems...

  8. #8
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Oh god! Thanks for the correction!

  9. #9
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Quote Originally Posted by manitou View Post
    Your T3.2 sketch is using Serial1 (pins 0 and 1), but your CapPin[] is also trying to use pins 0 and 1. There may be other problems...
    Did you notice any additional problem other than this? Now I am using pin 22 and 23 as the capacitive sensor instead of using 0 and 1. (previously it's was just a typo).

    Code:
    const char CapPin[] = { 16, 17, 18, 19, 22, 23 }; //define capacitive input
    But still not working... Teensy 3.2 is not reading the send request from teensy 3.6.
    Last edited by sabquat; 11-07-2017 at 12:10 AM.

  10. #10
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,444
    i haven't actually run the new sketches, but a possible problem on the T3.6 in the timerFlag if block, you need a
    while(!SlaveCommunicator.available());
    before if (SlaveCommunicator.available()) //slave detected

    There may be other problems .... you may need RTS/CTS to control the flow
    Last edited by manitou; 11-07-2017 at 11:14 AM.

  11. #11
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Quote Originally Posted by manitou View Post
    i haven't actually run the new sketches, but a possible problem on the T3.6 in the timerFlag if block, you need a
    while(!SlaveCommunicator.available());
    before if (SlaveCommunicator.available()) //slave detected

    There may be other problems .... you may need RTS/CTS to control the flow
    Thanks a lot. It really worked!

Posting Permissions

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