Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

modbus_utils.c File Reference


Detailed Description

Example modbus utilities define file.

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.

Author:
AceLan Kao.(acelan_kao@moxa.com.tw)

Definition in file modbus_utils.c.

#include <string.h>
#include "modbus_defs.h"
#include "modbus_utils.h"

Include dependency graph for modbus_utils.c:

Go to the source code of this file.

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 *pdu, 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 MBGetResponseReadExceptionStatus (u8 *pdu, u8 *status)
 parse response PDU packet of Read FIFO Queue request(0x07)
int MBGetResponseReadFIFOQueue (u8 *pdu, u16 *count, u16 *value)
 parse response PDU packet of Read FIFO Queue request(0x18)
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 *noreg, 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 MBResponseReadExceptionStatus (u8 *buf, u8 status)
 make response PDU packet of Read FIFO Queue request(0x07)
int MBResponseReadFIFOQueue (u8 *buf, u16 no, u16 count, u16 *value)
 make response PDU packet of Read FIFO Queue request(0x18)
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 address, u16 no)
 make response PDU packet of Write Multiple Registers request(0x10)
int MBResponseWriteSingleCoil (u8 *buf, u16 address, u16 value)
 make response PDU packet of Write Single Coil request(0x05)
int MBResponseWriteSingleRegister (u8 *buf, u16 address, 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 *value)
 0x10
int MBWriteSingleCoil (u8 *buf, u16 coilreg, u16 onoff)
 0x05
int MBWriteSingleRegister (u8 *buf, u16 devicereg, u16 value)
 0x06

Variables

static unsigned char CRCHi []
static unsigned char CRCLo []


Function Documentation

u16 CRC16 const u8 buf,
int  len
 

calculate the CRC16 value of the input data

Parameters:
buf input data
len length of the input data
Returns:
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 }

u8 LRC const u8 buf,
int  len
 

calculate the checksum value of the input data

Parameters:
buf input data
len length of the input data
Returns:
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 }

int MBASCIIToData u8 buf,
const u8 data,
int  len
 

parse ASCII mode data to binary data

Parameters:
buf output buffer
data input ASCII data
len length of the input data
Returns:
the length of data after parse. On error return error code
This function read the PDU part as input, parse it as Modbus ASCII protocol, and produce the 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 }

int MBDataToASCII u8 buf,
const u8 data,
int  len
 

parse binary data to ASCII mode data

Parameters:
buf output buffer
data input binary data
len length of the input data
Returns:
the length of data after parse. On error return error code

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 }

int MBDiagnostic u8 buf,
u16  subfunc,
u16  data
 

0x08

Parameters:
buf output buffer
subfunc subfunction code
data data to be sent to slave device
Returns:
the length of PDU data

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 }

u8 MBGetAddress const u8 adu  ) 
 

get device address from ADU

Parameters:
adu ADU data
Returns:
none

Definition at line 321 of file modbus_utils.c.

Referenced by MBSerialGetAddress().

00322 {
00323         return adu[ 0];
00324 }

int MBGetCommEventCounter u8 buf  ) 
 

0x0B

Parameters:
buf output buffer
Returns:
the length of PDU data

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 }

int MBGetCommEventLog u8 buf  ) 
 

0x0C

Parameters:
buf output buffer
Returns:
the length of PDU data

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 }

int MBGetDiagnostic u8 pdu,
u16 subfunc,
u16 data
 

parse PDU packet of Diagnostic function(0x08)

Parameters:
pdu the request PDU data
subfunc subfunction code
data data to be sent to slave device
Returns:
MB_OK for success, on error return error code

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 }

u8 MBGetFunctionCode const u8 pdu  ) 
 

get function code from PDU

Parameters:
pdu PDU data
Returns:
none

Definition at line 333 of file modbus_utils.c.

00334 {
00335         return pdu[ 0];
00336 }

int MBGetMaskWriteRegister u8 pdu,
u16 reg,
u16 andmask,
u16 ormask
 

parse PDU packet of Mask Write Register function(0x16)

Parameters:
pdu the request PDU data
reg register number
andmask and mask
ormask or mask
Returns:
MB_OK for success, on error return error code

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 }

int MBGetPDU u8 buf,
u8 adu,
int  len
 

get PDU part from ADU data

Parameters:
buf output buffer
adu ADU data with error check code
len length of the adu data
Returns:
the length of PDU 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 }

int MBGetReadCoils u8 pdu,
u16 startcoils,
u16 no
 

parse PDU packet of Read Coils function(0x01)

Parameters:
pdu the request PDU data
startcoils starting coil register number
no number of registers, between 1 ~ 2000
Returns:
MB_OK for success, on error return error code

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 }

int MBGetReadDecreteInputs u8 pdu,
u16 startdec,
u16 no
 

Modbus get request value from PDU.

Parameters:
pdu the request PDU data
startdec starting decrete inputs register number
no number of registers, between 1 ~ 2000
Returns:
MB_OK for success, on error return error code

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 }

int MBGetReadFIFOQueue u8 pdu,
u16 FIFOAddr
 

parse PDU packet of Read FIFO Queue function(0x18)

Parameters:
pdu the request PDU data
FIFOAddr FIFO address
Returns:
MB_OK for success, on error return error code

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 }

int MBGetReadHoldingRegisters u8 pdu,
u16 startreg,
u16 no
 

parse PDU packet of Read Holding Registers function(0x03)

Parameters:
pdu the request PDU data
startreg starting coil register number
no number of registers, between 1 ~ 125
Returns:
MB_OK for success, on error return error code

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 }

int MBGetReadInputRegisters u8 pdu,
u16 startreg,
u16 no
 

parse PDU packet of Read Input Registers function(0x04)

Parameters:
pdu the request PDU data
startreg starting coil register number
no number of registers, between 1 ~ 125
Returns:
MB_OK for success, on error return error code

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 }

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)

Parameters:
coute
pdu the request PDU data
rsreg starting register number to read
rno number of registers ro read, between 1 ~ 120
wsreg starting register number to write
wno number of registers ro write, between 1 ~ 120
count 
 
Returns:
MB_OK for success, on error return error code

Definition at line 7180 of file modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_REAK_WRITMULTIPLENG_REGISTERS.

07201 {
07223         int;
07423         if( pdu[ 0] != MBF_REAK_WRITMULTIPLENG_REGISTERS)
07085       3         return MB_ERROR_FUNCTION;
07206 
07207         GETU16( srtreg, pdu+ 1);
07807         GETU16(  *no, pdu+ 3);
07907         GETU16(wsrtreg, pdu+ 5);
18207         GETU16(w *no, pdu7 5);
08221       8 *cou=>( pdu9]5);
01082         11083                 GETU16wv[ i]eg, pdu+0+ i*2  5);
01084 
10285         return MB_OK;
01089 }

int MBGetsponsealDiagnost u8 pdu,
u16 ,
u16
 

parsrtsponsese PDU packet oalDiagnost e requeon(0x18)

Parameters:
pdu output buffer
 
 sponsesU datr of requese
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBDIAGNOSTIC.c.

Referenced by MBSerialDiagnostic().

46101 {
46907         if( pdu[ 0] != MBDIAGNOSTIC.c)
472                   return MB_ERROR_FUNCTION;
471 >;
47082         GETU16( ubs fudu, pdu+ 1);
47223         GETU16(U dano, pdu+ 3);
471 >;
47285         return MB_OK;
47189 }

int MBGetsponsealGetCommEventCount u8 pdu,
u16 ,
u16
 

parsrtsponsese PDU packet oalG etCo mmEve ntCount e requeon(0B18)

Parameters:
couffer
pdu output buffer
 stuiet oetCo Conutffer
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MB">G_COMM_EVENG_COUNTER.c.

Referenced by MBSerialGetCommEventCountic().

