Ejemplo n.º 1
0
int main(void)
  {
    int i, j;
    unsigned short CRC_Xmit;
    unsigned short CRC_Recv;
    int Detected_Count = 0;
    int Okay_Count     = 0;

    /* Initialize. */
    for (i = 0; i < 1024; i++) {
      Raw_Data[i] = (unsigned char)i;
    }
    CRC_Xmit = CRC_Clear();
    Initialize_Noise(1.0e-5);

    /* Compute the CRC checksum. */
    for (i = 0; i < 1024; i++) {
      CRC_Xmit = CRC_Update(CRC_Xmit, Raw_Data[i]);
    }
    CRC_Xmit = CRC_Finish(CRC_Xmit);

    /* Now loop many times sending the block of data through the channel. */
    for (i = 0; i < 1024; i++) {
      CRC_Recv = CRC_Clear();

      for (j = 0; j < 1024; j++) {
        CRC_Recv = CRC_Update(CRC_Recv, Channel(Raw_Data[j]));
      }
      CRC_Recv = CRC_Update(CRC_Recv, Channel((CRC_Xmit & 0xFF00) >> 8));
      CRC_Recv = CRC_Update(CRC_Recv, Channel(CRC_Xmit & 0x00FF));

      if (CRC_Recv != 0) Detected_Count++;
        else Okay_Count++;
    }

    printf("Blocks with detected errors: %d\n", Detected_Count);
    printf("Blocks okay: %d\n", Okay_Count);

    return 0;
  }
Ejemplo n.º 2
0
dsk_err_t w32serial_call(DSK_PDRIVER pDriver, unsigned char *input, 
		int inp_len, unsigned char *output, int *out_len)
{
	word16 wire_len;
	word16 crc;
	byte var;
	byte wvar[2];
	int n;
	dsk_err_t err;
	unsigned char *tmpbuf;

	W32SERIAL_REMOTE_DATA *self = (W32SERIAL_REMOTE_DATA *)pDriver->dr_remote;	
	if (!self || self->super.rd_class != &rpc_w32serial) return DSK_ERR_BADPTR;
	/* CRC tbe input... */
	wire_len = inp_len;
	CRC_Clear();
	for (n = 0; n < inp_len; n++) CRC_Update(input[n]);
	crc = CRC_Done();
/* 
	printf("rpc_w32serial: Input packet: ");
	for (n = 0; n < inp_len; n++) printf("%02x ", input[n]);
	printf("\n");
*/
	while (1)
	{
		/* Write SOH (start of request) */
		err = wrbyte(self, SOH); if (err) return err;
		/* Write packet length (network byte order) */
		wvar[0] = wire_len >> 8; 
		wvar[1] = wire_len & 0xFF;
		err = write_bytes(self, 2, wvar); 
		if (err) return err;
		err = write_bytes(self, inp_len, input);
		if (err) return err;
		wvar[0] = crc >> 8; 
		wvar[1] = crc & 0xFF;
		err = write_bytes(self, 2, wvar); 
		if (err) return err;

/*
		err = read_bytes(self, 1, &var); if (err) return err;
*/
/* Wait 'til we get an ACK or a NAK. */
		do
		{
			err = read_bytes(self, 1, &var); 
			if (err) return err;
		}
		while(var != ACK && var != NAK);
		if (var == ACK) break;
		if (var == NAK) continue;
	}
	/* Outgoing packet sent. Await response */
	while (1)
	{
		/* First byte of response must be STX. */
		err = read_bytes(self, 1, &var); if (err) return err;
		if (var != STX) continue;
		err = read_bytes(self, 2, wvar); if (err) return err;
		wire_len   = wvar[0];
		wire_len   = (wire_len << 8) | wvar[1];
		tmpbuf = dsk_malloc(wire_len + 2);
		if (!tmpbuf) return DSK_ERR_NOMEM;
		CRC_Clear();
		err = read_bytes(self, wire_len + 2, tmpbuf); 
		if (err) { dsk_free(tmpbuf); return err; }
		for (n = 0; n < wire_len; n++)
		{
			CRC_Update(tmpbuf[n]);
		}
		crc = tmpbuf[wire_len];
		crc = (crc << 8) | tmpbuf[wire_len + 1];
		/* If CRC matches, send ACK and return. Else send NAK. */
		if (crc == CRC_Done())
		{
/*
	printf("rpc_w32serial: Result packet: ");
	for (n = 0; n < wire_len; n++) printf("%02x ", tmpbuf[n]);
	printf("\n");
*/
			err = wrbyte(self, ACK);
			if (err) { dsk_free(tmpbuf); return err; }
/* Copy packet to waiting output buffer */
			if (wire_len < *out_len) *out_len = wire_len;
			memcpy(output, tmpbuf, *out_len);
			dsk_free(tmpbuf);
			return DSK_ERR_OK;
		}
/* Packet was garbled. NAK it and try again. */
		dsk_free(tmpbuf);
		err = wrbyte(self, NAK);
		if (err) return err;
	}
	/* Should never happen */
	return DSK_ERR_NOTIMPL;
}