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

# mbtcp.c File Reference

## Detailed Description

Example modbus TCP API define file.

This file contains higher level modbus TCP utility functions.

History: Date Author Comment 08-17-2005 AceLan Kao. Create it.

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

Definition in file mbtcp.c.

`#include <string.h>`
`#include "mbtcp.h"`

Include dependency graph for mbtcp.c:

Go to the source code of this file.

## Functions
int | MBTCPBlockRead (int fd, u8 *buf, int len)
| block read from TCP socket
int | MBTCPClientClose (int fd)
int | MBTCPClientConnect (const int clientfd, const char *addr, int port)
| connect to TCP server
int | MBTCPClientInit (int *clientfd)
| initialize modbus TCP client
int | MBTCPGetMBAP (u8 *adu, pMBAPStruct mbap)
| get MBAP struct from adu
int | MBTCPGetPDU (u8 *buf, u8 *adu, int len)
| get PDU from adu
int | MBTCPMakeMBAP (pMBAPStruct mbap, u16 tid, u16 len, u8 uid)
| make MBAP struct
int | MBTCPMaskwWriteRegister (int fd, u16 reg, u16 andmask, u16 ormask, pMBAPStruct mbap)
| send Mask Write Register request and get response(0x16)
int | MBTCPNonBlockRead (int fd, u8 *buf, int len)
| non-block read from TCP socket
int | MBTCPReadCoils (int fd, u16 startcoils, u16 no, u8 *value, pMBAPStruct mbap)
| send Read Coils request and get response(0x01)
int | MBTCPReadDecreteInputs (int fd, u16 startdec, u16 no, u8 *value, pMBAPStruct mbap)
| Modbus over TCP/IP master function.
int | MBTCPReadFIFOQueue (int fd, u16 FIFOAddr, u16 *FIFOCount, u16 *FIFOValue, pMBAPStruct mbap)
| send Read FIFO Queue request and get response(0x18)
int | MBTCPReadHoldingRegisters (int fd, u16 startreg, u16 no, u16 *value, pMBAPStruct mbap)
| send Read Holding Registers request and get response(0x03)
int | MBTCPReadInputRegisters (int fd, u16 startreg, u16 no, u16 *value, pMBAPStruct mbap)
| send Read Input Registers request and get response(0x04)
int | MBTCPReadWriteMultipleRegisters (int fd, u16 rsreg, u16 rno, u16 *rv, u16 wsreg, u16 wno, u8 count, u16 *wv, pMBAPStruct mbap)
| send Read Write Multiple Registers request and get response(0x17)
int | MBTCPSendAndWaitResponse (int fd, u8 *buf, u8 *pdu, int len, pMBAPStruct mbap)
| send adu packet through TCP socket and wait response adu from slave
int | MBTCPSendReadCoils (int fd, u16 no, u8 *value, pMBAPStruct mbap)
| send Read Coils response(0x01)
int | MBTCPSendReadDecreteInputs (int fd, u16 no, u8 *value, pMBAPStruct mbap)
| Modbus over TCP/IP slave function.
int | MBTCPSendReadFIFOQueue (int fd, u16 no, u16 count, u16 *value, pMBAPStruct mbap)
| send Read FIFO Queue response(0x18)
int | MBTCPSendReadHoldingRegisters (int fd, u16 no, u16 *value, pMBAPStruct mbap)
| send Read Holding Registers response(0x03)
int | MBTCPSendReadInputRegisters (int fd, u16 no, u16 *value, pMBAPStruct mbap)
| send Read Input Registers response(0x04)
int | MBTCPSendWriteMultipleRegisters (int fd, u16 startdevreg, u16 noreg, u8 count, u16 *value, pMBAPStruct mbap)
| send Write Multiple Registers response(0x10)
int | MBTCPSendWriteSingleCoil (int fd, u16 coilreg, u16 onoff, pMBAPStruct mbap)
| send Write Single Coil response(0x05)
int | MBTCPSendWriteSingleRegister (int fd, u16 reg, u16 value, pMBAPStruct mbap)
| send Write Single Register response(0x06)
int | MBTCPServerClose (int fd)
int | MBTCPServerInit (int port, int *serverfd)
| Modbus over TCP/IP API.
int | MBTCPServerWaitConnection (const int serverfd, int *clientfd, char *client_addr)
| wait modbus master device connect
int | MBTCPWrite (int fd, u8 *pdu, int len, pMBAPStruct mbap)
| write to TCP socket
int | MBTCPWriteMBAP (char *buf, pMBAPStruct mbap)
| write MBAP struct to buffer
int | MBTCPWriteMultipleCoils (int fd, u16 startcoils, u16 no, u8 *value, pMBAPStruct mbap)
| send Write Multiple Coils request and get response(0x0F)
int | MBTCPWriteMultipleRegisters (int fd, u16 startreg, u16 noreg, u8 count, u16 *value, pMBAPStruct mbap)
| send Write Multiple Registers request and get response(0x10)
int | MBTCPWriteSingleCoil (int fd, u16 coilreg, u16 onoff, pMBAPStruct mbap)
| send Write Single Coil request and get response(0x05)
int | MBTCPWriteSingleRegister (int fd, u16 devicereg, u16 value, pMBAPStruct mbap)
| send Write Single Register request and get response(0x06)