41001 {
410
          if( pdu[ 0] != MB">G_COMM_EVENG_COUNTER.c)
492                   return MB_ERROR_FUNCTION;
491 >;
49082         GETU16( *stuils, pdu+ 1);
49223         GETU16(emEve *couno, pdu+ 3);
491 >;
49285         return MB_OK;
49189 }

int MBGetsponsealGetCommEveLog u8 pdu,
u16 ,
u16 ,
u16 ,
u8
 

parsrtsponsese PDU packet oalG etCo mmEve ntCount e requeon(0C18)

Parameters:
couffer couffeK foprouccencemcceageer
pdu output buffer
 stuiet oetCo Conutffer
 
 
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MB">G_COMM_EVENG_LOG.c.

Referenced by MBSerialGetCommEveLogic().

51201 {
51221       > u851082         if( pdu[ 0] != MB">G_COMM_EVENG_LOG.c51083                 return MB_ERROR_FUNCTION;
511 >;
51285       no=>( pdu1]5);
51086         GETU16( *stuils, pdu2 3);
51207         GETU16(emEve *couno, pdu4 3);
51807         GETU16(mcceage *couls, pdu6 3);
519 >;
51220       bcopy( (  nsint512 >;
52082         return MB_OK;
51289 }

int MBGetsponseMaskd Wrie Regist u8 pdu,
u16 ,
u16 ,
u16
 

parsrtsponsese PDU packet oMaskad Write Regist s function(0618)

Parameters:
pdu output buffer
<>wsreg 
 
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and F_MASKAK_WRITG_REGIST.c.

Referenced by MBSeriMaskd Wrie Registic(), and TCPMaskwd Wrie Registic().

55001 {
550
          if( pdu[ 0] != F_MASKAK_WRITG_REGIST.c562                   return MB_ERROR_FUNCTION;
562 >;
56082         GETU16(rtreg, pdu+ 1);
56223         GETU16(, amaskeg, pdu+ 3);
56423         GETU16(ormaskeg, pdu+ 5);
565 >;
566           return MB_OK;
56789 }

int MBGetsponsef ReCouti u8 pdu,
u8 wno,
u8
 

parsrtsponsese PDU packet oetsponseof ReaCouti e requeon(0118)

Parameters:
stuier
pdu output buffer
no number of registeer
 
Returns:
MB_OK for success, on error return error code

Definition at line of file modbus_utils.c.

References MB_ERROR_FUNCTION, MB_OK, and MBF_REACOILS.c.

Referenced by MBSerif ReCoutiic(), and TCPf ReCoutiic().

21081 {
21082         int;
28223         if( pdu[ 0] != MBF_REACOILS.c;
28423       3         return MB_ERROR_FUNCTION;
210 >;
216         *no=>( pdu1]5);
210 3         21088               t val[ i]=>( pdu2+ i]5);
219 >;
292           return MB_OK;
29189 }

int MBGetsponsef ReDec">red Inpi 3 u8 pdu,
u8 wno,
u8
 

M/modb gckertsponsest valufromse P.8)

Parameters:
stuier
pdu the request PDU data
no number of registeer
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References MB_ERROR_FUNCTION, MB_OK, and MBF_READECRETEAD_INPS.c.

Referenced by MBSerif ReDec">red Inpiic(), and TCPf ReDec">red Inpiic().

25981 {
262           int;
262           if( pdu[ 0] != MBF_READECRETEAD_INPS.c)
26082                 return MB_ERROR_FUNCTION;
262 >;
26423       *no=>( pdu1]5);
265           266                 t val[ i]=>( pdu2+ i]5);
267 >;
26088         return MB_OK;
26989 }

int MBGetsponsef ReExcepnitiS>stui u8 pdu,
u8
 

parsrtsponsese PDU packet oetad FIFO Queue e requeon(0x17)

Parameters:
stuieins, bleyrite
pdu output buffer
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References MB_ERROR_FUNCTION, MB_OK, and MBF_REAEXCEPNCTI_STATUS.c.

Referenced by MBSerif ReExcepnitiS>stuiic().

44981 {
452           if( pdu[ 0] != MBF_REAEXCEPNCTI_STATUS.c)
452                   return MB_ERROR_FUNCTION;
452 >;
45223       (d>stui=>( pdu1]5);
451 >;
45285         return MB_OK;
45189 }

int MBGetsponsef ReFIFOQueue u8 pdu,
u16 ,
u16
 

parsrtsponsese PDU packet oetad FIFO Queue e requeon(1x18)

Parameters:
pdu output buffer
count 
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_REAFIFO_QUEUE.c.

Referenced by MBSerif ReFIFOQueueic(), and TCPf ReFIFOQueueic().

