Badly need help with Arduino serial communication problems

:smiley-roll-sweat: I am really loozing it…

been working on this for a while and tought I had it nailed, but I am getting some really strange comm issue,

that looks like timming. Aint a programmer so please dont laugh to hard… :~

Application monitors serial port for known commands;

S = All Stop

C= Give me your bearing (format of response AZ=BBB, where BBB is the bearing in degrees with leading zeros)

MBBB = Move to Bearing (BBB)

when the PC application driving the Arduino starts up, it starts polling for Bearing every second by sending

C’s. But the Arduino doesnt always respond…

My original issue was that the Arduino and the PC app would not link, to resolve this I had to insert a;

Serial.println();

It looked like the Arduino did not respond fast enough to the first C request for the PC app thus the PC apps considered

the link as failed and terminated…

then I noticed that the Arduino wasnt responding at all to the C commands, So I decided to insert a delay between the READ

command and the firts IF command to make certain the Arduino would be ready to respond to the data from teh serial port…

This did help… But… after tweeking the delay the best I can come up with is a response every second C command every

When I test the code manual using the serial terminal it works 100%…

I am assuming I aint dealing with the serial port properly and it could be optimised a LOT…

so here is my CRY for HELP!

… HELP! …

thanks in advance…

Richard ve2dx

void GetSerialData() {
		serialdata = Serial.read(); 
		Serial.println();
                delay (1000);

		if (serialdata == 'C') {

			if (AZreal > 99) {
				String GS232Boutput = (AZ + AZreal);
				Serial.println(GS232Boutput); 
			}
			else {
				if (AZreal < 10) {
					String GS232Boutput = (AZ00 + AZreal);
					Serial.println(GS232Boutput);
				}
				else  {
					String GS232Boutput = (AZ0 + AZreal);
					Serial.println(GS232Boutput);
				}
			}
		}
		if (serialdata == 'M') {
			digitalWrite(CCW1output, LOW);
			digitalWrite(CW1output, LOW);
			digitalWrite(CW1speed, LOW);
			int X = 0;
			String serialstring;
			while (X < 3) {
				//Serial.println(X);
				serialdata = Serial.read();
				//serialdata = byte (serialdata);
				serialstring = String (serialstring + serialdata);
				//Serial.println(serialdata);
				//Serial.println(serialstring);
				X = X++;
			}

			AZrequested = serialstring.toInt();

			AZtemp = AZreal;
		}

		if (serialdata == 'S') {
			digitalWrite(CCW1output, LOW);
			digitalWrite(CW1output, LOW);
			digitalWrite(CW1speed, LOW);
			AZrequested = AZreal;
			//Serial.println(AZrequested);
		}

:smiley-roll-sweat:

fixed, added a WHILE command in begining and realy made a big difference…

thanks again

Richard ve2dx

void GetSerialData() {						// this is my subroutine that gets the GS232 data from the serial port
	 while (Serial.available() > 0) {         				// Is there any data for me on the serial port?
		RealBearing() ;					// Start RealBearing routine (get present Bearing).
 		serialdata = Serial.read(); 				// Lets read the data from the serial port
		delay (500);

// we need to respond to a C command with bearing info under the following format AZ=BBB where BBB is the bearing info with leading zeros.

		if (serialdata == 'C') { 				// if data received on serial port is equal to the letter C or c then we send 
								// the AZ=BBB response via the serial port.
			//Serial.println("detected C command");		// check fern
			if (AZreal > 99) {				// check if bearing is 3 digit (100 or more)
								// GS232A or B protocol dictates that the bearing must have leading zeros
				String GS232Boutput = (AZ + AZreal);	// setting my output string for the serial port (AZ=+AZreal)
				Serial.println(GS232Boutput); 		// output my position as GS232B (or GS232A if code was mods) protocol
			}					// end of IF
			else {					// if wasnt 3 digit then
				if (AZreal < 10) {			// check if bearing is a single digit (9 or less)
					String GS232Boutput = (AZ00 + AZreal);	// setting my output string for the serial port (AZ=00+AZreal)
					Serial.println(GS232Boutput); 	// output my position as GS232B (or GS232A if code was mods) protocol
				}				// end of IF
				else  {				// then bearing should be 2 digit (10 to 99)
					String GS232Boutput = (AZ0 + AZreal);	// setting my output string for the serial port (AZ=0+AZreal)
					Serial.println(GS232Boutput); 	// output my position as GS232B (or GS232A if code was mods) protocol
				}				// end of IF
			}					// end of IF
		}						// end of IF

// Next we will define our destination bearing
// we look for MBBB command from the serial port by detecting the M then getting the 3 digit bearing info BBB.

		if (serialdata == 'M') { 				// if data received on serial port is equal to the letter M then 
								// start looking for the next 3 numerical digit these will be requested 
			//Serial.println("detected M command");		// check fern
			digitalWrite(CCW1output, LOW);		// Turn OFF Counter Clockwise relay
			digitalWrite(CW1output, LOW);			// Turn OFF Clockwise relay
			digitalWrite(CW1speed, LOW);			// Turn OFF Speed relay			
			int X = 0;					// set my counter (X) to 0
			String serialstring;				// I need this to clear the serialstring value
			while (X < 3) {				// check to see the value of my counter as long as it is not equal to 3
				//Serial.println(X);			// check fern
				serialdata = Serial.read(); 		// ok lets read the first byte of data
				//serialdata = byte (serialdata);		// convert it to text
				serialstring = String (serialstring + serialdata);	// Assemble it with the previous character (or number)
				//Serial.println(serialdata);		// check fern
				//Serial.println(serialstring);		// check fern again
				X = X++;				// increment my counter by 1
			}					// end of WHILE
			//Serial.println(serialstring);			// check fern resulting text
			AZrequested = serialstring.toInt();		// set my requested bearing eual to convert resulting serial 3 digit datatext 
								// to do so I need to convert the string into integer (read up on toInt() command!!!)
			AZtemp = AZreal;				// for speed control I must set a start point its AZtemp
		}						// end of IF

// Now lets see if we got an ALL STOP command (S)?

		if (serialdata == 'S') { 					// if data received on serial port is equal to the letter C then we send 
								// the AZ=BBB response via the serial port.
			//Serial.println("detected S command");		// check fern
			digitalWrite(CCW1output, LOW);		// Turn OFF Counter Clockwise relay
			digitalWrite(CW1output, LOW);			// Turn OFF Clockwise relay
			digitalWrite(CW1speed, LOW);			// Turn OFF Speed relay
			AZrequested = AZreal;			// set AZrequested equal to AZreal, I stop the rotor by making them equal
			//Serial.println(AZrequested);			// check fern
		}						// End of IF
                  MoveAndDisplay();						// Start the rotor moving according to data available
	}							// End of IF
}								// End of IF