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
Classes | Macros | Typedefs | Functions
ascii_serial_com.h File Reference
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "asc_exception.h"
#include "circular_buffer.h"
Include dependency graph for ascii_serial_com.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  __ascii_serial_com_struct
 ASCII Serial Com Interface State struct. More...
 

Macros

#define MAXMESSAGELEN   64
 
#define MAXDATALEN   54
 
#define MAXSPAYLOADEN   (MAXDATALEN - 3)
 
#define NCHARCHECKSUM   4
 

Typedefs

typedef struct __ascii_serial_com_struct ascii_serial_com
 ASCII Serial Com Interface State struct.
 

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_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 ascii_serial_com_compute_checksum (ascii_serial_com *asc, char *checksumOut, bool outputBuffer)
 ASCII Serial Com compute checksum of message.
 
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

◆ MAXDATALEN

#define MAXDATALEN   54

Definition at line 15 of file ascii_serial_com.h.

◆ MAXMESSAGELEN

#define MAXMESSAGELEN   64

Definition at line 14 of file ascii_serial_com.h.

◆ MAXSPAYLOADEN

#define MAXSPAYLOADEN   (MAXDATALEN - 3)

Definition at line 16 of file ascii_serial_com.h.

◆ NCHARCHECKSUM

#define NCHARCHECKSUM   4

Definition at line 17 of file ascii_serial_com.h.

Typedef Documentation

◆ ascii_serial_com

ASCII Serial Com Interface State struct.

Keeps track of the state of the ASCII Serial Com interface

Normally, for a device, usage would go something like:

1) allocate the ascii_serial_com

2) initialize it with ascii_serial_com_init

3) Get the input buffer with ascii_serial_com_get_input_buffer and output buffer with ascii_serial_com_get_output_buffer

4) poll on the input stream/file/peripheral (and output stream/file/peripheral if the output buffer is not empty)

5) read from the input stream/file/peripheral

6) push_back or push_back_block what is read to the input_buffer

7) run ascii_serial_com_get_message_from_input_buffer, if a message is received (unpacked) then act on it (possibly reply with ascii_serial_com_put_message_in_output_buffer)

8) pop from output_buffer and write to output stream/file/peripheral

9) go back to 4)

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().