gps c

41
/************************************************************************** * * $Id: gpsxmple.c 1.7 2009/10/02 14:18:08Z martin REL_M $ * $Name: GPSXMPLE_2_2 $ * * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany * * Description: * Sample program demonstrating how to access Meinberg GPS receivers * via the binary data protocol. * * Depending on the target operating system this program works * either via serial port (the default) or via a network socket * connection. * * Other modules needed to build the executable: * mbgextio.c, mbgserio.c, gpsserio.c, gpsutils.c * optionally aes128.c, if network socket I/O is used * * Supported target environments: * Windows / VC6 (serial and socket) * Linux / gcc (serial and socket) * QNX 6.x / gcc (socket only) * DOS / BC3.1 (serial only) * * For makefiles and build environment setups check the associated * subdirectories. *

Upload: ud-berkah-jaya-komputer

Post on 16-Feb-2017

8 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Gps c

/**************************************************************************

*

* $Id: gpsxmple.c 1.7 2009/10/02 14:18:08Z martin REL_M $

* $Name: GPSXMPLE_2_2 $

*

* Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany

*

* Description:

* Sample program demonstrating how to access Meinberg GPS receivers

* via the binary data protocol.

*

* Depending on the target operating system this program works

* either via serial port (the default) or via a network socket

* connection.

*

* Other modules needed to build the executable:

* mbgextio.c, mbgserio.c, gpsserio.c, gpsutils.c

* optionally aes128.c, if network socket I/O is used

*

* Supported target environments:

* Windows / VC6 (serial and socket)

* Linux / gcc (serial and socket)

* QNX 6.x / gcc (socket only)

* DOS / BC3.1 (serial only)

*

* For makefiles and build environment setups check the associated

* subdirectories.

*

Page 2: Gps c

* Please send changes required for other operating systems

* or build to <[email protected]>

*

* -----------------------------------------------------------------------

* $Log: gpsxmple.c $

* Revision 1.7 2009/10/02 14:18:08Z martin

* Changes due to renamed library functions.

* Optionally force connection.

* Read global receiver_info after startup.

* Read string type info and port info, if supported.

* Fixed a typo for passwd parameter in usage().

* Show SVs and DAC.

* Optionally loop and wait for automatic messages.

* Revision 1.6 2006/10/25 12:31:54Z martin

* Support serial I/O under Windows.

* Check return codes of API functions and print associated

* messages in case of error.

* Revision 1.5 2006/08/24 13:36:38Z martin

* Conditional support for network socket I/O.

* Serial I/O is now also conditional only.

* Use unified API calls from mbgextio module.

* Account for DAC bias.

* Revision 1.4 2006/05/17 10:20:29 martin

* Account for renamed structure.

* Revision 1.3 2005/09/08 14:28:34 martin

* Cleaned up display of synth settings.

* Revision 1.2 2005/04/26 11:29:16 martin

* Initial revision under RCS.

*

Page 3: Gps c

**************************************************************************/

#include <mbgextio.h>

#include <gpsutils.h>

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <string.h>

#include <ctype.h>

#if defined( MBG_TGT_DOS )

#include <conio.h>

#else

#define kbhit() 0

#endif

// the variables below are required for communication

MBG_MSG_BUFF rbuff;

MBG_MSG_BUFF tbuff;

MBG_MSG_CTL mctl =

{

{ &rbuff, sizeof( rbuff ) },

{ &tbuff, sizeof( tbuff ) }

};

Page 4: Gps c

// Define the minimum number of some resource types

// required by the software

#define N_COM_MIN 1 // minimum number of COM ports

// Define the maximum number of some resource types

// supported by the software

#define N_COM_MAX 4 // max. number of COM ports

#define N_STR_TYPE_MAX 20 // max. number of string types

#define N_POUT_MAX 3 // max. number of programmable outputs

#define _log_msg_0( _lvl, _fmt ) \

printf( _fmt ); printf( "\n" )

#define _log_msg_1( _lvl, _fmt, _v1 ) \

printf( _fmt, _v1 ); printf( "\n" )

#define _log_msg_2( _lvl, _fmt, _v1, _v2 ) \

printf( _fmt, _v1, _v2 ); printf( "\n" )