## Function Documentation

int MBTCPBlockRead | ( | int | fd,
|  | u8 * | buf,
|  | int | len
| ) |

  | block read from TCP socket Parameters:
  | fd | socket fd
  | buf | input buffer
  | len | buffer size
Returns: the length of read data Definition at line 103 of file mbtcp.c. References TCPBlockRead(). Referenced by MBTCPSendAndWaitResponse(), and serial_slave().
    00104 {
    00105         return TCPBlockRead( fd, buf, len);
    00106 }
Here is the call graph for this function:

int MBTCPClientClose | ( | int | fd | ) |

| Definition at line 135 of file mbtcp.c.
    00136 {
    00137         close( fd);
    00138 }

int MBTCPClientConnect | ( | const int | clientfd,
|  | const char * | addr,
|  | int | port
| ) |

  | connect to TCP server Parameters:
  | clientfd | client socket fd
  | addr | server address
  | port | server port number
Returns: return zero for success, on error -1 is returned Definition at line 75 of file mbtcp.c. References TCPClientConnect(). Referenced by serial_master(), and serial_master_child().
    00076 {
    00077         return TCPClientConnect( clientfd, addr, port);
    00078 }
Here is the call graph for this function:

int MBTCPClientInit | ( | int * | clientfd | ) |

  | initialize modbus TCP client Parameters:
  | clientfd | client socket fd
Returns: return client socked fd for success, on error return error code Definition at line 61 of file mbtcp.c. References TCPClientInit(). Referenced by serial_master(), and serial_master_child().
    00062 {
    00063         return TCPClientInit( clientfd);
    00064 }
Here is the call graph for this function:

int MBTCPGetMBAP | ( | u8 * | adu,
|  | pMBAPStruct | mbap
| ) |

  | get MBAP struct from adu Parameters:
  | adu | adu data
  | mbap | output buffer
Returns: MB_OK Definition at line 148 of file mbtcp.c. References GETU16, _MBAP_Struct::Length, MB_OK, _MBAP_Struct::ProtocolIdentifier, _MBAP_Struct::TransactionIdentifier, and _MBAP_Struct::UnitIdentifier. Referenced by MBTCPSendAndWaitResponse(), serial_master_child(), and serial_slave_child().
    00149 {
    00150         GETU16( mbap->TransactionIdentifier, adu);
    00151         GETU16( mbap->ProtocolIdentifier, adu+ 2);
    00152         GETU16( mbap->Length, adu+ 4);
    00153         mbap->UnitIdentifier= adu[ 6];
    00154
    00155         return MB_OK;
    00156 }

int MBTCPGetPDU | ( | u8 * | buf,
|  | u8 * | adu,
|  | int | len
| ) |

  | get PDU from adu Parameters:
  | buf | buffer to contain PDU
  | adu | adu data
  | len | length of the adu data
Returns: length of data actually wrote Definition at line 205 of file mbtcp.c. Referenced by MBTCPMaskwWriteRegister(), MBTCPReadCoils(), MBTCPReadDecreteInputs(), MBTCPReadFIFOQueue(), MBTCPReadHoldingRegisters(), MBTCPReadInputRegisters(), MBTCPReadWriteMultipleRegisters(), MBTCPWriteMultipleCoils(), MBTCPWriteMultipleRegisters(), MBTCPWriteSingleCoil(), MBTCPWriteSingleRegister(), serial_master_child(), and serial_slave_child().
    00206 {
    00207         bcopy( (const char *)(adu+ 7), (char *)buf, len- 7);
    00208
    00209         return len- 7;
    00210 }

int MBTCPMakeMBAP | ( | pMBAPStruct | mbap,
|  | u16 | tid,
|  | u16 | len,
|  | u8 | uid
| ) |

  | make MBAP struct Parameters:
  | mbap | output buffer
  | tid | transaction identifier
  | len | lenght of the following data
  | uid | unit identifier
Returns: MB_OK Definition at line 168 of file mbtcp.c. References _MBAP_Struct::Length, MB_OK, _MBAP_Struct::ProtocolIdentifier, _MBAP_Struct::TransactionIdentifier, and _MBAP_Struct::UnitIdentifier. Referenced by serial_master_child(), and serial_slave_child().
    00169 {
    00170         mbap->TransactionIdentifier= tid;
    00171         mbap->ProtocolIdentifier= 0;
    00172         mbap->Length= len;
    00173         mbap->UnitIdentifier= uid;
    00174
    00175         return MB_OK;
    00176 }

