//////////////////////////////////////// /// // <Br/>/ * <br/> readcoils function: <br/> the status of the read relay. If yes, true is returned. <Br/> send data length = 12 <br/> Recv Data Length = 10 <br/> Start address of the unsigned short int ADDR read relay (relay No.-1 ). <Br/> Note: 1 ~ Eight relays, starting from 0 <br/> unsigned short int Len Number of relays read, up to 8. <br/> If debug_state is 1, the mode information is printed. <br/> the status of the relay read by ubool * parado <br/> socket s_bnc, fd_set FDS, timeval TV is a secondary network interface parameter <br/> */<br/> ////////////////////// //////////////////////////////////////// //////////< br/> inline bool readcoils (unsigned short int ADDR, byte & coilstate, socket_int s_bnc) <br/>{< br/> bytes _len = 12; <br/> byte bytes _code = 1; // command code: 0 1. Read relays <br/> byte * pai_string; <br/> byte * echo_string; <br/> pai_string = new byte [pai_len]; <br/> echo_string = new byte [cmd_len]; <br/> cmd_string [0] = 0; // Modbus protocol specification mark <br/> cmd_string [1] = 0; <br/> pai_string [2] = 0; <br/> pai_string [3] = 0; <br/> pai_string [4] = 0; // The number of subsequent bytes of the high part of length command <br/> bytes _string [5] = 6; // The number of subsequent bytes of the length low part command <br/> bytes _string [6] = 0; // unit identifier: Identification of a remote slave connected on a serial line or on other buses. <br/> pai_string [7] = pai_code; <br/> pai_string [8] = (unsigned char) (ADDR> 8 ); <br/> pai_string [9] = (unsigned char) ADDR; <br/> pai_string [10] = 0; // (unsigned char) (LEN> 8 ); <br/> pai_string [11] = 8; // (unsigned char) Len; <br/> int iret; <br/> fd_set FDS; <br/> struct timeval TV; <br/> TV. TV _sec = sleep; // timeout <Br/> TV. TV _usec = 0; <br/> fd_zero (& FDs); <br/> fd_set (s_bnc, & FDs ); <br/>/* wait for permission to send (wait for write or not) */<br/> iret = select (s_bnc + 1, null, & FDS, null, & TV);/* write */<br/> If (iret = 0) <br/> {<br/> If (debug_state) printf ("timer over! N "); <br/> close (s_bnc); <br/> return false; <br/>}< br/> else if (iret <0) <br/>{< br/> If (debug_state) printf ("select is err! N "); <br/> close (s_bnc); <br/> return false; <br/>}< br/>/* Send write data */<br/> iret = Send (s_bnc, (char *) cmd_string, Len _len, 0 ); <br/> If (iret <cmd_len) <br/> {<br/> If (debug_state) printf ("failed to send chars: % DN", iret ); <br/> close (s_bnc); <br/> return false; <br/>}< br/>/* Wait for response (waiting for readable read) */<br/> fd_set (s_bnc, & FDs); <br/> iret = select (s_bnc + 1, & FDS, null, null, & TV ); <br/> If (iret = 0) <br/>{< br/> If (debug_state) printf ("timer over! N "); <br/> close (s_bnc); <br/> return false; <br/>}< br/> else if (iret <0) <br/>{< br/> If (debug_state) printf ("select is err! N "); <br/> close (s_bnc); <br/> return false; <br/>}< br/>/* read data */<br/> iret = Recv (s_bnc, (char *) echo_string, 12, 0 ); <br/> If (iret <= 0) <br/> {<br/> If (debug_state) printf ("An error occurred while receiving data Recv statements N "); <br/>}< br/> If (echo_string [7] = pai_code) /* The 7 position is the command */<br/> {<br/>/* If (debug_state) printf ("N has read data successfully N "); */<br/>}< br/> else if (echo_string [7] ==( 0x80 | cmd_code )) <br/>{< br/> If (debug_state) printf ("device active report: Operation error N"); <br/> return false; <br/>}< br/> else <br/> {<br/> If (debug_state) printf ("error N"); <br/> return false; <br/>}< br/> // int ndoflag = echo_string [9] | echo_string [10] <8 ); // position 10 is the relay status <br/> // unsigned char ndoflag = echo_string [9]; // because e3018 only has eight relay, therefore, there are no 10 locations <br/> coilstate = echo_string [9]; <br/> Delete [] pai_string; <br/> Delete [] echo_string; <br/> return true; <br/>}< br/>
All programs are being completed.