00001
00024
00025
00026 #ifndef MODBUS_UTILS_H
00027 #define MODBUS_UTILS_H
00028
00029
00036
00037 #define GETU16( var, buf) { \
00038 (var) = (*(buf) << 8) & 0xFF00; \
00039 (var) |= (*((buf)+1) & 0xFF); \
00040 }
00041
00048
00049 #define PUTU16( buf, var) { \
00050 (*(buf)) = ((var) >> 8); \
00051 *((buf)+1) = ((var) & 0xFF); \
00052 }
00053
00056 u16 CRC16( const u8 *buf, int len);
00057 u8 LRC( const u8 *buf, int len);
00058 int MBASCIIToData(u8 *buf, const u8 *data, int len);
00059 int MBDataToASCII(u8 *buf, const u8 *data, int len);
00060
00062 int MBMakeADU( u8 *buf, int protocol, u8 address, const u8 *pdu, int len, u16 tid);
00063 int MBGetPDU( u8 *buf, u8 *adu, int len);
00064 int MBMakePDU( u8 *buf, u8 function, u8 *data, int len);
00065 u8 MBGetAddress( const u8 *adu);
00066 u8 MBGetFunctionCode( const u8 *pdu);
00067
00069 int MBReadDecreteInputs(u8 *buf, u16 startdec, u16 no);
00070 int MBReadCoils(u8 *buf, u16 startcoils, u16 no);
00071 int MBWriteSingleCoil(u8 *buf, u16 coilreg, u16 onoff);
00072 int MBWriteMultipleCoils(u8 *buf, u16 startcoils, u16 no, u8 count, u8 *data);
00073 int MBReadInputRegisters(u8 *buf, u16 startreg, u16 no);
00074 int MBReadHoldingRegisters(u8 *buf, u16 startreg, u16 no);
00075 int MBWriteSingleRegister(u8 *buf, u16 devicereg, u16 value);
00076 int MBWriteMultipleRegisters(u8 *buf, u16 startreg, u16 noreg, u8 count, u16 *no);
00077 int MBReadWriteMultipleRegisters(u8 *buf, u16 rsreg, u16 rno, u16 wsreg, u16 wno, u8 count, u16 *wv);
00078 int MBMaskWriteRegister(u8 *buf, u16 reg, u16 andmask,u16 ormask);
00079 int MBReadFIFOQueue(u8 *buf, u16 FIFOAddr);
00080
00081
00082 int MBReadExceptionStatus(u8 *buf);
00083 int MBDiagnostic(u8 *buf, u16 subfunc, u16 data);
00084 int MBGetCommEventCounter(u8 *buf);
00085 int MBGetCommEventLog(u8 *buf);
00086 int MBReportSlaveID(u8 *buf);
00087
00089 int MBResponseReadDecreteInputs(u8 *buf, u8 no, u8 *value);
00090 int MBResponseReadCoils(u8 *buf, u8 no, u8 *value);
00091 int MBResponseReadInputRegisters(u8 *buf, u8 no, u16 *value);
00092 int MBResponseReadHoldingRegisters(u8 *buf, u8 no, u16 *value);
00093 int MBResponseWriteMultipleRegisters(u8 *buf, u16 startdevreg, u16 no);
00094 int MBResponseWriteSingleCoil(u8 *buf, u16 coilreg, u16 onoff);
00095 int MBResponseWriteSingleRegister(u8 *buf, u16 reg, u16 value);
00096 int MBResponseReadFIFOueue(u8 *buf, u16 count, u16 *value);
00097 int MBResponseReadExecptionStatus(u8 *buf, u8 status);
00098 int MBResponseDiagnostic(u8 *buf, u16 subfunc, u16 data);
00099 int MBResponseGetCommEventCounter(u8 *buf, u16 status, u16 eventcount);
00100 int MBResponseGetCommEventLog(u8 *buf, u8 no, u16 status, u16 eventcount, u16 messagecount, u8 *events);
00101
00102
00104 int MBGetReadDecreteInputs(u8 *pdu, u16 *startdec, u16 *no);
00105 int MBGetReadCoils(u8 *pdu, u16 *startcoils, u16 *no);
00106 int MBGetWriteSingleCoil(u8 *pdu, u16 *coilreg, u16 *onoff);
00107 int MBGetWriteMultipleCoils(u8 *pdu, u16 *startcoils, u16 *no);
00108 int MBGetReadInputRegisters(u8 *pdu, u16 *startreg, u16 *no);
00109 int MBGetReadHoldingRegisters(u8 *pdu, u16 *startreg, u16 *no);
00110 int MBGetWriteSingleRegister(u8 *pdu, u16 *devicereg, u16 *value);
00111 int MBGetWriteMultipleRegisters(u8 *pdu, u16 *startreg, u16 *no, u8 *count, u16 *value);
00112 int MBGetReadWriteMultipleRegisters(u8 *pdu, u16 *rsreg, u16 *rno, u16 *wsreg, u16 *wno, u8 *count, u16 *wv);
00113 int MBGetMaskWriteRegister(u8 *pdu, u16 *reg, u16 *andmask, u16 *ormask);
00114 int MBGetReadFIFOQueue(u8 *pdu, u16 *FIFOAddr);
00115 int MBGetDiagnostic(u8 *pdu, u16 *subfunc, u16 *data);
00116
00118 int MBGetResponseReadDecreteInputs(u8 *pdu, u8 *no, u8 *value);
00119 int MBGetResponseReadCoils(u8 *pdu, u8 *no, u8 *value);
00120 int MBGetResponseReadInputRegisters(u8 *pdu, u8 *no, u16 *value);
00121 int MBGetResponseReadHoldingRegisters(u8 *pdu, u8 *no, u16 *value);
00122 int MBGetResponseWriteMultipleRegisters(u8 *pdu, u16 *address, u16 *no);
00123 int MBGetResponseWriteSingleCoil(u8 *pdu, u16 *address, u16 *value);
00124 int MBGetResponseWriteMultipleCoils(u8 *pdu, u16 *address, u16 *value);
00125 int MBGetResponseWriteSingleRegister(u8 *pdu, u16 *address, u16 *value);
00126 int MBGetResponseReadFIFOueue(u8 *buf, u16 count, u16 *value);
00127 int MBGetResponseReadExecptionStatus(u8 *buf, u8 status);
00128 int MBGetResponseDiagnostic(u8 *pdu, u16 *subfunc, u16 *data);
00129 int MBGetResponseGetCommEventCounter(u8 *pdu, u16 *status, u16 *eventcount);
00130 int MBGetResponseGetCommEventLog(u8 *buf, u16 *status, u16 *eventcount, u16 *messagecount, u8 *events);
00131 int MBGetResponseReadWriteMultipleRegisters(u8 *pdu, u8 *no, u16 *value);
00132 int MBGetResponseMaskWriteRegister(u8 *pdu, u16 *reg, u16 *andmask, u16 *ormask);
00133 int MBGetResponseReportSlaveID(u8 *pdu, u8 *slave_id, u8 *status);
00134
00135 #endif
00136