int MBTCPMaskwWriteRegister | ( | int | fd,
|  | u16 | reg,
|  | u16 | andmask,
|  | u16 | ormask,
|  | pMBAPStruct | mbap
| ) |

  | send Mask Write Register request and get response(0x16) Parameters:
  | fd | socket fd
  | reg | register number
  | andmask | and mask
  | ormask | or mask
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 564 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBGetResponseMaskWriteRegister(), MBMaskWriteRegister(), MBTCPGetPDU(), and MBTCPSendAndWaitResponse().
    00565 {
    00566         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00567         int pdu_len= 0, adu_len= 0, ret= 0;
    00568         u16 ret_reg, ret_and, ret_or;
    00569
    00570         pdu_len= MBMaskWriteRegister( pdu, reg, andmask, ormask);       // make PDU
    00571         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00572
    00573         adu_len= MBTCPSendAndWaitResponse( fd, adu, pdu, pdu_len, mbap);
    00574
    00575         if( adu_len < 0)
    00576                 return adu_len;
    00577
    00578         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00579         ret= MBGetResponseMaskWriteRegister( pdu, &ret_reg, &ret_and, &ret_or);
    00580
    00581         if( ret < 0)
    00582                 return ret;
    00583         if( ( ret_reg != reg) || ( ret_and != andmask) || ( ret_or != ormask))
    00584                 return MB_ERROR_EXECPTION;
    00585         return MB_OK;
    00586 }
Here is the call graph for this function:

int MBTCPNonBlockRead | ( | int | fd,
|  | u8 * | buf,
|  | int | len
| ) |

  | non-block read from TCP socket Parameters:
  | fd | socket fd
  | buf | input buffer
  | len | buffer size
Returns: the length of read data Definition at line 89 of file mbtcp.c. References TCPNonBlockRead().
    00090 {
    00091         return TCPNonBlockRead( fd, buf, len);
    00092 }
Here is the call graph for this function:

int MBTCPReadCoils | ( | int | fd,
|  | u16 | startcoils,
|  | u16 | no,
|  | u8 * | value,
|  | pMBAPStruct | mbap
| ) |

  | send Read Coils request and get response(0x01) Parameters:
  | fd | socket fd
  | startcoils | starting coil register numbere
  | no | number of registers, between 1 ~ 2000
  | value | coils status slave returned
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 289 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MBGetResponseReadCoils(), MBReadCoils(), MBTCPGetPDU(), and MBTCPSendAndWaitResponse().
    00290 {
    00291         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00292         int pdu_len= 0, adu_len= 0, ret= 0;
    00293         u8 ret_no;
    00294
    00295         pdu_len= MBReadCoils( pdu, startcoils, no);             // make PDU
    00296         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00297
    00298         adu_len= MBTCPSendAndWaitResponse( fd, adu, pdu, pdu_len, mbap);
    00299
    00300         if( adu_len < 0)
    00301                 return adu_len;
    00302
    00303         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00304         ret= MBGetResponseReadCoils( pdu, &ret_no, value);
    00305         if( ret < 0)
    00306                 return ret;
    00307         return ret_no;
    00308 }
Here is the call graph for this function:

int MBTCPReadDecreteInputs | ( | int | fd,
|  | u16 | startdec,
|  | u16 | no,
|  | u8 * | value,
|  | pMBAPStruct | mbap
| ) |

  | Modbus over TCP/IP master function. Parameters:
  | fd | socket fd
  | startdec | starting decrete inputs register number
  | no | number of registers, between 1 ~ 2000
  | value | registers status slave returned
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 257 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MBGetResponseReadDecreteInputs(), MBReadDecreteInputs(), MBTCPGetPDU(), and MBTCPSendAndWaitResponse().
    00258 {
    00259         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00260         int pdu_len= 0, adu_len= 0, ret= 0;
    00261         u8 ret_no;
    00262
    00263         pdu_len= MBReadDecreteInputs( pdu, startdec, no);       // make PDU
    00264         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00265
    00266         adu_len= MBTCPSendAndWaitResponse( fd, adu, pdu, pdu_len, mbap);
    00267
    00268         if( adu_len < 0)
    00269                 return adu_len;
    00270
    00271         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00272         ret= MBGetResponseReadDecreteInputs( pdu, &ret_no, value);
    00273         if( ret < 0)
    00274                 return ret;
    00275         return ret_no;
    00276 }
Here is the call graph for this function:

int MBTCPReadFIFOQueue | ( | int | fd,
|  | u16 | FIFOAddr,
|  | u16 * | FIFOCount,
|  | u16 * | FIFOValue,
|  | pMBAPStruct | mbap
| ) |

  | send Read FIFO Queue request and get response(0x18) Parameters:
  | fd | socket fd
  | FIFOAddr | FIFO address
  | FIFOCount | FIFO value buffer size(<=31)
  | FIFOValue | values of FIFO register
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 599 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_OK, MBGetResponseReadFIFOQueue(), MBReadFIFOQueue(), MBTCPGetPDU(), and MBTCPSendAndWaitResponse().
    00600 {
    00601         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00602         int pdu_len= 0, adu_len= 0, ret= 0;
    00603
    00604         pdu_len= MBReadFIFOQueue( pdu, FIFOAddr);               // make PDU
    00605         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00606
    00607         adu_len= MBTCPSendAndWaitResponse( fd, adu, pdu, pdu_len, mbap);
    00608
    00609         if( adu_len < 0)
    00610                 return adu_len;
    00611
    00612         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00613         ret= MBGetResponseReadFIFOQueue( pdu, FIFOCount, FIFOValue);
    00614
    00615         if( ret < 0)
    00616                 return ret;
    00617
    00618         return MB_OK;
    00619 }
Here is the call graph for this function:

int MBTCPReadHoldingRegisters | ( | int | fd,
|  | u16 | startreg,
|  | u16 | no,
|  | u16 * | value,
|  | pMBAPStruct | mbap
| ) |

  | send Read Holding Registers request and get response(0x03) Parameters:
  | fd | socket fd
  | startreg | starting coil register number
  | no | number of registers, between 1 ~ 125
  | value | register status slave returned
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 425 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MBGetResponseReadHoldingRegisters(), MBReadHoldingRegisters(), MBTCPGetPDU(), and MBTCPSendAndWaitResponse().
    00426 {
    00427         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00428         int pdu_len= 0, adu_len= 0, ret= 0;
    00429         u8 ret_no;
    00430
    00431         pdu_len= MBReadHoldingRegisters( pdu, startreg, no);    // make PDU
    00432         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00433
    00434         adu_len= MBTCPSendAndWaitResponse( fd, adu, pdu, pdu_len, mbap);
    00435
    00436         if( adu_len < 0)
    00437                 return adu_len;
    00438
    00439         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00440         ret= MBGetResponseReadHoldingRegisters( pdu, &ret_no, value);
    00441         if( ret < 0)
    00442                 return ret;
    00443         return ret_no;
    00444 }
Here is the call graph for this function:

int MBTCPReadInputRegisters | ( | int | fd,
|  | u16 | startreg,
|  | u16 | no,
|  | u16 * | value,
|  | pMBAPStruct | mbap
| ) |

  | send Read Input Registers request and get response(0x04) Parameters:
  | fd | socket fd
  | startreg | starting coil register number
  | no | number of registers, between 1 ~ 125
  | value | register status slave returned
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 393 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MBGetResponseReadInputRegisters(), MBReadInputRegisters(), MBTCPGetPDU(), and MBTCPSendAndWaitResponse().
    00394 {
    00395         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00396         int pdu_len= 0, adu_len= 0, ret= 0;
    00397         u8 ret_no;
    00398
    00399         pdu_len= MBReadInputRegisters( pdu, startreg, no);      // make PDU
    00400         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00401
    00402         adu_len= MBTCPSendAndWaitResponse( fd, adu, pdu, pdu_len, mbap);
    00403
    00404         if( adu_len < 0)
    00405                 return adu_len;
    00406
    00407         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00408         ret= MBGetResponseReadInputRegisters( pdu, &ret_no, value);
    00409         if( ret < 0)
    00410                 return ret;
    00411         return ret_no;
    00412 }
Here is the call graph for this function:

int MBTCPReadWriteMultipleRegisters | ( | int | fd,
|  | u16 | rsreg,
|  | u16 | rno,
|  | u16 * | rv,
|  | u16 | wsreg,
|  | u16 | wno,
|  | u8 | count,
|  | u16 * | wv,
|  | pMBAPStruct | mbap
| ) |

  | send Read Write Multiple Registers request and get response(0x17) Parameters:
  | fd | socket fd
  | rsreg | starting register number to read
  | rno | number of registers ro read, between 1 ~ 120
  | rv | registers value slave returned
  | wsreg | starting register number to write
  | wno | number of registers ro write, between 1 ~ 120
  | count | value counter
  | wv | value to write
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 529 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBGetResponseReadWriteMultipleRegisters(), MBReadWriteMultipleRegisters(), MBTCPGetPDU(), and MBTCPSendAndWaitResponse().
    00530 {
    00531         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00532         int pdu_len= 0, adu_len= 0, ret= 0;
    00533         u8 ret_no;
    00534
    00535         pdu_len= MBReadWriteMultipleRegisters( pdu, rsreg, rno, wsreg, wno, count, wv);
    00536         mbap->Length= pdu_len+ 1;                               // plus 1 for address           // make PDU
    00537
    00538         adu_len= MBTCPSendAndWaitResponse( fd, adu, pdu, pdu_len, mbap);
    00539
    00540         if( adu_len < 0)
    00541                 return adu_len;
    00542
    00543         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00544         ret= MBGetResponseReadWriteMultipleRegisters( pdu, (u8 *)&ret_no, wv);
    00545
    00546         if( ret < 0)
    00547                 return ret;
    00548         if( ret_no != rno*2)
    00549                 return MB_ERROR_EXECPTION;
    00550         return MB_OK;
    00551 }
Here is the call graph for this function:

int MBTCPSendAndWaitResponse | ( | int | fd,
|  | u8 * | buf,
|  | u8 * | pdu,
|  | int | len,
|  | pMBAPStruct | mbap
| ) |

  | send adu packet through TCP socket and wait response adu from slave Parameters:
  | fd | TCP socket
  | buf | input buffer for receive adu from slave
  | pdu | pdu data prepare to send to slave
  | len | pdu length
  | mbap | mbap structure
Returns: MB_OK for success, on error return error code Definition at line 224 of file mbtcp.c. References MB_ERROR_EXECPTION, MB_RTU_ADU_MAX_LENGTH, MBTCPBlockRead(), MBTCPGetMBAP(), MBTCPWrite(), and _MBAP_Struct::TransactionIdentifier. Referenced by MBTCPMaskwWriteRegister(), MBTCPReadCoils(), MBTCPReadDecreteInputs(), MBTCPReadFIFOQueue(), MBTCPReadHoldingRegisters(), MBTCPReadInputRegisters(), MBTCPReadWriteMultipleRegisters(), MBTCPWriteMultipleCoils(), MBTCPWriteMultipleRegisters(), MBTCPWriteSingleCoil(), MBTCPWriteSingleRegister(), and serial_master_child().
    00225 {
    00226         int ret_len= 0;
    00227         int tid= 0;
    00228         pMBAPStruct ret_mbap= (pMBAPStruct)malloc(sizeof(MBAPStruct));
    00229
    00230         tid= mbap->TransactionIdentifier;
    00231         MBTCPWrite( fd, pdu, len, mbap);
    00232
    00233         ret_len= MBTCPBlockRead( fd, buf, MB_RTU_ADU_MAX_LENGTH);
    00234
    00235         MBTCPGetMBAP( buf, ret_mbap);
    00236
    00237         // TODO: reply for another transaction, drop it now, but may correct one day
    00238         if( ret_mbap->TransactionIdentifier != tid)
    00239                 return MB_ERROR_EXECPTION;
    00240
    00241         return ret_len;
    00242 }
Here is the call graph for this function:

int MBTCPSendReadCoils | ( | int | fd,
|  | u16 | no,
|  | u8 * | value,
|  | pMBAPStruct | mbap
| ) |

  | send Read Coils response(0x01) Parameters:
  | fd | socket fd
  | no | number of registers
  | value | registers status
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 658 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_OK, MBResponseReadCoils(), and MBTCPWrite().
    00659 {
    00660         u8 pdu[ MAX_BUFFER_SIZE];
    00661         int pdu_len= 0, adu_len= 0;
    00662
    00663         pdu_len= MBResponseReadCoils( pdu, no, value);          // make PDU
    00664         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00665
    00666         adu_len= MBTCPWrite( fd, pdu, pdu_len, mbap);
    00667
    00668         if( adu_len < 0)
    00669                 return adu_len;
    00670
    00671         return MB_OK;
    00672 }
Here is the call graph for this function:

int MBTCPSendReadDecreteInputs | ( | int | fd,
|  | u16 | no,
|  | u8 * | value,
|  | pMBAPStruct | mbap
| ) |

  | Modbus over TCP/IP slave function. Parameters:
  | fd | socket fd
  | no | number of registers
  | value | registers status
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 632 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_OK, MBResponseReadDecreteInputs(), and MBTCPWrite().
    00633 {
    00634         u8 pdu[ MAX_BUFFER_SIZE];
    00635         int pdu_len= 0, adu_len= 0;
    00636
    00637         pdu_len= MBResponseReadDecreteInputs( pdu, no, value);  // make PDU
    00638         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00639
    00640         adu_len= MBTCPWrite( fd, pdu, pdu_len, mbap);
    00641
    00642         if( adu_len < 0)
    00643                 return adu_len;
    00644
    00645         return MB_OK;
    00646 }
Here is the call graph for this function:

int MBTCPSendReadFIFOQueue | ( | int | fd,
|  | u16 | no,
|  | u16 | count,
|  | u16 * | value,
|  | pMBAPStruct | mbap
| ) |

  | send Read FIFO Queue response(0x18) Parameters:
  | fd | socket fd
  | no | number of bytes
  | count | FIFO value buffer size(<=31)
  | value | values of FIFO register
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 817 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_OK, MBResponseReadFIFOQueue(), and MBTCPWrite().
    00818 {
    00819         u8 pdu[ MAX_BUFFER_SIZE];
    00820         int pdu_len= 0, adu_len= 0;
    00821
    00822         pdu_len= MBResponseReadFIFOQueue( pdu, no, count, value);       // make PDU
    00823         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00824
    00825         adu_len= MBTCPWrite( fd, pdu, pdu_len, mbap);
    00826
    00827         if( adu_len < 0)
    00828                 return adu_len;
    00829
    00830         return MB_OK;
    00831 }
