⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tsip_utl.c

📁 gps开发专用的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
			days_this_month = days_in_month[1][months];
		else
			days_this_month = days_in_month[0][months];

		if (days < days_this_month)
			break;

		days -= days_this_month;
		months++;
	}
	month = months + 1;
	day = (short)days + 1;

//========================

	regs.h.ah = 0x2B;			/* set date in DOS */
	regs.x.cx = year;			/* year */
	regs.h.dh = month;		/* month */
	regs.h.dl = day;			/* day */

	intdos (&regs, &regs);
	if (regs.h.al != 0x00) {
		return FALSE;
	}

	regs.h.ah = 0x2D;		   /* set time in DOS */
	hour = (short) (fmod (gpstime, 86400.) / 3600.);	/* hours */
	minute = (short) (fmod (gpstime, 3600.) / 60.);		/* minutes */
	second = (short) (fmod (gpstime, 60.));				/* seconds */
	regs.h.ch = hour;
	regs.h.cl = minute;
	regs.h.dh = second;
	regs.h.dl = 0;														/* csecs */

	intdos (&regs, &regs);
	if (regs.h.al != 0x00) {
		return TRUE;
	}
	else {
		intdos (&regs, &regs);
		return FALSE;
	}
}


void display_current_PC_time (void)
{
	time_t
		pctime;

	pctime = time(NULL);
	clreol (); show_crlf ();
	/* asctime() inserts a \n and \0 after the first 24 characters */
	xprintf ("Current PC time is %s\r", asctime (localtime(&pctime)));
	clreol ();
}

void display_timezone (void)
{
		clreol (); show_crlf ();
		xprintf ("Local offset relative to GMT is %.1f hours", timezone_hrs);
		show_crlf ();
}

void ask_for_timezone (void)
{
	show_crlf (); show_crlf ();
	xprintf (
"Before setting the clock, the time zone offset must be entered.  If the PC");
	show_crlf ();
	xprintf (
"is configured for local time, it must be adjusted to GPS (GMT) time with the");
	show_crlf ();
	xprintf (
"proper time zone offset equal to the number of time zones east of Greenwich.");
	show_crlf ();
	xprintf (
"Time zones west of Greenwich are negative.  Add an additional '1' for Daylight");
	show_crlf ();
	xprintf (
"time (value must be between -14 and 14).");
	show_crlf ();
	show_crlf ();
	timezone_set = TRUE;
	do {
		/*
		** Some time zones are not integer hour offsets: Newfoundland,
		** Saudi Arabia, India, Singapore, parts of Australia, etc.
		*/
		timezone_hrs = ask_dbl (
			"Enter timezone (PST=-8, PDT=-7, EST=-5, GMT=0): ");
		if (cmd_esc ()) {
			timezone_set = FALSE;
			timezone_hrs = -24.;
			break;
		}
	} while ((timezone_hrs < -14.) || (timezone_hrs > 14.));
	show_crlf ();
}

void GPS_time_from_PC (double *gpstime)
{

	switch_window (CMDWIN);
	display_current_PC_time();

	if (timezone_set) {
		display_timezone ();
	}
	else
	{
		ask_for_timezone();
	}

	if (cmd_esc ()) {
		xprintf ("Timezone set failure");
		return;
	}

	*gpstime = (double)raw_PC_time() - (timezone_hrs * 3600.);
}

/* setting receiver time from PC */
#define WAITING_FOR_TIMEZONE	2
#define TIME_REQUEST_ACTIVE	1
#define NO_TIME_REQUEST			0
static short
	status_of_PC_time_set = NO_TIME_REQUEST;
	/*
	** 2 = requested, timezone unknown;
	** 1 = timezone known, & re-requested;
	** 0 = no pending request.
	*/

void request_PC_time_set (void)
{
	status_of_PC_time_set = timezone_set?
		TIME_REQUEST_ACTIVE : WAITING_FOR_TIMEZONE;
}

