Skip to content. | Skip to navigation

Personal tools

remote/io.cpp

remote/io.cpp

remote/io.cpp

/////////////////////////////////////////////////////////////////////////////
// IO.CPP
//
//  Last updated: 6/22/2005
//
//  Part of the source code for the remote M3 Program
//
//  This file contains numerous low level I/O routines.
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// SystemReset - Software based system reset function
void SystemReset()
// Perform a hardware reset on the system.
{
	outp(0x64,0xFE);
}

/////////////////////////////////////////////////////////////////////////////
// InitPorts - Function to configure the serial port parameters and install
// the serial interrupt driver
void InitPorts()
{
	// Use COM1: for LCU status querying
  // Use COM2: to communicate with upstrean computer (TCS)
  #ifdef ENABLEIO
    InitComPort(0,0x3F8,4,9600,"N81",1);
    InitComPort(1,0x2F8,3,38400,"O71",1);
  #endif
}

/////////////////////////////////////////////////////////////////////////////
// SendToSlave - Transmit a string to the downstream guest computer on COM1:
void SendToSlave(char *message)
// Send a message to the Guest computer (downstream).
// The string "message" is transmitted.
// This routine automatically adds the correct Response and
// Address characters to the beginning of the string.
{
	char line[255];	// String buffer

	line[0] = Prompt;
	line[1] = Address;
	strcpy(&line[2],message);
	SerialBufPutString(Slave,line);
	Log->AddX("SendToSlave(%d): Sent \"%s\" to slave computer",500,SHECGREEN,2,__LINE__,line);
}

void RespondToHost(char *message)
// Send a response message to the host computer (TCS usually).
// The string message is sent, adding the response and address characters
// at the beginning.
{
	char line[255]; //buffer

	line[0] = Response; //add the response character
	line[1] = Address; //add the address character
	strcpy(&line[2],message); //and add the message
	SerialBufPutString(Host,line); // send it over the serial line
	Log->AddX("SendToMaster(%d): Sent \"%s\" to master computer",500,SHECGREEN,2,__LINE__,line); //and log it
}

/////////////////////////////////////////////////////////////////////////////
// VerifyChecksum: Verifies that the checksum in a string is correct
int VerifyChecksum(char *s)
// Returns a zero if the string's checksum is OK, and a 1 if not.
// Note that this routine expects a full message, i.e.
//  "~JdataCC" where data is the body of the message, and CC is the
//  checksum.
{
	int i,Checksum;
	char check[10];

	// Start checksum calculation with M3 Turret address
	Checksum = Address;
	// Binary not all subsequent characters
	for (i=2;i<strlen(s)-2;i++)
		Checksum ^= s[i];
	// Reduce checksum to 1 byte
	Checksum &= 255;
	sprintf(check,"%02X",Checksum);
	if ((s[strlen(s)-2] == check[0]) && (s[strlen(s)-1] == check[1]))
		return(0);
	else
		return(1);
}

/////////////////////////////////////////////////////////////////////////////
// SendToSlaveWithChecksum: Add a hex checksum + CR to string and send
void SendToSlaveWithChecksum(char *s)
// Calculates the Shec-checksum for a string, and sends the
// string to the host computer (TCS)
{
	int i,Checksum;
	char s2[120];

	// Start checksum calculation with M3 Turret address
	Checksum = Address;
	// Binary not all subsequent characters
	for (i=0;i<strlen(s);i++)
		Checksum ^= s[i];
	// Reduce checksum to 1 byte
	Checksum &= 255;
	// Append checksum and a carriage return to the string given to us
	sprintf(s2,"%s%02X\r",s,Checksum);
	// Send total message to host
	SendToSlave(s2);
}

RespondToHostWithChecksum(char *s)
{
	int i,checksum;
	char s2[120];

	checksum=Address;

	for (i=0;i<strlen(s);i++)
		checksum ^= s[i];

	checksum &= 255;

	sprintf(s2,"%s%02X\r",s,checksum);

	RespondToHost(s2);
}

/////////////////////////////////////////////////////////////////////////////
// COMTerminal: opens a simple terminal with a com port
void COMTerminal(int Port)
// This routine acts as a simple terminal to a com port.
// It displays received characters, and transmits typed characters.
// Hitting ESC ends the terminal session.
{
	char c,serc; // Holds received and typed characters
	char buffer[255];
	int index = 0,receivecount;

	CloseVideo();
	cprintf("Terminal on COM%d: (ESC to end)\r\n",Port+1);
	do
	{
		receivecount = 0;
		if (kbhit())
		{
			c = getch();
			cprintf("%c",c);
			buffer[index] = c;
			index++;
			if (c == 13)
			{
				buffer[index] = 0;
				SerialBufPutString(Port,buffer);
				cprintf("\r\n");
				index = 0;
			}
		}
		while (SerialCharReady(Port) && (receivecount < 16))
		{
			receivecount++;
			serc = SerialGetChar(Port);
			if (serc == 13)
				cprintf("\r\n");
			else if (serc != 10)
				cprintf("%c",serc);
		}
	// Quit on an <ESC>
	} while (c != 27);
	InitVideo();
	InitDisplay();
}

Generated by GNU Enscript 1.6.5.2.
Document Actions