⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stxetx_8c-source.html

📁 avr cpu 库源代码 对avr单片机编程很有帮助
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Procyon AVRlib: stxetx.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.4.2 --><div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div><h1>stxetx.c</h1><a href="stxetx_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file stxetx.c \brief STX/ETX Packet Protocol Implementation Library. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name    : 'stxetx.c'</span>00005 <span class="comment">// Title        : STX/ETX Packet Protocol Implementation Library</span>00006 <span class="comment">// Author       : Pascal Stang - Copyright (C) 2002</span>00007 <span class="comment">// Created      : 10/9/2002</span>00008 <span class="comment">// Revised      : 6/30/2003</span>00009 <span class="comment">// Version      : 0.1</span>00010 <span class="comment">// Target MCU   : any</span>00011 <span class="comment">// Editor Tabs  : 4</span>00012 <span class="comment">//</span>00013 <span class="comment">// Description  : This library provides a set of functions needed to send and</span>00014 <span class="comment">//      receive STX/ETX packets.  STX/ETX is a simple packet protocol that can</span>00015 <span class="comment">//      be wrapped around user data for one or more of the following reasons:</span>00016 <span class="comment">//</span>00017 <span class="comment">//          1. packetization is needed</span>00018 <span class="comment">//              - Using packets can be helpful if your data naturally forms </span>00019 <span class="comment">//              little "bunches" or if different types of data must be sent</span>00020 <span class="comment">//              over the same channel (a serial cable, for example).  If your</span>00021 <span class="comment">//              data forms "bunches", you can send user data inside STX/ETX</span>00022 <span class="comment">//              packets with a predetermined structure, like an array of A/D</span>00023 <span class="comment">//              conversion results.  If you need a way to tell the receiver</span>00024 <span class="comment">//              what kind of data you're sending, you can use the TYPE field</span>00025 <span class="comment">//              in the STX/ETX packet.</span>00026 <span class="comment">//          2. error checking is needed</span>00027 <span class="comment">//              - STX/ETX packets will add a checksum to your data.  This</span>00028 <span class="comment">//              allows the receiver to verify that data was received correctly</span>00029 <span class="comment">//              and is error-free.  Packets which are corrupted in transmission</span>00030 <span class="comment">//              and fail the the checksum test are automatically discarded.</span>00031 <span class="comment">//              Error checking is especially useful when the data transmission</span>00032 <span class="comment">//              channel is unreliable or noisy (examples: radio, infrared, long</span>00033 <span class="comment">//              cables, etc)</span>00034 <span class="comment">//  </span>00035 <span class="comment">//      STX/ETX packets have the following structure:</span>00036 <span class="comment">//</span>00037 <span class="comment">//      [STX][status][type][length][user data...][checksum][ETX]</span>00038 <span class="comment">//</span>00039 <span class="comment">//      All fields are 1 byte except for user data which may be 0-255 bytes.</span>00040 <span class="comment">//      Uppercase fields are constant (STX=0x02, ETX=0x03), lowercase fields</span>00041 <span class="comment">//      vary.  The length field is the number of bytes in the user data area.</span>00042 <span class="comment">//      The checksum is the 8-bit sum of all bytes between but not including</span>00043 <span class="comment">//      STX/ETX.</span>00044 <span class="comment">//</span>00045 <span class="comment">// This code is distributed under the GNU Public License</span>00046 <span class="comment">//      which can be found at http://www.gnu.org/licenses/gpl.txt</span>00047 <span class="comment">//</span>00048 <span class="comment">//*****************************************************************************</span>00049 00050 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00051 <span class="preprocessor">#include "<a class="code" href="stxetx_8h.html">stxetx.h</a>"</span>00052 <span class="comment">//#include "rprintf.h"</span>00053 00054 <span class="comment">// function pointer to data output routine</span>00055 <span class="keyword">static</span> void (*stxetxDataOut)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data);00056 00057 <span class="comment">// received packet data buffer</span>00058 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> stxetxRxPacket[STXETX_MAXRXPACKETSIZE];00059 00060 <span class="comment">// functions</span>00061 00062 00063 <span class="comment">// Initialize STX/ETX packet protocol library</span><a name="l00064"></a><a class="code" href="group__stxetx.html#ga0">00064</a> <span class="keywordtype">void</span> <a class="code" href="group__stxetx.html#ga0">stxetxInit</a>(<span class="keywordtype">void</span> (*dataout_func)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data))00065 {00066     stxetxDataOut = dataout_func;00067 }00068 00069 <span class="comment">// Send/Create STX/ETX packet</span><a name="l00070"></a><a class="code" href="group__stxetx.html#ga1">00070</a> <span class="keywordtype">void</span> <a class="code" href="group__stxetx.html#ga1">stxetxSend</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> status, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> type, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> datalength, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* dataptr)00071 {00072     <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> checksum = 0;00073     <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> i;00074     00075     <span class="comment">// write packet header</span>00076     stxetxDataOut(STX);00077     stxetxDataOut(status);00078     stxetxDataOut(type);00079     stxetxDataOut(datalength);00080     <span class="comment">// update checksum</span>00081     checksum += status + type + datalength;00082     <span class="comment">// copy data into packet</span>00083     <span class="keywordflow">for</span>(i = 0; i &lt; datalength; i++)00084     {00085         stxetxDataOut(*dataptr);00086         checksum += *dataptr;00087         dataptr++;00088     }00089     <span class="comment">// write packet trailer</span>00090     stxetxDataOut(checksum);00091     stxetxDataOut(ETX);00092 }00093 00094 <span class="comment">// process buffer containing STX/ETX packets</span><a name="l00095"></a><a class="code" href="group__stxetx.html#ga2">00095</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="group__stxetx.html#ga2">stxetxProcess</a>(<a class="code" href="structstruct__cBuffer.html">cBuffer</a>* rxBuffer)00096 {00097     <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> foundpacket = FALSE;00098     <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> i;00099     <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> length, checksum;00100     <span class="comment">//unsigned char type;</span>00101 00102     <span class="comment">// process the buffer</span>00103     <span class="comment">// go through buffer looking for packets</span>00104     <span class="comment">// the STX must be located at least STXETX_HEADERLENGTH+STXETX_TRAILERLENGTH from end</span>00105     <span class="comment">// otherwise we must not have a complete packet</span>00106     <span class="keywordflow">while</span>( rxBuffer-&gt;<a class="code" href="structstruct__cBuffer.html#o2">datalength</a> &gt;= ((u16)STXETX_HEADERLENGTH+(u16)STXETX_TRAILERLENGTH) )00107     {00108         <span class="comment">// look for a potential start of packet</span>00109         <span class="keywordflow">if</span>(<a class="code" href="group__buffer.html#ga4">bufferGetAtIndex</a>(rxBuffer, 0) == STX)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -