ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
Macros | Functions
ascii_serial_com.c File Reference
#include "ascii_serial_com.h"
#include "crc_16_dnp_tbl4bit.h"
#include <inttypes.h>
Include dependency graph for ascii_serial_com.c:

Go to the source code of this file.

Macros

#define crc_bbb   0
 
#define crc_bbf   1
 
#define crc_tbl4bit   2
 
#define crc_crcmod   3
 
#define crc_option   crc_tbl4bit
 
#define crc_init   crc_16_dnp_tbl4bit_init
 
#define crc_update   crc_16_dnp_tbl4bit_update
 
#define crc_finalize   crc_16_dnp_tbl4bit_finalize
 
#define error_message_max_copy_data_len   9
 
#define max_remove_unfinished_frames_tries   10
 

Functions

void ascii_serial_com_init (ascii_serial_com *asc)
 ASCII Serial Com Interface init method.
 
void ascii_serial_com_put_message_in_output_buffer (ascii_serial_com *asc, char ascVersion, char appVersion, char command, const char *data, size_t dataLen)
 ASCII Serial Com Pack and put message in output buffer.
 
void ascii_serial_com_get_message_from_input_buffer (ascii_serial_com *asc, char *ascVersion, char *appVersion, char *command, char *data, size_t *dataLen)
 ASCII Serial Com pop message from input buffer and unpack.
 
circular_buffer_uint8ascii_serial_com_get_input_buffer (ascii_serial_com *asc)
 ASCII Serial Com get input buffer.
 
circular_buffer_uint8ascii_serial_com_get_output_buffer (ascii_serial_com *asc)
 ASCII Serial Com get output buffer.
 
void ascii_serial_com_compute_checksum (ascii_serial_com *asc, char *checksumOut, bool outputBuffer)
 ASCII Serial Com compute checksum of message.
 
void ascii_serial_com_put_s_message_in_output_buffer (ascii_serial_com *asc, char ascVersion, char appVersion, const char *data, size_t dataLen)
 ASCII Serial Com Pack and put 's' message in output buffer.
 
void ascii_serial_com_put_error_in_output_buffer (ascii_serial_com *asc, char ascVersion, char appVersion, char command, char *data, size_t dataLen, enum asc_exception errorCode)
 ASCII Serial Com put error message in out buffer.
 
void ascii_serial_com_set_ignore_CRC_mismatch (ascii_serial_com *asc)
 
void ascii_serial_com_unset_ignore_CRC_mismatch (ascii_serial_com *asc)
 
void convert_uint8_to_hex (uint8_t num, char *outstr, bool caps)
 convert uint8 to hex string
 
void convert_uint16_to_hex (uint16_t num, char *outstr, bool caps)
 convert uint16 to hex string
 
void convert_uint32_to_hex (uint32_t num, char *outstr, bool caps)
 convert uint32 to hex string
 
uint8_t convert_hex_to_uint8 (const char *instr)
 convert hex string to uint8
 
uint16_t convert_hex_to_uint16 (const char *instr)
 convert hex string to uint16
 
uint32_t convert_hex_to_uint32 (const char *instr)
 convert hex string to uint32
 

Macro Definition Documentation

◆ crc_bbb

#define crc_bbb   0

Definition at line 3 of file ascii_serial_com.c.

◆ crc_bbf

#define crc_bbf   1

Definition at line 4 of file ascii_serial_com.c.

◆ crc_crcmod

#define crc_crcmod   3

Definition at line 6 of file ascii_serial_com.c.

◆ crc_finalize

#define crc_finalize   crc_16_dnp_tbl4bit_finalize

Definition at line 23 of file ascii_serial_com.c.

◆ crc_init

#define crc_init   crc_16_dnp_tbl4bit_init

Definition at line 21 of file ascii_serial_com.c.

◆ crc_option

#define crc_option   crc_tbl4bit

Definition at line 7 of file ascii_serial_com.c.

◆ crc_tbl4bit

#define crc_tbl4bit   2

Definition at line 5 of file ascii_serial_com.c.

◆ crc_update

#define crc_update   crc_16_dnp_tbl4bit_update

Definition at line 22 of file ascii_serial_com.c.

◆ error_message_max_copy_data_len

#define error_message_max_copy_data_len   9

Definition at line 35 of file ascii_serial_com.c.

◆ max_remove_unfinished_frames_tries

#define max_remove_unfinished_frames_tries   10

Definition at line 36 of file ascii_serial_com.c.

Function Documentation

◆ ascii_serial_com_compute_checksum()

void ascii_serial_com_compute_checksum ( ascii_serial_com * asc,
char * checksumOut,
bool outputBuffer )

ASCII Serial Com compute checksum of message.

Computes the checksum of the last message in the input or output buffer. Specifically finds the last substring starting with the last '>' and ending in the last '.'.

Parameters
ascis a pointer to an initialized ascii_serial_com struct
checksumOutpointer to already initialized buffer NCHARCHECKSUM long
outputBufferif true, use output buffer, if false use input buffer