void GPS_time_to_PC (double gpstime)
{
	static short
		under_way = FALSE;

	if (status_of_PC_time_set == NO_TIME_REQUEST || under_way) return;

	/*
	**	under_way prevents the routine from being invoked again by a 0x41
	** while it is waiting for inputs in compute_timezone_secs().
	*/

	under_way = TRUE;
	if (gpstime == 0.0)
	{
		clreol (); show_crlf ();
		xprintf ("GPS time is not available yet. Try again later.");
		set_cmd_esc (TRUE);
		timezone_set = FALSE;
		status_of_PC_time_set = NO_TIME_REQUEST;
	}
	else
	{

		switch_window (CMDWIN);

		display_current_PC_time();

		if (timezone_set)
		{
			display_timezone ();
		}
		else
		{
			ask_for_timezone();
		}

		if (cmd_esc ()) {
			xprintf ("Timezone set aborted");
			status_of_PC_time_set = NO_TIME_REQUEST;
		}
		else
		{
			if (status_of_PC_time_set == WAITING_FOR_TIMEZONE) {
				/* timezone set; re-get gpstime to get updated time */
				cmd_0x21 ();
				status_of_PC_time_set = TIME_REQUEST_ACTIVE;
			}
			else
			{
				if (set_raw_PC_time(gpstime + timezone_hrs*3600.))
				{
					xprintf ("Time set failure");
				}
				status_of_PC_time_set = NO_TIME_REQUEST;
				display_current_PC_time();
			}
		}
	}
	show_crlf ();
	under_way = FALSE;
}
/**/
/* *******************  4. STORAGE FILE CONTROL ****************** */
/* Following two routines called by open_, close_file_storage () below.
 * Return TRUE or FALSE.
 */
static short append_or_overwrite (char *newname, short *append_mode)
/* returns need_filename */
{
	FILE *stream_ptr;

	if (newname[0] == END_STRING) return TRUE;

	stream_ptr = fopen (newname, "r");
	if (stream_ptr != NULL) {
		fclose (stream_ptr);
		xprintf ("Append to existing file '%s'?", newname);
		if (ask_verify ()) {
			*append_mode = TRUE;
			return FALSE;
		} else {
			*append_mode = FALSE;
			xprintf ("Overwrite existing file '%s'?", newname);
			return (!ask_verify ());
		}
	}
	return FALSE;
}

static void autoname (char *filename)
{
	short
		week_num,
		hour_of_week;
	double
		current_time;

	current_time = get_tsip_time ();
	if (current_time == 0.0) {
		xprintf ("No current time - aborted - please retry.");
		show_crlf ();
		cmd_0x21 ();
		set_cmd_esc (TRUE);
		return;
	}
	week_num = (short) (current_time / WEEK);
	hour_of_week = (short)((current_time - week_num * WEEK) / 3600.);
	sprintf (filename, "&%03d-%03d.tsp", week_num%1000, hour_of_week);
}

void open_file_storage (FILE **stream_ptr, char *filename, short filetype)
{
	short
		append_mode,
		need_filename;
	char
		newname[80];
	char
		open_mode[3] = {END_STRING, END_STRING, END_STRING};

	set_cmd_esc (FALSE);
	if (*stream_ptr != NULL) return;
	
	append_mode = FALSE;
	if (filetype == AUTOFILE) {
		autoname (newname);
		need_filename = FALSE;
	} else {
		strcpy (newname, filename);
		if (newname[0] == END_STRING) {
			need_filename = TRUE;
			xprintf ("No previous storage file.");
         clreol ();
			show_crlf ();
		} else {
			need_filename = append_or_overwrite (newname, &append_mode);
		}
	}
	if (cmd_esc ()) return;

	do {  /* open file */
		while (need_filename) {
			xprintf ("Enter new output file name ('^Z' to abort): ");
			clreol ();
			if (!get_prompt_string (newname, 70)) return;
			/* open to read; check to see if file exists already */
			need_filename = append_or_overwrite (newname, &append_mode);
			if (cmd_esc ()) return;
		}
		open_mode[0] = (append_mode) ? 'a' : 'w';
		open_mode[1] = 'b';
		*stream_ptr = fopen (newname, open_mode);
		/* open file for binary write or append */
		if (*stream_ptr != NULL)
		{
			break;
		}
		/* file open failed; loop and ask for new file name. */
		xprintf ("Could not open '%s'.", newname);
		show_crlf ();
		if (filetype == AUTOFILE) return;
		need_filename = TRUE;
	} while (*stream_ptr == NULL);

	strcpy (filename, newname);
	xprintf ("Saving to '%s'.", filename);
	show_crlf ();
}