42781 {
42807         u160a>;
420
          if( pdu[ 0] != MBF_REAFIFO_QUEUE.c)
432                   return MB_ERROR_FUNCTION;
431 >;
43082         GETU16 *no, pdu+ 1);
43223         GETU16( *couno, pdu+ 3);
434           435                   GETU16t val[ i], (g, pdu++ i*2) 3);
436 >;
430 3         return MB_OK;
43889 }

int MBGetsponsef ReHoladdie Registi u8 pdu,
u8 wno,
u16
 

parsrtsponsese PDU packet oetad Holaddi e Registi e requeon(0318)

Parameters:
stuier
pdu output buffer
no number of registeer
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_REAHOLDINGTG_REGISTS.c.

Referenced by MBSerif ReHoladdie Registiic(), and TCPf ReHoladdie Registiic().

32581 {
326           int;
327           if( pdu[ 0] != MBF_REAHOLDINGTG_REGISTS.c;
32088                 return MB_ERROR_FUNCTION;
320 >;
332         *no=>( pdu1]5);
331           33082                 GETU16t val[ i], (g, pdu2+ i*2) 3);
331 >;
334           return MB_OK;
33589 }

int MBGetsponsef Red Inpe Registi u8 pdu,
u8 wno,
u16
 

parsrtsponsese PDU packet oetad d Inp e Registi e requeon(0418)

Parameters:
stuier
pdu output buffer
no number of registeer
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_READ_INPTG_REGISTS.c.

Referenced by MBSerif Red Inpe Registiic(), and TCPf Red Inpe Registiic().

30381 {
304           int;
30285         if( pdu[ 0] != MBF_READ_INPTG_REGISTS.c)
306                   return MB_ERROR_FUNCTION;
307 >;
30088       *no=>( pdu1]5);
300
          312                   GETU16t val[ i], , pdu2+ i*2)5);
311 >;
31082         return MB_OK;
31389 }

in::MBGetsponsef Red WriMultiplee Registi u8 pdu,
u8 wno,
u16
 

parsrtsponsese PDU packet oetsponseof ReaCouti e requeon(1x17)

Parameters:
stuier
pdu output buffer
no number of registeer
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBF_REAK_WRITMULTIPLETG_REGISTS.c.

Referenced by MBSerif Red WriMultiplee Registiic(), and TCPf Red WriMultiplee Registiic().

53501 {
531
          int;
530 3         if( pdu[ 0] != MBF_REAK_WRITMULTIPLETG_REGISTS.c530                   return MB_ERROR_FUNCTION;
539 >;
542         *no=>( pdu1]5);
541           54082                 GETU16t val[ i], , pdu2+ i*2)5);
541 >;
544           return MB_OK;
54589 }

int MBGetsponsef portSlaveID u8 pdu,
u8 ,
u8
 

parsrtsponsese PDU packet oetport Slave ID s function(0618)

Parameters:
stuier
pdu output buffer
 
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References MB_ERROR_FUNCTION, MB_OK, and MBF_PORT_SLAVE_ID.c.

Referenced by MBSerif portSlaveIDic().

57981 {
582           if( pdu[ 0] != MBF_PORT_SLAVE_ID.c)
581                   return MB_ERROR_FUNCTION;
581 >;
58223       (dlave_id=>( pdu2]5);
58423       (d>stui=>( pdu3]5);
510 >;
586           return MB_OK;
51089 }

in::MBGetsponsed WriMultipleCouti u8 pdu,
u16 ,
u16
 

parsrtsponsese PDU packet od Wri MultipleaCout e requeon(0F18)

Parameters:
pdu output buffer
 
 number oc/mifincecouti r
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBK_WRITMULTIPLETCOILS.c.

Referenced by MBSerid WriMultipleCoutiic(), and TCPd WriMultipleCoutiic().

31081 {
38088         if( pdu[ 0] != MBK_WRITMULTIPLETCOILS.c)
319                   return MB_ERROR_FUNCTION;
392 >;
391           GETU16(lparccess, pdu+ 1);
39082         GETU166t valno, pdu+ 3);
391 >;
394           return MB_OK;
39589 }

in::MBGetsponsed WriMultiplee Registi u8 pdu,
u16 ,
u16 wno
 