Raises ASC_ERROR_INVALID_FRAME or ASC_ERROR_INVALID_FRAME_PERIOD if frame invalid

Definition at line 173 of file ascii_serial_com.c.

References circular_buffer_find_first_uint8(), circular_buffer_find_last_uint8(), circular_buffer_get_blocks_uint8(), circular_buffer_get_size_uint8(), convert_uint16_to_hex(), __ascii_serial_com_struct::in_buf, and __ascii_serial_com_struct::out_buf.

Referenced by ascii_serial_com_get_message_from_input_buffer(), and ascii_serial_com_put_message_in_output_buffer().

◆ ascii_serial_com_get_input_buffer()

circular_buffer_uint8 * ascii_serial_com_get_input_buffer ( ascii_serial_com * asc)

ASCII Serial Com get input buffer.

Get a pointer to the input buffer.

Parameters
ascis a pointer to an initialized ascii_serial_com struct
Returns
a pointer to the input buffer.

Definition at line 164 of file ascii_serial_com.c.

References __ascii_serial_com_struct::in_buf.

Referenced by ascii_serial_com_device_get_input_buffer().

◆ ascii_serial_com_get_message_from_input_buffer()

void ascii_serial_com_get_message_from_input_buffer ( ascii_serial_com * asc,
char * ascVersion,
char * appVersion,
char * command,
char * data,
size_t * dataLen )

ASCII Serial Com pop message from input buffer and unpack.

Unpacks the first message found in the input buffer

Parameters
ascis a pointer to an initialized ascii_serial_com struct

All other parameters are outputs. Command will be set to \0 if no message found in buffer

Parameters
ascVersionthe single char ASCII-Serial-Com version in the message will be written here
appVersionthe single char application version in the message will be written here
commandthe single char command will be written to this byte
dataThe message data will be put here. Should point to a MAXDATALEN long buffer
dataLenThe length of the data put in data

May raise ASC_ERROR_INVALID_FRAME or ASC_ERROR_INVALID_FRAME_PERIOD

Definition at line 77 of file ascii_serial_com.c.

References ascii_serial_com_compute_checksum(), circular_buffer_find_first_uint8(), circular_buffer_get_element_uint8(), circular_buffer_get_size_uint8(), circular_buffer_pop_front_uint8(), circular_buffer_remove_front_unfinished_frames_uint8(), __ascii_serial_com_struct::ignoreCRCMismatch, and __ascii_serial_com_struct::in_buf.

Referenced by ascii_serial_com_device_receive().

◆ ascii_serial_com_get_output_buffer()

circular_buffer_uint8 * ascii_serial_com_get_output_buffer ( ascii_serial_com * asc)

ASCII Serial Com get output buffer.

Get a pointer to the output buffer.

Parameters
ascis a pointer to an initialized ascii_serial_com struct
Returns
a pointer to the output buffer.

Definition at line 169 of file ascii_serial_com.c.

References __ascii_serial_com_struct::out_buf.

Referenced by ascii_serial_com_device_get_output_buffer().

◆ ascii_serial_com_init()

void ascii_serial_com_init ( ascii_serial_com * asc)

◆ ascii_serial_com_put_error_in_output_buffer()

void ascii_serial_com_put_error_in_output_buffer ( ascii_serial_com * asc,
char ascVersion,
char appVersion,
char command,
char * data,
size_t dataLen,
enum asc_exception errorCode )

ASCII Serial Com put error message in out buffer.

Called when you want to return an error message related to some input message

The same parameters as ascii_serial_com_put_message_in_output_buffer, except data isn't const (should it be const?) and errorCode

Definition at line 239 of file ascii_serial_com.c.

References ascii_serial_com_put_message_in_output_buffer(), and convert_uint8_to_hex().

Referenced by ascii_serial_com_device_receive(), and ascii_serial_com_register_pointers_handle_message().

◆ ascii_serial_com_put_message_in_output_buffer()

void ascii_serial_com_put_message_in_output_buffer ( ascii_serial_com * asc,
char ascVersion,
char appVersion,
char command,
const char * data,
size_t dataLen )

ASCII Serial Com Pack and put message in output buffer.

Packs the message into the output format and push it onto the output buffer USER'S RESPONSIBILITY TO MAKE SURE MESSAGE CAN FIT IN OUTPUT CIRCULAR BUFFER. Message length is dataLen + (MAXMESSAGELEN-MAXDATALEN)

Parameters
ascis a pointer to an initialized ascii_serial_com struct
ascVersionthe single char ASCII Serial Com version (probably '0')
appVersionthe single char application version (user application info)
commandthe single char command will be written to this byte
dataThe message data
dataLenThe length of the data

May raise ASC_ERROR_DATA_TOO_LONG or the errors ascii_serial_com_compute_checksum raises

Definition at line 50 of file ascii_serial_com.c.

References ascii_serial_com_compute_checksum(), circular_buffer_push_back_uint8(), and __ascii_serial_com_struct::out_buf.

