This file contains much modbus utility functions. It helps to merge modbus function code and data to a PDU and split PDU to modbus function code and data. We split one request and response into four functions, for example if modbus master sends Read Coils request to slave and gets slave's response, we might have to use these four functions.
Modbus Master Modbus Slave 1. MBReadCoils() ----> 2. MBGetReadCoils() 4. MBGetResponseReadCoils() <---- 3. MBResponseReadCoils
History: Date Author Comment 08-01-2005 AceLan Kao. Create it.
Definition in file modbus_utils.h.
This graph shows which files directly or indirectly include this file:
Go to the source code of this file.
Defines | |
#define | GETU16(var, buf) |
get 16 bits data from buf | |
#define | PUTU16(buf, var) |
put 16 bits data to buf | |
Functions | |
u16 | CRC16 (const u8 *buf, int len) |
calculate the CRC16 value of the input data | |
u8 | LRC (const u8 *buf, int len) |
calculate the checksum value of the input data | |
int | MBASCIIToData (u8 *buf, const u8 *data, int len) |
parse ASCII mode data to binary data | |
int | MBDataToASCII (u8 *buf, const u8 *data, int len) |
parse binary data to ASCII mode data | |
int | MBDiagnostic (u8 *buf, u16 subfunc, u16 data) |
0x08 | |
u8 | MBGetAddress (const u8 *adu) |
get device address from ADU | |
int | MBGetCommEventCounter (u8 *buf) |
0x0B | |
int | MBGetCommEventLog (u8 *buf) |
0x0C | |
int | MBGetDiagnostic (u8 *pdu, u16 *subfunc, u16 *data) |
parse PDU packet of Diagnostic function(0x08) | |
u8 | MBGetFunctionCode (const u8 *pdu) |
get function code from PDU | |
int | MBGetMaskWriteRegister (u8 *pdu, u16 *reg, u16 *andmask, u16 *ormask) |
parse PDU packet of Mask Write Register function(0x16) | |
int | MBGetPDU (u8 *buf, u8 *adu, int len) |
get PDU part from ADU data | |
int | MBGetReadCoils (u8 *pdu, u16 *startcoils, u16 *no) |
parse PDU packet of Read Coils function(0x01) | |
int | MBGetReadDecreteInputs (u8 *pdu, u16 *startdec, u16 *no) |
Modbus get request value from PDU. | |
int | MBGetReadFIFOQueue (u8 *pdu, u16 *FIFOAddr) |
parse PDU packet of Read FIFO Queue function(0x18) | |
int | MBGetReadHoldingRegisters (u8 *pdu, u16 *startreg, u16 *no) |
parse PDU packet of Read Holding Registers function(0x03) | |
int | MBGetReadInputRegisters (u8 *pdu, u16 *startreg, u16 *no) |
parse PDU packet of Read Input Registers function(0x04) | |
int | MBGetReadWriteMultipleRegisters (u8 *pdu, u16 *rsreg, u16 *rno, u16 *wsreg, u16 *wno, u8 *count, u16 *wv) |
parse PDU packet of Read Write Multiple Registers function(0x17) | |
int | MBGetResponseDiagnostic (u8 *pdu, u16 *subfunc, u16 *data) |
parse response PDU packet of Diagnostic request(0x08) | |
int | MBGetResponseGetCommEventCounter (u8 *pdu, u16 *status, u16 *eventcount) |
parse response PDU packet of Get Comm Event Counter request(0x0B) | |
int | MBGetResponseGetCommEventLog (u8 *buf, u16 *status, u16 *eventcount, u16 *messagecount, u8 *events) |
parse response PDU packet of Get Comm Event Counter request(0x0C) | |
int | MBGetResponseMaskWriteRegister (u8 *pdu, u16 *reg, u16 *andmask, u16 *ormask) |
parse response PDU packet of Mask Write Register function(0x16) | |
int | MBGetResponseReadCoils (u8 *pdu, u8 *no, u8 *value) |
parse response PDU packet of Response Read Coils request(0x01) | |
int | MBGetResponseReadDecreteInputs (u8 *pdu, u8 *no, u8 *value) |
Modbus get response value from PDU. | |
int | MBGetResponseReadExecptionStatus (u8 *buf, u8 status) |
int | MBGetResponseReadFIFOueue (u8 *buf, u16 count, u16 *value) |
int | MBGetResponseReadHoldingRegisters (u8 *pdu, u8 *no, u16 *value) |
parse response PDU packet of Read Holding Registers request(0x03) | |
int | MBGetResponseReadInputRegisters (u8 *pdu, u8 *no, u16 *value) |
parse response PDU packet of Read Input Registers request(0x04) | |
int | MBGetResponseReadWriteMultipleRegisters (u8 *pdu, u8 *no, u16 *value) |
parse response PDU packet of Response Read Coils request(0x17) | |
int | MBGetResponseReportSlaveID (u8 *pdu, u8 *slave_id, u8 *status) |
parse response PDU packet of Report Slave ID function(0x16) | |
int | MBGetResponseWriteMultipleCoils (u8 *pdu, u16 *address, u16 *value) |
parse response PDU packet of Write Multiple Coil request(0x0F) | |
int | MBGetResponseWriteMultipleRegisters (u8 *pdu, u16 *address, u16 *no) |
parse response PDU packet of Write Multiple Registers request(0x10) | |
int | MBGetResponseWriteSingleCoil (u8 *pdu, u16 *address, u16 *value) |
parse response PDU packet of Write Single Coil request(0x05) | |
int | MBGetResponseWriteSingleRegister (u8 *pdu, u16 *address, u16 *value) |
parse response PDU packet of Write Single Register request(0x06) | |
int | MBGetWriteMultipleCoils (u8 *pdu, u16 *startcoils, u16 *no) |
parse PDU packet of Write Multiple Coils function(0x0F) | |
int | MBGetWriteMultipleRegisters (u8 *pdu, u16 *startreg, u16 *no, u8 *count, u16 *value) |
parse PDU packet of Write Multiple Registers function(0x10) | |
int | MBGetWriteSingleCoil (u8 *pdu, u16 *coilreg, u16 *onoff) |
parse PDU packet of Write Single Coil function(0x05) | |
int | MBGetWriteSingleRegister (u8 *pdu, u16 *devicereg, u16 *value) |
parse PDU packet of Write Single Register function(0x06) | |
int | MBMakeADU (u8 *buf, int protocol, u8 address, const u8 *pdu, int len, u16 tid) |
Modbus utilities functions. | |
int | MBMakePDU (u8 *buf, u8 function, u8 *data, int len) |
assemble function code and data to ADU | |
int | MBMaskWriteRegister (u8 *buf, u16 reg, u16 andmask, u16 ormask) |
0x16 | |
int | MBReadCoils (u8 *buf, u16 startcoils, u16 no) |
0x01 | |
int | MBReadDecreteInputs (u8 *buf, u16 startdec, u16 no) |
Master function 0x02. | |
int | MBReadExceptionStatus (u8 *buf) |
0x07 | |
int | MBReadFIFOQueue (u8 *buf, u16 FIFOAddr) |
0x18 | |
int | MBReadHoldingRegisters (u8 *buf, u16 startreg, u16 no) |
0x03 | |
int | MBReadInputRegisters (u8 *buf, u16 startreg, u16 no) |
0x04 | |
int | MBReadWriteMultipleRegisters (u8 *buf, u16 rsreg, u16 rno, u16 wsreg, u16 wno, u8 count, u16 *wv) |
0x17 | |
int | MBReportSlaveID (u8 *buf) |
0x11 | |
int | MBResponseDiagnostic (u8 *buf, u16 subfunc, u16 data) |
make response PDU packet of Diagnostic request(0x08) | |
int | MBResponseGetCommEventCounter (u8 *buf, u16 status, u16 eventcount) |
make response PDU packet of Get Comm Event Counter request(0x0B) | |
int | MBResponseGetCommEventLog (u8 *buf, u8 no, u16 status, u16 eventcount, u16 messagecount, u8 *events) |
make response PDU packet of Get Comm Event Counter request(0x0C) | |
int | MBResponseReadCoils (u8 *buf, u8 no, u8 *value) |
make response PDU packet of Response Read Coils request(0x01) | |
int | MBResponseReadDecreteInputs (u8 *buf, u8 no, u8 *value) |
Response function. | |
int | MBResponseReadExecptionStatus (u8 *buf, u8 status) |
int | MBResponseReadFIFOueue (u8 *buf, u16 count, u16 *value) |
int | MBResponseReadHoldingRegisters (u8 *buf, u8 no, u16 *value) |
make response PDU packet of Read Holding Registers request(0x03) | |
int | MBResponseReadInputRegisters (u8 *buf, u8 no, u16 *value) |
make response PDU packet of Read Input Registers request(0x04) | |
int | MBResponseWriteMultipleRegisters (u8 *buf, u16 startdevreg, u16 no) |
make response PDU packet of Write Multiple Registers request(0x10) | |
int | MBResponseWriteSingleCoil (u8 *buf, u16 coilreg, u16 onoff) |
make response PDU packet of Write Single Coil request(0x05) | |
int | MBResponseWriteSingleRegister (u8 *buf, u16 reg, u16 value) |
make response PDU packet of Write Single Register request(0x06) | |
int | MBWriteMultipleCoils (u8 *buf, u16 startcoils, u16 no, u8 count, u8 *data) |
0x0F | |
int | MBWriteMultipleRegisters (u8 *buf, u16 startreg, u16 noreg, u8 count, u16 *no) |
0x10 | |
int | MBWriteSingleCoil (u8 *buf, u16 coilreg, u16 onoff) |
0x05 | |
int | MBWriteSingleRegister (u8 *buf, u16 devicereg, u16 value) |
0x06 |
|
|
Value: { \ (*(buf)) = ((var) >> 8); \ *((buf)+1) = ((var) & 0xFF); \ }
Definition at line 49 of file modbus_utils.h. Referenced by MBDiagnostic(), MBMaskWriteRegister(), MBReadCoils(), MBReadDecreteInputs(), MBReadFIFOQueue(), MBReadHoldingRegisters(), MBReadInputRegisters(), MBReadWriteMultipleRegisters(), MBResponseDiagnostic(), MBResponseGetCommEventCounter(), MBResponseGetCommEventLog(), MBResponseReadCoils(), MBResponseReadDecreteInputs(), MBResponseReadFIFOQueue(), MBResponseReadHoldingRegisters(), MBResponseReadInputRegisters(), MBResponseWriteMultipleRegisters(), MBResponseWriteSingleCoil(), MBResponseWriteSingleRegister(), MBTCPWriteMBAP(), MBWriteMultipleCoils(), MBWriteMultipleRegisters(), MBWriteSingleCoil(), and MBWriteSingleRegister(). |
|
calculate the CRC16 value of the input data
Definition at line 79 of file modbus_utils.c. References CRCHi, CRCLo, and MB_ERROR_PARAMETER. Referenced by MBMakeADU(), and MBSerialReadRTU(). 00080 { 00081 unsigned char hi=0xFF; 00082 unsigned char lo=0xFF; 00083 unsigned short index; 00084 00085 if ( buf == 0 || len <= 0 ) 00086 return(MB_ERROR_PARAMETER); 00087 00088 while ( len-- ) { 00089 index = lo ^ (u8)*buf++; 00090 lo = hi ^ CRCHi[index]; 00091 hi = CRCLo[index]; 00092 } 00093 00094 index = lo << 8; 00095 index |= hi; 00096 return(index); 00097 }
|
|
calculate the checksum value of the input data
Definition at line 107 of file modbus_utils.c. References MB_ERROR_PARAMETER. Referenced by MBMakeADU(), and MBSerialReadASCII(). 00108 { 00109 unsigned char ch=0; 00110 00111 if ( len <= 0 || buf == 0 ) 00112 return(MB_ERROR_PARAMETER); 00113 00114 while ( len-- ) 00115 ch += (unsigned char)*buf++; 00116 00117 return(ch); 00118 }
|
|
parse ASCII mode data to binary data
Definition at line 132 of file modbus_utils.c. References MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_FORMAT, and MB_ERROR_LENGTH. Referenced by MBSerialReadASCII(). 00133 { 00134 int i= 0; 00135 u8 ch1, ch2; 00136 00137 /* to do length error check */ 00138 if ( len & 1 ) 00139 return( MB_ERROR_LENGTH); 00140 if ( len <=1 || len >= MB_ASCII_ADU_MAX_LENGTH ) 00141 return( MB_ERROR_LENGTH); 00142 00143 for ( i=0; i<len; i+=2 ) 00144 { 00145 ch2 = 0; 00146 ch1 = *data++; 00147 if ( ch1 >= '0' && ch1 <= '9' ) 00148 ch1 -= '0'; 00149 else if ( ch1 >= 'A' && ch1 <= 'F' ) 00150 ch1 = ch1 - 'A' + 10; 00151 else 00152 return(MB_ERROR_FORMAT); 00153 ch2 = ch1 << 4; 00154 ch1 = *data++; 00155 if ( ch1 >= '0' && ch1 <= '9' ) 00156 ch1 -= '0'; 00157 else if ( ch1 >= 'A' && ch1 <= 'F' ) 00158 ch1 = ch1 - 'A' + 10; 00159 else 00160 return(MB_ERROR_FORMAT); 00161 ch2 |= ch1; 00162 *buf++ = ch2; 00163 } 00164 00165 return(len/2); 00166 }
|
|
parse binary data to ASCII mode data
Definition at line 177 of file modbus_utils.c. Referenced by MBMakeADU(). 00178 { 00179 unsigned char lrc= 0; 00180 int i= 0, j= 0; 00181 00182 for( i= 0; i< len; i++) 00183 { 00184 u8 ch= ((data[ i] & 0xF0) >> 4); 00185 if( ch >= 0 && ch <= 9) 00186 ch+= '0'; 00187 else 00188 ch+= ('A'- 10); 00189 buf[ j++]= ch; 00190 00191 ch= (data[ i] & 0x0F); 00192 if( ch >= 0 && ch <= 9) 00193 ch+= '0'; 00194 else 00195 ch+= ('A'-10); 00196 buf[ j++]= ch; 00197 } 00198 00199 return j; 00200 }
|
|
0x08
Definition at line 640 of file modbus_utils.c. References MBF_DIAGNOSTIC, and PUTU16. Referenced by MBSerialDiagnostic(). 00641 { 00642 int len= 0; 00643 buf[ len]= MBF_DIAGNOSTIC; 00644 len++; 00645 PUTU16( buf+ len, subfunc); // don't put ++ in the macro 00646 len+= 2; 00647 PUTU16( buf+ len, data); // all data of subfunc is 16bits long 00648 len+= 2; 00649 00650 return len; 00651 }
|
|
get device address from ADU
Definition at line 321 of file modbus_utils.c. Referenced by MBSerialGetAddress().
|
|
0x0B
Definition at line 660 of file modbus_utils.c. References MBF_GET_COMM_EVENT_COUNTER. Referenced by MBSerialGetCommEventCounter(). 00661 { 00662 int len= 0; 00663 buf[ len]= MBF_GET_COMM_EVENT_COUNTER; 00664 len++; 00665 00666 return len; 00667 }
|
|
0x0C
Definition at line 676 of file modbus_utils.c. References MBF_GET_COMM_EVENT_LOG. Referenced by MBSerialGetCommEventLog(). 00677 { 00678 int len= 0; 00679 buf[ len]= MBF_GET_COMM_EVENT_LOG; 00680 len++; 00681 00682 return len; 00683 }
|
|
parse PDU packet of Diagnostic function(0x08)
Definition at line 1237 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_DIAGNOSTIC. 01238 { 01239 if( pdu[ 0] != MBF_DIAGNOSTIC) 01240 return MB_ERROR_FUNCTION; 01241 01242 GETU16( *subfunc, pdu+ 1); 01243 GETU16( *data, pdu+ 3); 01244 01245 return MB_OK; 01246 }
|
|
get function code from PDU
Definition at line 333 of file modbus_utils.c.
|
|
parse PDU packet of Mask Write Register function(0x16)
Definition at line 1198 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_MASK_WRITE_REGISTER. 01199 { 01200 if( pdu[ 0] != MBF_MASK_WRITE_REGISTER) 01201 return MB_ERROR_FUNCTION; 01202 01203 GETU16( *reg, pdu+ 1); 01204 GETU16( *andmask, pdu+ 3); 01205 GETU16( *ormask, pdu+ 5); 01206 01207 return MB_OK; 01208 }
|
|
get PDU part from ADU data
Definition at line 283 of file modbus_utils.c. Referenced by MBSerialDiagnostic(), MBSerialGetCommEventCounter(), MBSerialGetCommEventLog(), MBSerialGetPDU(), MBSerialMaskWriteRegister(), MBSerialReadCoils(), MBSerialReadDecreteInputs(), MBSerialReadExceptionStatus(), MBSerialReadFIFOQueue(), MBSerialReadHoldingRegisters(), MBSerialReadInputRegisters(), MBSerialReadWriteMultipleRegisters(), MBSerialReportSlaveID(), MBSerialWriteMultipleCoils(), MBSerialWriteMultipleRegisters(), MBSerialWriteSingleCoil(), and MBSerialWriteSingleRegister(). 00284 { 00285 int minus= 2; 00286 if( adu[ 0] == ':') 00287 minus= 1; 00288 bcopy( (const char *)(adu+1), (char *)buf, len- 1- minus); 00289 00290 return len- 1- minus; 00291 }
|
|
parse PDU packet of Read Coils function(0x01)
Definition at line 1020 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_READ_COILS. 01021 { 01022 if( pdu[ 0] != MBF_READ_COILS) 01023 return MB_ERROR_FUNCTION; 01024 01025 GETU16( *startcoils, pdu+ 1); 01026 GETU16( *no, pdu+ 3); 01027 01028 return MB_OK; 01029 }
|
|
Modbus get request value from PDU.
Definition at line 1000 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_READ_DECRETE_INPUTS. 01001 { 01002 if( pdu[ 0] != MBF_READ_DECRETE_INPUTS) 01003 return MB_ERROR_FUNCTION; 01004 01005 GETU16( *startdec, pdu+ 1); 01006 GETU16( *no, pdu+ 3); 01007 01008 return MB_OK; 01009 }
|
|
parse PDU packet of Read FIFO Queue function(0x18)
Definition at line 1218 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_READ_FIFO_QUEUE. 01219 { 01220 if( pdu[ 0] != MBF_READ_FIFO_QUEUE) 01221 return MB_ERROR_FUNCTION; 01222 01223 GETU16( *FIFOAddr, pdu+ 1); 01224 01225 return MB_OK; 01226 }
|
|
parse PDU packet of Read Holding Registers function(0x03)
Definition at line 1100 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_READ_HOLDING_REGISTERS. 01101 { 01102 if( pdu[ 0] != MBF_READ_HOLDING_REGISTERS) 01103 return MB_ERROR_FUNCTION; 01104 01105 GETU16( *startreg, pdu+ 1); 01106 GETU16( *no, pdu+ 3); 01107 01108 return MB_OK; 01109 }
|
|
parse PDU packet of Read Input Registers function(0x04)
Definition at line 1080 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_READ_INPUT_REGISTERS. 01081 { 01082 if( pdu[ 0] != MBF_READ_INPUT_REGISTERS) 01083 return MB_ERROR_FUNCTION; 01084 01085 GETU16( *startreg, pdu+ 1); 01086 GETU16( *no, pdu+ 3); 01087 01088 return MB_OK; 01089 }
|
|
parse PDU packet of Read Write Multiple Registers function(0x17)
Definition at line 1171 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_READ_WRITE_MULTIPLE_REGISTERS. 01172 { 01173 int i= 0; 01174 if( pdu[ 0] != MBF_READ_WRITE_MULTIPLE_REGISTERS) 01175 return MB_ERROR_FUNCTION; 01176 01177 GETU16( *rsreg, pdu+ 1); 01178 GETU16( *rno, pdu+ 3); 01179 GETU16( *wsreg, pdu+ 5); 01180 GETU16( *wno, pdu+ 7); 01181 *count= pdu[ 9]; 01182 for( i= 0; i< (*count)/2; i++) 01183 GETU16( wv[ i], pdu+ 10+ i*2 ); 01184 01185 return MB_OK; 01186 }
|
|
parse response PDU packet of Diagnostic request(0x08)
Definition at line 1467 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_DIAGNOSTIC. Referenced by MBSerialDiagnostic(). 01468 { 01469 if( pdu[ 0] != MBF_DIAGNOSTIC) 01470 return MB_ERROR_FUNCTION; 01471 01472 GETU16( *subfunc, pdu+ 1); 01473 GETU16( *data, pdu+ 3); 01474 01475 return MB_OK; 01476 }
|
|
parse response PDU packet of Get Comm Event Counter request(0x0B)
Definition at line 1487 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_GET_COMM_EVENT_COUNTER. Referenced by MBSerialGetCommEventCounter(). 01488 { 01489 if( pdu[ 0] != MBF_GET_COMM_EVENT_COUNTER) 01490 return MB_ERROR_FUNCTION; 01491 01492 GETU16( *status, pdu+ 1); 01493 GETU16( *eventcount, pdu+ 3); 01494 01495 return MB_OK; 01496 }
|
|
parse response PDU packet of Get Comm Event Counter request(0x0C)
Definition at line 1509 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_GET_COMM_EVENT_LOG. Referenced by MBSerialGetCommEventLog(). 01510 { 01511 u8 no; 01512 if( pdu[ 0] != MBF_GET_COMM_EVENT_LOG) 01513 return MB_ERROR_FUNCTION; 01514 01515 no= pdu[ 1]; 01516 GETU16( *status, pdu+ 2); 01517 GETU16( *eventcount, pdu+ 4); 01518 GETU16( *messagecount, pdu+ 6); 01519 01520 bcopy( (const char *)(pdu+ 8), (char *)events, no- 6); 01521 01522 return MB_OK; 01523 }
|
|
parse response PDU packet of Mask Write Register function(0x16)
Definition at line 1557 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_MASK_WRITE_REGISTER. Referenced by MBSerialMaskWriteRegister(), and MBTCPMaskwWriteRegister(). 01558 { 01559 if( pdu[ 0] != MBF_MASK_WRITE_REGISTER) 01560 return MB_ERROR_FUNCTION; 01561 01562 GETU16( *reg, pdu+ 1); 01563 GETU16( *andmask, pdu+ 3); 01564 GETU16( *ormask, pdu+ 5); 01565 01566 return MB_OK; 01567 }
|
|
parse response PDU packet of Response Read Coils request(0x01)
Definition at line 1280 of file modbus_utils.c. References MB_ERROR_FUNCTION, MB_OK, and MBF_READ_COILS. Referenced by MBSerialReadCoils(), and MBTCPReadCoils(). 01281 { 01282 int i= 0; 01283 if( pdu[ 0] != MBF_READ_COILS) 01284 return MB_ERROR_FUNCTION; 01285 01286 *no= pdu[ 1]; 01287 for( i= 0; i< *no; i++) 01288 value[ i]= pdu[ 2+ i]; 01289 01290 return MB_OK; 01291 }
|
|
Modbus get response value from PDU.
Definition at line 1258 of file modbus_utils.c. References MB_ERROR_FUNCTION, MB_OK, and MBF_READ_DECRETE_INPUTS. Referenced by MBSerialReadDecreteInputs(), and MBTCPReadDecreteInputs(). 01259 { 01260 int i= 0; 01261 if( pdu[ 0] != MBF_READ_DECRETE_INPUTS) 01262 return MB_ERROR_FUNCTION; 01263 01264 *no= pdu[ 1]; 01265 for( i= 0; i< *no; i++) 01266 value[ i]= pdu[ 2+ i]; 01267 01268 return MB_OK; 01269 }
|
|
|
|
|
|
parse response PDU packet of Read Holding Registers request(0x03)
Definition at line 1324 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_READ_HOLDING_REGISTERS. Referenced by MBSerialReadHoldingRegisters(), and MBTCPReadHoldingRegisters(). 01325 { 01326 int i= 0; 01327 if( pdu[ 0] != MBF_READ_HOLDING_REGISTERS) 01328 return MB_ERROR_FUNCTION; 01329 01330 *no= pdu[ 1]; 01331 for( i= 0; i< *no/2; i++) 01332 GETU16( value[ i], ( pdu+ 2+ i*2)); 01333 01334 return MB_OK; 01335 }
|
|
parse response PDU packet of Read Input Registers request(0x04)
Definition at line 1302 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_READ_INPUT_REGISTERS. Referenced by MBSerialReadInputRegisters(), and MBTCPReadInputRegisters(). 01303 { 01304 int i= 0; 01305 if( pdu[ 0] != MBF_READ_INPUT_REGISTERS) 01306 return MB_ERROR_FUNCTION; 01307 01308 *no= pdu[ 1]; 01309 for( i= 0; i< *no/2; i++) 01310 GETU16( value[ i], pdu+ 2+ i*2); 01311 01312 return MB_OK; 01313 }
|
|
parse response PDU packet of Response Read Coils request(0x17)
Definition at line 1534 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_READ_WRITE_MULTIPLE_REGISTERS. Referenced by MBSerialReadWriteMultipleRegisters(), and MBTCPReadWriteMultipleRegisters(). 01535 { 01536 int i= 0; 01537 if( pdu[ 0] != MBF_READ_WRITE_MULTIPLE_REGISTERS) 01538 return MB_ERROR_FUNCTION; 01539 01540 *no= pdu[ 1]; 01541 for( i= 0; i< *no/2; i++) 01542 GETU16( value[ i], pdu+ 2+ i*2); 01543 01544 return MB_OK; 01545 }
|
|
parse response PDU packet of Report Slave ID function(0x16)
Definition at line 1578 of file modbus_utils.c. References MB_ERROR_FUNCTION, MB_OK, and MBF_REPORT_SLAVE_ID. Referenced by MBSerialReportSlaveID(). 01579 { 01580 if( pdu[ 0] != MBF_REPORT_SLAVE_ID) 01581 return MB_ERROR_FUNCTION; 01582 01583 *slave_id= pdu[ 2]; 01584 *status= pdu[ 3]; 01585 01586 return MB_OK; 01587 }
|
|
parse response PDU packet of Write Multiple Coil request(0x0F)
Definition at line 1386 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_WRITE_MULTIPLE_COILS. Referenced by MBSerialWriteMultipleCoils(), and MBTCPWriteMultipleCoils(). 01387 { 01388 if( pdu[ 0] != MBF_WRITE_MULTIPLE_COILS) 01389 return MB_ERROR_FUNCTION; 01390 01391 GETU16( *address, pdu+ 1); 01392 GETU16( *value, pdu+ 3); 01393 01394 return MB_OK; 01395 }
|
|
parse response PDU packet of Write Multiple Registers request(0x10)
Definition at line 1346 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_WRITE_MULTIPLE_REGISTERS. Referenced by MBSerialWriteMultipleRegisters(), and MBTCPWriteMultipleRegisters(). 01347 { 01348 if( pdu[ 0] != MBF_WRITE_MULTIPLE_REGISTERS) 01349 return MB_ERROR_FUNCTION; 01350 01351 GETU16( *address, pdu+ 1); 01352 GETU16( *no, pdu+ 3); 01353 01354 return MB_OK; 01355 }
|
|
parse response PDU packet of Write Single Coil request(0x05)
Definition at line 1366 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_WRITE_SINGLE_COIL. Referenced by MBSerialWriteSingleCoil(), and MBTCPWriteSingleCoil(). 01367 { 01368 if( pdu[ 0] != MBF_WRITE_SINGLE_COIL) 01369 return MB_ERROR_FUNCTION; 01370 01371 GETU16( *address, pdu+ 1); 01372 GETU16( *value, pdu+ 3); 01373 01374 return MB_OK; 01375 }
|
|
parse response PDU packet of Write Single Register request(0x06)
Definition at line 1406 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_WRITE_SINGLE_REGISTER. Referenced by MBSerialWriteSingleRegister(), and MBTCPWriteSingleRegister(). 01407 { 01408 if( pdu[ 0] != MBF_WRITE_SINGLE_REGISTER) 01409 return MB_ERROR_FUNCTION; 01410 01411 GETU16( *address, pdu+ 1); 01412 GETU16( *value, pdu+ 3); 01413 01414 return MB_OK; 01415 }
|
|
parse PDU packet of Write Multiple Coils function(0x0F)
Definition at line 1060 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_WRITE_MULTIPLE_COILS. 01061 { 01062 if( pdu[ 0] != MBF_WRITE_MULTIPLE_COILS) 01063 return MB_ERROR_FUNCTION; 01064 01065 GETU16( *startcoils, pdu+ 1); 01066 GETU16( *no, pdu+ 3); 01067 01068 return MB_OK; 01069 }
|
|
parse PDU packet of Write Multiple Registers function(0x10)
Definition at line 1142 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_WRITE_MULTIPLE_REGISTERS. 01143 { 01144 int i= 0; 01145 if( pdu[ 0] != MBF_WRITE_MULTIPLE_REGISTERS) 01146 return MB_ERROR_FUNCTION; 01147 01148 GETU16( *startreg, pdu+ 1); 01149 GETU16( *noreg, pdu+ 3); 01150 *count= pdu[ 5]; 01151 01152 for( i= 0; i< (*count)/2; i++) 01153 GETU16( value[ i], pdu+ 6+ i* 2); 01154 01155 return MB_OK; 01156 }
|
|
parse PDU packet of Write Single Coil function(0x05)
Definition at line 1040 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_WRITE_SINGLE_COIL. 01041 { 01042 if( pdu[ 0] != MBF_WRITE_SINGLE_COIL) 01043 return MB_ERROR_FUNCTION; 01044 01045 GETU16( *coilreg, pdu+ 1); 01046 GETU16( *onoff, pdu+ 3); 01047 01048 return MB_OK; 01049 }
|
|
parse PDU packet of Write Single Register function(0x06)
Definition at line 1120 of file modbus_utils.c. References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_WRITE_SINGLE_REGISTER. 01121 { 01122 if( pdu[ 0] != MBF_WRITE_SINGLE_REGISTER) 01123 return MB_ERROR_FUNCTION; 01124 01125 GETU16( *devicereg, pdu+ 1); 01126 GETU16( *value, pdu+ 3); 01127 01128 return MB_OK; 01129 }
|
|
Modbus utilities functions.
Definition at line 214 of file modbus_utils.c. References CRC16(), LRC(), MB_ASCII_ADU_MAX_LENGTH, MB_ASCII_PROTOCOL, MB_ERROR_PROTOCOL, MB_RTU_PROTOCOL, MB_TCP_PROTOCOL, and MBDataToASCII(). Referenced by MBSerialWrite(). 00215 { 00216 u8 tmp[ MB_ASCII_ADU_MAX_LENGTH]; 00217 u8 lrc; 00218 u16 crc; 00219 int i= 0, tmp_len= 0, buf_len= 0; 00220 00221 switch( protocol) 00222 { 00223 case MB_ASCII_PROTOCOL: 00224 buf[ 0]= ':'; 00225 00226 tmp[ 0]= address; 00227 tmp_len++; 00228 00229 bcopy( (const char *)pdu, (char *)(tmp+ tmp_len), len); 00230 tmp_len+= len; 00231 00232 lrc= LRC( tmp, tmp_len); 00233 tmp[ tmp_len++]= lrc; 00234 00235 buf_len= MBDataToASCII( buf+ 1, tmp, tmp_len); 00236 buf_len++; 00237 00238 buf[ buf_len++]= 0xd; 00239 buf[ buf_len++]= 0xa; 00240 break; 00241 case MB_RTU_PROTOCOL: 00242 buf[ 0]= address; 00243 buf_len++; 00244 00245 bcopy( (const char *)pdu, (char *)(buf+ 1), len); 00246 buf_len+= len; 00247 00248 crc= CRC16( buf, buf_len); 00249 buf[ buf_len++]= crc >> 8; 00250 buf[ buf_len++]= crc & 0x00FF; 00251 break; 00252 case MB_TCP_PROTOCOL: 00253 buf[ 0]= tid >> 8; 00254 buf[ 1]= tid & 0x00FF; 00255 00256 buf[ 2]= 0; 00257 buf[ 3]= 0; 00258 00259 buf[ 4]= (len+1) >> 8; // length of address + pdu 00260 buf[ 5]= (len+1) & 0x00FF; // length of address + pdu 00261 00262 buf[ 6]= address; 00263 00264 bcopy( (const char *)pdu, (char *)(buf+ 7), len); 00265 buf_len= 7+ len; 00266 break; 00267 default: 00268 return MB_ERROR_PROTOCOL; 00269 } 00270 00271 return buf_len; 00272 }
Here is the call graph for this function: ![]() |
|
assemble function code and data to ADU
Definition at line 303 of file modbus_utils.c. 00304 { 00305 int ret_len= 0; 00306 00307 buf[ 0]= function; 00308 bcopy( (const char *)data, (char *)(buf+ 1), len); 00309 ret_len= len+ 1; 00310 00311 return ret_len; 00312 }
|
|
0x16
Definition at line 578 of file modbus_utils.c. References MBF_MASK_WRITE_REGISTER, and PUTU16. Referenced by MBSerialMaskWriteRegister(), and MBTCPMaskwWriteRegister(). 00579 { 00580 int len= 0; 00581 buf[ len]= MBF_MASK_WRITE_REGISTER; 00582 len++; 00583 PUTU16( buf+ len, reg); // don't put ++ in the macro 00584 len+= 2; 00585 PUTU16( buf+ len, andmask); 00586 len+= 2; 00587 PUTU16( buf+ len, ormask); 00588 len+= 2; 00589 00590 return len; 00591 }
|
|
0x01
Definition at line 371 of file modbus_utils.c. References MBF_READ_COILS, and PUTU16. Referenced by MBSerialReadCoils(), and MBTCPReadCoils(). 00372 { 00373 int len= 0; 00374 buf[ len]= MBF_READ_COILS; 00375 len++; 00376 PUTU16( buf+ len, startcoils); // don't put ++ in the macro 00377 len+= 2; 00378 PUTU16( buf+ len, no); 00379 len+= 2; 00380 00381 return len; 00382 }
|
|
Master function 0x02.
Definition at line 349 of file modbus_utils.c. References MBF_READ_DECRETE_INPUTS, and PUTU16. Referenced by MBSerialReadDecreteInputs(), and MBTCPReadDecreteInputs(). 00350 { 00351 int len= 0; 00352 buf[ len]= MBF_READ_DECRETE_INPUTS; 00353 len++; 00354 PUTU16( buf+ len, startdec); // don't put ++ in the macro 00355 len+= 2; 00356 PUTU16( buf+ len, no); 00357 len+= 2; 00358 00359 return len; 00360 }
|
|
0x07
Definition at line 622 of file modbus_utils.c. References MBF_READ_EXCEPTION_STATUS. Referenced by MBSerialReadExceptionStatus(). 00623 { 00624 int len= 0; 00625 buf[ len]= MBF_READ_EXCEPTION_STATUS; 00626 len++; 00627 00628 return len; 00629 }
|
|
0x18
Definition at line 601 of file modbus_utils.c. References MBF_READ_FIFO_QUEUE, and PUTU16. Referenced by MBSerialReadFIFOQueue(), and MBTCPReadFIFOQueue(). 00602 { 00603 int len= 0; 00604 buf[ len]= MBF_READ_FIFO_QUEUE; 00605 len++; 00606 PUTU16( buf+ len, FIFOAddr); // don't put ++ in the macro 00607 len+= 2; 00608 00609 return len; 00610 }
|
|
0x03
Definition at line 469 of file modbus_utils.c. References MBF_READ_HOLDING_REGISTERS, and PUTU16. Referenced by MBSerialReadHoldingRegisters(), and MBTCPReadHoldingRegisters(). 00470 { 00471 int len= 0; 00472 buf[ len]= MBF_READ_HOLDING_REGISTERS; 00473 len++; 00474 PUTU16( buf+ len, startreg); // don't put ++ in the macro 00475 len+= 2; 00476 PUTU16( buf+ len, no); 00477 len+= 2; 00478 00479 return len; 00480 }
|
|
0x04
Definition at line 447 of file modbus_utils.c. References MBF_READ_INPUT_REGISTERS, and PUTU16. Referenced by MBSerialReadInputRegisters(), and MBTCPReadInputRegisters(). 00448 { 00449 int len= 0; 00450 buf[ len]= MBF_READ_INPUT_REGISTERS; 00451 len++; 00452 PUTU16( buf+ len, startreg); // don't put ++ in the macro 00453 len+= 2; 00454 PUTU16( buf+ len, no); 00455 len+= 2; 00456 00457 return len; 00458 }
|
|
0x17
Definition at line 547 of file modbus_utils.c. References MBF_READ_WRITE_MULTIPLE_REGISTERS, and PUTU16. Referenced by MBSerialReadWriteMultipleRegisters(), and MBTCPReadWriteMultipleRegisters(). 00548 { 00549 int len= 0, i= 0; 00550 buf[ len]= MBF_READ_WRITE_MULTIPLE_REGISTERS; 00551 len++; 00552 PUTU16( buf+ len, rsreg); // don't put ++ in the macro 00553 len+= 2; 00554 PUTU16( buf+ len, rno); 00555 len+= 2; 00556 PUTU16( buf+ len, wsreg); 00557 len+= 2; 00558 PUTU16( buf+ len, wno); 00559 len+= 2; 00560 buf[ len++]= count; 00561 00562 for( i= 0; i< wno/2; i++, len+= 2) 00563 PUTU16( buf+len, wv[ i]); 00564 00565 return len; 00566 }
|
|
0x11
Definition at line 692 of file modbus_utils.c. References MBF_REPORT_SLAVE_ID. Referenced by MBSerialReportSlaveID(). 00693 { 00694 int len= 0; 00695 buf[ len]= MBF_REPORT_SLAVE_ID; 00696 len++; 00697 00698 return len; 00699 }
|
|
make response PDU packet of Diagnostic request(0x08)
Definition at line 924 of file modbus_utils.c. References MBF_DIAGNOSTIC, and PUTU16. Referenced by MBSerialSendDiagnostic(). 00925 { 00926 int len= 0; 00927 buf[ len]= MBF_DIAGNOSTIC; 00928 len++; 00929 PUTU16( buf+len, subfunc); 00930 len+= 2; 00931 PUTU16( buf+len, data); 00932 len+= 2; 00933 00934 return len; 00935 }
|
|
make response PDU packet of Get Comm Event Counter request(0x0B)
Definition at line 946 of file modbus_utils.c. References MBF_GET_COMM_EVENT_COUNTER, and PUTU16. Referenced by MBSerialSendGetCommEventCounter(). 00947 { 00948 int len= 0; 00949 buf[ len]= MBF_GET_COMM_EVENT_COUNTER; 00950 len++; 00951 PUTU16( buf+len, status); 00952 len+= 2; 00953 PUTU16( buf+len, eventcount); 00954 len+= 2; 00955 00956 return len; 00957 }
|
|
make response PDU packet of Get Comm Event Counter request(0x0C)
Definition at line 971 of file modbus_utils.c. References MBF_GET_COMM_EVENT_LOG, and PUTU16. Referenced by MBSerialSendGetCommEventLog(). 00972 { 00973 int len= 0; 00974 buf[ len]= MBF_GET_COMM_EVENT_LOG; 00975 len++; 00976 buf[ len]= no; 00977 len++; 00978 PUTU16( buf+len, status); 00979 len+= 2; 00980 PUTU16( buf+len, eventcount); 00981 len+= 2; 00982 PUTU16( buf+len, messagecount); 00983 len+= 2; 00984 00985 bcopy( (const char *)events, (char *)(buf+ len), no- 6); 00986 00987 return len; 00988 }
|
|
make response PDU packet of Response Read Coils request(0x01)
Definition at line 739 of file modbus_utils.c. References MBF_READ_COILS, and PUTU16. Referenced by MBSerialSendReadCoils(), and MBTCPSendReadCoils(). 00740 { 00741 int len= 0, i= 0; 00742 buf[ len]= MBF_READ_COILS; 00743 len++; 00744 buf[ len]= no; 00745 len++; 00746 if( no & 0x07) // test if no could be divied by 8 00747 no= (no >> 3) + 1; 00748 else 00749 no>>= 3; 00750 00751 for( i= 0; i< no/2; i++, len+= 2) 00752 PUTU16( buf+len, value[ i]); 00753 00754 00755 return len; 00756 }
|
|
Response function.
Definition at line 711 of file modbus_utils.c. References MBF_READ_DECRETE_INPUTS, and PUTU16. Referenced by MBSerialSendReadDecreteInputs(), and MBTCPSendReadDecreteInputs(). 00712 { 00713 int len= 0, i= 0; 00714 buf[ len]= MBF_READ_DECRETE_INPUTS; 00715 len++; 00716 buf[ len]= no; 00717 len++; 00718 if( no & 0x07) // test if no could be divied by 8 00719 no= (no >> 3) + 1; 00720 else 00721 no>>= 3; 00722 00723 for( i= 0; i< no/2; i++, len+= 2) 00724 PUTU16( buf+len, value[ i]); 00725 00726 00727 return len; 00728 }
|
|
|
|
|
|
make response PDU packet of Read Holding Registers request(0x03)
Definition at line 790 of file modbus_utils.c. References MBF_READ_HOLDING_REGISTERS, and PUTU16. Referenced by MBSerialSendReadHoldingRegisters(), and MBTCPSendReadHoldingRegisters(). 00791 { 00792 int len= 0, i= 0; 00793 buf[ len]= MBF_READ_HOLDING_REGISTERS; 00794 len++; 00795 buf[ len]= no; 00796 len++; 00797 00798 for( i= 0; i< no/2; i++, len+= 2) 00799 PUTU16( buf+len, value[ i]); 00800 00801 return len; 00802 }
|
|
make response PDU packet of Read Input Registers request(0x04)
Definition at line 767 of file modbus_utils.c. References MBF_READ_INPUT_REGISTERS, and PUTU16. Referenced by MBSerialSendReadInputRegisters(), and MBTCPSendReadInputRegisters(). 00768 { 00769 int len= 0, i= 0; 00770 buf[ len]= MBF_READ_INPUT_REGISTERS; 00771 len++; 00772 buf[ len]= no; 00773 len++; 00774 00775 for( i= 0; i< no/2; i++, len+= 2) 00776 PUTU16( buf+len, value[ i]); 00777 00778 return len; 00779 }
|
|
make response PDU packet of Write Multiple Registers request(0x10)
Definition at line 813 of file modbus_utils.c. References MBF_WRITE_MULTIPLE_REGISTERS, and PUTU16. Referenced by MBSerialSendWriteMultipleRegisters(), and MBTCPSendWriteMultipleRegisters(). 00814 { 00815 int len= 0; 00816 buf[ len]= MBF_WRITE_MULTIPLE_REGISTERS; 00817 len++; 00818 PUTU16( buf+len, address); 00819 len+= 2; 00820 PUTU16( buf+len, no); 00821 len+= 2; 00822 00823 return len; 00824 }
|
|
make response PDU packet of Write Single Coil request(0x05)
Definition at line 835 of file modbus_utils.c. References MBF_WRITE_SINGLE_COIL, and PUTU16. Referenced by MBSerialSendWriteSingleCoil(), and MBTCPSendWriteSingleCoil(). 00836 { 00837 int len= 0; 00838 buf[ len]= MBF_WRITE_SINGLE_COIL; 00839 len++; 00840 PUTU16( buf+len, address); 00841 len+= 2; 00842 PUTU16( buf+len, value); 00843 len+= 2; 00844 00845 return len; 00846 }
|
|
make response PDU packet of Write Single Register request(0x06)
Definition at line 857 of file modbus_utils.c. References MBF_WRITE_SINGLE_REGISTER, and PUTU16. Referenced by MBSerialSendWriteSingleRegister(), and MBTCPSendWriteSingleRegister(). 00858 { 00859 int len= 0; 00860 buf[ len]= MBF_WRITE_SINGLE_REGISTER; 00861 len++; 00862 PUTU16( buf+len, address); 00863 len+= 2; 00864 PUTU16( buf+len, value); 00865 len+= 2; 00866 00867 return len; 00868 }
|
|
0x0F
Definition at line 417 of file modbus_utils.c. References MBF_WRITE_MULTIPLE_COILS, and PUTU16. Referenced by MBSerialWriteMultipleCoils(), and MBTCPWriteMultipleCoils(). 00418 { 00419 int len= 0; 00420 buf[ len]= MBF_WRITE_MULTIPLE_COILS; 00421 len++; 00422 PUTU16( buf+ len, startcoils); // don't put ++ in the macro 00423 len+= 2; 00424 PUTU16( buf+ len, no); 00425 len+= 2; 00426 buf[ len]= count; 00427 len++; 00428 if( count & 0x07) // test if no could be divied by 8 00429 count = ( count >> 3)+ 1; 00430 else 00431 count >>= 3; 00432 bcopy( (const char *)data, (char *)(buf+ len), count); 00433 len+= count; 00434 00435 return len; 00436 }
|
|
0x10
Definition at line 515 of file modbus_utils.c. References MBF_WRITE_MULTIPLE_REGISTERS, and PUTU16. Referenced by MBSerialWriteMultipleRegisters(), and MBTCPWriteMultipleRegisters(). 00516 { 00517 int len= 0, i= 0; 00518 buf[ len]= MBF_WRITE_MULTIPLE_REGISTERS; 00519 len++; 00520 PUTU16( buf+ len, startreg); // don't put ++ in the macro 00521 len+= 2; 00522 PUTU16( buf+ len, noreg); 00523 len+= 2; 00524 buf[ len]= count; 00525 len++; 00526 00527 for( i= 0; i< count/2; i++, len+= 2) 00528 PUTU16( buf+len, value[ i]); 00529 00530 00531 return len; 00532 }
|
|
0x05
Definition at line 393 of file modbus_utils.c. References MBF_WRITE_SINGLE_COIL, and PUTU16. Referenced by MBSerialWriteSingleCoil(), and MBTCPWriteSingleCoil(). 00394 { 00395 int len= 0; 00396 buf[ len]= MBF_WRITE_SINGLE_COIL; 00397 len++; 00398 PUTU16( buf+ len, coilreg); // don't put ++ in the macro 00399 len+= 2; 00400 PUTU16( buf+ len, onoff); 00401 len+= 2; 00402 00403 return len; 00404 }
|
|
0x06
Definition at line 491 of file modbus_utils.c. References MBF_WRITE_SINGLE_REGISTER, and PUTU16. Referenced by MBSerialWriteSingleRegister(), and MBTCPWriteSingleRegister(). 00492 { 00493 int len= 0; 00494 buf[ len]= MBF_WRITE_SINGLE_REGISTER; 00495 len++; 00496 PUTU16( buf+ len, devicereg); // don't put ++ in the macro 00497 len+= 2; 00498 PUTU16( buf+ len, value); 00499 len+= 2; 00500 00501 return len; 00502 }
|