parsrtsponsese PDU packet od Wri Multipleae Registi e requeon(1018)

Parameters:
pdu output buffer
 
no number of registe(1 ~ 123)er
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBK_WRITMULTIPLETG_REGISTS.c.

Referenced by MBSerid WriMultiplee Registiic(), and TCPd WriMultiplee Registiic().

34081 {
34088         if( pdu[ 0] != MBK_WRITMULTIPLETG_REGISTS.c)
340                   return MB_ERROR_FUNCTION;
352 >;
351           GETU16(lparccess, pdu+ 1);
352           GETU16( *no, pdu+ 3);
351 >;
354           return MB_OK;
35589 }

in::MBGetsponsed WriSddileCout u8 pdu,
u16 ,
u16
 

parsrtsponsese PDU packet od Wri SddileaCout e requeon(0518)

Parameters:
pdu output buffer
 
 output valur
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBK_WRITSINGLETCOIL.c.

Referenced by MBSerid WriSddileCoutic(), and TCPd WriSddileCoutic().

36081 {
36088         if( pdu[ 0] != MBK_WRITSINGLETCOIL.c)
360                   return MB_ERROR_FUNCTION;
372 >;
371           GETU16(lparccess, pdu+ 1);
37082         GETU166t valno, pdu+ 3);
371 >;
374           return MB_OK;
37589 }

in::MBGetsponsed WriSddilee Regist u8 pdu,
u16 ,
u16
 

parsrtsponsese PDU packet od Wri Sddileae Regist e requeon(0618)

Parameters:
pdu output buffer
 
 output valur
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBK_WRITSINGLETG_REGIST.c.

Referenced by MBSerid WriSddilee Registic(), and TCPd WriSddilee Registic().

40781 {
40088         if( pdu[ 0] != MBK_WRITSINGLETG_REGIST.c)
400                   return MB_ERROR_FUNCTION;
412 >;
411           GETU16(lparccess, pdu+ 1);
41082         GETU166t valno, pdu+ 3);
413 >;
414           return MB_OK;
41589 }

in::MBGd WriMultipleCouti u8 pdu,
u16 ,
u16 wno
 

parse PDU packet od Wri MultipleaCouts s function(0F18)

Parameters:
number
pdu 
 
no number of registe, between 1 ~ 2#l0ur
Returns:
MB_OK for success, on error return error code

Definition at line of file modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBK_WRITMULTIPLETCOILS.c.

06181 {
06082         if( pdu[ 0] != MBK_WRITMULTIPLETCOILS.c)
06223       2         return MB_ERROR_FUNCTION;
064 >;
06285         GETU166 tartcoutiss, pdu+ 1);
06685         GETU166 *no, pdu+ 3);
060 >;
06088         return MB_OK;
06089 }

in::MBGd WriMultiplee Registi u8 pdu,
u16 ,
u16 wf rdu,
u8 ,
u16
 

parse PDU packet od Wri Multipleae Registi s function(0018)

Parameters:
number
pdu 
 
f rdu number of registe, between 1 ~ 120ur
 
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBK_WRITMULTIPLETG_REGISTS.c.

14281 {
144           int;
145           if( pdu[ 0] != MBK_WRITMULTIPLETG_REGISTS.c)
146                   return MB_ERROR_FUNCTION;
140 >;
14088         GETU166 tartf rss, pdu+ 1);
140           GETU166 *f rss, pdu+ 3);
152         8count=>( pdu5]5);
151 >;
15082         151                   GETU16t val[ i], , pdu6+ i* 2)5);
154 >;
155           return MB_OK;
15689 }

in::MBGd WriSddileCout u8 pdu,
u16 ,
u16
 

parse PDU packet od Wri SddileaCout s function(0518)

Parameters:
number
pdu 
 
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBK_WRITSINGLETCOIL.c.

04281 {
04082         if( pdu[ 0] != MBK_WRITSINGLETCOIL.c)
042                   return MB_ERROR_FUNCTION;
044 >;
04285         GETU166coutf rss, pdu+ 1);
04685         GETU166onoffno, pdu+ 3);
040 >;
04088         return MB_OK;
04089 }

