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

# mbserial.c File Reference

## Detailed Description

Example modbus serial API define file.

This file contains higher level modbus serial utility functions. It supports both RTU and ASCII protocol and converts them implicity.

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

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

Definition in file mbserial.c.

`#include "mbserial.h"`
`#include "modbus_utils.h"`

Include dependency graph for mbserial.c:

Go to the source code of this file.

## Functions
int | MBSerialBlockRead (int port, u8 *buf, int len)
| Read ADU.
int | MBSerialClose (int port)
| close the serial port
int | MBSerialDiagnostic (int port, u8 address, u16 subfunc, u16 data)
| send Diagnostic request and get response(0x08)
int | MBSerialFlowCtrl (int port, int control)
| set flow control
u8 | MBSerialGetAddress (const u8 *adu)
| get device address from ADU
u32 | MBSerialGetCharTimeout (int port)
| get char timeout
int | MBSerialGetCommEventCounter (int port, u8 address, u16 *status, u16 *eventcount)
| send Get Comm Event Counter request and get response(0x0B)
int | MBSerialGetCommEventLog (int port, u8 address, u16 *status, u16 *eventcount, u16 *messagecount, u8 *events)
| send Get Comm Event Counter request and get response(0x0C)
u32 | MBSerialGetFrameTimeout (int port)
| get frame timeout
int | MBSerialGetPDU (u8 *buf, u8 *adu, int len)
| get PDU part from ADU data
u32 | MBSerialGetTimeout (int port)
| get response timeout
void | MBSerialInit (void)
| Modbus over serial line API.
int | MBSerialMaskWriteRegister (int port, u8 address, u16 reg, u16 andmask, u16 ormask)
| send Mask Write Register request and get response(0x16)
int | MBSerialNonBlockRead (int port, u8 *buf, int len)
| non-block read data from port
int | MBSerialOpen (int port, int protocol)
| open serial port for modbus protocol
int | MBSerialReadASCII (int port, u8 *buf, int len)
| read ASCII data from port
int | MBSerialReadCoils (int port, u8 address, u16 startcoils, u16 no, u8 *value)
| send Read Coils request and get response(0x01)
int | MBSerialReadDecreteInputs (int port, u8 address, u16 startdec, u16 no, u8 *value)
| Modbus master function.
int | MBSerialReadExceptionStatus (int port, u8 address, u8 *status)
| Modbus master function over serial only.
int | MBSerialReadFIFOQueue (int port, u8 address, u16 FIFOAddr, u16 *FIFOCount, u16 *FIFOValue)
| send Read FIFO Queue request and get response(0x18)
int | MBSerialReadHoldingRegisters (int port, u8 address, u16 startreg, u16 no, u16 *value)
| send Read Holding Registers request and get response(0x03)
int | MBSerialReadInputRegisters (int port, u8 address, u16 startreg, u16 no, u16 *value)
| send Read Input Registers request and get response(0x04)
int | MBSerialReadRTU (int port, u8 *buf, int len)
| read RTU data from port
int | MBSerialReadWriteMultipleRegisters (int port, u8 address, u16 rsreg, u16 rno, u16 *rv, u16 wsreg, u16 wno, u8 count, u16 *wv)
| send Read Write Multiple Registers request and get response(0x17)
int | MBSerialReportSlaveID (int port, u8 address, u8 *slave_id, u8 *status)
| send Report Slave ID request and get response(0x11)
int | MBSerialSendAndWaitResponse (int port, u8 *buf, u8 *pdu, int len, u8 address)
| send adu packet through serial port and wait response adu from slave
int | MBSerialSendDiagnostic (int port, u8 address, u16 subfunc, u16 data)
| send Diagnostic response(0x08)
int | MBSerialSendGetCommEventCounter (int port, u8 address, u16 status, u16 eventcount)
| send Get Comm Event Counter response(0x0B)
int | MBSerialSendGetCommEventLog (int port, u8 address, u8 no, u16 status, u16 eventcount, u16 messagecount, u8 *events)
| send Get Comm Event Counter response(0x0C)
int | MBSerialSendReadCoils (int port, u8 address, u16 no, u8 *value)
| send Read Coils response(0x01)
int | MBSerialSendReadDecreteInputs (int port, u8 address, u16 no, u8 *value)
| Modbus slave function or response to master.
int | MBSerialSendReadExecptionStatus (int port, u8 address, u8 status)
| send Read Execption Status response(0x07)
int | MBSerialSendReadFIFOueue (int port, u8 address, u16 no, u16 count, u16 *value)
| send Read FIFO Queue response(0x18)
int | MBSerialSendReadHoldingRegisters (int port, u8 address, u16 no, u16 *value)
| send Read Holding Registers response(0x03)
int | MBSerialSendReadInputRegisters (int port, u8 address, u16 no, u16 *value)
| send Read Input Registers response(0x04)
int | MBSerialSendWriteMultipleRegisters (int port, u8 address, u16 startdevreg, u16 noreg)
| send Write Multiple Registers response(0x10)
int | MBSerialSendWriteSingleCoil (int port, u8 address, u16 coilreg, u16 onoff)
| send Write Single Coil response(0x05)
int | MBSerialSendWriteSingleRegister (int port, u8 address, u16 addr, u16 value)
| send Write Single Register response(0x06)
void | MBSerialSetCharTimeout (int port, u32 timeout)
| set char timeout
void | MBSerialSetFrameTimeout (int port, u32 timeout)
| set frame timeout
int | MBSerialSetMode (int port, unsigned int mode)
| set serial port mode for RS232/RS422/RS485
int | MBSerialSetParam (int port, int parity, int databits, int stopbit)
| set serial port parameter
int | MBSerialSetSpeed (int port, unsigned int speed)
| set serial speed and make changes now
void | MBSerialSetTimeout (int port, u32 timeout)
| set new response timeout
int | MBSerialWrite (int port, u8 *pdu, int len, u8 address)
| Make ADU and sent out.
int | MBSerialWriteMultipleCoils (int port, u8 address, u16 startcoils, u16 no, u8 *value)
| send Write Multiple Coils request and get response(0x0F)
int | MBSerialWriteMultipleRegisters (int port, u8 address, u16 startdevreg, u16 noreg, u8 count, u16 *value)
| send Write Multiple Registers request and get response(0x10)
int | MBSerialWriteSingleCoil (int port, u8 address, u16 coilreg, u16 onoff)
| send Write Single Coil request and get response(0x05)
int | MBSerialWriteSingleRegister (int port, u8 address, u16 devicereg, u16 value)
| send Write Single Register request and get response(0x06)
## Variables
static struct MB_SERIAL_INFO | MBSerialInfo [MAX_PORT_NUM]

## Function Documentation

int MBSerialBlockRead | ( | int | port,
|  | u8 * | buf,
|  | int | len
| ) |

  | Read ADU. Parameters:
  | port | port number
  | buf | buffer for store received data
  | len | buffer length
Returns: length of data actually read It blocks and reads data, and will return actually one frame of binary data On error return error code Definition at line 243 of file mbserial.c. References MB_ASCII_PROTOCOL, MB_ERROR_PROTOCOL, MB_RTU_PROTOCOL, MBSerialReadASCII(), MBSerialReadRTU(), and MB_SERIAL_INFO::Protocol. Referenced by serial_master_child().
    00244 {
    00245         int ret_len= 0;
    00246         if( MBSerialInfo[ port].Protocol == MB_RTU_PROTOCOL)
    00247                 ret_len= MBSerialReadRTU( port, buf, len);
    00248         else if( MBSerialInfo[ port].Protocol == MB_ASCII_PROTOCOL)
    00249                 ret_len= MBSerialReadASCII( port, buf, len);
    00250         else
    00251                 return MB_ERROR_PROTOCOL;
    00252
    00253         return ret_len;
    00254 }
Here is the call graph for this function:

int MBSerialClose | ( | int | port | ) |

  | close the serial port Parameters:
  | port | port number
Returns: MB_OK for success, on error return error code Definition at line 297 of file mbserial.c. References SerialClose().
    00298 {
    00299         // TODO : need to do more checking before close
    00300         return SerialClose( port);
    00301 }
Here is the call graph for this function:

int MBSerialDiagnostic | ( | int | port,
|  | u8 | address,
|  | u16 | subfunc,
|  | u16 | data
| ) |

  | send Diagnostic request and get response(0x08) Parameters:
  | port | which port to use
  | address | slave device address
  | subfunc | subfunction code
  | data | data to be sent to slave device
Returns: MB_OK for success, on error return error code make PDU Definition at line 912 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBDiagnostic(), MBGetPDU(), MBGetResponseDiagnostic(), and MBSerialSendAndWaitResponse().
    00913 {
    00914         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00915         int pdu_len= 0, adu_len= 0, ret= 0;
    00916         u16 ret_subfunc, ret_data;
    00917
    00919         pdu_len= MBDiagnostic( pdu, subfunc, data);
    00920
    00921         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00922
    00923         if( adu_len < 0)
    00924                 return adu_len;
    00925
    00926         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00927         ret= MBGetResponseDiagnostic( pdu, &ret_subfunc, &ret_data);
    00928
    00929         if( ret < 0)
    00930                 return ret;
    00931         if( ( ret_subfunc != subfunc) || ( ret_data != data))
    00932                 return MB_ERROR_EXECPTION;
    00933
    00934         return MB_OK;
    00935 }
Here is the call graph for this function:

int MBSerialFlowCtrl | ( | int | port,
|  | int | control
| ) |

  | set flow control Parameters:
  | port | port number
  | control | HW_FLOW_CONTROL or SW_FLOW_CONTROL
Returns: MB_OK for success, on error return error code Definition at line 311 of file mbserial.c. References SerialFlowControl().
    00312 {
    00313         return SerialFlowControl( port, control);
    00314 }
Here is the call graph for this function:

u8 MBSerialGetAddress | ( | const u8 * | adu | ) |

  | get device address from ADU Parameters:
  | adu | ADU data
Returns: none Definition at line 446 of file mbserial.c. References MBGetAddress(). Referenced by serial_master_child(), and serial_slave_child().
    00447 {
    00448         return MBGetAddress( adu);
    00449 }
Here is the call graph for this function:

u32 MBSerialGetCharTimeout | ( | int | port | ) |

  | get char timeout Parameters:
  | port | port number
Returns: char timeout value Definition at line 396 of file mbserial.c. References MB_SERIAL_INFO::CharTimeout. Referenced by MBSerialReadRTU().
    00397 {
    00398         return MBSerialInfo[ port].CharTimeout;
    00399 }

int MBSerialGetCommEventCounter | ( | int | port,
|  | u8 | address,
|  | u16 * | status,
|  | u16 * | eventcount
| ) |

  | send Get Comm Event Counter request and get response(0x0B) Parameters:
  | port | which port to use
  | address | slave device address
  | status | status of Comm Conuter
  | eventcount | event counter
Returns: MB_OK for success, on error return error code make PDU Definition at line 947 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_OK, MBGetCommEventCounter(), MBGetPDU(), MBGetResponseGetCommEventCounter(), and MBSerialSendAndWaitResponse().
    00948 {
    00949         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00950         int pdu_len= 0, adu_len= 0, ret= 0;
    00951
    00953         pdu_len= MBGetCommEventCounter( pdu);
    00954
    00955         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00956
    00957         if( adu_len < 0)
    00958                 return adu_len;
    00959
    00960         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00961         ret= MBGetResponseGetCommEventCounter( pdu,  status,  eventcount);
    00962
    00963         if( ret < 0)
    00964                 return ret;
    00965
    00966         return MB_OK;
    00967 }
Here is the call graph for this function:

int MBSerialGetCommEventLog | ( | int | port,
|  | u8 | address,
|  | u16 * | status,
|  | u16 * | eventcount,
|  | u16 * | messagecount,
|  | u8 * | events
| ) |

  | send Get Comm Event Counter request and get response(0x0C) Parameters:
  | port | which port to use
  | address | slave device address
  | status | status of Comm Conuter
  | eventcount | event counter
  | messagecount | counter for processed message
  | events | new events
Returns: MB_OK for success, on error return error code make PDU Definition at line 981 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_OK, MBGetCommEventLog(), MBGetPDU(), MBGetResponseGetCommEventLog(), and MBSerialSendAndWaitResponse().
    00982 {
    00983         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00984         int pdu_len= 0, adu_len= 0, ret= 0;
    00985
    00987         pdu_len= MBGetCommEventLog( pdu);
    00988
    00989         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00990
    00991         if( adu_len < 0)
    00992                 return adu_len;
    00993
    00994         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00995         ret= MBGetResponseGetCommEventLog( pdu, status, eventcount, messagecount, events);
    00996
    00997         if( ret < 0)
    00998                 return ret;
    00999
    01000         return MB_OK;
    01001 }
Here is the call graph for this function:

u32 MBSerialGetFrameTimeout | ( | int | port | ) |

  | get frame timeout Parameters:
  | port | port number
Returns: frame timeout value Definition at line 371 of file mbserial.c. References MB_SERIAL_INFO::FrameTimeout. Referenced by MBSerialReadRTU().
    00372 {
    00373         return MBSerialInfo[ port].FrameTimeout;
    00374 }

int MBSerialGetPDU | ( | 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 460 of file mbserial.c. References MBGetPDU(). Referenced by serial_master_child(), and serial_slave_child().
    00461 {
    00462         return MBGetPDU( buf, adu, len);
    00463 }
Here is the call graph for this function:

u32 MBSerialGetTimeout | ( | int | port | ) |

  | get response timeout Parameters:
  | port | port number
Returns: timeout value Definition at line 421 of file mbserial.c. References MB_SERIAL_INFO::Timeout. Referenced by MBSerialReadASCII(), and MBSerialReadRTU().
    00422 {
    00423         return MBSerialInfo[ port].Timeout;
    00424 }

void MBSerialInit | ( | void |  | ) |

| Modbus over serial line API. Returns: none < in seconds Definition at line 29 of file mbserial.c. References MB_SERIAL_INFO::CharTimeout, MB_SERIAL_INFO::FrameTimeout, MB_SERIAL_INFO::Protocol, and MB_SERIAL_INFO::Timeout. Referenced by main().
    00030 {
    00031         int i= 0;
    00032
    00033         for( i= 0; i< MAX_PORT_NUM; i++)
    00034         {
    00035                 MBSerialInfo[ i].Protocol= -1;
    00036                 MBSerialInfo[ i].FrameTimeout= 3000;
    00037                 MBSerialInfo[ i].CharTimeout= 100;
    00038                 MBSerialInfo[ i].Timeout= 3;
    00039         }
    00040 }

int MBSerialMaskWriteRegister | ( | int | port,
|  | u8 | address,
|  | u16 | reg,
|  | u16 | andmask,
|  | u16 | ormask
| ) |

  | send Mask Write Register request and get response(0x16) Parameters:
  | port | which port to use
  | address | slave device address
  | reg | register number
  | andmask | and mask
  | ormask | or mask
Returns: MB_OK for success, on error return error code make PDU Definition at line 813 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBGetPDU(), MBGetResponseMaskWriteRegister(), MBMaskWriteRegister(), and MBSerialSendAndWaitResponse().
    00814 {
    00815         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00816         int pdu_len= 0, adu_len= 0, ret= 0;
    00817         u16 ret_reg, ret_and, ret_or;
    00818
    00820         pdu_len= MBMaskWriteRegister( pdu, reg, andmask, ormask);
    00821
    00822         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00823
    00824         if( adu_len < 0)
    00825                 return adu_len;
    00826
    00827         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00828         ret= MBGetResponseMaskWriteRegister( pdu, &ret_reg, &ret_and, &ret_or);
    00829
    00830         if( ret < 0)
    00831                 return ret;
    00832         if( ( ret_reg != reg) || ( ret_and != andmask) || ( ret_or != ormask))
    00833                 return MB_ERROR_EXECPTION;
    00834         return MB_OK;
    00835 }
Here is the call graph for this function:

int MBSerialNonBlockRead | ( | int | port,
|  | u8 * | buf,
|  | int | len
| ) |

  | non-block read data from port Parameters:
  | port | port number
  | buf | buffer for store received data
  | len | buffer length
Returns: length of data actually read Definition at line 265 of file mbserial.c. References SerialNonBlockRead().
    00266 {
    00267         return SerialNonBlockRead( port, (char *)buf, len);
    00268 }
Here is the call graph for this function:

int MBSerialOpen | ( | int | port,
|  | int | protocol
| ) |

  | open serial port for modbus protocol Parameters:
  | port | port number
  | protocol | which protocol this port use
Returns: MB_OK for success, on error return error code < clear struct for new port settings < inter-character timer unused < blocking read until 1 character arrives Definition at line 50 of file mbserial.c. References MB_ASCII_PROTOCOL, MB_ERROR_OPEN, MB_ERROR_PROTOCOL, MB_OK, MB_RTU_PROTOCOL, MB_TCP_PROTOCOL, MB_SERIAL_INFO::Protocol, and SerialOpen(). Referenced by serial_master(), and serial_slave().
    00051 {
    00052         int RetValue;
    00053         struct termios tio;
    00054         if( ( protocol != MB_RTU_PROTOCOL)
    00055                 && ( protocol != MB_ASCII_PROTOCOL)
    00056                 && ( protocol != MB_TCP_PROTOCOL))
    00057                 return ( MB_ERROR_PROTOCOL);
    00058
    00059         bzero(&tio, sizeof(tio));
    00060
    00061         tio.c_iflag = 0;
    00062         tio.c_oflag = 0;
    00063         tio.c_cflag = B9600|CS8|CREAD|CLOCAL;
    00064         tio.c_lflag = 0;
    00065
    00066         tio.c_cc[ VTIME] = 0;
    00067         tio.c_cc[ VMIN] = 1;
    00068
    00069         RetValue= SerialOpen( port, O_RDWR|O_NOCTTY, &tio);
    00070         if( RetValue < 0)
    00071                 return MB_ERROR_OPEN;
    00072
    00073         MBSerialInfo[ port].Protocol= protocol;
    00074
    00075         return MB_OK;
    00076 }
Here is the call graph for this function:

int MBSerialReadASCII | ( | int | port,
|  | u8 * | buf,
|  | int | len
| ) |

  | read ASCII data from port Parameters:
  | port | port number
  | buf | buffer for store received data
  | len | buffer length
Returns: length of data actually read It will block to read data, and will return actually one frame of binary data It will verify the data, if format error, it return MB_ERROR_FORMAT < ascii char timeout Serial timeout Definition at line 158 of file mbserial.c. References FindFD(), GETCLOCKSECOND, LRC(), MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_FORMAT, MBASCIIToData(), MBSerialGetTimeout(), SerialBlockRead(), and SerialDataInInputQueue(). Referenced by MBSerialBlockRead(), and MBSerialSendAndWaitResponse().
    00159 {
    00160         u8 tmp[ MB_ASCII_ADU_MAX_LENGTH];
    00161         int tmp_len= 0, ret_len= 0, end_flag= 0;
    00162         u32 starttime, endtime;
    00163         u8 ch;
    00164         u8 lrc;
    00165
    00166         struct timeval tv;
    00167         int readsocks, len1= 0, len2= 0;
    00168         fd_set fdset;
    00169
    00170         tv.tv_sec = 1;
    00171         tv.tv_usec = 0;
    00172
    00174         GETCLOCKSECOND( starttime);
    00175         while( 1)
    00176         {
    00177                 if( SerialDataInInputQueue( port) > 2)
    00178                         break;
    00179                 GETCLOCKSECOND( endtime);
    00180                 if( (endtime-starttime) > MBSerialGetTimeout( port))
    00181                         return 0;
    00182         }
    00183
    00184         do
    00185         {
    00186                 SerialBlockRead( port, (char *)&ch, 1);
    00187         }while( ch != ':');
    00188
    00189         FD_ZERO(&fdset);
    00190         FD_SET( FindFD(port), &fdset);
    00191         while( 1)
    00192         {
    00193                 readsocks = select( FindFD(port)+ 1, &fdset, NULL, NULL, &tv);
    00194                 if(readsocks == 0)                      // char timeout
    00195                         break;
    00196
    00197                 if( SerialBlockRead( port, (char *)&ch, 1))
    00198                 {
    00199                         if( ch == ':')                  // a new frame start here!!
    00200                         {                               // normally, we'll drop a frame
    00201                                 tmp_len= 0;             // but it's better to read it in
    00202                         }
    00203                         else if( ch == 0x0d)            // prepare to stop
    00204                         {
    00205                                 if( end_flag == 0)
    00206                                         end_flag= 1;
    00207                                 else                    // receive two CR char
    00208                                         return MB_ERROR_FORMAT;
    00209                         }
    00210                         else if( ch == 0x0a)
    00211                         {
    00212                                 if( end_flag == 1)      // actually one frame receive
    00213                                         break;
    00214                                 else
    00215                                         return MB_ERROR_FORMAT;
    00216                         }
    00217
    00218                         tmp[ tmp_len++]= ch;
    00219                 }
    00220         }
    00221         ret_len= MBASCIIToData( buf, tmp, tmp_len);
    00222
    00223         // check LRC
    00224         lrc= LRC( buf, ret_len- 1);
    00225         if( lrc != buf[ ret_len- 1])
    00226                 return MB_ERROR_FORMAT;
    00227
    00228         return ret_len;
    00229 }
Here is the call graph for this function:

int MBSerialReadCoils | ( | int | port,
|  | u8 | address,
|  | u16 | startcoils,
|  | u16 | no,
|  | u8 * | value
| ) |

  | send Read Coils request and get response(0x01) Parameters:
  | port | which port to use
  | address | slave device address
  | startcoils | starting coil register numbere
  | no | number of registers, between 1 ~ 2000
  | value | coils status slave returned
Returns: number of status bytes, on error return error code < make PDU Definition at line 540 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MBGetPDU(), MBGetResponseReadCoils(), MBReadCoils(), and MBSerialSendAndWaitResponse().
    00541 {
    00542         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00543         int pdu_len= 0, adu_len= 0, ret= 0;
    00544         u8 ret_no;
    00545
    00546         pdu_len= MBReadCoils( pdu, startcoils, no);
    00547
    00548         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00549
    00550         if( adu_len < 0)
    00551                 return adu_len;
    00552
    00553         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00554         ret= MBGetResponseReadCoils( pdu, &ret_no, value);
    00555         if( ret < 0)
    00556                 return ret;
    00557         return ret_no;
    00558 }
Here is the call graph for this function:

int MBSerialReadDecreteInputs | ( | int | port,
|  | u8 | address,
|  | u16 | startdec,
|  | u16 | no,
|  | u8 * | value
| ) |

  | Modbus master function. Parameters:
  | port | which port to use
  | address | slave device address
  | startdec | starting decrete inputs register number
  | no | number of registers, between 1 ~ 2000
  | value | registers status slave returned
Returns: number of status bytes, on error return error code < make PDU Definition at line 509 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MBGetPDU(), MBGetResponseReadDecreteInputs(), MBReadDecreteInputs(), and MBSerialSendAndWaitResponse().
    00510 {
    00511         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00512         int pdu_len= 0, adu_len= 0, ret= 0;
    00513         u8 ret_no;
    00514
    00515         pdu_len= MBReadDecreteInputs( pdu, startdec, no);
    00516
    00517         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00518
    00519         if( adu_len < 0)
    00520                 return adu_len;
    00521
    00522         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00523         ret= MBGetResponseReadDecreteInputs( pdu, &ret_no, value);
    00524         if( ret < 0)
    00525                 return ret;
    00526         return ret_no;
    00527 }
Here is the call graph for this function:

int MBSerialReadExceptionStatus | ( | int | port,
|  | u8 | address,
|  | u8 * | status
| ) |

  | Modbus master function over serial only. Parameters:
  | port | which port to use
  | address | slave device address
  | status | eight exception status in one byte
Returns: MB_OK for success, on error return error code make PDU Definition at line 880 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_OK, MBGetPDU(), MBGetResponseReadExceptionStatus(), MBReadExceptionStatus(), and MBSerialSendAndWaitResponse().
    00881 {
    00882         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00883         int pdu_len= 0, adu_len= 0, ret= 0;
    00884
    00886         pdu_len= MBReadExceptionStatus( pdu);
    00887
    00888         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00889
    00890         if( adu_len < 0)
    00891                 return adu_len;
    00892
    00893         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00894         ret= MBGetResponseReadExceptionStatus( pdu, status);
    00895
    00896         if( ret < 0)
    00897                 return ret;
    00898
    00899         return MB_OK;
    00900 }
Here is the call graph for this function:

int MBSerialReadFIFOQueue | ( | int | port,
|  | u8 | address,
|  | u16 | FIFOAddr,
|  | u16 * | FIFOCount,
|  | u16 * | FIFOValue
| ) |

  | send Read FIFO Queue request and get response(0x18) Parameters:
  | port | which port to use
  | address | slave device address
  | FIFOAddr | FIFO address
  | FIFOCount | FIFO value buffer size(<=31)
  | FIFOValue | values of FIFO register
Returns: MB_OK for success, on error return error code make PDU Definition at line 848 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_OK, MBGetPDU(), MBGetResponseReadFIFOQueue(), MBReadFIFOQueue(), and MBSerialSendAndWaitResponse().
    00849 {
    00850         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00851         int pdu_len= 0, adu_len= 0, ret= 0;
    00852
    00854         pdu_len= MBReadFIFOQueue( pdu, FIFOAddr);
    00855
    00856         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00857
    00858         if( adu_len < 0)
    00859                 return adu_len;
    00860
    00861         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00862         ret= MBGetResponseReadFIFOQueue( pdu, FIFOCount, FIFOValue);
    00863
    00864         if( ret < 0)
    00865                 return ret;
    00866
    00867         return MB_OK;
    00868 }
Here is the call graph for this function:

int MBSerialReadHoldingRegisters | ( | int | port,
|  | u8 | address,
|  | u16 | startreg,
|  | u16 | no,
|  | u16 * | value
| ) |

  | send Read Holding Registers request and get response(0x03) Parameters:
  | port | which port to use
  | address | slave device address
  | startreg | starting coil register number
  | no | number of registers, between 1 ~ 125
  | value | register status slave returned
Returns: number of registers for success, on error return error code make PDU Definition at line 674 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MBGetPDU(), MBGetResponseReadHoldingRegisters(), MBReadHoldingRegisters(), and MBSerialSendAndWaitResponse().
    00675 {
    00676         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00677         int pdu_len= 0, adu_len= 0, ret= 0;
    00678         u8 ret_no;
    00679
    00681         pdu_len= MBReadHoldingRegisters( pdu, startreg, no);
    00682
    00683         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00684
    00685         if( adu_len < 0)
    00686                 return adu_len;
    00687
    00688         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00689         ret= MBGetResponseReadHoldingRegisters( pdu, &ret_no, value);
    00690         if( ret < 0)
    00691                 return ret;
    00692         return ret_no;
    00693 }
Here is the call graph for this function:

int MBSerialReadInputRegisters | ( | int | port,
|  | u8 | address,
|  | u16 | startreg,
|  | u16 | no,
|  | u16 * | value
| ) |

  | send Read Input Registers request and get response(0x04) Parameters:
  | port | which port to use
  | address | slave device address
  | startreg | starting coil register number
  | no | number of registers, between 1 ~ 125
  | value | register status slave returned
Returns: number of registers for success, on error return error code make PDU Definition at line 642 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MBGetPDU(), MBGetResponseReadInputRegisters(), MBReadInputRegisters(), and MBSerialSendAndWaitResponse().
    00643 {
    00644         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00645         int pdu_len= 0, adu_len= 0, ret= 0;
    00646         u8 ret_no;
    00647
    00649         pdu_len= MBReadInputRegisters( pdu, startreg, no);
    00650
    00651         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00652
    00653         if( adu_len < 0)
    00654                 return adu_len;
    00655
    00656         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00657         ret= MBGetResponseReadInputRegisters( pdu, &ret_no, value);
    00658         if( ret < 0)
    00659                 return ret;
    00660         return ret_no;
    00661 }
Here is the call graph for this function:

int MBSerialReadRTU | ( | int | port,
|  | u8 * | buf,
|  | int | len
| ) |

  | read RTU data from port Parameters:
  | port | port number
  | buf | buffer for store received data
  | len | buffer length
Returns: length of data actually read It will block to read data, and will return actually one frame of binary data It will verify the data, if format error, it return MB_ERROR_FORMAT < char timeout Serial timeout < for frame timeout < char timeout < frame timeout < number loop back Definition at line 90 of file mbserial.c. References CRC16(), FindFD(), GETCLOCKMS, GETCLOCKSECOND, GETU16, MB_ERROR_FORMAT, MBSerialGetCharTimeout(), MBSerialGetFrameTimeout(), MBSerialGetTimeout(), SerialBlockRead(), SerialDataInInputQueue(), and U32_MAX_VALUE. Referenced by MBSerialBlockRead(), and MBSerialSendAndWaitResponse().
    00091 {
    00092         u32 starttime, endtime;
    00093         struct timeval tv;
    00094         int readsocks, len1= 0, len2= 0;
    00095         fd_set fdset;
    00096         u16 crc, org_crc;
    00097
    00098         tv.tv_sec = 0;
    00099         tv.tv_usec = MBSerialGetCharTimeout( port)* 1000;
    00100
    00102         GETCLOCKSECOND( starttime);
    00103         while( 1)
    00104         {
    00105                 if( SerialDataInInputQueue( port) > 2)
    00106                         break;
    00107                 GETCLOCKSECOND( endtime);
    00108                 if( (endtime-starttime) > MBSerialGetTimeout( port))
    00109                         return 0;
    00110         }
    00111
    00112         len1= SerialBlockRead( port, (char *)buf, len);
    00113         GETCLOCKMS( starttime);
    00114
    00115         FD_ZERO(&fdset);
    00116         FD_SET( FindFD(port), &fdset);
    00117         while( 1)
    00118         {
    00119                 readsocks = select( FindFD(port)+ 1, &fdset, NULL, NULL, &tv);
    00120                 if(readsocks == 0)
    00121                         break;
    00122
    00123                 len2= SerialBlockRead( port, (char *)buf+ len1, len- len1);
    00124                 len1+= len2;
    00125
    00126                 GETCLOCKMS( endtime);
    00127                 if( endtime > starttime)
    00128                 {
    00129                         if( endtime-starttime > MBSerialGetFrameTimeout( port))
    00130                                 break;
    00131                 }
    00132                 else
    00133                         if( ( U32_MAX_VALUE- starttime) + endtime > MBSerialGetFrameTimeout( port))
    00134                                 break;
    00135         }
    00136
    00137         // Check CRC
    00138         crc= CRC16( buf, len1- 2);
    00139         GETU16( org_crc, buf+ len1- 2);
    00140         if( crc != org_crc)
    00141                 return MB_ERROR_FORMAT;
    00142
    00143         return len1;
    00144 }
Here is the call graph for this function:

int MBSerialReadWriteMultipleRegisters | ( | int | port,
|  | u8 | address,
|  | u16 | rsreg,
|  | u16 | rno,
|  | u16 * | rv,
|  | u16 | wsreg,
|  | u16 | wno,
|  | u8 | count,
|  | u16 * | wv
| ) |

  | send Read Write Multiple Registers request and get response(0x17) Parameters:
  | port | which port to use
  | address | slave device address
  | 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
Returns: MB_OK for success, on error return error code make PDU Definition at line 778 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBGetPDU(), MBGetResponseReadWriteMultipleRegisters(), MBReadWriteMultipleRegisters(), and MBSerialSendAndWaitResponse().
    00779 {
    00780         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00781         int pdu_len= 0, adu_len= 0, ret= 0;
    00782         u8 ret_no;
    00783
    00785         pdu_len= MBReadWriteMultipleRegisters( pdu, rsreg, rno, wsreg, wno, count, wv);         // make PDU
    00786
    00787         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00788
    00789         if( adu_len < 0)
    00790                 return adu_len;
    00791
    00792         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00793         ret= MBGetResponseReadWriteMultipleRegisters( pdu, (u8 *)&ret_no, wv);
    00794
    00795         if( ret < 0)
    00796                 return ret;
    00797         if( ret_no != rno*2)
    00798                 return MB_ERROR_EXECPTION;
    00799         return MB_OK;
    00800 }
Here is the call graph for this function:

int MBSerialReportSlaveID | ( | int | port,
|  | u8 | address,
|  | u8 * | slave_id,
|  | u8 * | status
| ) |

  | send Report Slave ID request and get response(0x11) Parameters:
  | port | which port to use
  | address | slave device address
  | slave_id | buffer to store slave id
  | status | status of the device
Returns: MB_OK for success, on error return error code make PDU Definition at line 1013 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_OK, MBGetPDU(), MBGetResponseReportSlaveID(), MBReportSlaveID(), and MBSerialSendAndWaitResponse().
    01014 {
    01015         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    01016         int pdu_len= 0, adu_len= 0, ret= 0;
    01017
    01019         pdu_len= MBReportSlaveID( pdu);
    01020
    01021         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    01022
    01023         if( adu_len < 0)
    01024                 return adu_len;
    01025
    01026         pdu_len= MBGetPDU( pdu, adu, adu_len);
    01027         ret= MBGetResponseReportSlaveID( pdu, slave_id, status);
    01028
    01029         if( ret < 0)
    01030                 return ret;
    01031
    01032         return MB_OK;
    01033 }
Here is the call graph for this function:

int MBSerialSendAndWaitResponse | ( | int | port,
|  | u8 * | buf,
|  | u8 * | pdu,
|  | int | len,
|  | u8 | address
| ) |

  | send adu packet through serial port and wait response adu from slave Parameters:
  | port | port number
  | buf | input buffer for receive adu from slave
  | pdu | pdu data prepare to send to slave
  | len | pdu length
  | address | slave id
Returns: MB_OK for success, on error return error code Definition at line 343 of file mbserial.c. References MB_ASCII_ADU_MAX_LENGTH, MB_ASCII_PROTOCOL, MB_ERROR_PROTOCOL, MB_RTU_ADU_MAX_LENGTH, MB_RTU_PROTOCOL, MBSerialReadASCII(), MBSerialReadRTU(), MBSerialWrite(), and MB_SERIAL_INFO::Protocol. Referenced by MBSerialDiagnostic(), MBSerialGetCommEventCounter(), MBSerialGetCommEventLog(), MBSerialMaskWriteRegister(), MBSerialReadCoils(), MBSerialReadDecreteInputs(), MBSerialReadExceptionStatus(), MBSerialReadFIFOQueue(), MBSerialReadHoldingRegisters(), MBSerialReadInputRegisters(), MBSerialReadWriteMultipleRegisters(), MBSerialReportSlaveID(), MBSerialWriteMultipleCoils(), MBSerialWriteMultipleRegisters(), MBSerialWriteSingleCoil(), MBSerialWriteSingleRegister(), and serial_slave_child().
    00344 {
    00345         int ret_len= 0;
    00346
    00347         MBSerialWrite( port, pdu, len, address);
    00348
    00349         if( MBSerialInfo[ port].Protocol == MB_ASCII_PROTOCOL)
    00350                 ret_len= MBSerialReadASCII( port, buf, MB_ASCII_ADU_MAX_LENGTH);
    00351         else if( MBSerialInfo[ port].Protocol == MB_RTU_PROTOCOL)
    00352                 ret_len= MBSerialReadRTU( port, buf, MB_RTU_ADU_MAX_LENGTH);
    00353         else
    00354                 return MB_ERROR_PROTOCOL;
    00355
    00356         // Multiple slave devices connect to the same serial port
    00357         // TODO: make the response data useable
    00358         //if( MBGetAddress( buf) != address)
    00359         //      return MB_ERROR_EXECPTION;
    00360
    00361         return ret_len;
    00362 }
Here is the call graph for this function:

int MBSerialSendDiagnostic | ( | int | port,
|  | u8 | address,
|  | u16 | subfunc,
|  | u16 | data
| ) |

  | send Diagnostic response(0x08) Parameters:
  | port | which port to use
  | address | slave device address
  | subfunc | subfunction code
  | data | response data of request
Returns: MB_OK for success, on error return error code make PDU Definition at line 1280 of file mbserial.c. References MAX_BUFFER_SIZE, MB_OK, MBResponseDiagnostic(), and MBSerialWrite().
    01281 {
    01282         u8 pdu[ MAX_BUFFER_SIZE];
    01283         int pdu_len= 0, adu_len= 0;
    01284
    01286         pdu_len= MBResponseDiagnostic( pdu, subfunc, data);
    01287
    01288         adu_len= MBSerialWrite( port, pdu, pdu_len, address);
    01289
    01290         if( adu_len < 0)
    01291                 return adu_len;
    01292
    01293         return MB_OK;
    01294 }
Here is the call graph for this function:

int MBSerialSendGetCommEventCounter | ( | int | port,
|  | u8 | address,
|  | u16 | status,
|  | u16 | eventcount
| ) |

  | send Get Comm Event Counter response(0x0B) Parameters:
  | port | which port to use
  | address | slave device address
  | status | status of Comm Conuter
  | eventcount | event counter
Returns: MB_OK for success, on error return error code make PDU Definition at line 1306 of file mbserial.c. References MAX_BUFFER_SIZE, MB_OK, MBResponseGetCommEventCounter(), and MBSerialWrite().
    01307 {
    01308         u8 pdu[ MAX_BUFFER_SIZE];
    01309         int pdu_len= 0, adu_len= 0;
    01310
    01312         pdu_len= MBResponseGetCommEventCounter( pdu, status, eventcount);
    01313
    01314         adu_len= MBSerialWrite( port, pdu, pdu_len, address);
    01315
    01316         if( adu_len < 0)
    01317                 return adu_len;
    01318
    01319         return MB_OK;
    01320 }
Here is the call graph for this function:

int MBSerialSendGetCommEventLog | ( | int | port,
|  | u8 | address,
|  | u8 | no,
|  | u16 | status,
|  | u16 | eventcount,
|  | u16 | messagecount,
|  | u8 * | events
| ) |

  | send Get Comm Event Counter response(0x0C) Parameters:
  | port | which port to use
  | address | slave device address
  | no | number of the following data
  | status | status of Comm Conuter
  | eventcount | event counter
  | messagecount | counter for processed message
  | events | new events
Returns: MB_OK for success, on error return error code make PDU Definition at line 1335 of file mbserial.c. References MAX_BUFFER_SIZE, MB_OK, MBResponseGetCommEventLog(), and MBSerialWrite().
    01336 {
    01337         u8 pdu[ MAX_BUFFER_SIZE];
    01338         int pdu_len= 0, adu_len= 0;
    01339
    01341         pdu_len= MBResponseGetCommEventLog( pdu, no, status, eventcount, messagecount, events);
    01342
    01343         adu_len= MBSerialWrite( port, pdu, pdu_len, address);
    01344
    01345         if( adu_len < 0)
    01346                 return adu_len;
    01347
    01348         return MB_OK;
    01349 }
Here is the call graph for this function:

int MBSerialSendReadCoils | ( | int | port,
|  | u8 | address,
|  | u16 | no,
|  | u8 * | value
| ) |

  | send Read Coils response(0x01) Parameters:
  | port | which port to use
  | address | slave device address
  | no | number of registers
  | value | registers status
Returns: MB_OK for success, on error return error code make PDU Definition at line 1072 of file mbserial.c. References MAX_BUFFER_SIZE, MB_OK, MBResponseReadCoils(), and MBSerialWrite().
    01073 {
    01074         u8 pdu[ MAX_BUFFER_SIZE];
    01075         int pdu_len= 0, adu_len= 0;
    01076
    01078         pdu_len= MBResponseReadCoils( pdu, no, value);
    01079
    01080         adu_len= MBSerialWrite( port, pdu, pdu_len, address);
    01081
    01082         if( adu_len < 0)
    01083                 return adu_len;
    01084
    01085         return MB_OK;
    01086 }
Here is the call graph for this function:

int MBSerialSendReadDecreteInputs | ( | int | port,
|  | u8 | address,
|  | u16 | no,
|  | u8 * | value
| ) |

  | Modbus slave function or response to master. Parameters:
  | port | which port to use
  | address | slave device address
  | no | number of registers
  | value | registers status
Returns: MB_OK for success, on error return error code make PDU Definition at line 1046 of file mbserial.c. References MAX_BUFFER_SIZE, MB_OK, MBResponseReadDecreteInputs(), and MBSerialWrite().
    01047 {
    01048         u8 pdu[ MAX_BUFFER_SIZE];
    01049         int pdu_len= 0, adu_len= 0;
    01050
    01052         pdu_len= MBResponseReadDecreteInputs( pdu, no, value);
    01053
    01054         adu_len= MBSerialWrite( port, pdu, pdu_len, address);
    01055
    01056         if( adu_len < 0)
    01057                 return adu_len;
    01058
    01059         return MB_OK;
    01060 }
Here is the call graph for this function:

int MBSerialSendReadExecptionStatus | ( | int | port,
|  | u8 | address,
|  | u8 | status
| ) |

  | send Read Execption Status response(0x07) Parameters:
  | port | which port to use
  | address | slave device address
  | status | eight exception status in one byte
Returns: MB_OK for success, on error return error code make PDU Definition at line 1254 of file mbserial.c. References MAX_BUFFER_SIZE, MB_OK, MBResponseReadExceptionStatus(), and MBSerialWrite().
    01255 {
    01256         u8 pdu[ MAX_BUFFER_SIZE];
    01257         int pdu_len= 0, adu_len= 0;
    01258
    01260         pdu_len= MBResponseReadExceptionStatus( pdu, status);
    01261
    01262         adu_len= MBSerialWrite( port, pdu, pdu_len, address);
    01263
    01264         if( adu_len < 0)
    01265                 return adu_len;
    01266
    01267         return MB_OK;
    01268 }
Here is the call graph for this function:

int MBSerialSendReadFIFOueue | ( | int | port,
|  | u8 | address,
|  | u16 | no,
|  | u16 | count,
|  | u16 * | value
| ) |

  | send Read FIFO Queue response(0x18) Parameters:
  | port | which port to use
  | address | slave device address
  | no | bytes of the following data
  | count | FIFO value buffer size(<=31)
  | value | values of FIFO register
Returns: MB_OK for success, on error return error code make PDU Definition at line 1229 of file mbserial.c. References MAX_BUFFER_SIZE, MB_OK, MBResponseReadFIFOQueue(), and MBSerialWrite().
    01230 {
    01231         u8 pdu[ MAX_BUFFER_SIZE];
    01232         int pdu_len= 0, adu_len= 0;
    01233
    01235         pdu_len= MBResponseReadFIFOQueue( pdu, no, count, value);
    01236
    01237         adu_len= MBSerialWrite( port, pdu, pdu_len, address);
    01238
    01239         if( adu_len < 0)
    01240                 return adu_len;
    01241
    01242         return MB_OK;
    01243 }
Here is the call graph for this function:

int MBSerialSendReadHoldingRegisters | ( | int | port,
|  | u8 | address,
|  | u16 | no,
|  | u16 * | value
| ) |

  | send Read Holding Registers response(0x03) Parameters:
  | port | which port to use
  | address | slave device address
  | no | number of registers
  | value | registers status
Returns: MB_OK for success, on error return error code make PDU Definition at line 1124 of file mbserial.c. References MAX_BUFFER_SIZE, MB_OK, MBResponseReadHoldingRegisters(), and MBSerialWrite().
    01125 {
    01126         u8 pdu[ MAX_BUFFER_SIZE];
    01127         int pdu_len= 0, adu_len= 0;
    01128
    01130         pdu_len= MBResponseReadHoldingRegisters( pdu, no, value);
    01131
    01132         adu_len= MBSerialWrite( port, pdu, pdu_len, address);
    01133
    01134         if( adu_len < 0)
    01135                 return adu_len;
    01136
    01137         return MB_OK;
    01138 }
Here is the call graph for this function:

int MBSerialSendReadInputRegisters | ( | int | port,
|  | u8 | address,
|  | u16 | no,
|  | u16 * | value
| ) |

  | send Read Input Registers response(0x04) Parameters:
  | port | which port to use
  | address | slave device address
  | no | number of registers
  | value | registers status
Returns: MB_OK for success, on error return error code make PDU Definition at line 1098 of file mbserial.c. References MAX_BUFFER_SIZE, MB_OK, MBResponseReadInputRegisters(), and MBSerialWrite().
    01099 {
    01100         u8 pdu[ MAX_BUFFER_SIZE];
    01101         int pdu_len= 0, adu_len= 0;
    01102
    01104         pdu_len= MBResponseReadInputRegisters( pdu, no, value);
    01105
    01106         adu_len= MBSerialWrite( port, pdu, pdu_len, address);
    01107
    01108         if( adu_len < 0)
    01109                 return adu_len;
    01110
    01111         return MB_OK;
    01112 }
Here is the call graph for this function:

int MBSerialSendWriteMultipleRegisters | ( | int | port,
|  | u8 | address,
|  | u16 | startdevreg,
|  | u16 | noreg
| ) |

  | send Write Multiple Registers response(0x10) Parameters:
  | port | which port to use
  | address | slave device address
  | startdevreg | starting register address
  | noreg | number of registers, between 1 ~ 120
Returns: MB_OK for success, on error return error code make PDU Definition at line 1150 of file mbserial.c. References MAX_BUFFER_SIZE, MB_OK, MBResponseWriteMultipleRegisters(), and MBSerialWrite().
    01151 {
    01152         u8 pdu[ MAX_BUFFER_SIZE];
    01153         int pdu_len= 0, adu_len= 0;
    01154
    01156         pdu_len= MBResponseWriteMultipleRegisters( pdu, startdevreg, noreg);
    01157
    01158         adu_len= MBSerialWrite( port, pdu, pdu_len, address);
    01159
    01160         if( adu_len < 0)
    01161                 return adu_len;
    01162
    01163         return MB_OK;
    01164 }
Here is the call graph for this function:

int MBSerialSendWriteSingleCoil | ( | int | port,
|  | u8 | address,
|  | u16 | coilreg,
|  | u16 | onoff
| ) |

  | send Write Single Coil response(0x05) Parameters:
  | port | which port to use
  | address | slave device address
  | coilreg | coil register address
  | onoff | 0xFF00 for on,0x0000 for off
Returns: MB_OK for success, on error return error code make PDU Definition at line 1176 of file mbserial.c. References MAX_BUFFER_SIZE, MB_OK, MBResponseWriteSingleCoil(), and MBSerialWrite().
    01177 {
    01178         u8 pdu[ MAX_BUFFER_SIZE];
    01179         int pdu_len= 0, adu_len= 0;
    01180
    01182         pdu_len= MBResponseWriteSingleCoil( pdu, coilreg, onoff);
    01183
    01184         adu_len= MBSerialWrite( port, pdu, pdu_len, address);
    01185
    01186         if( adu_len < 0)
    01187                 return adu_len;
    01188
    01189         return MB_OK;
    01190 }
Here is the call graph for this function:

int MBSerialSendWriteSingleRegister | ( | int | port,
|  | u8 | address,
|  | u16 | addr,
|  | u16 | value
| ) |

  | send Write Single Register response(0x06) Parameters:
  | port | which port to use
  | address | slave device address
  | addr | register address
  | value | value to write
Returns: MB_OK for success, on error return error code make PDU Definition at line 1202 of file mbserial.c. References MAX_BUFFER_SIZE, MB_OK, MBResponseWriteSingleRegister(), and MBSerialWrite().
    01203 {
    01204         u8 pdu[ MAX_BUFFER_SIZE];
    01205         int pdu_len= 0, adu_len= 0;
    01206
    01208         pdu_len= MBResponseWriteSingleRegister( pdu, addr, value);
    01209
    01210         adu_len= MBSerialWrite( port, pdu, pdu_len, address);
    01211
    01212         if( adu_len < 0)
    01213                 return adu_len;
    01214
    01215         return MB_OK;
    01216 }
Here is the call graph for this function:

void MBSerialSetCharTimeout | ( | int | port,
|  | u32 | timeout
| ) |

  | set char timeout Parameters:
  | port | port number
  | timeout | new frame timeout value
Returns: none Definition at line 409 of file mbserial.c. References MB_SERIAL_INFO::CharTimeout.
    00410 {
    00411         MBSerialInfo[ port].CharTimeout= timeout;
    00412 }

void MBSerialSetFrameTimeout | ( | int | port,
|  | u32 | timeout
| ) |

  | set frame timeout Parameters:
  | port | port number
  | timeout | new frame timeout value
Returns: none Definition at line 384 of file mbserial.c. References MB_SERIAL_INFO::FrameTimeout.
    00385 {
    00386         MBSerialInfo[ port].FrameTimeout= timeout;
    00387 }

int MBSerialSetMode | ( | int | port,
|  | unsigned int | mode
| ) |

  | set serial port mode for RS232/RS422/RS485 Parameters:
  | port | port number
  | mode | serial port mode {RS232_MODE/RS485_2WIRE_MODE/RS422_MODE/RS485_4WIRE_MODE}
Returns: return MB_OK for success, on error return error code Definition at line 474 of file mbserial.c. References MB_ERROR_MODE, MB_OK, and SerialSetMode().
    00475 {
    00476         int ret= SerialSetMode( port, mode);
    00477
    00478         if( ret <= 0)
    00479                 return MB_OK;
    00480
    00481         return MB_ERROR_MODE;
    00482 }
Here is the call graph for this function:

int MBSerialSetParam | ( | int | port,
|  | int | parity,
|  | int | databits,
|  | int | stopbit
| ) |

  | set serial port parameter Parameters:
  | port | port number
  | parity | parity check, 0: none, 1: odd, 2: even, 3: space
  | databits | data bits
  | stopbit | stop bit
Returns: return MB_OK for success, on error return error code Definition at line 326 of file mbserial.c. References SerialSetParam().
    00327 {
    00328         return SerialSetParam( port, parity, databits, stopbit);
    00329 }
Here is the call graph for this function:

int MBSerialSetSpeed | ( | int | port,
|  | unsigned int | speed
| ) |

  | set serial speed and make changes now Parameters:
  | port | port number
  | speed | unsigned integer for new speed
Returns: return SERIAL_OK for success, on error return error code Definition at line 492 of file mbserial.c. References SerialSetSpeed().
    00493 {
    00494         return SerialSetSpeed( port, speed);
    00495 }
Here is the call graph for this function:

void MBSerialSetTimeout | ( | int | port,
|  | u32 | timeout
| ) |

  | set new response timeout Parameters:
  | port | port number
  | timeout | new response timeout value
Returns: none Definition at line 434 of file mbserial.c. References MB_SERIAL_INFO::Timeout.
    00435 {
    00436         MBSerialInfo[ port].Timeout= timeout;
    00437 }

int MBSerialWrite | ( | int | port,
|  | u8 * | pdu,
|  | int | len,
|  | u8 | address
| ) |

  | Make ADU and sent out. Parameters:
  | port | port number
  | pdu | PDU packet
  | len | PDU size
  | address | destination device id
Returns: length of data actually write, on error return error code Definition at line 280 of file mbserial.c. References MB_ASCII_ADU_MAX_LENGTH, MBMakeADU(), and SerialWrite(). Referenced by MBSerialSendAndWaitResponse(), MBSerialSendDiagnostic(), MBSerialSendGetCommEventCounter(), MBSerialSendGetCommEventLog(), MBSerialSendReadCoils(), MBSerialSendReadDecreteInputs(), MBSerialSendReadExecptionStatus(), MBSerialSendReadFIFOueue(), MBSerialSendReadHoldingRegisters(), MBSerialSendReadInputRegisters(), MBSerialSendWriteMultipleRegisters(), MBSerialSendWriteSingleCoil(), MBSerialSendWriteSingleRegister(), and serial_master_child().
    00281 {
    00282         u8 adu[ MB_ASCII_ADU_MAX_LENGTH];
    00283         int adu_len;
    00284
    00285         adu_len= MBMakeADU( adu, MBSerialInfo[ port].Protocol, address, pdu, len, 0);
    00286
    00287         return SerialWrite( port, (char *)adu, adu_len);
    00288 }
Here is the call graph for this function:

int MBSerialWriteMultipleCoils | ( | int | port,
|  | u8 | address,
|  | u16 | startcoils,
|  | u16 | no,
|  | u8 * | value
| ) |

  | send Write Multiple Coils request and get response(0x0F) Parameters:
  | port | which port to use
  | address | slave device address
  | startcoils | starting coil register number
  | no | number of registers, between 1 ~ 2000
  | value | value to write
Returns: return MB_OK for success, on error return error code make PDU Definition at line 603 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBGetPDU(), MBGetResponseWriteMultipleCoils(), MBSerialSendAndWaitResponse(), and MBWriteMultipleCoils().
    00604 {
    00605         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00606         int pdu_len= 0, adu_len= 0, ret= 0;
    00607         u16 count;
    00608         u16 ret_addr, ret_value;
    00609
    00610         count= (u8)no/8;
    00611         if( count*8 < no)
    00612                 count++;
    00613
    00615         pdu_len= MBWriteMultipleCoils( pdu, startcoils, no, count, value);
    00616
    00617         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00618
    00619         if( adu_len < 0)
    00620                 return adu_len;
    00621
    00622         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00623         ret= MBGetResponseWriteMultipleCoils( pdu, &ret_addr, &ret_value);
    00624         if( ret < 0)
    00625                 return ret;
    00626         if( ( ret_addr != startcoils) || ( ret_value != no))
    00627                 return MB_ERROR_EXECPTION;
    00628         return MB_OK;
    00629 }
Here is the call graph for this function:

int MBSerialWriteMultipleRegisters | ( | int | port,
|  | u8 | address,
|  | u16 | startdevreg,
|  | u16 | noreg,
|  | u8 | count,
|  | u16 * | value
| ) |

  | send Write Multiple Registers request and get response(0x10) Parameters:
  | port | which port to use
  | address | slave device address
  | startdevreg | starting register number
  | noreg | number of registers, between 1 ~ 120
  | count | value counter
  | value | register status slave returned
Returns: MB_OK for success, on error return error code make PDU Definition at line 740 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBGetPDU(), MBGetResponseWriteMultipleRegisters(), MBSerialSendAndWaitResponse(), and MBWriteMultipleRegisters().
    00741 {
    00742         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00743         int pdu_len= 0, adu_len= 0, ret= 0;
    00744         u16 ret_addr, ret_value;
    00745
    00747         pdu_len= MBWriteMultipleRegisters( pdu, startdevreg, noreg, count, value);              // make PDU
    00748
    00749         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00750
    00751         if( adu_len < 0)
    00752                 return adu_len;
    00753
    00754         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00755         ret= MBGetResponseWriteMultipleRegisters( pdu, &ret_addr, &ret_value);
    00756         if( ret < 0)
    00757                 return ret;
    00758         if( ( ret_addr != startdevreg) || ( ret_value != noreg))
    00759                 return MB_ERROR_EXECPTION;
    00760         return MB_OK;
    00761 }
Here is the call graph for this function:

int MBSerialWriteSingleCoil | ( | int | port,
|  | u8 | address,
|  | u16 | coilreg,
|  | u16 | onoff
| ) |

  | send Write Single Coil request and get response(0x05) Parameters:
  | port | which port to use
  | address | slave device address
  | coilreg | coil register number
  | onoff | 0xFF00 for on,0x0000 for off.
Returns: return MB_OK for success, on error return error code < make PDU Definition at line 570 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBGetPDU(), MBGetResponseWriteSingleCoil(), MBSerialSendAndWaitResponse(), and MBWriteSingleCoil().
    00571 {
    00572         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00573         int pdu_len= 0, adu_len= 0, ret= 0;
    00574         u16 ret_addr, ret_value;
    00575
    00576         pdu_len= MBWriteSingleCoil( pdu, coilreg, onoff);
    00577
    00578         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00579
    00580         if( adu_len < 0)
    00581                 return adu_len;
    00582
    00583         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00584         ret= MBGetResponseWriteSingleCoil( pdu, &ret_addr, &ret_value);
    00585         if( ret < 0)
    00586                 return ret;
    00587         if( ( ret_addr != coilreg) || ( ret_value != onoff))
    00588                 return MB_ERROR_EXECPTION;
    00589         return MB_OK;
    00590 }
Here is the call graph for this function:

int MBSerialWriteSingleRegister | ( | int | port,
|  | u8 | address,
|  | u16 | devicereg,
|  | u16 | value
| ) |

  | send Write Single Register request and get response(0x06) Parameters:
  | port | which port to use
  | address | slave device address
  | devicereg | register number
  | value | register status slave returned
Returns: MB_OK for success, on error return error code make PDU Definition at line 705 of file mbserial.c. References MAX_BUFFER_SIZE, MB_ASCII_ADU_MAX_LENGTH, MB_ERROR_EXECPTION, MB_OK, MBGetPDU(), MBGetResponseWriteSingleRegister(), MBSerialSendAndWaitResponse(), and MBWriteSingleRegister().
    00706 {
    00707         u8 adu[ MB_ASCII_ADU_MAX_LENGTH], pdu[ MAX_BUFFER_SIZE];
    00708         int pdu_len= 0, adu_len= 0, ret= 0;
    00709         u16 ret_addr, ret_value;
    00710
    00712         pdu_len= MBWriteSingleRegister( pdu, devicereg, value);
    00713
    00714         adu_len= MBSerialSendAndWaitResponse( port, adu, pdu, pdu_len, address);
    00715
    00716         if( adu_len < 0)
    00717                 return adu_len;
    00718
    00719         pdu_len= MBGetPDU( pdu, adu, adu_len);
    00720         ret= MBGetResponseWriteSingleRegister( pdu, &ret_addr, &ret_value);
    00721         if( ret < 0)
    00722                 return ret;
    00723         if( ( ret_addr != devicereg) || ( ret_value != value))
    00724                 return MB_ERROR_EXECPTION;
    00725         return MB_OK;
    00726 }
Here is the call graph for this function:

## Variable Documentation

struct MB_SERIAL_INFO MBSerialInfo[MAX_PORT_NUM]`[static]`

| Definition at line 21 of file mbserial.c.