Does serial1Event or serial2Event work in Teensy 3.1?

Status
Not open for further replies.
Does serial1Event and/or serial2Event work on a Teensy 3.1? I've successfully used serialEvent to receive commands from the Arduino Serial Monitor, but it doesn't work from serial2 (attached to bluetooth module).

Here's the code I'm working with (modified from Atlas Scientific source):

Code:
//This sample code was written on an Arduino UNO (modified to work with Teensy 3.1)
//It will allow you to control up to 4 Atlas Scientific devices through 1 soft serial RX/TX line.
//To open a channel (marked on the board as PRB 1 to PRB 4) send the number of the channel, a colon and the command ending with a carriage return.

//1:r<CR>
//2:i<CR>
//3:c<CR>
//4:r<CR>

//To open a channel and not send a command just send the channel number followed by a colon.

//1:<CR>
//3:<CR> 

// define pins for multiport toggling
int Pin_a = 2;                      //pin to control pin A
int Pin_b = 3;                      //pin to control pin B

char computerdata[20];              //A 20 byte character array to hold incoming data from a pc/mac/other 
char sensordata[30];                //A 30 byte character array to hold incoming data from the sensors
byte computer_bytes_received=0;     //We need to know how many characters bytes have been received         
byte sensor_bytes_received=0;       //We need to know how many characters bytes have been received

char *channel;                      //Char pointer used in string parsing
char *cmd;                          //Char pointer used in string parsing

void setup() {
  // start serial port to serial monitor/USB
  Serial.begin(9600);
  while( !Serial);
  Serial.println("Starting multiport on Teensy 3.1...");
  
  // start serial port to multiport (Serial1 on Teensy 3.1)
  Serial1.begin(38400);  
  // define pins for multiport switch
  pinMode(Pin_a, OUTPUT);           //Set the digital pin as output.
  pinMode(Pin_b, OUTPUT);          //Set the digital pin as output.
 
  // start serial port to bluetooth (Serial2 on Teensy 3.1)
  Serial2.begin(9600);
  //while( !Serial2);
  Serial2.println("Starting multiport on Teensy 3.1...");
  }


// serial port interup -- from USB/Serial monitor on pc 
void serialEvent(){               //This interrupt will trigger when the data coming from the serial monitor(pc/mac/other) is received   
           computer_bytes_received=Serial.readBytesUntil(13,computerdata,20); //We read the data sent from the serial monitor(pc/mac/other) until we see a <CR>. We also count how many characters have been received    
           computerdata[computer_bytes_received]=0; //We add a 0 to the spot in the array just after the last character we received.. This will stop us from transmitting incorrect data that may have been left in the buffer
           }    
           
// serial port interup -- from bluetooth 
void serial2Event(){               //This interrupt will trigger when the data coming from the serial monitor(pc/mac/other) is received   
           Serial.println("Bluetooth command received");
           computer_bytes_received=Serial2.readBytesUntil(13,computerdata,20); //We read the data sent from the serial monitor(pc/mac/other) until we see a <CR>. We also count how many characters have been received    
           computerdata[computer_bytes_received]=0; //We add a 0 to the spot in the array just after the last character we received.. This will stop us from transmitting incorrect data that may have been left in the buffer
           }  
       
void loop(){
     
      if(computer_bytes_received!=0){                 //If computer_bytes_received does not equal zero  
        channel=strtok(computerdata, ":");            //Let's pars the string at each colon
        cmd=strtok(NULL, ":");                        //Let's pars the string at each colon 
        open_channel();                               //Call the function "open_channel" to open the correct data path
          if(cmd!=0){                                 //if no command has been sent, send nothing
            Serial.print("Sending command: ");
            Serial.println(cmd);
            Serial2.print("Sending command: ");
            Serial2.println(cmd);
            Serial1.print(cmd);                      //Send the command from the computer to the Atlas Scientific device using the softserial port 
            Serial1.print("\r");                     //After we send the command we send a carriage return <CR> 
          }    
        computer_bytes_received=0;                    //Reset the var computer_bytes_received to equal 0 
        }
  
     if(Serial1.available() > 0){                   //If data has been transmitted from an Atlas Scientific device
       sensor_bytes_received=Serial1.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received 
       sensordata[sensor_bytes_received]=0;            //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer
       Serial.print("Response: ");
       Serial.println(sensordata);                    //let’s transmit the data received from the Atlas Scientific device to the serial monitor   
       Serial2.print("Response: ");
       Serial2.println(sensordata);
     }  
}
 
  
void open_channel(){                                  //This function controls what UART port is opened. 
      
     switch (*channel) {                             //Looking to see what channel to open   
   
       case '1':                                     //If *channel==1 then we open channel 1     
         digitalWrite(Pin_a, LOW);                   //Pin_x and pin_y control what channel opens 
         digitalWrite(Pin_b, LOW);                   //Pin_x and pin_y control what channel opens 
         Serial.println("Switching to channel 1");
         Serial2.println("Switching to channel 1");
       break;                                        //Exit switch case
        
       case '2':
         digitalWrite(Pin_a, LOW);
         digitalWrite(Pin_b, HIGH);
         Serial.println("Switching to channel 2");
         Serial2.println("Switching to channel 2");
       break;

       case '3':
         digitalWrite(Pin_a, HIGH);
         digitalWrite(Pin_b, LOW);
         Serial.println("Switching to channel 3");
         Serial2.println("Switching to channel 3");
       break;

       case '4':
         digitalWrite(Pin_a, HIGH);
         digitalWrite(Pin_b, HIGH);
         Serial.println("Switching to channel 4");
         Serial2.println("Switching to channel 4");
       break;
      }
 }

Thanks,

Eric
 
I believe it works, but you need to use serialEvent1(), serialEvent2() or serialEvent3().

The name "serial2Event" is incorrect. The number goes at the end, not in the middle.

Please let me know if that solves the problem? If not, I'll wire up a test with a couple USB-Serial cables and double-check Teensyduino's code.
 
Duh...didn't think to try alternative spelling.

I confirmed that SerialEvent1, SerialEvent2, and so on works fine with my code.

Paul and defragster, thanks for the help.
 
Status
Not open for further replies.
Back
Top