Referenced by ascii_serial_com_device_put_message_in_output_buffer(), ascii_serial_com_put_error_in_output_buffer(), ascii_serial_com_put_s_message_in_output_buffer(), ascii_serial_com_register_block_handle_message(), and ascii_serial_com_register_pointers_handle_message().

◆ ascii_serial_com_put_s_message_in_output_buffer()

void ascii_serial_com_put_s_message_in_output_buffer ( ascii_serial_com * asc,
char ascVersion,
char appVersion,
const char * data,
size_t dataLen )

ASCII Serial Com Pack and put 's' message in output buffer.

Packs the message into the output format and push it onto the output buffer USER'S RESPONSIBILITY TO MAKE SURE MESSAGE CAN FIT IN OUTPUT CIRCULAR BUFFER. Message length is dataLen + (MAXMESSAGELEN-MAXDATALEN)

This sends streaming data. If this is a device, you should only do this once you've received an 'n' message and stop after receiving an 'f' message.

Parameters
ascis a pointer to an initialized ascii_serial_com struct
ascVersionthe single char ASCII Serial Com version (probably '0')
appVersionthe single char application version (user application info)
dataThe message data
dataLenThe length of the data, must be <= MAXSPAYLOADEN

May raise ASC_ERROR_DATA_TOO_LONG or the errors ascii_serial_com_compute_checksum raises

Definition at line 219 of file ascii_serial_com.c.

References ascii_serial_com_put_message_in_output_buffer(), convert_uint8_to_hex(), and __ascii_serial_com_struct::send_stream_frame_counter.

Referenced by ascii_serial_com_device_put_s_message_in_output_buffer().

◆ ascii_serial_com_set_ignore_CRC_mismatch()

void ascii_serial_com_set_ignore_CRC_mismatch ( ascii_serial_com * asc)

Definition at line 258 of file ascii_serial_com.c.

◆ ascii_serial_com_unset_ignore_CRC_mismatch()

void ascii_serial_com_unset_ignore_CRC_mismatch ( ascii_serial_com * asc)

Definition at line 262 of file ascii_serial_com.c.

◆ convert_hex_to_uint16()

uint16_t convert_hex_to_uint16 ( const char * instr)

convert hex string to uint16

Converts hex string to uint16

Parameters
stra pointer to a 4-byte long string that holds the hex input
Returns
the uint16_t

May throw ASC_ERROR_NOT_HEX_CHAR

Definition at line 312 of file ascii_serial_com.c.

References convert_hex_to_uint8().

Referenced by ascii_serial_com_register_block_handle_message(), and ascii_serial_com_register_pointers_handle_message().

◆ convert_hex_to_uint32()

uint32_t convert_hex_to_uint32 ( const char * instr)

convert hex string to uint32

Converts hex string to uint32

Parameters
stra pointer to a 8-byte long string that holds the hex input
Returns
the uint32_t

May throw ASC_ERROR_NOT_HEX_CHAR

Definition at line 320 of file ascii_serial_com.c.

References convert_hex_to_uint8().

◆ convert_hex_to_uint8()

uint8_t convert_hex_to_uint8 ( const char * instr)

convert hex string to uint8

Converts hex string to uint8

Parameters
stra pointer to a 2-byte long string that holds the hex input
Returns
the uint8_t

May throw ASC_ERROR_NOT_HEX_CHAR

Definition at line 293 of file ascii_serial_com.c.

Referenced by convert_hex_to_uint16(), and convert_hex_to_uint32().

◆ convert_uint16_to_hex()

void convert_uint16_to_hex ( uint16_t num,
char * outstr,
bool caps )

convert uint16 to hex string

Converts uint16 to capital hex string, 4-bytes long, zero padded

Parameters
numthe number to convert to hex
outstra pointer to a 4-byte long string that will hold the result
capshex letters are caps A-F if true, and lowercase a-f if false

Definition at line 281 of file ascii_serial_com.c.

References convert_uint8_to_hex().

Referenced by ascii_serial_com_compute_checksum().

◆ convert_uint32_to_hex()

void convert_uint32_to_hex ( uint32_t num,
char * outstr,
bool caps )

convert uint32 to hex string

Converts uint32 to capital hex string, 8-bytes long, zero padded

Parameters
numthe number to convert to hex
outstra pointer to a 8-byte long string that will hold the result
capshex letters are caps A-F if true, and lowercase a-f if false

Definition at line 287 of file ascii_serial_com.c.

References convert_uint8_to_hex().

◆ convert_uint8_to_hex()

void convert_uint8_to_hex ( uint8_t num,
char * outstr,
bool caps )

convert uint8 to hex string

Converts uint8 to capital hex string, 2-bytes long, zero padded

Parameters
numthe number to convert to hex
outstra pointer to a 2-byte long string that will hold the result
capshex letters are caps A-F if true, and lowercase a-f if false

Definition at line 268 of file ascii_serial_com.c.

Referenced by ascii_serial_com_put_error_in_output_buffer(), ascii_serial_com_put_s_message_in_output_buffer(), convert_uint16_to_hex(), and convert_uint32_to_hex().