void close_file_storage (FILE **stream_ptr, char *filename, short filetype)
{
	set_cmd_esc (FALSE);
	if (*stream_ptr == NULL) return;

	if (filetype != AUTOFILE) {
		xprintf ("Currently storing data to '%s'. Close file?", filename);
		if (!ask_verify ()) {
			show_crlf ();
			set_cmd_esc (TRUE);
			return ;
		}
	}
	if (fclose (*stream_ptr) == EOF) {
	   xprintf ("File close error, '%s'", filename);
	} else {
		*stream_ptr = NULL;
		xprintf ("Closing '%s'.", filename);
	}
	show_crlf ();
}

/**/
/* *****************  5. SERIAL PORT CONTROL  ****************** */

short serial_port_select (short *io_code)
{
	static char
		*databit_text[4]  =  {"", "", "7", "8"},
		*parity_text[4]   =  {"no", "odd", "", "even"},
		*baud_text[10]    =  {"50 bps", "110 bps", "300 bps", "600 bps",
						   "1200 bps",	"2400 bps", "4800 bps",
						   "9600 bps", "38.4k bps", "19.2k bps"},
		*stopbit_text[2]  =  {"1", "2"};
	unsigned char
		baud, databits, stopbits, parity;
	short
		iocode;

	set_cmd_esc (FALSE);
	show_crlf ();
	xprintf ("Current serial I\\O setting: %s, %s-%s-%s",
		baud_text[(*io_code >> 5) & 0x0F], databit_text[*io_code & 3],
		parity_text[(*io_code >> 3) & 3], stopbit_text[(*io_code >> 2) & 1]);
	show_crlf ();

	baud = pick_one ("Baud rate: ", baud_text, 10);
	if (cmd_esc ()) return FALSE;

	databits = pick_one ("Data bits: ", databit_text, 4);
	if (cmd_esc ()) return FALSE;

	parity = pick_one ("Parity: ", parity_text, 4);
	if (cmd_esc ()) return FALSE;

	stopbits = pick_one ("Stop bits: ", stopbit_text, 2);
	if (cmd_esc ()) return FALSE;

	iocode = databits + (stopbits << 2) + (parity << 3) + (baud << 5);
	xprintf ("Set port to %s, %s-%s-%s?",
		baud_text[(iocode >> 5) & 0x0F], databit_text[iocode & 3],
		parity_text[(iocode >> 3) & 3], stopbit_text[(iocode >> 2) & 1]);

	if (!ask_verify_nocmdcrlf ()) return FALSE;
	*io_code = iocode;
	return TRUE;
}

/* formerly in tsip_sio.c */
#define FLOW_OFF 0
#define FLOW_ON  1

#define NUM_PORTS 3

static short  			tsip_port;
static short				port_initialized[NUM_PORTS];
static short  			io_code[NUM_PORTS];

void toggle_TSR_flow_control (short cntrl)
{
	union REGS
		regs;

	switch (cntrl) {
	case FLOW_ON:        /* enable receive flow control */
		regs.h.ah = 9;
		regs.h.dh = 0;
		regs.h.dl = 1;    /* 0=flow control */
		break;

	case FLOW_OFF:       /* disable flow control */
		regs.h.ah = 9;
		regs.h.dh = 0;
		regs.h.dl = 0;
		break;
	}
	int86 (0x14, &regs, &regs);
}

void set_serial_port (void)
{
	if (tsip_port == 0)
	{
		union REGS
			regs;

		regs.h.al = io_code[tsip_port];
		regs.h.ah = 4;
		regs.h.dl = 1;
		int86 (0x14, &regs, &regs);
		toggle_TSR_flow_control (FLOW_ON);
	}
	else
	{
		unsigned short
			baud, databits, parity, stopbits;

		baud     = (io_code[tsip_port] >> 5) & 0x0F;
		databits = (io_code[tsip_port]     ) & 3;
		parity   = (io_code[tsip_port] >> 3) & 3;
		stopbits = (io_code[tsip_port] >> 2) & 1;
		initsio (tsip_port, (75 << baud), databits + 5, stopbits + 1, (parity << 3));
	}
}

