Hi there. I am trying to communicate between a Teensy 4.1 and a Teensy 3.6 using Bluetooth serial devices. The Teensy 4.1 is attached to a BlueSmirf Silver and the Teensy 3.6 is a attached to a HC-05 BT serial device.
I have set up my code where the 4.1 sends a query via BT serial and then receives a string reply. I did it this way because in later projects I want the 4.1 to query when new info from the 3.6 is necessary. Currently the code is set up using elapsedMillis to query at 50 Hz. The problem is that the code works great at <2 Hz, but in higher values the message becomes chopped or erratic. The worst part is that the code works great when the UART is a hardwired connection between both devices and doesn't use the bluetooth serial devices.
I can reproduce the same issue using a BT terminal on a cellphone and quickly querying the Teensy 3.6. I'm thinking this is something simple I'm missing about buffers and the BT and I'd appreciate any advice. I'm not using CTS/RTS pins but that is an option with the bluetooth devices. The string buffer is set at 64 bytes on either end currently to facilitate longer strings in the future.
Here is a snippet of what incorrect output looks like:
Hold On To you Buttttssssssssssssss!!!!!
Hold On To you Buttttssssssssssssss!!!!!
Hold On To you Buttttssssssssssssss!!!!!
Hold On To you Buttttssssssssssssss!!!!!
Hold On To you Buttttssssssssssssss!!!!!
Hold On To you Buttttssssssssssssss!!!!!
Hold On To you ButtttsssssssssHold On To you B
Hold On To you Buttttssssssssssssss!!!!!
Here is the 4.1 code:
And here is the 3.6 code:
I have set up my code where the 4.1 sends a query via BT serial and then receives a string reply. I did it this way because in later projects I want the 4.1 to query when new info from the 3.6 is necessary. Currently the code is set up using elapsedMillis to query at 50 Hz. The problem is that the code works great at <2 Hz, but in higher values the message becomes chopped or erratic. The worst part is that the code works great when the UART is a hardwired connection between both devices and doesn't use the bluetooth serial devices.
I can reproduce the same issue using a BT terminal on a cellphone and quickly querying the Teensy 3.6. I'm thinking this is something simple I'm missing about buffers and the BT and I'd appreciate any advice. I'm not using CTS/RTS pins but that is an option with the bluetooth devices. The string buffer is set at 64 bytes on either end currently to facilitate longer strings in the future.
Here is a snippet of what incorrect output looks like:
Hold On To you Buttttssssssssssssss!!!!!
Hold On To you Buttttssssssssssssss!!!!!
Hold On To you Buttttssssssssssssss!!!!!
Hold On To you Buttttssssssssssssss!!!!!
Hold On To you Buttttssssssssssssss!!!!!
Hold On To you Buttttssssssssssssss!!!!!
Hold On To you ButtttsssssssssHold On To you B
Hold On To you Buttttssssssssssssss!!!!!
Here is the 4.1 code:
Code:
/*
Frame Module Code
Hardware consists of a Teensy 4.1 connected to a BlueSmirf silver
The code sends a query at a set sampling rate and receives data back
*/
#define HWSERIAL Serial1 //serial port for bluetooth module
const byte numChars = 64; //incoming buffer size
char receivedChars[numChars]; //incoming buffer
boolean newData = false; //flag to determine when all queries have been received and put into one string
elapsedMillis newTimer; //Time that sets the sampling rate
int startMicros = 0;
int endMicros = 0;
void setup()
{
Serial.begin(230400); // Begin the serial monitor at 230400
delay(100);
HWSERIAL.begin(230400); // The Bluetooth Mate definitions for connection
delay(250);
HWSERIAL.print("$$$"); // Print three times individually
delay(250); // Short delay, wait for the Mate to send back CMD
HWSERIAL.println("C,98d351fdadf5"); //address and connect
delay(100);
HWSERIAL.println("---"); //close command mode
delay(100);
delay(5000);
Serial.println("Setup Complete");
}
void loop() {
if (newTimer >= 20) { //using elapsedMillis to set a 50 Hz sampling rate
HWSERIAL.print('?'); //send query to wheel module
recvWithStartEndMarkers(); //receive data back
showNewData(); //print new data monitor
newTimer = 0; //reset timer
}
}
/*
* Function that finds the start and end characters of the received UART message
*/
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = 'S';
char endMarker = 'E';
char rc;
if (HWSERIAL.available() > 0) {
while (HWSERIAL.available() > 0 && newData == false) {
rc = HWSERIAL.read();
if (recvInProgress == true) {
if (rc != endMarker && rc != startMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else if ( rc == endMarker) {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
else { //rc is start marker
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
}
//function to print new data
void showNewData() {
if (newData == true) {
Serial.println(receivedChars);
newData = false;
}
}
And here is the 3.6 code:
Code:
//**** Communication Definitions ****//
// set this to the hardware serial port you wish to use
#define HWSERIAL Serial1
// Variables
char buffer[64];
char receivedChar; //buffer for message to send to roboteq
boolean newData = false; //flag to determine when all queries have been received and put into one string
void setup() {
HWSERIAL.begin(230400); // The Bluetooth Mate defaults to 115200bps
delay(5000); // Short delay, wait for the Mate to send back CMD
}
void loop() {
recvOneChar();
if(newData == true && receivedChar == '?'){ //if a query has been received
newData = false;
sprintf(buffer,"SHold On To you Buttttssssssssssssss!!!!!E");
HWSERIAL.print(buffer);
}
}
void recvOneChar() {
if (HWSERIAL.available() > 0) {
receivedChar = HWSERIAL.read();
newData = true;
}
}