Arduhdlc is an HDLC library for Arduino. Library is modified from several different HDLC implementations, mainly the HDLC library of Piconomic FW Library by Pieter Conradie. This HDLC library can be used over any communications bus, like SPI, I2C, USB or user defined bit banging interface. This is possible, cause the library only has two functions, to receive/validate received frame, and to wrap given arbitrary data into a HDLC frame. It is upt to the user what to do with those frames.
What is modified from other implementations, is the used CRC16 calculation, which is changed to _crc_ccitt_update() -function from avrlibc. This function produces the same CRC sum as the Qt CRC function qChecksum(). This makes it possible to easily implement communication between Qt GUI program in PC and Arduino, over the serial port.
The Arduino HDLC Example Project also includes HDLC C++ Class for use in Qt. The Qt C++ HDLC class has exactly the same methods as the Arduino version, but it is implemented as a singleton class.
How to use Arduino HDLC Library
The library requires the user to define two functions, one for handling the received valid frame, and one for sending out frame byte at a time. Also, all data received through serial port must be passed to the HDLC byte receiving function, so that the frame can be identified and validated. The function that must get all received data is called charReceiver(uint8_t data) and the function that is used to wrap data to HDLC frame, to be sent out, is called frameDecode(const char *framebuffer, uint8_t frame_length). HDLC frame handling function is user defined function, which the library passes the valid frame it found from the received data stream. HDLC class is then initialized by passing it pointers to the user defined sending function and received frame handler. So the HDLC library does not define the frame content structure at all, it is up to the user. User can transmit ASCII characters, binary data like serialized C variables, structs or audio stream, depending on the application.
Arduino HDLC Example Project
The Arduino HDLC Example Project assumes that the user defined frame structure is [8 bit command byte][arbitrary data] and it includes a command router or command dispatcher to find a command and execute it by passing the data to it. Each command then defines what is in the data and handles or parses it.
Download the library files (Arduhdlc.c and Arduhdlc.h) into your project. Include the header file and implement the two required functions for handling a received HDLC frame and sending the frame (byte at a time).
[cpp]#include “Arduhdlc.h”
#define MAX_HDLC_FRAME_LENGTH 32
void send_character(uint8_t data);
void hdlc_command_router(const uint8_t *framebuffer, uint16_t framelength);
Arduhdlc hdlc(&send_character, &hdlc_command_router, MAX_HDLC_FRAME_LENGTH);
void send_character(uint8_t data) {
Serial.print((char) data);
}
void hdlc_command_router(const uint8_t *framebuffer, uint16_t framelength) {
// Do somethin here with the data in buffer
}
void setup() {
// TX pin to output
pinMode(1, OUTPUT);
// initialize serial:
Serial.begin(9600);
}
void loop() {
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char) Serial.read();
hdlc.hdlc_on_rx_byte(inChar);
}
}[/cpp]
Recent Comments