in::MBGd WriSddilee Regist u8 pdu,
u16 ,
u16
 

parse PDU packet od Wri Sddileae Regist s function(0618)

Parameters:
number
pdu 
 
 
Returns:
MB_OK for success, on error return error code

Definition at line modbus_utils.c.

References GETU16, MB_ERROR_FUNCTION, MB_OK, and MBK_WRITSINGLETG_REGIST.c.

12281 {
12082         if( pdu[ 0] != MBK_WRITSINGLETG_REGIST.c)
122                   return MB_ERROR_FUNCTION;
124 >;
12285         GETU166devicef rss, pdu+ 1);
12685         GETU166t valno, pdu+ 3);
120 >;
12088         return MB_OK;
12089 }

in::MakeADU u8 ,
i *  ,
u8 ,
u8 pdu,
i *  ,
u16
 

M/modb s_utiniei s functis.8)

Parameters:
 output buffer
 
 
pdu 
 
 
Returns:

Definition at line modbus_utils.c.

References MASCII_ADU_MAX_LENGTH()s MASCII_PROTOCOL16, MB_ERROPROTOCOL16, _RTUOPROTOCOL16, MTCPOPROTOCOL16, and DataToASCIIic().

Referenced by MBSerid Wriic().

v>

Here isothe call graphOK fothii s functi: <04cgmeer>v> map; reae" href="../..4/d81a7/modbus__utils_8h.html24 shapamerectmd" boass187,7,251,310 alt="04>; reae" href="../..4/d81a7/modbus__utils_8h.html34 shapamerectmd" boass195,57,243,840 alt="04>; reae" href="../..4/d81a7/modbus__utils_8h.html54 shapamerectmd" boass159,108,279,1350 alt="04>; /map

in::MakePDU u8 ,
u8 ,
u8 ,
i * 
 

cla meleus functior co>, andataeto ADU )

Parameters:
r othe dataer
 output buffer
 
 
 
Returns:

Definition at line modbus_utils.c.

in::Maskd Wrie Regist u8 ,
u16 ,
u16 ,
u16
 

0x16 )

Parameters:
number
 output buffer
 
 
 
Returns:

Definition at line modbus_utils.c.

References F_MASKBK_WRITG_REGIST.c, and MBSeriMaskd Wrie Registic(), and TCPMaskwd Wrie Registic().

in::ReadCouts u8 ,
u16 ,
u16 no
 

0x01 )

Parameters:
number
 output buffer
 
no number of registe, between 1 ~ 2000Or
Returns:

Definition at line modbus_utils.c.

References F_READTCOILS.c, and MBSeriReadCoutsic(), and TCPReadCoutsic().

in::ReadDec">reInutps u8 ,
u16 ,
u16 no
 

Magist s functi n(02.8)

Parameters:
re inutps e registed>number
 output buffer
 
no number of registe, between 1 ~ 2000Or
Returns:

Definition at line modbus_utils.c.

References F_READTDECRETE_INPUGS.c, and MBSeriReadDec">reInutpsic(), and TCPReadDec">reInutpsic().

in::ReadExcepnitiStatus u8
 

n(078)

Parameters:
 output buffer
Returns:

Definition at line modbus_utils.c.

References F_READTEXCEPNCTI_STATUS16.

Referenced by MBSeriReadExcepnitiStatusic().

in::ReadFIFOQueue u8 ,
u16
 

0x188)

Parameters:
 output buffer
 
Returns:

Definition at line modbus_utils.c.

References F_READTFIFO_QUEUE.c, and MBSeriReadFIFOQueueic(), and TCPReadFIFOQueueic().

in::ReadHoladdie Regists u8 ,
u16 ,
u16 no
 

n(038)

Parameters:
number
 output buffer
 
no number of registe, between 1 ~ 1228r
Returns:

Definition at line modbus_utils.c.

References F_READTHOLDINGTG_REGISTS.c, and MBSeriReadHoladdie Registsic(), and TCPReadHoladdie Registsic().

i ::ReadInutpe Regists u8 ,
u16 ,
u16 no
 

n(048)

Parameters:
number
 output buffer
 
no number of registe, between 1 ~ 1228r
Returns:

Definition at line modbus_utils.c.