Here is the call graph for this function:

int MBTCPSendReadHoldingRegisters | ( | int | fd,
|  | u16 | no,
|  | u16 * | value,
|  | pMBAPStruct | mbap
| ) |

  | send Read Holding Registers response(0x03) Parameters:
  | fd | socket fd
  | no | number of registers
  | value | registers status
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 710 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_OK, MBResponseReadHoldingRegisters(), and MBTCPWrite().
    00711 {
    00712         u8 pdu[ MAX_BUFFER_SIZE];
    00713         int pdu_len= 0, adu_len= 0;
    00714
    00715         pdu_len= MBResponseReadHoldingRegisters( pdu, no, value);       // make PDU
    00716         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00717
    00718         adu_len= MBTCPWrite( fd, pdu, pdu_len, mbap);
    00719
    00720         if( adu_len < 0)
    00721                 return adu_len;
    00722
    00723         return MB_OK;
    00724 }
Here is the call graph for this function:

int MBTCPSendReadInputRegisters | ( | int | fd,
|  | u16 | no,
|  | u16 * | value,
|  | pMBAPStruct | mbap
| ) |

  | send Read Input Registers response(0x04) Parameters:
  | fd | socket fd
  | no | number of registers
  | value | registers status
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 684 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_OK, MBResponseReadInputRegisters(), and MBTCPWrite().
    00685 {
    00686         u8 pdu[ MAX_BUFFER_SIZE];
    00687         int pdu_len= 0, adu_len= 0;
    00688
    00689         pdu_len= MBResponseReadInputRegisters( pdu, no, value); // make PDU
    00690         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00691
    00692         adu_len= MBTCPWrite( fd, pdu, pdu_len, mbap);
    00693
    00694         if( adu_len < 0)
    00695                 return adu_len;
    00696
    00697         return MB_OK;
    00698 }
Here is the call graph for this function:

int MBTCPSendWriteMultipleRegisters | ( | int | fd,
|  | u16 | startdevreg,
|  | u16 | noreg,
|  | u8 | count,
|  | u16 * | value,
|  | pMBAPStruct | mbap
| ) |

  | send Write Multiple Registers response(0x10) Parameters:
  | fd | socket fd
  | startdevreg | starting register number
  | noreg | number of registers, between 1 ~ 120
  | count | value counter
  | value | value to write
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 738 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_OK, MBResponseWriteMultipleRegisters(), and MBTCPWrite().
    00739 {
    00740         u8 pdu[ MAX_BUFFER_SIZE];
    00741         int pdu_len= 0, adu_len= 0;
    00742
    00743         pdu_len= MBResponseWriteMultipleRegisters( pdu, startdevreg, noreg);    // make PDU
    00744         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00745
    00746         adu_len= MBTCPWrite( fd, pdu, pdu_len, mbap);
    00747
    00748         if( adu_len < 0)
    00749                 return adu_len;
    00750
    00751         return MB_OK;
    00752 }
Here is the call graph for this function:

int MBTCPSendWriteSingleCoil | ( | int | fd,
|  | u16 | coilreg,
|  | u16 | onoff,
|  | pMBAPStruct | mbap
| ) |

  | send Write Single Coil response(0x05) Parameters:
  | fd | socket fd
  | coilreg | coil register number
  | onoff | 0xFF00 for on,0x0000 for off
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 764 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_OK, MBResponseWriteSingleCoil(), and MBTCPWrite().
    00765 {
    00766         u8 pdu[ MAX_BUFFER_SIZE];
    00767         int pdu_len= 0, adu_len= 0;
    00768
    00769         pdu_len= MBResponseWriteSingleCoil( pdu, coilreg, onoff);       // make PDU
    00770         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00771
    00772         adu_len= MBTCPWrite( fd, pdu, pdu_len, mbap);
    00773
    00774         if( adu_len < 0)
    00775                 return adu_len;
    00776
    00777         return MB_OK;
    00778 }
Here is the call graph for this function:

int MBTCPSendWriteSingleRegister | ( | int | fd,
|  | u16 | reg,
|  | u16 | value,
|  | pMBAPStruct | mbap
| ) |

  | send Write Single Register response(0x06) Parameters:
  | fd | socket fd
  | reg | register number
  | value | value to write
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 790 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_OK, MBResponseWriteSingleRegister(), and MBTCPWrite().
    00791 {
    00792         u8 pdu[ MAX_BUFFER_SIZE];
    00793         int pdu_len= 0, adu_len= 0;
    00794
    00795         pdu_len= MBResponseWriteSingleRegister( pdu, reg, value);       // make PDU
    00796         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00797
    00798         adu_len= MBTCPWrite( fd, pdu, pdu_len, mbap);
    00799
    00800         if( adu_len < 0)
    00801                 return adu_len;
    00802
    00803         return MB_OK;
    00804 }
