CRC16 Library

Lugar para comentar temas relacionados con librerías del entorno de desarrollo Arduino.
Avatar de Usuario
BotContacto
Mensajes: 0
Registrado: 14 Jun 2019, 12:55
Agradecido: 0
Agradecimiento recibido: 0

CRC16 Library

Mensaje sin leer por BotContacto »

Name: Arne Spangenberg
Subject: CRC16 Library
Has entered the following message into the contact form:
Hi,

my name is Arne, I live in Juelich, Germany, and want to equip a high altitude balloon with a radio tracker.
These radio trackers transmit the data of the flying balloon to distributed listeners wherever the balloon can be heard by radio recievers; and then the telemetry data is uploaded stored in a database (www.habhub.org)
Before this is done, the data (a string) is checked with a checksum; this checksum then appended to the data string.
I did this already, based on an Arduino Uno, and all worked fine: data acquisition, checksum generatioin, radio transmission, upload and storage in the habhub database.
As recommended by www.ukhas.org.uk, I used the #include <util/crc16.h> on the arduino platform.

For several reasons, I now intend to build such a tracker based on a Adafruit Feather M0 Adalogger.
I/Os, GPS and external sensors already work fine, data can also be stored on SD card.
Only the checksum generation like on the Arduino does not work; i.e., #include <util/crc16.h> cannot be compiled on the Feather.

With excitement I found your platform-independent checksum generator in the Arduino libraries, sent it to the Feather – and voilá: It works great.
It works, but the checksum which is generated is not compatible to the requirement of the balloon server.

All requirements can be found here:

https://ukhas.org.uk/communication:protocol?s[]=crc16

Example of a CRC16_CCITT'd string:
$$hadie,181,10:42:10,54.422829,-6.741293,27799.3,1:10*002A

The first two $$ are ignored in checksum generation. The appended checksum is separated from the datastring with the *

My question is: Is there a way to use your library in such a manner that the Checksum is generated like in the example above?

Your help is highly appreciated, as no other variant of the CRC16 is accepted by the habhub server. There are some others allowed, but also for these I did not find a code that works…

Maybe you can help? I am very happy to hearing from you.

Best wishes

Arne
Mensaje enviado desde el formulario de contacto del foro.

Tags:
Avatar de Usuario
Naguissa
Administrador del Sitio
Mensajes: 502
Registrado: 04 Jul 2016, 11:17
Agradecido: 110 veces
Agradecimiento recibido: 134 veces

Re: CRC16 Library

Mensaje sin leer por Naguissa »

I'll take a look ASAP. But if the only problem are $ symbols you could trim that part.
Avatar de Usuario
Naguissa
Administrador del Sitio
Mensajes: 502
Registrado: 04 Jul 2016, 11:17
Agradecido: 110 veces
Agradecimiento recibido: 134 veces

Re: CRC16 Library

Mensaje sin leer por Naguissa »

I've been checking and both algorithms uses different polynomial and maybe different initial value.

Following instructions I've made two functions to calculate it and works, at least in a Arduino Nano (I cannot test in other devices right now).

Código: Seleccionar todo

#include "Arduino.h"

uint16_t my_crc_xmodem_update (uint16_t crc, uint8_t data) {
  int i;
  crc = crc ^ ((uint16_t)data << 8);
  for (i=0; i<8; i++) {
    if (crc & 0x8000) {
      crc = (crc << 1) ^ 0x1021;
    } else {
      crc <<= 1;
    }
  }
  return crc;
}

uint16_t gps_CRC16_checksum (char *string) {
  size_t i, len = strlen(string);
  uint16_t crc = 0xFFFF;
  uint8_t c;
 
  // Calculate checksum ignoring the first two $s
  for (i = 2; i < len; i++) {
    c = string[i];
    crc = my_crc_xmodem_update (crc, c);
  }
 
  return crc;
}


void setup() {
    Serial.begin(9600);
    while (!Serial) {
      ; // wait for serial port to connect. Needed for Leonardo only
    }

	char string[] = "$$hadie,181,10:42:10,54.422829,-6.741293,27799.3,1:10\0";

	Serial.print("The crc of \"");
	Serial.print(string);
	Serial.print("\" is 0x002A. crc16 returned ");
	Serial.println(gps_CRC16_checksum(string), HEX);
	Serial.println();


	Serial.println(" - END -");
	Serial.println();

}


void loop() {
}

Result is:

Código: Seleccionar todo

The crc of "$$hadie,181,10:42:10,54.422829,-6.741293,27799.3,1:10" is 0x002A. crc16 returned 2A

 - END -

Take in mind I'm using Serial.print(CRC, HEX) directly so it trims leading zeros.

Key functions are my_crc_xmodem_update and gps_CRC16_checksum.



Cheers!
Estos usuarios agradecieron al autor Naguissa por el mensaje:
Daniel
Valoración: 33%
Avatar de Usuario
Naguissa
Administrador del Sitio
Mensajes: 502
Registrado: 04 Jul 2016, 11:17
Agradecido: 110 veces
Agradecimiento recibido: 134 veces

Re: CRC16 Library

Mensaje sin leer por Naguissa »

Me han confirmado que esta solución funciona perfectamente.

Según la documentación el algoritmo usado es el CRC16 XMODEM.
Estos usuarios agradecieron al autor Naguissa por el mensaje:
Daniel
Valoración: 33%
Avatar de Usuario
Naguissa
Administrador del Sitio
Mensajes: 502
Registrado: 04 Jul 2016, 11:17
Agradecido: 110 veces
Agradecimiento recibido: 134 veces

Re: CRC16 Library

Mensaje sin leer por Naguissa »

He creado una librería llamada uCRC16XModemLib y la he enviado al gestor de librerías de Arduino para que la tengáis disponible en el IDE Arduino. Así podéis usar la librería cómodamente desde vuestro programa.

Detalles aquí:
  • Similar Topics
    Respuestas
    Vistas
    Último mensaje