00001 /*! \file stxetx.h \brief STX/ETX Packet Protocol Implementation Library. */ 00002 //***************************************************************************** 00003 // 00004 // File Name : 'stxetx.h' 00005 // Title : STX/ETX Packet Protocol Implementation Library 00006 // Author : Pascal Stang - Copyright (C) 2002-2003 00007 // Created : 10/9/2002 00008 // Revised : 02/10/2003 00009 // Version : 0.1 00010 // Target MCU : any 00011 // Editor Tabs : 4 00012 // 00013 // Description : This library provides a set of functions needed to send and 00014 // receive STX/ETX packets. STX/ETX is a simple packet protocol that can 00015 // be wrapped around user data for one or more of the following reasons: 00016 // 00017 // 1. packetization is needed 00018 // - Using packets can be helpful if your data naturally forms 00019 // little "bunches" or if different types of data must be sent 00020 // over the same channel (a serial cable, for example). If your 00021 // data forms "bunches", you can send user data inside STX/ETX 00022 // packets with a predetermined structure, like an array of A/D 00023 // conversion results. If you need a way to tell the receiver 00024 // what kind of data you're sending, you can use the TYPE field 00025 // in the STX/ETX packet. 00026 // 2. error checking is needed 00027 // - STX/ETX packets will add a checksum to your data. This 00028 // allows the receiver to verify that data was received correctly 00029 // and is error-free. Packets which are corrupted in transmission 00030 // and fail the the checksum test are automatically discarded. 00031 // Error checking is especially useful when the data transmission 00032 // channel is unreliable or noisy (examples: radio, infrared, long 00033 // cables, etc) 00034 // 00035 // STX/ETX packets have the following structure: 00036 // 00037 // [STX][status][type][length][user data...][checksum][ETX] 00038 // 00039 // All fields are 1 byte except for user data which may be 0-255 bytes. 00040 // Uppercase fields are constant (STX=0x02, ETX=0x03), lowercase fields 00041 // vary. The length field is the number of bytes in the user data area. 00042 // The checksum is the 8-bit sum of all bytes between but not including 00043 // STX/ETX. 00044 // 00045 // This code is distributed under the GNU Public License 00046 // which can be found at http://www.gnu.org/licenses/gpl.txt 00047 // 00048 //***************************************************************************** 00049 00050 #ifndef STXETX_H 00051 #define STXETX_H 00052 00053 #include "buffer.h" 00054 00055 // include project-dependent configuration options 00056 #include "stxetxconf.h" 00057 00058 // constants 00059 // pcaket markers 00060 #define STX 0x02 // start transmission marker 00061 #define ETX 0x03 // end transmission marker 00062 // packet length parameters 00063 #define STXETX_HEADERLENGTH 4 // number of bytes required for packet header 00064 #define STXETX_TRAILERLENGTH 2 // number of bytes required for packet trailer 00065 // packet field offsets 00066 #define STXETX_STATUSOFFSET 1 // number of bytes from STX to STATUS 00067 #define STXETX_TYPEOFFSET 2 // number of bytes from STX to TYPE 00068 #define STXETX_LENGTHOFFSET 3 // number of bytes from STX to LENGTH 00069 #define STXETX_DATAOFFSET 4 // number of bytes from STX to the data 00070 #define STXETX_CHECKSUMOFFSET 4 // number of bytes from STX+[length] to CHECKSUM 00071 #define STXETX_NOETXSTXCHECKSUM 3 // number of bytes used by STX,ETX,CHECKSUM 00072 00073 00074 // function prototypes 00075 00076 //! Initialize STX/ETX packet protocol library 00077 void stxetxInit(void (*dataout_func)(unsigned char data)); 00078 00079 //! Send/Create STX/ETX packet 00080 void stxetxSend(unsigned char status, unsigned char type, unsigned char datalength, unsigned char* dataptr); 00081 00082 //! Process a buffer containing STX/ETX packets 00083 unsigned char stxetxProcess(cBuffer* rxBuffer); 00084 00085 //! Returns the received packet's status 00086 unsigned char stxetxGetRxPacketStatus(void); 00087 00088 //! Returns the received packet's type 00089 unsigned char stxetxGetRxPacketType(void); 00090 00091 //! Returns the received packet's datalength 00092 unsigned char stxetxGetRxPacketDatalength(void); 00093 00094 //! Returns pointer to the received packet's data 00095 unsigned char* stxetxGetRxPacketData(void); 00096 00097 00098 #endif
1.3-rc2