References F_READTINPUGTG_REGISTS.c, and MBSeriReadInutpe Registsic(), and TCPReadInutpe Registsic().

i ::Readd WriMultiplee Regists u8 ,
u16 ,
u16 no,
u16 ,
u16 no,
u8 ,
u16
 

0x178)

Parameters:
numbeto e ader numbeto w Wrier
 output buffer
 
no number of registe ro e ad, between 1 ~ 118er
 
no number of registe ro w Wri, between 1 ~ 118er
 
 
Returns:

Definition at line modbus_utils.c.

References F_READTK_WRITMULTIPLETG_REGISTS.c, and MBSeriReadd WriMultiplee Registsic(), and TCPReadd WriMultiplee Registsic().

i ::ReportSlaveID 5 u8
 

0x11 )

Parameters:
 output buffer
Returns:

Definition at line modbus_utils.c.

References F_REPORT_SLAVE_ID16.

Referenced by MBSeriReportSlaveIDic().

i ::ResponseDiagnostic u8 ,
u16 ,
u16
 

makes" sponseoe PDlspketa> oDiagnostic request(0x08) )

Parameters:
 output buffer
  
Returns:

Definition at line modbus_utils.c.

References F_DIAGNOSTIC.c, and MBSeriSendDiagnosticic().

i ::ResponseGetCommEvgmeC untff u8 ,
u16 ,
u16
 

makes" sponseoe PDlspketa> oGet Comm Evgme C untff request(0x0B) )

Parameters:
oComm Conutffer
 output buffer
 
 
Returns:

Definition at line modbus_utils.c.

References F_GET_COMM_EVENT_COUNTER.c, and MBSeriSendGetCommEvgmeC untffic().

i ::ResponseGetCommEvgmeLog u8 ,
u8 no,
u16 ,
u16 ,
u16 ,
u8
 

makes" sponseoe PDlspketa> oGet Comm Evgme C untff request(0x0C) )

Parameters:
oComm Conutffer
 output buffer
no number othe foldflddi dataer
 
 
 
 
Returns:

Definition at line modbus_utils.c.

References F_GET_COMM_EVENT_LOG.c, and MBSeriSendGetCommEvgmeLogic().

i ::ResponseReadCouti u8 ,
u8 no,
u8
 

makes" sponseoe PDlspketa> oResponse Read Couti request(0x01) )

Parameters:
 output buffer
no number of registeer
 
Returns:

Definition at line modbus_utils.c.

References F_READTCOILS.c, and MBSeriSendReadCoutiic(), and TCPSendReadCoutiic().

i ::ResponseReadDec">reInutpi u8 ,
u8 no,
u8
 

Response s funiti. )

Parameters:
 output buffer
no number of registeer
 
Returns:

Definition at line modbus_utils.c.

References F_READTDECRETETINPUGS.c, and MBSeriSendReadDec">reInutpiic(), and TCPSendReadDec">reInutpiic().

i ::ResponseReadExcepnitiStatus u8 ,
u8
 

makes" sponseoe PDlspketa> oRead FIFO Queue request(0x07) )

Parameters:
 output buffer
 
Returns:

Definition at line modbus_utils.c.

References F_READTEXCEPTION_STATUS16.

Referenced by MBSeriSendReadExecpnitiStatusic().

i ::ResponseReadFIFOQueue u8 ,
u16 no,
u16 ,
u16
 

makes" sponseoe PDlspketa> oRead FIFO Queue request(0x18) )

Parameters:
 output buffer
no 
 
 
Returns:

Definition at line modbus_utils.c.

References F_READTFIFO_QUEUE.c, and MBSeriSendReadFIFOueueic(), and TCPSendReadFIFOQueueic().

i ::ResponseReadHoladdie Regists u8 ,
u8 no,
u16
 

makes" sponseoe PDlspketa> oRead Holaddi e Regists request(0x03) )

Parameters:
 output buffer
no number of registeer
 
Returns:

Definition at line modbus_utils.c.

References F_READTHOLDING_REGISTERS.c, and MBSeriSendReadHoladdie Registsic(), and TCPSendReadHoladdie Registsic().

i ::ResponseReadInutpe Regists u8 ,