00001 /************************************************************************************************/ 00002 /*! \file common.h 00003 * \brief Communicate with serial device. 00004 * \author $Author: jayhawk_hokie $ 00005 * \version $Revision: 1.4 $ 00006 * \date $Date: 2007/08/02 23:18:28 $ 00007 ************************************************************************************************/ 00008 /*! 00009 * 00010 ************************************************************************************************/ 00011 00012 #ifndef _COMMON_H_ 00013 #define _COMMON_H_ 00014 #define CRC_16 0xa001 /* CRC-16 polynomial (used by modbus) */ 00015 #define CRC_CCITT 0x8408 /* CCITT polynomial */ 00016 typedef __uint8_t uint8; 00017 typedef __uint16_t uint16; 00018 00019 /************************************************************************ 00020 * Function: mbcrc_calc, mbcrc_tlu 00021 * Parameters: data - pointer to array of unsigned *8 bit characters. 00022 * len - length of data array 00023 * Purpose: These function will calculate or lookup the 16 bit CRC 00024 * value of a character array. Numerical CRC caluclation 00025 * requires more processing time than table lookup. 00026 * Types: uint8 is an 8 bit unsigned integer type. Usually uint8 00027 * is realized with 'unsigned char'. 00028 * uint16 is a 16 bit unisgned integer type. Usually uint16 00029 * is realized with 'unsigned short'. 00030 ************************************************************************/ 00031 00032 uint16 mbcrc_calc(uint8 *data, int len); 00033 uint16 mbcwc_tlu(uint8 *data, int len); 00034 00035 /* Table of CRC values for high-order byte */ 00036 /* 00037 static unsigned char au8CRCHi[] = { 00038 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 00039 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 00040 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 00041 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 00042 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 00043 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 00044 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 00045 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 00046 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 00047 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 00048 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 00049 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 00050 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 00051 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 00052 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 00053 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 00054 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 00055 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 00056 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 00057 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 00058 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 00059 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 00060 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 00061 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 00062 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 00063 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 00064 } ; 00065 */ 00066 /* Table of CRC values for low-order byte */ 00067 /* 00068 static unsigned char au8CRCLo[] = { 00069 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 00070 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 00071 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 00072 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 00073 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 00074 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 00075 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 00076 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 00077 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 00078 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 00079 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 00080 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 00081 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 00082 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 00083 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 00084 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 00085 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 00086 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 00087 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 00088 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 00089 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 00090 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 00091 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 00092 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 00093 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 00094 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 00095 } ; 00096 */ 00097 00098 00099 /********************************************************************** 00100 * Function: init_serial() 00101 * Description: Configure, open and initialize the serial port 00102 * Arguments: 00103 * portfn - - Port filename 00104 * brate - - baudrate 00105 * portfd - out - file descriptor returned by open() 00106 * Return Values: 00107 * 0 - OK 00108 * -1 - open() failed 00109 * Bugs: none known 00110 * Notes: Stable through several program revisions. 00111 * Last Update: 6/15/02 00112 * Last Updated By: Cengiz Akinli 00113 **********************************************************************/ 00114 00115 int init_serial(char *portfn, speed_t brate, int *portfd); 00116 00117 00118 00119 00120 /********************************************************************** 00121 * Function: say() 00122 * Description: Perform blocked, looped, write to a file 00123 * Arguments: 00124 * fd - target file descriptor 00125 * str - string to write 00126 * Return Values: 00127 * 0 - OK 00128 * Bugs: Vulnerable to signal interruption of read loop. See code comment. 00129 * Notes: Does dumb writing only. Checks bytes successfully written, 00130 * but does not loop to write the remainder. Such a loop, when it 00131 * is added to this software, should go here. 00132 * Last Update: 7/1/02 00133 * Last Updated By: Cengiz Akinli 00134 **********************************************************************/ 00135 00136 int say(int fd, char *str); 00137 00138 00139 00140 00141 /********************************************************************** 00142 * Function: hear() 00143 * Description: Read data from the given fd into str with various parameters. 00144 * Reads up to maxlen bytes. Blocks for no more than maxbtime*3 usecs 00145 * until either minlen bytes are read (if minlen is non-zero) or the 00146 * term character is read in (which is included in the output string). 00147 * Arguments: 00148 * fd - - source file descriptor 00149 * str - out - pre-allocated string buffer in which results 00150 * will be stored 00151 * maxlen - - Maxmimum number of bytes to read (must be <= number 00152 * of bytes allocated for str) 00153 * minlen - - 0 - No minumum input byte counting 00154 * >0 - Minumum number of bytes to read (see desc.) 00155 * maxbtime - - Max blocking time per select() in usec (see desc) 00156 * term - - Terminator character to mark end of input 00157 * 00158 * Return Values: 00159 * 0 - OK 00160 * 1 - Read 0 bytes 00161 * Bugs: Vulnerable to signal interruption of write loop. See code comment. 00162 * Notes: Use of minlen is deprecated vs term character selection. In either 00163 * case, blocking time is still limited to 3*maxbtime usec. 00164 * Last Update: 7/1/02 00165 * Last Updated By: Cengiz Akinli 00166 **********************************************************************/ 00167 00168 int hear(int fd, char *str, int maxlen, int minlen, int maxbtime, char term); 00169 00170 00171 00172 #endif 00173 00174 /* Do not change the comments below - they will be added automatically by CVS*/ 00175 /***************************************************************************** 00176 * $Log: common.h,v $ 00177 * Revision 1.4 2007/08/02 23:18:28 jayhawk_hokie 00178 * Fixed Warnings. 00179 * 00180 * 00181 * 00182 ******************************************************************************/ 00183