I am replacing an Arduino Uno with a Teensy 4.1, but I'm having trouble with serial communication. On the Uno, when I send "CHAN" from the serial monitor, the Arduino responds with "#OK." Using the same sketch on the Teensy, the Teensy seems to freeze after receiving "CHAN" and becomes unresponsive. To upload new code to the Teensy after it freezes, I have to press the physical program button.
The end goal is to have the Teensy read thermocouple data and send that data to coffee roasting software on my Windows 10 machine. The coffee roasting software uses the "CHAN" message to initialize before sending a "READ" command to request temperature data.
I did some searching on this forum and tried adding "delay(10)" to the loop without success. I have the Teensy 4.1 selected under "Teensy Ports."
I have tried the "HelloSerialMonitor" Teensy example code and it works.
Source code:
Error messages: When trying to upload after using the serial monitor (and freezing the Teensy) I get this message:
"Unable to open COM4 for reboot request
Windows Error Info: Access is denied.
more ideas... https://forum.pjrc.com/threads/40632?p=126667&viewfull=1#post126667
Teensy did not respond to a USB-based request to enter program mode.
Please press the PROGRAM MODE BUTTON on your Teensy to upload your sketch."
Hardware: Teensy 4.1, Windows 10 PC
Wiring: Teensy 4.1 with Micro USB cable plugged into PC, no other wires connected.
Software: Arduino IDE 1.8.13, Teensyduino 1.53
The end goal is to have the Teensy read thermocouple data and send that data to coffee roasting software on my Windows 10 machine. The coffee roasting software uses the "CHAN" message to initialize before sending a "READ" command to request temperature data.
I did some searching on this forum and tried adding "delay(10)" to the loop without success. I have the Teensy 4.1 selected under "Teensy Ports."
I have tried the "HelloSerialMonitor" Teensy example code and it works.
Source code:
Code:
//Arduino --> Artisan Roaster Scope Communication Test
// TC4 Communication Code adapted from FilePhil https://github.com/FilePhil/TC4-Emulator/blob/master/TC4-Emulator.ino
bool unit_F = false; //true = °F - false = °C
const long BAUD = 115200;
double temp1 = 101;
double temp2 = 102;
double temp3 = 103;
double temp4 = 104;
String msg;
// These variables are used for serial data input and parsing. Taken from example 4 and 5 of Robin2's thread "Serial Input Basics - updated" https://forum.arduino.cc/index.php?topic=396450.0
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;
boolean newData = false;
// end serial variables
void recvWithEndMarker() { //changed example 5 to eliminate start markers - Artisan uses newlines only, no start markers.
//removed "recvInProgress" because it's not used in recvwithEndMarker - will this cause issues elsewhere?
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
//============
void parseData() { // split the data into its parts
// Artisan lines to parse: "CHAN;ijkl" "UNITS;u" "READ"
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ";"); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
//Serial.println(messageFromPC);
if (strcmp(messageFromPC, "READ") == 0) {
Command_READ();
}
else if (strcmp(messageFromPC, "CHAN") == 0) {
Serial.println("#OK");
}
else if (strcmp(messageFromPC, "UNITS") == 0) {
strtokIndx = strtok(NULL, ";");
//Serial.println(strtokIndx);
if (strcmp(strtokIndx, "F") == 0) {
unit_F = true;
Serial.println("#OK Fahrenheit");
}
else if (strcmp(strtokIndx, "C") == 0) {
unit_F = false;
Serial.println("#OK Celsius");
}
}
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
floatFromPC = atof(strtokIndx); // convert this part to a float
}
//Send Data
void Command_READ() {
Serial.print("0.00,");
Serial.print(temp1);
Serial.print(",");
Serial.print(temp2);
Serial.print(",");
Serial.print(temp3);
Serial.print(",");
Serial.println(temp4);
}
//Parsing Serial Commands
void handleSerialCommand() {
recvWithEndMarker();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
//showParsedData();
newData = false;
}
}
void setup() {
Serial.begin(BAUD);
}
void loop() {
handleSerialCommand();
delay(10);
}
Error messages: When trying to upload after using the serial monitor (and freezing the Teensy) I get this message:
"Unable to open COM4 for reboot request
Windows Error Info: Access is denied.
more ideas... https://forum.pjrc.com/threads/40632?p=126667&viewfull=1#post126667
Teensy did not respond to a USB-based request to enter program mode.
Please press the PROGRAM MODE BUTTON on your Teensy to upload your sketch."
Hardware: Teensy 4.1, Windows 10 PC
Wiring: Teensy 4.1 with Micro USB cable plugged into PC, no other wires connected.
Software: Arduino IDE 1.8.13, Teensyduino 1.53