home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware 1 2 the Maxx
/
sw_1.zip
/
sw_1
/
PROGRAM
/
PCL4C30.ZIP
/
XYPACKET.C
< prev
Wrap
Text File
|
1992-01-04
|
9KB
|
296 lines
#include <stdio.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#include "pcl4c.h"
#include "ascii.h"
#define FALSE 0
#define TRUE !FALSE
#define ONESECOND 18
#define MAXTRY 5
#define LIMIT 20
extern GetChar();
extern PutChar();
extern SayError();
extern DisplayLine();
int TxPacket(Port,PacketNbr,PacketSize,Buffer,CRCflag)
int Port; /* COM port [0..3] */
int PacketNbr; /* Packet # to send */
int PacketSize; /* Packet size ( must be 128 or 1024 ) */
char Buffer[]; /* Data buffer */
int CRCflag; /* use CRC if TRUE */
{int i;
int Code;
unsigned short CheckSum;
int Attempt;
int PacketType;
char temp[81];
/* begin */
#if 0
printf("[TxP: COM%d PacketNbr=%d PacketSize=%d CRCflag=%d]",
1+Port,PacketNbr,PacketSize,CRCflag);
#endif
/* better be 128 or 1024 packet length */
if(PacketSize==1024) PacketType = STX;
else PacketType = SOH;
PacketNbr &= 0x00ff;
/* make up to MAXTRY attempts to send this packet */
for(Attempt=1;Attempt<=MAXTRY;Attempt++)
{/* send SOH/STX */
Code = PutChar(Port,PacketType);
/* send packet # */
Code = PutChar(Port,PacketNbr);
/* send 1's complement of packet */
Code = PutChar(Port,255-PacketNbr);
/* send data */
CheckSum = 0;
for(i=0;i<PacketSize;i++)
{Code = PutChar(Port,Buffer[i]);
if(CRCflag) CheckSum = UpdateCRC(CheckSum, Buffer[i]);
else CheckSum += Buffer[i];
}
/* send checksum */
if(CRCflag)
{Code = PutChar(Port, (CheckSum>>8) & 0x00ff );
Code = PutChar(Port, CheckSum & 0x00ff );
}
else Code = PutChar(Port,CheckSum );
/* wait for receivers ACK */
Code = GetChar(Port,10*ONESECOND);
if((char)Code==CAN)
{PacketError(Port,PacketNbr,Attempt,"Canceled by remote");
return(FALSE);
}
if((char)Code==ACK) return(TRUE);
if((char)Code!=NAK)
{PacketError(Port,PacketNbr,Attempt,"Out of sync");
return(FALSE);
}
/* Attempt again */
}/* end -- for(Attempt) */
/* can't send packet ! */
SayError(Port,"packet timeout");
return(FALSE);
} /* end -- TxPacket */
int RxPacket(Port,PacketNbr,PacketSizePtr,Buffer,CRCflag,EOTptr)
int Port; /* COM port [0..3] */
int PacketNbr; /* Packet # expected */
int *PacketSizePtr; /* Pointer to PacketSize received ( 128 or 1024) */
char Buffer[]; /* 1024 byte data buffer */
int CRCflag; /* use CRC if TRUE */
int *EOTptr; /* Pointer to EOT flag */
{int i;
int Code;
int Attempt;
int RxPacketNbr;
int RxPacketNbrComp;
unsigned short CheckSum;
unsigned short RxCheckSum;
unsigned short RxCheckSum1, RxCheckSum2;
char PacketType;
char temp[81];
/* begin */
#if 0
printf("[RxP: COM%d PacketNbr=%d CRCflag=%d EOTflag=%d]",
1+Port,PacketNbr,CRCflag,*EOTptr);
#endif
PacketNbr &= 0x00ff;
for(Attempt=1;Attempt<=MAXTRY;Attempt++)
{/* wait for SOH / STX */
Code = GetChar(Port,10*ONESECOND);
if(Code==-1)
{PacketError(Port,PacketNbr,Attempt,"timed out waiting for sender");
return(FALSE);
}
switch((char)Code)
{
case SOH:
/* 128 byte buffer incoming */
PacketType = SOH;
*PacketSizePtr = 128;
break;
case STX:
/* 1024 byte buffer incoming */
PacketType = STX;
*PacketSizePtr = 1024;
break;
case CAN:
/* sender has canceled ! */
SayError(Port,"Canceled by remote");
return(FALSE);
case EOT:
/* all packets have been sent */
Code = PutChar(Port,ACK);
*EOTptr = TRUE;
return(TRUE);
default:
/* error ! */
sprintf(temp,"Expecting SOH/STX/EOT/CAN not %xH",(char)Code);
PacketError(Port,PacketNbr,Attempt,temp);
return(FALSE);
}
/* receive packet # */
Code = GetChar(Port,ONESECOND);
if(Code==-1)
{PacketError(Port,PacketNbr,Attempt,"timed out waiting for packet #");
return(FALSE);
}
RxPacketNbr = 0x00ff & Code;
/* receive 1's complement */
Code = GetChar(Port,ONESECOND);
if(Code==-1)
{PacketError(Port,PacketNbr,Attempt,"timed out waiting for complement of packet #");
return(FALSE);
}
RxPacketNbrComp = 0x00ff & Code;
/* receive data */
CheckSum = 0;
for(i=0;i<*PacketSizePtr;i++)
{Code = GetChar(Port,ONESECOND);
if(Code==-1)
{PacketError(Port,PacketNbr,Attempt,"timed out waiting for data for packet #");
return(FALSE);
}
Buffer[i] = Code;
/* compute CRC or checksum */
if(CRCflag) CheckSum = UpdateCRC(CheckSum,Code);
else CheckSum = (CheckSum + Code) & 0x00ff;
}
/* receive CRC/checksum */
if(CRCflag)
{/* receive 2 byte CRC */
Code = GetChar(Port,ONESECOND);
if(Code==-1)
{PacketError(Port,PacketNbr,Attempt,"timed out waiting for 1st CRC byte");
return(FALSE);
}
RxCheckSum1 = Code & 0x00ff;
Code = GetChar(Port,ONESECOND);
if(Code==-1)
{PacketError(Port,PacketNbr,Attempt,"timed out waiting for 2nd CRC byte");
return(FALSE);
}
RxCheckSum2 = Code & 0x00ff;
RxCheckSum = (RxCheckSum1<<8) | RxCheckSum2;
}
else
{/* receive one byte checksum */
Code = GetChar(Port,ONESECOND);
if(Code==-1)
{PacketError(Port,PacketNbr,Attempt,"timed out waiting for checksum");
return(FALSE);
}
RxCheckSum = Code & 0x00ff;
}
/* packet # and checksum OK ? */
if((RxCheckSum==CheckSum)&&(RxPacketNbr==PacketNbr))
{/* ACK the packet */
PutChar(Port,ACK);
return(TRUE);
} /* end if */
/* bad packet */
DisplayLine("Bad Packet",NULL,0);
Code = PutChar(Port,NAK);
} /* end -- for(Attempt) */
/* can't receive packet */
SayError(Port,"RX packet timeout");
return(FALSE);
} /* end -- RxPacket */
PacketError(Port,Packet,Attempt,MsgPtr)
int Port;
int Packet;
int Attempt;
char *MsgPtr;
{char temp[81];
sprintf(temp,"Packet %d : Attempt %d : %s",Packet,Attempt,MsgPtr);
return( SayError(Port,temp) );
}
int TxStartup(Port,CRCflagPtr)
int Port;
int *CRCflagPtr;
{int i;
int Code;
/* clear Rx buffer */
SioRxFlush(Port);
/* wait for receivers start up NAK or 'C' */
for(i=1;i<LIMIT;i++)
{if(SioKeyPress())
{SayError(Port,"Aborted by user");
return(FALSE);
}
Code = GetChar(Port,3*ONESECOND);
if(Code==-1) continue;
/* received a byte */
if((char)Code==NAK)
{*CRCflagPtr = FALSE;
#if 0
printf("### TxStartup(CS) OK\n");
#endif
return(TRUE);
}
if((char)Code=='C')
{*CRCflagPtr = TRUE;
#if 0
printf("### TxStartup(CRC) OK\n");
#endif
return(TRUE);
}
} /* end -- for(i) */
/* no response */
SayError(Port,"No response from receiver");
return(FALSE);
} /* end -- TxStartup */
int RxStartup(Port,CRCflagPtr)
int Port;
int *CRCflagPtr;
{int i;
int Code;
/* clear Rx buffer */
SioRxFlush(Port);
/* Send NAKs or 'C's */
for(i=1;i<LIMIT;i++)
{if(SioKeyPress())
{SayError(Port,"Canceled by user");
return(FALSE);
}
/* stop attempting CRC after 1st 4 tries */
if((*CRCflagPtr)&&(i==5)) *CRCflagPtr = FALSE;
/* tell sender that I am ready to receive */
if(*CRCflagPtr) Code = PutChar(Port,'C');
else Code = PutChar(Port,NAK);
Code = GetChar(Port,3*ONESECOND);
if(Code==-1) continue;
/* no error -- must be incoming byte -- push byte back onto queue ! */
SioUnGetc(Port,Code);
#if 0
printf("### RxStartup OK\n");
#endif
return(TRUE);
} /* end -- for(i) */
/* no response */
SayError(Port,"No response from sender");
return(FALSE);
} /* end -- RxStartup */
int TxEOT(Port)
int Port;
{int i;
int Code;
for(i=0;i<10;i++)
{Code = PutChar(Port,EOT);
/* await response */
Code = GetChar(Port,3*ONESECOND);
if((char)Code==ACK) return(TRUE);
} /* end -- for(i) */
return(FALSE);
} /* end -- TxEOT */