void set_buf_serial_port (void)
{
	short port_param_set;
   /* first time through - set to COM1 and initialize to 9600 8-odd-1 */
	if (port_initialized[tsip_port]) {
	  xprintf ("Current port is COM%d", tsip_port);
	  clreol (); show_crlf ();
		if (tsip_port != 0)
		{
			xprintf ("Change port?");
			if (ask_verify ()) {
				tsip_port = 3 - tsip_port;
				xprintf ("Port selected is COM%d\r\n", tsip_port);
			}
		}
	}

	if (port_initialized[tsip_port]) {
		port_param_set = FALSE;
		xprintf ("Change port parameters?");
		clreol ();
		if (!ask_verify ()) {
			port_param_set = TRUE;
		}
		if (!port_param_set	&& io_code[tsip_port] != 0xC3)
		{
			xprintf ("Set to 4800 8-none-1?");
			if (ask_verify ())
			{
				io_code[tsip_port] = 0xC3;
				port_param_set = TRUE;
			}
		}
		if (!port_param_set	&& io_code[tsip_port] != 0xEB)
		{
			xprintf ("Set to 9600 8-odd-1?");
			if (ask_verify ())
			{
				io_code[tsip_port] = 0xEB;
				port_param_set = TRUE;
			}
		}
		if (!port_param_set)
		{
			if (!serial_port_select (&io_code[tsip_port])) {
				show_crlf ();
				xprintf ("-- aborted --");
			}
		}
	}
	else
	{
		port_initialized[tsip_port] = TRUE;
	}
	set_serial_port ();
	/* clear serial link */
	send_byte (DLE);
	send_byte (ETX);
}

/* functions to wrap around the non-SPDRIVE functions to provide a   */
/* common interface for the near_term                                */

/* serial port functions related to SPDRIVE */
short initialize_serial_port (short comport)
{
	union REGS
		regs;

	short iport;

	if (comport < 0 || comport > 2) return TRUE;
	for (iport = 0; iport < NUM_PORTS; iport++) {
		port_initialized[iport] = FALSE;
		io_code[iport] = 0xEB;
	}
	tsip_port = comport;
	if (tsip_port == 0)
	{
		tsip_port = 0;
		tsip_port = 1;
		regs.h.ah = 5;
		regs.h.al = 0;
		regs.x.dx = 0;
		int86 (0x14, &regs, &regs);
		if (regs.h.ah != 1) return TRUE;
	}
	set_buf_serial_port ();
	return FALSE;
}

void reset_serial_port ()
{
	short iport;

	show_crlf ();
	for (iport = 0; iport < NUM_PORTS; iport++) {
		if (port_initialized[iport] && (io_code[iport] != 0xEB)) {
			xprintf ("Reset COM%d to 9600 baud, 8-odd-1?", iport + 1);
			if (ask_verify ()) {
				io_code[iport] = 0xEB;
				tsip_port = iport + 1;
				set_serial_port ();
			}
		}
	}
	if (tsip_port == 0) {
		toggle_TSR_flow_control (FLOW_OFF);
	} else {
		closeserial ();
	}
}

short get_byte (void)
{
	if (tsip_port == 0) {
		union REGS
			regs;
		regs.h.ah = 6;
		int86 (0x14, &regs, &regs);
		if (regs.h.ah == 1) return regs.h.al;
		return -1;
	} else {
		return get_char (tsip_port);
	}
}

short send_byte (unsigned char db)
/* send a unsigned char, if able */
{
	if (tsip_port == 0) {
		union REGS
			regs;
		regs.h.ah = 7;		/*  send unsigned char  */
		regs.h.al = db;
		int86 (0x14, &regs, &regs);
		return TRUE;
	} else {
		return put_char(tsip_port, db) ? -1 : 0;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -