00001
00016
00017
00018 #include "mbserial.h"
00019 #include "modbus_utils.h"
00020
00021 static struct MB_SERIAL_INFO MBSerialInfo[ MAX_PORT_NUM];
00022
00023
00028
00029 void MBSerialInit(void)
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 }
00041
00042
00049
00050 int MBSerialOpen(int port, int protocol)
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 }
00077
00078
00089
00090 int MBSerialReadRTU( int port, u8 *buf, int len)
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
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 }
00145
00146
00157
00158 int MBSerialReadASCII( int port, u8 *buf, int len)
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)
00195 break;
00196
00197 if( SerialBlockRead( port, (char *)&ch, 1))
00198 {
00199 if( ch == ':')
00200 {
00201 tmp_len= 0;
00202 }
00203 else if( ch == 0x0d)
00204 {
00205 if( end_flag == 0)
00206 end_flag= 1;
00207 else
00208 return MB_ERROR_FORMAT;
00209 }
00210 else if( ch == 0x0a)
00211 {
00212 if( end_flag == 1)
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
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 }
00230
00231
00242
00243 int MBSerialBlockRead(int port, u8 *buf, int len)
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 }
00255
00256
00264
00265 int MBSerialNonBlockRead(int port, u8 *buf, int len)
00266 {
00267 return SerialNonBlockRead( port, (char *)buf, len);
00268 }
00269
00270
00279
00280 int MBSerialWrite(int port, u8 *pdu, int len, u8 address)
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 }
00289
00290
00296
00297 int MBSerialClose(int port)
00298 {
00299
00300 return SerialClose( port);
00301 }
00302
00303
00310
00311 int MBSerialFlowCtrl(int port, int control)
00312 {
00313 return SerialFlowControl( port, control);
00314 }
00315
00316
00325
00326 int MBSerialSetParam( int port, int parity, int databits, int stopbit)
00327 {
00328 return SerialSetParam( port, parity, databits, stopbit);
00329 }
00330
00331
00342
00343 int MBSerialSendAndWaitResponse(int port, u8 *buf, u8 *pdu, int len, u8 address)
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
00357
00358
00359
00360
00361 return ret_len;
00362 }
00363
00364
00370
00371 u32 MBSerialGetFrameTimeout(int port)
00372 {
00373 return MBSerialInfo[ port].FrameTimeout;
00374 }
00375
00376
00383
00384 void MBSerialSetFrameTimeout(int port, u32 timeout)
00385 {
00386 MBSerialInfo[ port].FrameTimeout= timeout;
00387 }
00388
00389
00395
00396 u32 MBSerialGetCharTimeout(int port)
00397 {
00398 return MBSerialInfo[ port].CharTimeout;
00399 }
00400
00401
00408
00409 void MBSerialSetCharTimeout(int port, u32 timeout)
00410 {
00411 MBSerialInfo[ port].CharTimeout= timeout;
00412 }
00413
00414
00420
00421 u32 MBSerialGetTimeout(int port)
00422 {
00423 return MBSerialInfo[ port].Timeout;
00424 }
00425
00426
00433
00434 void MBSerialSetTimeout(int port, u32 timeout)
00435 {
00436 MBSerialInfo[ port].Timeout= timeout;
00437 }
00438
00439
00445
00446 u8 MBSerialGetAddress( const u8 *adu)
00447 {
00448 return MBGetAddress( adu);
00449 }
00450
00451
00459
00460 int MBSerialGetPDU( u8 *buf, u8 *adu, int len)
00461 {
00462 return MBGetPDU( buf, adu, len);
00463 }
00464
00465
00473
00474 int MBSerialSetMode( int port, unsigned int mode)
00475 {
00476 int ret= SerialSetMode( port, mode);
00477
00478 if( ret <= 0)
00479 return MB_OK;
00480
00481 return MB_ERROR_MODE;
00482 }
00483
00484
00491
00492 int MBSerialSetSpeed( int port, unsigned int speed)
00493 {
00494 return SerialSetSpeed( port, speed);
00495 }
00496
00497
00498
00508
00509 int MBSerialReadDecreteInputs(int port, u8 address, u16 startdec, u16 no, u8 *value)
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 }
00528
00529
00539
00540 int MBSerialReadCoils(int port, u8 address, u16 startcoils, u16 no, u8 *value)
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 }
00559
00560
00569
00570 int MBSerialWriteSingleCoil(int port, u8 address, u16 coilreg, u16 onoff)
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 }
00591
00592
00602
00603 int MBSerialWriteMultipleCoils(int port, u8 address, u16 startcoils, u16 no, u8 *value)
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 }
00630
00631
00641
00642 int MBSerialReadInputRegisters(int port, u8 address, u16 startreg, u16 no, u16 *value)
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 }
00662
00663
00673
00674 int MBSerialReadHoldingRegisters(int port, u8 address, u16 startreg, u16 no, u16 *value)
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 }
00694
00695
00704
00705 int MBSerialWriteSingleRegister(int port, u8 address, u16 devicereg, u16 value)
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 }
00727
00728
00739
00740 int MBSerialWriteMultipleRegisters(int port, u8 address, u16 startdevreg, u16 noreg, u8 count, u16 *value)
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);
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 }
00762
00763
00777
00778 int MBSerialReadWriteMultipleRegisters(int port, u8 address, u16 rsreg, u16 rno, u16 *rv, u16 wsreg, u16 wno, u8 count, u16 *wv)
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);
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 }
00801
00802
00812
00813 int MBSerialMaskWriteRegister(int port, u8 address, u16 reg, u16 andmask, u16 ormask)
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 }
00836
00837
00847
00848 int MBSerialReadFIFOQueue(int port, u8 address, u16 FIFOAddr, u16 *FIFOCount, u16 *FIFOValue)
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 }
00869
00870
00871
00879
00880 int MBSerialReadExceptionStatus(int port, u8 address, u8 *status)
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 }
00901
00902
00911
00912 int MBSerialDiagnostic(int port, u8 address, u16 subfunc, u16 data)
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 }
00936
00937
00946
00947 int MBSerialGetCommEventCounter(int port, u8 address, u16 *status, u16 *eventcount)
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 }
00968
00969
00980
00981 int MBSerialGetCommEventLog(int port, u8 address, u16 *status, u16 *eventcount, u16 *messagecount, u8 *events)
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 }
01002
01003
01012
01013 int MBSerialReportSlaveID(int port, u8 address, u8 *slave_id, u8 *status)
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 }
01034
01035
01036
01045
01046 int MBSerialSendReadDecreteInputs(int port, u8 address, u16 no, u8 *value)
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 }
01061
01062
01071
01072 int MBSerialSendReadCoils(int port, u8 address, u16 no, u8 *value)
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 }
01087
01088
01097
01098 int MBSerialSendReadInputRegisters(int port, u8 address, u16 no, u16 *value)
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 }
01113
01114
01123
01124 int MBSerialSendReadHoldingRegisters(int port, u8 address, u16 no, u16 *value)
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 }
01139
01140
01149
01150 int MBSerialSendWriteMultipleRegisters(int port, u8 address, u16 startdevreg, u16 noreg)
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 }
01165
01166
01175
01176 int MBSerialSendWriteSingleCoil(int port, u8 address, u16 coilreg, u16 onoff)
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 }
01191
01192
01201
01202 int MBSerialSendWriteSingleRegister(int port, u8 address, u16 addr, u16 value)
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 }
01217
01218
01228
01229 int MBSerialSendReadFIFOueue(int port, u8 address, u16 no, u16 count, u16 *value)
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 }
01244
01245
01253
01254 int MBSerialSendReadExecptionStatus(int port, u8 address, u8 status)
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 }
01269
01270
01279
01280 int MBSerialSendDiagnostic(int port, u8 address, u16 subfunc, u16 data)
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 }
01295
01296
01305
01306 int MBSerialSendGetCommEventCounter(int port, u8 address, u16 status, u16 eventcount)
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 }
01321
01322
01334
01335 int MBSerialSendGetCommEventLog(int port, u8 address, u8 no, u16 status, u16 eventcount, u16 messagecount, u8 *events)
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 }