コード例 #1
0
ファイル: main.cpp プロジェクト: CCJY/coliru
int main( int argc, char *argv[] ) {

    unsigned char test[] = "abcd";

    fprintf( stdout, "check '%s' of size %lu output: %u\n", "lol", sizeof test, checksum_crc16( test, sizeof test ) );

    exit( EXIT_SUCCESS );
}
コード例 #2
0
ファイル: msg.c プロジェクト: gmanwal/wsg50-ros-pkg
int msg_send( msg_t *msg )
{
	unsigned char header[MSG_PREAMBLE_LEN + 3];
	unsigned char checksum[2];
	unsigned short crc;
	int i, res;

	// Preamble
	for ( i = 0; i < MSG_PREAMBLE_LEN; i++ ) header[i] = MSG_PREAMBLE_BYTE;

	// Command ID
	header[MSG_PREAMBLE_LEN] = msg->id;

	// Length
	header[MSG_PREAMBLE_LEN + 1] = lo( msg->len );
	header[MSG_PREAMBLE_LEN + 2] = hi( msg->len );

	// Checksum
	crc = checksum_crc16( header, 6 );
	crc = checksum_update_crc16( msg->data, msg->len, crc );

	checksum[0] = lo( crc );
	checksum[1] = hi( crc );

	if ( interface->write )
	{

		unsigned char *buf = malloc( 6 + msg->len + 2 ); // 6+2 fixes (PREAMBLE, ID, PAILOAD / ... / CRC)
		memcpy( buf, header, 6 );
		memcpy( buf + 6, msg->data, msg->len );
		memcpy( buf + 6 + msg->len, (unsigned char *) &crc, 2 );

		res = interface->write( buf, 6 + msg->len + 2 );
		if ( res < 6 + msg->len + 2 )
		{
			interface->close();
			quit( "Failed to submit message checksum" );
		}

		free( buf );

// The following implementation doesn't work:
//
//		// Submit header
//		res = interface->write( header, 6 );
//		if ( res < 6 )
//		{
//			interface->close();
//			quit( "Failed to submit message header" );
//		}
//
//		// Submit payload
//		res = interface->write( msg->data, msg->len );
//		if ( res < msg->len )
//		{
//			interface->close();
//			quit( "Failed to submit message payload" );
//		}
//
//		res = interface->write( (unsigned char *) &crc, 2 );
//		if ( res < sizeof( crc ) )
//		{
//			interface->close();
//			quit( "Failed to submit message checksum" );
//		}

//		unsigned short check = checksum_crc16( header, 6 );
//		check = checksum_update_crc16( msg->data, msg->len, check );
//		check = checksum_update_crc16( (unsigned char *) &crc, sizeof( short ), check );

		return msg->len + 8;
	}

	return -1;
}