Here is the call graph for this function:

int MBTCPServerClose | ( | int | fd | ) |

| Definition at line 130 of file mbtcp.c.
    00131 {
    00132         close( fd);
    00133 }

int MBTCPServerInit | ( | int | port,
|  | int * | serverfd
| ) |

  | Modbus over TCP/IP API. Parameters:
  | port | port number
  | serverfd | server socket fd
Returns: return server socked fd for success, on error return error code Definition at line 31 of file mbtcp.c. References TCPServerInit(). Referenced by serial_slave().
    00032 {
    00033         TCPServerInit( port, serverfd);
    00034 }
Here is the call graph for this function:

int MBTCPServerWaitConnection | ( | const int | serverfd,
|  | int * | clientfd,
|  | char * | client_addr
| ) |

  | wait modbus master device connect Parameters:
  | serverfd | server socket fd
  | clientfd | client socket fd
  | client_addr | used for store client ip address
Returns: return client address for success, on error return error code Definition at line 45 of file mbtcp.c. References MB_ERROR_TCP_NOT_CONNECTED, and TCPServerWaitConnection(). Referenced by serial_slave().
    00046 {
    00047         *clientfd= TCPServerWaitConnection( serverfd, clientfd, client_addr);
    00048         if( *clientfd < 0 )
    00049                 return MB_ERROR_TCP_NOT_CONNECTED;
    00050
    00051         return *clientfd;
    00052 }
Here is the call graph for this function:

int MBTCPWrite | ( | int | fd,
|  | u8 * | pdu,
|  | int | len,
|  | pMBAPStruct | mbap
| ) |

  | write to TCP socket Parameters:
  | fd | socket fd
  | pdu | output buffer
  | len | output string length
  | mbap | MBAP struct
Returns: the length of the actual written data Definition at line 118 of file mbtcp.c. References MB_RTU_ADU_MAX_LENGTH, MBTCPWriteMBAP(), and TCPWrite(). Referenced by MBTCPSendAndWaitResponse(), MBTCPSendReadCoils(), MBTCPSendReadDecreteInputs(), MBTCPSendReadFIFOQueue(), MBTCPSendReadHoldingRegisters(), MBTCPSendReadInputRegisters(), MBTCPSendWriteMultipleRegisters(), MBTCPSendWriteSingleCoil(), MBTCPSendWriteSingleRegister(), and serial_slave_child().
    00119 {
    00120         u8 adu[ MB_RTU_ADU_MAX_LENGTH];
    00121         int adu_len= 0;
    00122
    00123         adu_len= MBTCPWriteMBAP( adu, mbap);
    00124         bcopy( (const char *)pdu, (char *)(adu+ adu_len), len);
    00125         adu_len+= len;
    00126
    00127         return TCPWrite( fd, adu, adu_len);
    00128 }
Here is the call graph for this function:

int MBTCPWriteMBAP | ( | char * | buf,
|  | pMBAPStruct | mbap
| ) |

  | write MBAP struct to buffer Parameters:
  | buf | buffer to contain MBAP
  | mbap | output buffer
Returns: length of data actually wrote Definition at line 186 of file mbtcp.c. References _MBAP_Struct::Length, _MBAP_Struct::ProtocolIdentifier, PUTU16, _MBAP_Struct::TransactionIdentifier, and _MBAP_Struct::UnitIdentifier. Referenced by MBTCPWrite().
    00187 {
    00188         PUTU16( buf, mbap->TransactionIdentifier);
    00189         PUTU16( buf+ 2, mbap->ProtocolIdentifier);
    00190         PUTU16( buf+ 4, mbap->Length);
    00191         buf[ 6]= mbap->UnitIdentifier;
    00192
    00193         return 7;                               // MBAP length
    00194 }

int MBTCPWriteMultipleCoils | ( | int | fd,
|  | u16 | startcoils,
|  | u16 | no,
|  | u8 * | value,
|  | pMBAPStruct | mbap
| ) |

  | send Write Multiple Coils request and get response(0x0F) Parameters:
  | fd | socket fd
  | startcoils | starting coil register number
  | no | number of registers, between 1 ~ 2000
  | value | coils status slave returned
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 354 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBGetResponseWriteMultipleCoils(), MBTCPGetPDU(), MBTCPSendAndWaitResponse(), and MBWriteMultipleCoils().
    00355 {
    00356         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00357         int pdu_len= 0, adu_len= 0, ret= 0;
    00358         u16 count;
    00359         u16 ret_addr, ret_value;
    00360
    00361         count= (u8)no/8;
    00362         if( count*8 < no)
    00363                 count++;
    00364
    00365         pdu_len= MBWriteMultipleCoils( pdu, startcoils, no, count, value);      // make PDU
    00366         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00367
    00368         adu_len= MBTCPSendAndWaitResponse( fd, adu, pdu, pdu_len, mbap);
    00369
    00370         if( adu_len < 0)
    00371                 return adu_len;
    00372
    00373         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00374         ret= MBGetResponseWriteMultipleCoils( pdu, &ret_addr, &ret_value);
    00375         if( ret < 0)
    00376                 return ret;
    00377         if( ( ret_addr != startcoils) || ( ret_value != no))
    00378                 return MB_ERROR_EXECPTION;
    00379         return MB_OK;
    00380 }
