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
|
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_uint8 * | ascii_serial_com_get_input_buffer (ascii_serial_com *asc) |
ASCII Serial Com get input buffer. | |
circular_buffer_uint8 * | ascii_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 | |
#define crc_bbb 0 |
Definition at line 3 of file ascii_serial_com.c.
#define crc_bbf 1 |
Definition at line 4 of file ascii_serial_com.c.
#define crc_crcmod 3 |
Definition at line 6 of file ascii_serial_com.c.
#define crc_finalize crc_16_dnp_tbl4bit_finalize |
Definition at line 23 of file ascii_serial_com.c.
#define crc_init crc_16_dnp_tbl4bit_init |
Definition at line 21 of file ascii_serial_com.c.
#define crc_option crc_tbl4bit |
Definition at line 7 of file ascii_serial_com.c.
#define crc_tbl4bit 2 |
Definition at line 5 of file ascii_serial_com.c.
#define crc_update crc_16_dnp_tbl4bit_update |
Definition at line 22 of file ascii_serial_com.c.
#define error_message_max_copy_data_len 9 |
Definition at line 35 of file ascii_serial_com.c.
#define max_remove_unfinished_frames_tries 10 |
Definition at line 36 of file ascii_serial_com.c.
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 '.'.
asc | is a pointer to an initialized ascii_serial_com struct |
checksumOut | pointer to already initialized buffer NCHARCHECKSUM long |
outputBuffer | if 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().
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.
asc | is a pointer to an initialized ascii_serial_com struct |
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().
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
asc | is 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
ascVersion | the single char ASCII-Serial-Com version in the message will be written here |
appVersion | the single char application version in the message will be written here |
command | the single char command will be written to this byte |
data | The message data will be put here. Should point to a MAXDATALEN long buffer |
dataLen | The 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().
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.
asc | is a pointer to an initialized ascii_serial_com struct |
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().
void ascii_serial_com_init | ( | ascii_serial_com * | asc | ) |
ASCII Serial Com Interface init method.
Initialize interface
asc | is a pointer to an uninitialized ascii_serial_com struct |
Definition at line 38 of file ascii_serial_com.c.
References circular_buffer_init_uint8(), __ascii_serial_com_struct::ignoreCRCMismatch, __ascii_serial_com_struct::in_buf, __ascii_serial_com_struct::out_buf, __ascii_serial_com_struct::raw_buffer, __ascii_serial_com_struct::receive_stream_frame_counter, __ascii_serial_com_struct::receive_stream_frame_counter_initialized, and __ascii_serial_com_struct::send_stream_frame_counter.
Referenced by ascii_serial_com_device_init().
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().
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)
asc | is a pointer to an initialized ascii_serial_com struct |
ascVersion | the single char ASCII Serial Com version (probably '0') |
appVersion | the single char application version (user application info) |
command | the single char command will be written to this byte |
data | The message data |
dataLen | The 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().
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.
asc | is a pointer to an initialized ascii_serial_com struct |
ascVersion | the single char ASCII Serial Com version (probably '0') |
appVersion | the single char application version (user application info) |
data | The message data |
dataLen | The 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().
void ascii_serial_com_set_ignore_CRC_mismatch | ( | ascii_serial_com * | asc | ) |
Definition at line 258 of file ascii_serial_com.c.
void ascii_serial_com_unset_ignore_CRC_mismatch | ( | ascii_serial_com * | asc | ) |
Definition at line 262 of file ascii_serial_com.c.
uint16_t convert_hex_to_uint16 | ( | const char * | instr | ) |
convert hex string to uint16
Converts hex string to uint16
str | a pointer to a 4-byte long string that holds the hex input |
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().
uint32_t convert_hex_to_uint32 | ( | const char * | instr | ) |
convert hex string to uint32
Converts hex string to uint32
str | a pointer to a 8-byte long string that holds the hex input |
May throw ASC_ERROR_NOT_HEX_CHAR
Definition at line 320 of file ascii_serial_com.c.
References convert_hex_to_uint8().
uint8_t convert_hex_to_uint8 | ( | const char * | instr | ) |
convert hex string to uint8
Converts hex string to uint8
str | a pointer to a 2-byte long string that holds the hex input |
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().
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
num | the number to convert to hex |
outstr | a pointer to a 4-byte long string that will hold the result |
caps | hex 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().
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
num | the number to convert to hex |
outstr | a pointer to a 8-byte long string that will hold the result |
caps | hex 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().
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
num | the number to convert to hex |
outstr | a pointer to a 2-byte long string that will hold the result |
caps | hex 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().