#define _log_msg_3( _lvl, _fmt, _v1, _v2, _v3 ) \

printf( _fmt, _v1, _v2, _v3 ); printf( "\n" )

static const char *target;

static int is_socket;

Page 5: Gps c

static int must_force_connection;

static int must_send_auto;

static RECEIVER_INFO receiver_info;

static const char *feature_names[] = DEFAULT_GPS_FEATURE_NAMES;

#if !defined( DEFAULT_DEV_NAME )

#define DEFAULT_DEV_NAME NULL

#endif

static const char *default_target = DEFAULT_DEV_NAME;

#if _USE_SERIAL_IO

static uint32_t default_baudrate = 19200L;

static const char *default_framing = "8N1";

static uint32_t baudrate;

static const char *framing;

#endif

#if _USE_SOCKET_IO

static const char *password;

#endif

static char ws[256];

static const char *fmt = "%-20s ";

static const char *dow_str[] =

{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

Page 6: Gps c

static /*HDR*/

int check_rc( int rc )

{

const char *cp = NULL;

switch ( rc )

{

case TR_COMPLETE:

cp = "Data received completely.";

break;

case TR_RECEIVING:

cp = "Received data not complete.";

break;

case TR_WAITING:

cp = "Waiting for data.";

break;

case TR_TIMEOUT:

cp = "Timeout receiving data";

break;

case TR_CSUM_HDR:

cp = "Header checksum error in received data.";

break;

Page 7: Gps c

case TR_CSUM_DATA:

cp = "Data checksum error in received data.";

break;

case TR_DECRYPTION:

cp = "Failed to decrypt received data.";

break;

case TR_OPEN_ERR:

cp = "Failed to establish connection.";

break;

case TR_IO_ERR:

cp = "I/O error receiving data";

break;

case TR_AUTH_ERR:

cp = "Authentication error";

break;

} // switch

if ( cp )

printf( "%s\n", cp );

else

printf( "Unknow error code: %i\n", rc );

return rc;

Page 8: Gps c

} // check_rc

/*HDR*/

void sprint_tm( char *s, TM_GPS *tm, int print_frac )

{

int n = 0;

int year = tm->year & ( DL_AUTO_FLAG - 1 );

n += sprintf( &s[n], "%02i.%02i.%04i %02i:%02i:%02i",

tm->mday,

tm->month,

year,

tm->hour,

tm->min,

tm->sec

);

if ( tm->year & DL_AUTO_FLAG )

strncpy( &s[6], "****", 4 );

if ( print_frac )

n += sprintf( &s[n], ".%07li", (long) tm->frac );

} /* sprint_tm */

Page 9: Gps c

/*HDR*/

void sprint_lla( char *s, LLA lla )

{

static const double r2d = 180 / PI;

sprintf( s, "%c %.4f deg, %c %.4f deg, %.0fm",

( lla[LAT] < 0 ) ? 'S' : 'N', lla[LAT] * r2d,

( lla[LON] < 0 ) ? 'W' : 'E', lla[LON] * r2d,

lla[ALT]

);

} /* sprint_lla */

/*HDR*/

void print_stat_info( STAT_INFO *p )

{

const char *cp;

switch ( p->mode )

{

case TRACK: cp = "SINGLE SV MODE"; break;

case AUTO_166: cp = "NORMAL OPERATION"; break;

case WARM_166: cp = "WARM BOOT"; break;

case COLD_166: cp = "COLD BOOT"; break;

case AUTO_BC: cp = "REMOTE OPERATION"; break;

case WARM_BC: cp = "REMOTE WARM"; break;

case COLD_BC: cp = "REMOTE COLD"; break;

Page 10: Gps c

case UPDA_166: cp = "UPDATE ALMANAC"; break;

case UPDA_BC: cp = "REMOTE UPD. ALM."; break;

default: cp = "INVALID MODE";

};

printf( "%s, %u/%u SVs, DAC: %+li/%+li\n",

cp,

p->good_svs,

p->svs_in_view,

(long) p->dac_val - OSC_DAC_BIAS,

(long) p->dac_cal - OSC_DAC_BIAS

);

} /* print_stat_info */

/*HDR*/

void print_time( TTM *p )

{

static const char *s[] =

{

"(current time)",

"(capture 0)",

"(capture 1)"

};

sprint_tm( ws, &p->tm, 1 );

Page 11: Gps c

printf( "\rCh: %2i %-14s %s Status: %04X\n",

p->channel,

s[p->channel + 1],

ws,

p->tm.status

);

} /* print_time */

static /*HDR*/

void check_receiver_info( RECEIVER_INFO *p, int log )

{

const char *fmt_min = "Number of %s from device (%u) is less than number required (%u)";

const char *fmt_max = "Number of %s from device (%u) exceeds number supported by the software

(%u)";

// Check if numbers of resources provided by the device

// don't exceed numbers of resources supported by the software

#define _check_min( _n, _min, _info ) \

{ \

if ( (_n) < (_min) ) \

{ \

if ( log ) \

_log_msg_3( LOG_ERR, fmt_min, _info, _n, _min ); \

} \

}

Page 12: Gps c

#define _check_max( _n, _max, _info ) \

{ \

if ( (_n) > (_max) ) \

{ \

if ( log ) \

_log_msg_3( LOG_WARNING, fmt_max, _info, _n, _max ); \

\

_n = _max; \

} \

}

if ( p->model_code == GPS_MODEL_UNKNOWN )

{

_log_msg_0( LOG_ERR, "Refclock model_code not set: maybe receiver_info has not been received

correctly" );

}

else

if ( log )

{

if ( p->model_code >= N_GPS_MODEL )

{

_log_msg_2( LOG_WARNING, "Unsupported refclock model_code %u (name: %s)", p->model_code,

p->model_name );

}

}

_check_min( p->n_com_ports, N_COM_MIN, "COM ports" );

_check_max( p->n_com_ports, N_COM_MAX, "COM ports" );

Page 13: Gps c

_check_max( p->n_str_type, N_STR_TYPE_MAX, "string types" );

_check_max( p->n_prg_out, N_POUT_MAX, "progr. outputs" );

} // check_receiver_info

/*HDR*/

int get_receiver_info( MBG_MSG_CTL *pmctl, RECEIVER_INFO *p )

{

int rc;

int i;

int l;

printf( fmt, "Receiver info:" );

rc = mbgextio_get_receiver_info( &mctl, p );

if ( rc != TR_COMPLETE )

{

check_rc( rc );

if ( rc == TR_TIMEOUT )

{

printf( "If you are using a very old GPS model then reading the\n"

"receiver info structure may not be supported.\n" );

}

return rc;

Page 14: Gps c

}

printf( "%s v%X.%02X",

p->model_name,

p->sw_rev.code >> 8,

p->sw_rev.code & 0xFF

);

l = strlen( p->sw_rev.name );

if ( l )

{

// skip trailing spaces

for ( i = l - 1; i > 0; i-- )

if ( p->sw_rev.name[i] != ' ' )

break;

if ( i != 0 )

printf( " \"%s\"", p->sw_rev.name );

#if 0 // hex output for testing

for ( i= 0; i < l; i++ )

printf( " %02X", p->sw_rev.name[i] );

#endif

}

printf( ", S/N: %s", p->sernum );

printf( "\n" );

// print features

Page 15: Gps c

printf( fmt, "" );

printf( "Features: 0x%08lX\n", (long) p->features );

for ( i = 0; i < 8 * sizeof( p->features ); i++ )

{

if ( p->features & (1UL << i) )

{

printf( fmt, "" );

printf( " - " );

if ( i < N_GPS_FEATURE )

printf( "%s\n", feature_names[i] );

else

printf( "Unknown feature flag 0x%08lX\n", (1UL << i) );

}

}

printf( "\n" );

return rc;

} // get_receiver_info

/*HDR*/

void show_sw_rev( MBG_MSG_CTL *pmctl )

{

SW_REV sw_rev;

Page 16: Gps c

int rc;

printf( fmt, "Software Revision:" );

rc = mbgextio_get_sw_rev( pmctl, &sw_rev );

if ( rc != TR_COMPLETE )

{

check_rc( rc );

return;

}

printf( "%X.%02X %s\n",

sw_rev.code >> 8,

sw_rev.code & 0xFF,

sw_rev.name

);

} /* show_sw_rev */

/*HDR*/

void show_bvar_stat( MBG_MSG_CTL *pmctl )

{

BVAR_STAT bvar_stat;

int rc;

printf( fmt, "BVAR status:" );

Page 17: Gps c

rc = mbgextio_get_bvar_stat( pmctl, &bvar_stat );

if ( rc != TR_COMPLETE )

{

check_rc( rc );

return;

}

printf( "%04X\n", bvar_stat );

} /* show_bvar_stat */

/*HDR*/

void show_stat_info( MBG_MSG_CTL *pmctl )

{

STAT_INFO stat_info;

int rc;

printf( fmt, "Receiver status:" );

rc = mbgextio_get_stat_info( pmctl, &stat_info );

if ( rc != TR_COMPLETE )

{

check_rc( rc );

return;

Page 18: Gps c

}

print_stat_info( &stat_info );

} /* show_stat_info */

/*HDR*/

void show_pos( MBG_MSG_CTL *pmctl )

{

LLA lla; // Position as logitude, latitude, altitude

int rc;

printf( fmt, "Receiver Position:" );

rc = mbgextio_get_pos_lla( pmctl, lla );

if ( rc != TR_COMPLETE )

{

check_rc( rc );

return;

}

sprint_lla( ws, lla );

printf( "%s\n", ws );

} /* show_pos */

Page 19: Gps c

/*HDR*/

void show_tzdl( MBG_MSG_CTL *pmctl )

{

TZDL tzdl;

int rc;

printf( fmt, "Time Zone:" );

rc = mbgextio_get_tzdl( pmctl, &tzdl );

if ( rc != TR_COMPLETE )

{

check_rc( rc );

return;

}

printf( "%-5s = UTC%+lisec\n",

tzdl.name[0],

(long) tzdl.offs

);

printf( fmt, " " );

printf( "%-5s = UTC%+lisec\n",

tzdl.name[1],

(long) tzdl.offs + tzdl.offs_dl

);

Page 20: Gps c

printf( fmt, "Daylight Saving" );

printf( "starts: " );

if ( tzdl.tm_on.year & DL_AUTO_FLAG )

{

printf( "%s after ",

dow_str[(int) tzdl.tm_on.wday]

);

}

sprint_tm( ws, &tzdl.tm_on, 0 );

printf( "%sh\n", ws );

printf( fmt, " " );

printf( "ends: " );

if ( tzdl.tm_off.year & DL_AUTO_FLAG )

{

printf( "%s after ",

dow_str[(int) tzdl.tm_off.wday]

);

}

sprint_tm( ws, &tzdl.tm_off, 0 );

printf( "%sh\n", ws );

} /* show_tzdl */

/*HDR*/

void show_synth( MBG_MSG_CTL *pmctl )

{

Page 21: Gps c

SYNTH synth;

double f;

int rc;

printf( fmt, "Synthesizer:" );

rc = mbgextio_get_synth( pmctl, &synth );

if ( rc != TR_COMPLETE )

{

check_rc( rc );

return;

}

if ( synth.freq == 0 )

{

printf( "disabled\n" );

return;

}

if ( synth.range == 0 )

{

// freq field is in 0.1 Hz units, so if

// the range is 0 we must divide by 10

// to yield the correct result

f = (double) synth.freq / 10.0;

}

else

Page 22: Gps c

{

ulong scale = 1;

int i;

for ( i = 1; i < synth.range; i++ )

scale *= 10L;

f = synth.freq * (double) scale;

}

printf( "%.1f Hz, Phase: %+.1f deg",

(double) f,

(double) synth.phase / 10.0

);

printf( ( f < SYNTH_PHASE_SYNC_LIMIT ) ? "\n" : " (phase ignored)\n" );

} /* show_synth */

/*HDR*/

void set_synth( void )

{

SYNTH synth = { 0 };

int rc;

if ( !( receiver_info.features & GPS_HAS_SYNTH ) )

{

Page 23: Gps c

printf( "The device doesn't provide a synthesizer.\n" );

return;

}

// In this example we set the synthesizer frequency to

// 1 kHz, and the phase to 180 degrees.

// The effective frequency is: (freq/10)*(10^^range)

synth.freq = 1000; // base frequency value in 1/10 Hz Units

synth.range = 1; // multiplier 10^^range

synth.phase = 1800; // phase in 1/10 degrees

rc = mbgextio_set_synth( &mctl, &synth );

if ( rc < 0 )

printf( "Failed to set synthesizer output, code: %i\n", rc );

else

printf( "Synthesizer has been modified.\n" );

} // set_synth

// set programmable output mode

/*HDR*/

void set_pout_mode( void )

{

POUT_INFO_IDX pout_info_idx;

Page 24: Gps c

POUT_SETTINGS pout_settings = { 0 }; // new settings, init'd to 0

uint16_t pout_idx = 0; // index of the programmable output

int rc;

if ( receiver_info.n_prg_out == 0 )

{

printf( "The device doesn't support programmable outputs.\n" );

return;

}

// valid pout_idx numbers include 0..receiver_info.n_prg_out-1

if ( pout_idx >= receiver_info.n_prg_out )

{

printf( "Programmable output index %u out of range (%u..%u)\n",

pout_idx, 0, receiver_info.n_prg_out - 1 );

return;

}

// OK, now get the current settings, and see which modes are

// supported by the selected programmable pulse output.

rc = mbgextio_get_pout_info_idx( &mctl, &pout_info_idx, pout_idx );

if ( rc != TR_COMPLETE )

{

check_rc( rc );

return;

}

Page 25: Gps c

// As an example we want to set POUT_CYCLIC_PULSE mode.

// First see if that mode is supported for that output.

if ( !( pout_info_idx.pout_info.supp_modes & MSK_POUT_CYCLIC_PULSE ) )

{

printf( "The selected mode is not supported by the selected output.\n" );

return;

}

// Generate a pulse every pout_settings.tm[0].on.t interval.

// In this example we want an interval of 20 minutes, and a pulse length

// of 300 milliseconds

pout_settings.mode = POUT_CYCLIC_PULSE;

pout_settings.tm[0].on.t.hour = 0;

pout_settings.tm[0].on.t.min = 20;

pout_settings.tm[0].on.t.sec = 0;

pout_settings.pulse_len = 30; // 10 millisecond units

rc = mbgextio_set_pout_settings_idx( &mctl, &pout_settings, pout_idx );

if ( rc < 0 )

printf( "Failed to set programmable pulse output mode, code: %i\n", rc );

else

printf( "Programmable output mode has been changed.\n" );

} // set_pout_mode

Page 26: Gps c

/*HDR*/

void show_port_parm( MBG_MSG_CTL *pmctl )

{

PORT_PARM port_parm;

int rc;

int i;

rc = mbgextio_get_port_parm( pmctl, &port_parm );

if ( rc != TR_COMPLETE )

{

check_rc( rc );

return;

}

for ( i = 0; ; )

{

printf( "COM%i: %5lu Baud, %s, ",

i,

(ulong) port_parm.com[i].baud_rate,

port_parm.com[i].framing

);

switch ( port_parm.mode[i] )

{

Page 27: Gps c

case 0: printf( "Time string on request only\n" ); break;

case 1: printf( "Time string once per second\n" ); break;

case 2: printf( "Time string once per minute\n" ); break;

case 3: printf( "Capture Events\n" ); break;

default: printf( "Unknown mode\n" );

}

if ( ++i >= 2 )

break;

printf( fmt, "" );

}

} /* show_port_parm */

/*HDR*/

void show_port_settings( MBG_MSG_CTL *pmctl )

{

const char *mode_names[N_STR_MODE] = DEFAULT_ENG_MODE_NAMES;

STR_TYPE_INFO sti[N_STR_TYPE_MAX];

STR_TYPE_INFO_IDX stii;

PORT_INFO_IDX pii;

PORT_SETTINGS *p;

int rc;

int i;

printf( fmt, "Serial ports:" );

Page 28: Gps c

if ( receiver_info.model_code == GPS_MODEL_UNKNOWN )

{

// This may be an old GPS model which does not provide

// a receiver info structure.

show_port_parm( pmctl );

return;

}

// The number of serial ports and time string formats supported

// by this device is specified in the receiver info structure.

// Read time string information

for ( i = 0; i < receiver_info.n_str_type; i++ )

{

rc = mbgextio_get_str_type_info_idx( pmctl, &stii, (uint16_t) i );

if ( rc != TR_COMPLETE )

{

check_rc( rc );

return;

}

if ( stii.idx != i )

{

printf( "** Info for string type %i requested, but for %i received.\n",

stii.idx, i );

return;

Page 29: Gps c

}

sti[i] = stii.str_type_info;

}

// Read and print port settings

for ( i = 0; ; )

{

rc = mbgextio_get_port_info_idx( pmctl, &pii, (uint16_t) i );

if ( rc != TR_COMPLETE )

{

check_rc( rc );

return;

}

if ( pii.idx != i )

{

printf( "** Info for port %i requested, but for %i received.\n",

pii.idx, i );

return;

}

p = &pii.port_info.port_settings;

printf( "COM%i: %5lu Baud, %s",

i,

Page 30: Gps c

(ulong) p->parm.baud_rate,

p->parm.framing

);

// Make sure indices received from the device do not exceed

// maximum numbers supported by this program.

if ( p->str_type >= N_STR_TYPE_MAX )

printf( " (string type exceeds max)" );

else

if ( p->mode >= N_STR_MODE )

printf( " (string mode exceeds max)" );

else

printf( ", \"%s\" string %s", sti[p->str_type].long_name, mode_names[p->mode] );

printf( "\n" );

if ( ++i >= receiver_info.n_com_ports )

break;

printf( fmt, "" );

}

} /* show_port_settings */

/*HDR*/ static

void exit_gpsxmple( void )

Page 31: Gps c

{

mbgextio_close_connection( &mctl );

printf( "\n\n" );

} /* exit_gpsxmple */

/*HDR*/ static

void exit_auto_off( void )

{

mbgextio_xmt_cmd( &mctl, GPS_AUTO_OFF );

} /* exit_auto_off */

// Do a very simple processing of command line parameters:

static /*HDR*/

int check_command_line( int argc, char *argv[] )

{

int must_print_usage = 0;

int i;

for ( i = 1; i < argc; i++ )

{

if ( strcmp( argv[i], "-a" ) == 0 )

{

Page 32: Gps c

must_send_auto = 1;

continue;

}

if ( strcmp( argv[i], "-?" ) == 0 )

{

must_print_usage = 1;

continue;

}

if ( strcmp( argv[i], "-h" ) == 0 )

{

must_print_usage = 1;

continue;

}

#if _USE_SOCKET_IO

if ( strcmp( argv[i], "-n" ) == 0 )

{

is_socket = 1;

continue;

}

#endif

#if _USE_SOCKET_IO

if ( strcmp( argv[i], "-p" ) == 0 )

{

if ( ++i < argc )

password = argv[i];

Page 33: Gps c

else

must_print_usage = 1;

continue;

}

#endif

#if _USE_SERIAL_IO

if ( strcmp( argv[i], "-b" ) == 0 )

{

if ( ++i < argc )

baudrate = strtoul( argv[i], NULL, 10 );

else

must_print_usage = 1;

continue;

}

#endif

#if _USE_SERIAL_IO

if ( strcmp( argv[i], "-f" ) == 0 )

{

if ( ++i < argc )

framing = argv[i];

else

must_print_usage = 1;

continue;

}

Page 34: Gps c

#endif

#if _USE_SERIAL_IO

if ( strcmp( argv[i], "-F" ) == 0 )

{

must_force_connection = 1;

continue;

}

#endif

if ( argv[i][0] != '-' )

target = argv[i];

else

must_print_usage = 1;

}

if ( target == NULL )

{

if ( is_socket )

must_print_usage = 1;

else

target = default_target;

}

if ( must_print_usage )

{

printf( "Usage: %s [options] target\n"

"\n"

#if _USE_SERIAL_IO

Page 35: Gps c

" target can specify a serial port\n"

#endif

#if _USE_SOCKET_IO

" target can specify a network host name or IP address\n"

#endif

"\n"

"Options\n"

" -? or -h Print this usage\n"

#if _USE_SOCKET_IO

" -n Connect to network target, not serial port\n"

" -p passwd Use specified password to connect\n"

#endif

#if _USE_SERIAL_IO

" -b speed serial port baud rate, default: %lu\n"

" -f xxx serial port framing, default: %s\n"

" -F force serial connection to %lu/%s\n"

#endif

"\n"

"Example:\n"

#if _USE_SERIAL_IO

" %s Connect to serial port %s using %lu/%s\n"

#endif

#if _USE_SOCKET_IO

" %s -n -p secret 172.16.1.1 Connect to the specified network host\n"

#endif

"\n",

argv[0]

#if _USE_SERIAL_IO

, (long) default_baudrate

Page 36: Gps c

, framing

, (long) default_baudrate

, framing

, argv[0]

, default_target

, (long) default_baudrate

, framing

#endif

#if _USE_SOCKET_IO

, argv[0]

#endif

);

return -1;

}

return 0;

} // check_command_line

int main( int argc, char *argv[] )

{

int str_mode_0;

int rc;

printf( "\n\n\nExample Program Accessing Meinberg GPS Receiver\n" );

Page 37: Gps c

baudrate = default_baudrate;

framing = default_framing;

if ( check_command_line( argc, argv ) < 0 )

return 1;

#if _USE_SOCKET_IO

if ( is_socket )

{

rc = mbgextio_open_socket( &mctl, target, password );

if ( rc < 0 )

{

if ( rc == TR_OPEN_ERR )

perror( "Error connecting" );

else

fprintf( stderr, "mbgextio_open_socket returned %i", rc );

exit( 1 );

}

goto doit;

}

#endif

#if _USE_SERIAL_IO

if ( must_force_connection )

{

Page 38: Gps c

printf( "Trying to force connection to 19200/8N1 ..." );

mbgextio_force_connection( target );

printf( "\n" );

baudrate = default_baudrate;

framing = default_framing;

}

rc = mbgextio_open_serial( &mctl, target, baudrate, framing );

if ( rc < 0 )

{

printf( "Error using port %s.\n", target );

return -1; // Error ...

}

printf( " (Using %s with %li baud, %s)\n\n",

target, (long) baudrate, framing );

goto doit;

#endif

#if !_USE_SOCKET_IO && !_USE_SERIAL_IO

return 1;

#endif

doit:

Page 39: Gps c

atexit( exit_gpsxmple );

// now start communication with whichever device has been opened above:

get_receiver_info( &mctl, &receiver_info );

check_receiver_info( &receiver_info, 1 );

/* display system specific values */

show_sw_rev( &mctl );

show_bvar_stat( &mctl );

show_stat_info( &mctl );

show_pos( &mctl );

show_tzdl( &mctl );

show_port_settings( &mctl );

str_mode_0 = mctl.rcv.pmb->u.msg_data.port_parm.mode[0];

show_synth( &mctl );

// set_synth();

// set_pout_mode();

if ( str_mode_0 != STR_PER_SEC )

return 0; // device will not send time automatically

if ( !must_send_auto )

return 0;

printf( "\n" );

Page 40: Gps c

// We will start to wait for automatically transmitted messages

// which require a longer timeout than we have used by default

// to wait for replies to dedicated request messages.

mbgextio_set_char_rcv_timeout( &mctl, 2000 ); // [msec]

mbgextio_set_msg_rcv_timeout( &mctl, 2000 ); // [msec]

mbgextio_xmt_cmd( &mctl, GPS_AUTO_ON );

atexit( exit_auto_off );

do

{

MBG_MSG_BUFF *pmb;

MSG_DATA *p;

rc = mbgextio_rcv_msg( &mctl, -1 );

if ( rc != TR_COMPLETE )

{

check_rc( rc );

break;

}

mbgextio_xmt_cmd( &mctl, GPS_AUTO_ON );

pmb = mctl.rcv.pmb;

p = &pmb->u.msg_data;

switch ( pmb->hdr.cmd )

Page 41: Gps c

{

case GPS_TIME:

print_time( &p->ttm );

if ( p->ttm.channel == -1 )

{

mbgextio_xmt_cmd( &mctl, GPS_AUTO_ON );

mbgextio_xmt_cmd( &mctl, GPS_STAT_INFO );

}

break;

case GPS_STAT_INFO:

print_stat_info( &p->stat_info );

break;

} /* switch */

} while ( !kbhit() );

return 0;

} /* main */