@Robin2, I'm sure I'm missing the obvious here but after a couple of days of trying various things I'm getting no where and would greatly appreciate you reviewing what I have and letting me know what the problem is.
I copied the Python and Arduino examples you gave me the link to, they work fine. I'm now trying to incorporate the Arduino code into my sketch. My sketch reads two joysticks and a switch from a transmitter, with an Uno, via xBee wi-fi. That part works fine. Ultimately I want to send that data to the Pi and send other data back but for now I'm just trying to get it to work as you have it. I use your "recvWithStartEndMarkers()" to decode the string from the transmitter. So as the "recvWithStartEndMarkers()" on the Python communications wouldn't interact I've changed the name of it and all the variables that are used. Not sure if they would of but one of the things I tried to isolate the problem.
I made those changes in the copy of your code and it also works fine, but when I add that to my code I do not get a response from the Python code. I do get the first three messages, setting up the port, waiting for the Arduino and that the Arduino is ready, but nothing after that. I've commented out the code that I use to read the transmitter and just have the calls to the 2 functions for receiving and replying to the Python and it just doesn't work. I've made no changes to the Python code. I couldn't add the Python code as the message was to large.
Here's the copy of your Arduino sketch with the names changed
// This is very similar to Example 3 - Receive with start- and end-markers
// in Serial Input Basics http://forum.arduino.cc/index.php?topic=396450.0
const byte numChars_pi = 64;
char receivedChars_pi[numChars_pi];
boolean newData_pi = false;
byte ledPin = 13; // the onboard LED
//===============
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
Serial.println("<Arduino is ready>");
}
//===============
void loop() {
recvFromPi();
replyToPython();
}
//===============
void recvFromPi() {
static boolean recvInProgress_pi = false;
static byte ndx = 0;
char startMarker_pi = '<';
char endMarker_pi = '>';
char rc_pi;
while (Serial.available() > 0 && newData_pi == false) {
rc_pi = Serial.read();
if (recvInProgress_pi == true) {
if (rc_pi != endMarker_pi) {
receivedChars_pi[ndx] = rc_pi;
ndx++;
if (ndx >= numChars_pi) {
ndx = numChars_pi - 1;
}
}
else {
receivedChars_pi[ndx] = '\0'; // terminate the string
recvInProgress_pi = false;
ndx = 0;
newData_pi = true;
}
}
else if (rc_pi == startMarker_pi) {
recvInProgress_pi = true;
}
}
}
//===============
void replyToPython() {
if (newData_pi == true) {
Serial.print("<This just in ... ");
Serial.print(receivedChars_pi);
Serial.print(" ");
Serial.print(millis());
Serial.print('>');
// change the state of the LED everytime a reply is sent
digitalWrite(ledPin, ! digitalRead(ledPin));
newData_pi = false;
}
}
//===============
And here's my sketch
// Test communication to and from pi
// temporary array for use when parsing
const byte numChars = 64;
char receivedChars[numChars];
char tempChars[numChars];
// variables to hold the parsed data
int Throttle_Ref = 0;
int Steering_Ref = 0;
int servoRef_F = 500; //position refererence for front servo
int servoTrim_F = 15; // trim to center front servo
int servoRef_R = 500; //position refererence for rear servo
int servoTrim_R = 0; // trim to center rear servo
int escRef = 500; // ESC speed cpontrol reference
int escTrim = 0; // ESC speed control trim
int ThrottleOutValue = 0;
int SteeringOutValue = 0;
unsigned long Comm_Time = 0;
unsigned long Comm_Time_2 = 0;
int Comm_Delay_Time = 1000;
int index = 0;
int Control_Mode = 0;
boolean newData = false;
boolean newDataPi = false;
int PiSpdRef = 0;
int PiDircRef = 0;
int PiSum = 0;
// Variables to send data to Xmitter
char a;
char startMarker = '<';
char endMarker = '>';
char comma = ',';
String SendBuffer;
unsigned long StartTime;
int DelayTime;
// data to pi
String Hdng_pi; // Compass Heading yp pi
String Lat_pi; // Latitude to pi
String Lon_pi; // Longitude to pi
String Sw_pi; // Switch status to pi
String Data_pi; // Combined values to pi
String SpdRef_pi; // Speed reference to pi
String Direc_pi; // Direction to pi
String Sum_pi;
// for comm to/from pi
const byte numChars_pi = 64;
char receivedChars_pi[numChars_pi];
boolean newData_pi = false;
void setup() {
Serial.begin(115200); // Serial Monitor
Serial1.begin(9600); // Read Data from Wifi Controller
Serial.println("<Arduino is Ready>");
}
void loop() {
// read transmitter
/*
recvWithStartEndMarkers(); // Read data from controller
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(); // Split data into separate variables
newData = false;
} //end if (newData == true)
Serial.print(Throttle_Ref);
Serial.print("\t");
Serial.print(Steering_Ref);
Serial.print("\t");
Serial.println(Control_Mode);
*/
// Comm to/from pi
// SpdRef_pi = Throttle_Ref;
// Direc_pi = Steering_Ref;
// PiSum = Throttle_Ref + Steering_Ref;
// Sum_pi = PiSpdRef + PiDircRef;
recvFromPi();
replyToPython();
} // end loop
void recvWithStartEndMarkers() { // reads variables from controller
static boolean recvInProgress = false;
static byte ndx = 0;
// char startMarker = '<';
// char endMarker = '>';
char rc;
while (Serial1.available() > 0 && newData == false) {
rc = Serial1.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
} // end while (Serial1.available()
} // end void recvWithStartEndMarkers()
//============
void parseData() { // split the data into its parts
strcpy(tempChars, receivedChars);
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part - the string
Throttle_Ref = atoi(strtokIndx); // Throttle reference
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Steering_Ref = atoi(strtokIndx); // Steering reference
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Control_Mode = atoi(strtokIndx); // Watch Dog Communication Pulse
} // end void parseData()
//=============== Communications to pi
void recvFromPi() {
static boolean recvInProgress_pi = false;
static byte ndx = 0;
char startMarker_pi = '<';
char endMarker_pi = '>';
char rc_pi;
while (Serial.available() > 0 && newData_pi == false) {
rc_pi = Serial.read();
if (recvInProgress_pi == true) {
if (rc_pi != endMarker_pi) {
receivedChars_pi[ndx] = rc_pi;
ndx++;
if (ndx >= numChars_pi) {
ndx = numChars_pi - 1;
}
}
else {
receivedChars_pi[ndx] = '\0'; // terminate the string
recvInProgress_pi = false;
ndx = 0;
newData_pi = true;
}
}
else if (rc_pi == startMarker_pi) {
recvInProgress_pi = true;
}
}
}
//===============
void replyToPython() {
if (newData_pi == true) {
Serial.print("<This just in ... ");
Serial.print(receivedChars_pi);
Serial.print(" ");
Serial.print(millis());
Serial.print('>');
newData_pi = false;
}
}
//===============
Thanks for your help and your time
John