1717 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1818 */
1919
20- #define VERSION " OSHMI Modbus Driver v.1.10 - Copyright 2015-2020 Ricardo L. Olsen"
20+ #define VERSION " OSHMI Modbus Driver v.1.11 - Copyright 2015-2020 Ricardo L. Olsen"
2121
2222#include < stdio.h>
2323#include < conio.h>
@@ -106,6 +106,10 @@ struct mb_rturead
106106 int stop_bits = 1 ;
107107 char parity = ' N' ;
108108 int log_debug = 0 ;
109+ int tcp_pi_mode = 0 ;
110+ int rs485_mode = 0 ;
111+ int rts_mode = MODBUS_RTU_RTS_NONE;
112+ int rts_delay = 0 ;
109113 vector <mb_read> readhr;
110114 vector <mb_read> readhr_float; // read consecutive 16 bit values as floats (assume 1st byte=exp, 2nd=MSB mant, 3rd=middle mant, 4=LSB mant so intel 2,3,0,1 order)
111115 vector <mb_read> readhr_long; // read consecutive 16 bit values as a long (32 bit integer=DWORD)
@@ -538,39 +542,72 @@ int main(void)
538542 if (!reader.HasSection (rtun))
539543 break ;
540544 mb_rturead rtu;
541- rtu.ip = reader.GetString (rtun, " IP" , " 127.0.0.1 " );
545+ rtu.ip = reader.GetString (rtun, " IP" , " " );
542546 rtu.port = reader.GetInteger (rtun, " PORT" , 502 );
543547 rtu.slave_id = reader.GetInteger (rtun, " SLAVE_ID" , -1 );
544548 rtu.timeout_ms = reader.GetInteger (rtun, " TIMEOUT" , 500 );
545549 rtu.delay = reader.GetInteger (rtun, " DELAY" , 0 );
546550 rtu.endianness = reader.GetInteger (rtun, " ENDIANNESS" , 0 ); // for 32 bit 0=BIG 1=MIDDLE LITTLE
551+ rtu.tcp_pi_mode = reader.GetInteger (rtun, " TCP_PI_MODE" , 0 );
547552
553+ rtu.rs485_mode = reader.GetInteger (rtun, " RS485_MODE" , 0 );
554+ rtu.rts_mode = reader.GetInteger (rtun, " RTS_MODE" , 0 );
555+ rtu.rts_delay = reader.GetInteger (rtun, " RTS_DELAY" , 0 );
548556 rtu.serial_port_name = reader.GetString (rtun, " SERIAL_PORT_NAME" , " " );
549557 rtu.baud_rate = reader.GetInteger (rtun, " BAUD_RATE" , 9600 );
550558 rtu.data_bits = reader.GetInteger (rtun, " DATA_BITS" , 8 );
551559 rtu.stop_bits = reader.GetInteger (rtun, " STOP_BITS" , 1 );
552- string sparity = reader.GetString (rtun, " DATA_BITS " , " N" );
560+ string sparity = reader.GetString (rtun, " PARITY " , " N" );
553561 if (sparity.length () > 0 )
554562 rtu.parity = sparity[0 ];
555563
556564 rtu.log_debug = reader.GetInteger (rtun, " LOG_LEVEL" , 0 );
557565
558- if (i > 0 &&
559- (
560- rtu.ip != " " && mb_queue[i - 1 ].ip == rtu.ip && mb_queue[i - 1 ].port == rtu.port
566+ if (i > 0 &&
567+ (
568+ rtu.ip != " " && mb_queue[i - 1 ].ip == rtu.ip && mb_queue[i - 1 ].port == rtu.port
561569 ||
562- rtu.serial_port_name != " " && mb_queue[i - 1 ].serial_port_name == rtu.serial_port_name
570+ rtu.serial_port_name != " " && ( mb_queue[i - 1 ].serial_port_name == rtu.serial_port_name )
563571 )
564- )
572+ ) {
573+ printf (" ........\n " );
574+ printf (" RTU %d\n " , i + 1 );
575+ if (rtu.slave_id == -1 )
576+ printf (" SLAVE_ID [default]\n " );
577+ else
578+ printf (" SLAVE_ID %d\n " , rtu.slave_id );
565579 rtu.ctx = mb_queue[i - 1 ].ctx ; // same previous IP and PORT or serial port, reuse connection
580+ }
566581 else
567582 {
568- if (rtu.serial_port_name != " " )
583+ printf (" -------------------------------\n " );
584+ printf (" NEW CHANNEL\n " );
585+ if (rtu.serial_port_name != " " ) {
569586 rtu.ctx = modbus_new_rtu (rtu.serial_port_name .c_str (), rtu.baud_rate , rtu.parity , rtu.data_bits , rtu.stop_bits );
570- else
571- rtu.ctx = modbus_new_tcp (rtu.ip .c_str (), rtu.port );
587+ if (rtu.rs485_mode != 0 )
588+ modbus_rtu_set_serial_mode (rtu.ctx , MODBUS_RTU_RS485);
589+ if (rtu.rts_mode != 0 ) {
590+ if (rtu.rts_mode == 1 )
591+ modbus_rtu_set_rts (rtu.ctx , MODBUS_RTU_RTS_UP);
592+ else
593+ modbus_rtu_set_rts (rtu.ctx , MODBUS_RTU_RTS_DOWN);
594+ }
595+ if (rtu.rts_delay != 0 ) {
596+ modbus_rtu_set_rts (rtu.ctx , rtu.rts_delay );
597+ }
598+ }
599+ else {
600+ if (rtu.tcp_pi_mode != 0 ) {
601+ char service[33 ];
602+ _itoa (rtu.port , service, 10 );
603+ rtu.ctx = modbus_new_tcp_pi (rtu.ip .c_str (), service);
604+ }
605+ else {
606+ rtu.ctx = modbus_new_tcp (rtu.ip .c_str (), rtu.port );
607+ }
608+ }
572609 if (rtu.ctx == NULL ) {
573- cout << " Unable to create the libmodbus context (slave " << i << " )!" << endl;
610+ cout << " Unable to create the libmodbus context (slave " << i+ 1 << " )!" << endl;
574611 exit (1 );
575612 }
576613 if (rtu.log_debug == 0 )
@@ -584,19 +621,25 @@ int main(void)
584621 uint32_t old_response_to_sec;
585622 uint32_t old_response_to_usec;
586623 modbus_get_response_timeout (rtu.ctx , &old_response_to_sec, &old_response_to_usec);
587- printf ( " ------------------------------- \n " );
588- printf ( " RTU %d \n " , i + 1 );
589- if (rtu. serial_port_name != " " )
624+ if (rtu. serial_port_name != " " ) {
625+ if (rtu. rs485_mode != 0 )
626+ printf ( " RS485 \n " );
590627 printf (" SERIAL PORT %s\n " , rtu.serial_port_name .c_str ());
591- else
592- printf (" IP %s PORT %d\n " , rtu.ip .c_str (), rtu.port );
628+ }
629+ else {
630+ if (rtu.tcp_pi_mode != 0 )
631+ printf (" PI MODE\n " );
632+ printf (" IP %s PORT %d\n " , rtu.ip .c_str (), rtu.port );
633+ }
634+ printf (" DELAY %d ms\n " , rtu.delay );
635+ printf (" TIMEOUT %d s : %d us \n " , old_response_to_sec, old_response_to_usec);
636+
637+ printf (" ........\n " );
638+ printf (" RTU %d\n " , i + 1 );
593639 if (rtu.slave_id == -1 )
594640 printf (" SLAVE_ID [default]\n " );
595641 else
596- printf (" SLAVE_ID %d\n " , rtu.slave_id );
597- printf (" DELAY %d ms\n " , rtu.delay );
598- printf (" TIMEOUT %d s : %d us \n " , old_response_to_sec, old_response_to_usec);
599- printf (" -------------------------------\n " );
642+ printf (" SLAVE_ID %d\n " , rtu.slave_id );
600643 }
601644
602645 for (int j = 0 ; j < MAX_INPREAD; j++)
@@ -693,6 +736,7 @@ int main(void)
693736
694737 mb_queue.push_back (rtu);
695738 }
739+ printf (" *******************************************************\n " );
696740
697741 int max_pointspkt;
698742 int count;
0 commit comments