Here is the call graph for this function:

int MBTCPWriteMultipleRegisters | ( | int | fd,
|  | u16 | startreg,
|  | u16 | noreg,
|  | u8 | count,
|  | u16 * | value,
|  | pMBAPStruct | mbap
| ) |

  | send Write Multiple Registers request and get response(0x10) Parameters:
  | fd | socket fd
  | startreg | starting register number
  | noreg | number of registers, between 1 ~ 120
  | count | value counter
  | value | register status slave returned
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 491 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBGetResponseWriteMultipleRegisters(), MBTCPGetPDU(), MBTCPSendAndWaitResponse(), and MBWriteMultipleRegisters().
    00492 {
    00493         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00494         int pdu_len= 0, adu_len= 0, ret= 0;
    00495         u16 ret_addr, ret_value;
    00496
    00497         pdu_len= MBWriteMultipleRegisters( pdu, startreg, noreg, count, value);
    00498         mbap->Length= pdu_len+ 1;                               // plus 1 for address           // make PDU
    00499
    00500         adu_len= MBTCPSendAndWaitResponse( fd, adu, pdu, pdu_len, mbap);
    00501
    00502         if( adu_len < 0)
    00503                 return adu_len;
    00504
    00505         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00506         ret= MBGetResponseWriteMultipleRegisters( pdu, &ret_addr, &ret_value);
    00507         if( ret < 0)
    00508                 return ret;
    00509         if( ( ret_addr != startreg) || ( ret_value != noreg))
    00510                 return MB_ERROR_EXECPTION;
    00511         return MB_OK;
    00512 }
Here is the call graph for this function:

int MBTCPWriteSingleCoil | ( | int | fd,
|  | u16 | coilreg,
|  | u16 | onoff,
|  | pMBAPStruct | mbap
| ) |

  | send Write Single Coil request and get response(0x05) Parameters:
  | fd | socket fd
  | coilreg | coil register number
  | onoff | 0xFF00 for on,0x0000 for off
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 320 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBGetResponseWriteSingleCoil(), MBTCPGetPDU(), MBTCPSendAndWaitResponse(), and MBWriteSingleCoil().
    00321 {
    00322         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00323         int pdu_len= 0, adu_len= 0, ret= 0;
    00324         u16 ret_addr, ret_value;
    00325
    00326         pdu_len= MBWriteSingleCoil( pdu, coilreg, onoff);       // make PDU
    00327         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00328
    00329         adu_len= MBTCPSendAndWaitResponse( fd, adu, pdu, pdu_len, mbap);
    00330
    00331         if( adu_len < 0)
    00332                 return adu_len;
    00333
    00334         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00335         ret= MBGetResponseWriteSingleCoil( pdu, &ret_addr, &ret_value);
    00336         if( ret < 0)
    00337                 return ret;
    00338         if( ( ret_addr != coilreg) || ( ret_value != onoff))
    00339                 return MB_ERROR_EXECPTION;
    00340         return MB_OK;
    00341 }
Here is the call graph for this function:

int MBTCPWriteSingleRegister | ( | int | fd,
|  | u16 | devicereg,
|  | u16 | value,
|  | pMBAPStruct | mbap
| ) |

  | send Write Single Register request and get response(0x06) Parameters:
  | fd | socket fd
  | devicereg | register number
  | value | register status slave returned
  | mbap | mbap structure
Returns: return 0 for success, on error return error code Definition at line 456 of file mbtcp.c. References _MBAP_Struct::Length, MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBGetResponseWriteSingleRegister(), MBTCPGetPDU(), MBTCPSendAndWaitResponse(), and MBWriteSingleRegister().
    00457 {
    00458         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00459         int pdu_len= 0, adu_len= 0, ret= 0;
    00460         u16 ret_addr, ret_value;
    00461
    00462         pdu_len= MBWriteSingleRegister( pdu, devicereg, value); // make PDU
    00463         mbap->Length= pdu_len+ 1;                               // plus 1 for address
    00464
    00465         adu_len= MBTCPSendAndWaitResponse( fd, adu, pdu, pdu_len, mbap);
    00466
    00467         if( adu_len < 0)
    00468                 return adu_len;
    00469
    00470         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00471         ret= MBGetResponseWriteSingleRegister( pdu, &ret_addr, &ret_value);
    00472         if( ret < 0)
    00473                 return ret;
    00474         if( ( ret_addr != devicereg) || ( ret_value != value))
    00475                 return MB_ERROR_EXECPTION;
    00476         return MB_OK;
    00477 }
Here is the call graph for this function: