-
xmodem crc
hello,
I want to send a file with xmodem crc.
After the first "C" from the reciever, I prepare the first packet and send it to the reciever. But then I get another "C" instead a "NAK" or an "ACK".
Has anybody an idea, or a working example for windows/dos with the source code.
Thanks a lot.
cu hick.hack
-
yes, I know already what the xmodem protocol is for use.
But it dosn't work in my code.
Want I want is to send a file is the same way like hyperterminal.
Here is my code for preparing the xmodem block.
Code:
switch (szBuffer[0])
{
case 'C': // 'C' means the reciever want data in XModem CRC
packetno = 1;
filepointer = fopen("start.sre", "rb"); // open the file for binary read
while (!feof(filepointer))
{
// xbuff[0] = SOH; bufsz = 128; // 128 Byte block size
xbuff[0] = STX; bufsz = 1024; // 1024 Byte block size
xbuff[1] = packetno;
xbuff[2] = ~packetno;
memset (&xbuff[3], 0, bufsz);
numread = fread( &xbuff[3], sizeof( char ), bufsz, filepointer);
if (numread < bufsz) xbuff[3+numread] = CTRLZ;
// calculate the crc checksum
unsigned short ccrc = calcrc((char *) &xbuff[3], bufsz);
xbuff[bufsz+3] = (ccrc>>8) & 0xFF;
xbuff[bufsz+4] = ccrc & 0xFF;
do
{
// the serial class is from this homepage
// http://home.ict.nl/~ramklein/Projects/Serial.html
serial.Write(&xbuff[0], bufsz + 5);
serial.Read(szBuffer, sizeof(szBuffer)-1, &dwBytesRead);
if (dwBytesRead > 0)
{
switch (szBuffer[0])
{
case NAK: printf ("%d NAK recieved \n", szBuffer[0]);
break;
case ACK: printf ("%d ACK recieved \n", szBuffer[0]);
break;
case CTRLZ: printf ("%d Cancel by remote \n", szBuffer[0]);
break;
default: printf ("%d Unknown \n", szBuffer[0]);
break;
}
}
} while ((szBuffer[0] != ACK));
// If sending was succesfully
packetno++;
if (packetno >= 256) packetno = 0;
}
// the whole file was send
fclose (filepointer);
}
and here is the code for the calccrc function
Code:
int calcrc(char *ptr, int count)
{
int crc, i;
crc = 0;
while (--count >= 0) {
crc = crc ^ (int)*ptr++ << 8;
for (i = 0; i < 8; ++i)
if (crc & 0x8000)
crc = crc << 1 ^ 0x8408;
else
crc = crc << 1;
}
return (crc & 0xFFFF);
}
thanks for your help