mserial.h

00001 /* Modbus_Ser.h
00002 Modbus ISO/OSI level 2 protocol for
00003 communication over a serial line.
00004 Nathan Z. Gustavson 
00005 ngustavson@emacinc.com
00006 n_gustavson@yahoo.com
00007 3/13/03
00008 Emac.inc
00009 */
00010 #ifndef MSERIAL_H
00011 #define MSERIAL_H
00012 
00013 #define ASCIISTART_FRAME ':'
00014 #define ASCIISTART_STR   ":"
00015 #define ASCIISTOP_FRAME  "\r\n"
00016 
00017 #define RTU_PROTOCOL 0
00018 #define ASCII_PROTOCOL 1
00019 
00020 #define RTU_DATA_MAX 252
00021 #define RTU_CRC_BYTES 2
00022 #define RTU_ADDRESS_BYTES 1
00023 #define RTU_FUNCTION_BYTES 1
00024 #define RTU_FRAME_MIN  RTU_CRC_BYTES + RTU_FUNCTION_BYTES + RTU_ADDRESS_BYTES
00025 #define RTU_FRAME_MAX  RTU_FRAME_MIN + RTU_DATA_MAX 
00026 
00027 #define ASCII_DATA_MAX 504
00028 #define ASCII_LRC_BYTES 2
00029 #define ASCII_ADDRESS_BYTES 2
00030 #define ASCII_FUNCTION_BYTES 2
00031 #define ASCII_START_BYTES 1
00032 #define ASCII_STOP_BYTES  2
00033 #define ASCII_FRAME_MIN  ASCII_LRC_BYTES + ASCII_FUNCTION_BYTES + ASCII_ADDRESS_BYTES + ASCII_START_BYTES + ASCII_STOP_BYTES
00034 #define ASCII_FRAME_MAX  ASCII_FRAME_MIN + ASCII_DATA_MAX 
00035 
00036 #define PDU_DATA_MAX RTU_DATA_MAX
00037 
00038 typedef struct RTU_Frame{
00039 u8 address;
00040 u8 function;
00041 u8 *data;
00042 int datasize;
00043 u8 CRC[2];
00044 }RTU_Frame;
00045 
00046 
00047 typedef struct ASCII_Frame{
00048 //const char *start;
00049 char address[ASCII_ADDRESS_BYTES + 1];
00050 char function[ASCII_FUNCTION_BYTES + 1];
00051 //char *data;
00052 char data[ASCII_FRAME_MAX];
00053 int datasize;
00054 char LRC[3];
00055 const char *start;
00056 const char *stop;
00057 }ASCII_Frame;
00058 
00059 
00060 void  __attribute__((far))ASCIIcallback(int port,void *arg);
00061 void  __attribute__((far))RTUcallback(int port,void *arg);
00062 //PDUstruct *ASCII2PDU(ASCII_Frame *frame);
00063 //PDUstruct *ASCII2PDU(PDU_struct *PDU, ASCII_Frame *frame);
00064 //PDUstruct *RTU2PDU(RTU_Frame *frame);
00065 //PDUstruct *RTU2PDU(PDU_struct *PDU, RTU_Frame *frame);
00066 
00067 unsigned short  __attribute__((far))CRC16 (u8 *puchMsg,unsigned short usDataLen );
00068 int  __attribute__((far))DestroyPDU(PDUstruct *PDU);
00069 u8  __attribute__((far))LRC(u8 *auchMsg, unsigned short usDataLen);
00070 //RTU_Frame *RTU_Build_Frame(u8 address, u8 function, u8 *data,int datasize);
00071 RTU_Frame __attribute__((far)) *RTU_Build_Frame(u8 address, u8 function,
00072     RTU_Frame *frame, u8 *data,int datasize);
00073 int __attribute__((far)) RTU_Transmit_Frame(RTU_Frame *frame, int port);
00074 
00075 //RTU_Frame *RTU_Receive_Frame(Modbus_Port *modport);
00076 RTU_Frame __attribute__((far)) *RTU_Receive_Frame(RTU_Frame *frame,Modbus_Port *modport);
00077 
00078 //ASCII_Frame *ASCII_Build_Frame(u8 address, u8 function,
00079 //  ASCII_Frame *data,int datasize);
00080 
00081 ASCII_Frame __attribute__((far)) *ASCII_Build_Frame(u8 address, u8 function, 
00082                             ASCII_Frame *frame, u8 *data,int datasize);
00083 
00084 //int ASCII_Transmit_Frame(ASCII_Frame *frame, int port);
00085 int __attribute__((far)) ASCII_Transmit_Frame(ASCII_Frame *frame, char delim, int port);
00086 //ASCII_Frame *ASCII_Receive_Frame(Modbus_Port *modport);
00087 ASCII_Frame __attribute__((far)) *ASCII_Receive_Frame(ASCII_Frame *frame,Modbus_Port *modport);
00088 int __attribute__((far)) DestroyFrame(int protocol,void *frame);
00089 
00090 void __attribute__((far)) RTUframecomplete(taskdata port);
00091 
00092 /***************************************************************
00093  * bend2int exchange high and low bytes of a 16 bit word
00094  * @param u8 pointer to the word to bend
00095  * @return the flipped word
00096  */
00097 static inline int bend2int(u8 *bend)
00098 {
00099 int bent;
00100 
00101 bent = bend[0];
00102 bent<<=8;
00103 bent+=bend[1];
00104 
00105 return bent;
00106 }
00107 
00108 static inline int int2bend(u8 *bend)
00109 {
00110 int bent;
00111 
00112 bent = bend[1];
00113 bent<<=8;
00114 bent+=bend[0];
00115 
00116 return bent;
00117 }
00118 
00119 
00120 
00121 static inline char ctoa(u8 nibble)
00122 {
00123 if (nibble>9)
00124     return(nibble + 55);
00125 else
00126     return(nibble + 48);
00127 }
00128 
00129 static inline void nwconvert(long data, char *asciiout, int size)
00130 {
00131 int i;
00132 
00133     for(i=(size-1);i>=0;i--)
00134      {
00135       asciiout[i] = ctoa(((u8)data&0x0F));
00136       data>>=4;
00137      }
00138 } 
00139 
00140 #endif
00141 

Generated on Fri Mar 3 17:25:45 2006 for EMAC-HCS12-SDK by  doxygen 1.4.6