uint8_t MG2639_Cell::begin(unsigned long baud)
{
	unsigned long setBaud = 0;
	uint8_t tries = 0;
	
	initializePins(); // Set up power and UART pin direction
	initializeUART(baud); // Initialize UART to requested baud
	
	// Try turning echo off first (send ATE0 command). If it succeeds,
	// we're already at the target baud and the module's on.
	if (setEcho(0) <= 0)
	{	
		// If setEcho fails, we can't communicate with the shield. The baud
		// may be incorrect, or the shield may be off. First time in, we'll
		// assume the module is on.
		// tries should be at least > 2 - one time assuming module is on,
		// another time assuming module is off
		while (setBaud <= 0 && tries < 4)
		{
			setBaud = autoBaud(); // Try to find the baud rate
			
			if (setBaud <= 0)
			{
				// If autoBaud fails to find anything, the module must be off.
				powerPulse(); // Send a power pulse (hold PWRKEY for 3s)
				delay(MODULE_WARM_UP_TIME); // Delay ~3s for module to warm up
			}
			tries++; // Increment tries and go again
		}
		
		// If we still can't find the baud rate, or communicate with the shield
		// we give up. Return a fail.
		if (setBaud <= 0)
			return 0;
		
		// If autoBaud succeeds, the module is on, we just need to 
		// change the baud rate.
		if (setBaud != baud)
		{
			int baudRsp;
	#if (ARDUINO >= 10601) 
			// Software serial after 1.6.1 works much better, no need for brute force
			baudRsp = changeBaud(setBaud, baud);
	#else
			baudRsp = bruteForceBaudChange(setBaud, baud, 100);
	#endif
			// Look for any error _except_ ERROR_UNKOWN_RESPONSE -- a change from 
			// 115200 will result in that error, even though the change baud 
			// worked. (SoftwareSerial can't read reliably at that high rate.)
			if ((baudRsp == 0) || (baudRsp == ERROR_FAIL_RESPONSE) || 
				(baudRsp == ERROR_TIMEOUT))
			{
				return 0;
			}
		}
		delay(COMMAND_RESPONSE_TIME);
	}
	
	return 1;
}
Esempio n. 2
0
/*
 Set baud rate and various message frequencies.
 */
void Gps::setupGps() {

	/*
	 Value Description
	 0 GGA Fix information
	 1 GLL Lat/Lon data
	 2 GSA Overall Satellite data
	 3 GSV Detailed Satellite data
	 4 RMC Recommended minimum info
	 5 VTG Vector track an Speed over the Ground
	 6 MSS (If internal beacon is supported)
	 7 Not defined
	 8 ZDA (if 1PPS output is supported)
	 9 Not defined
	 */
	autoBaud();
	//Serial1.begin(38400, 8, 1, 0); //gps
	//set debug on
	Serial1.println("$PSRF105,1*3E");

	//set VTG off -  Vector track an Speed over the Ground
	Serial1.println("$PSRF103,05,00,00,01*21");
	//set GLL off -  Lat/Lon data
	Serial1.println("$PSRF103,01,00,00,01*25");
	//set GGA on, 5sec, constant -  Fix information
	Serial1.println("$PSRF103,00,00,05,01*21");
	//set GSA on, 5 sec, constant - Overall Satellite data
	Serial1.println("$PSRF103,02,00,05,01*23");
	//set GSV on, 20sec, constant -Detailed Satellite data
	Serial1.println("$PSRF103,03,00,20,01*25");
	//set RMC on, 1sec, constant, Recommended minimum info
	Serial1.println("$PSRF103,04,00,01,01*21");

	//get debug
	//delay(1000);
//	while (Serial1.available() > 0) {
//		Serial.print(Serial1.read());
//	}
	//debug off
	Serial1.println("$PSRF105,0*3F");

	//set baud faster ?
	//GPS SIRF configuration strings...
	//#define SIRF_BAUD_RATE_4800    "$PSRF100,1,4800,8,1,0*0E\r\n"
	//#define SIRF_BAUD_RATE_9600    "$PSRF100,1,9600,8,1,0*0D\r\n"
	//#define SIRF_BAUD_RATE_19200    "$PSRF100,1,19200,8,1,0*38\r\n"
	//#define SIRF_BAUD_RATE_38400    "$PSRF100,1,38400,8,1,0*3D\r\n"
	//#define SIRF_BAUD_RATE_57600    "$PSRF100,1,57600,8,1,0*36\r\n"

	//$PSRF100,1,38400,8,1,0*3D\r\n
	Serial1.print("$PSRF100,1,38400,8,1,0*3D\r\n");
	Serial1.flush();
	Serial1.end();
	Serial1.begin(38400);

}