User Tools

Site Tools


devel:modbus_tcp_ip

Use of this connection

RTS2 provides connection for working with Modbus TCP/IP protocol. The advantage of this connection is that it inherits from Rts2Conn, and so can be easily integrated into Rts2 as another connection. There is a sample code how to use it:

class Zelio:public Rts2Sensor
{
	private:
		HostString *host;
 
		rts2core::ConnModbus *zelioConn;
 
	protected:
		virtual int processOption (int in_opt);
 
	public:
		Zelio (int argc, char **argv);
		virtual ~Zelio (void);
		virtual int init ();
 
		virtual int info ();
};
 
}
 
 
int
Zelio::processOption (int in_opt)
{
	switch (in_opt)
	{
		// process hoststring (port can be specified after :)
		case 'z':
			host = new HostString (optarg, "502");
			break;
		default:
			return Rts2Sensor::processOption (in_opt);
	}
	return 0;
}
 
 
Zelio::Zelio (int argc, char **argv)
:Rts2Sensor (argc, argv)
{
	host = NULL;
 
	addOption ('z', NULL, 1, "Zelio TCP/IP address and port (separated by :)");
}
 
 
Zelio::~Zelio (void)
{
	delete zelioConn;
	delete host;
}
 
 
int
Zelio::info ()
{
	// call Modbus TCP/IP coild query
	zelioConn->readCoils (0, 8);
	return Rts2Sensor::info ();
}
 
 
int
Zelio::init ()
{
	int ret = Rts2Sensor::init ();
	if (ret)
		return ret;
 
	zelioConn = new rts2core::ConnModbus (this, host->getHostname (), host->getPort ());
	return zelioConn->init ();
}
 
 
int
main (int argc, char **argv)
{
	Zelio device = Zelio (argc, argv);
	return device.run ();
}
devel/modbus_tcp_ip.txt · Last modified: 2008/11/14 00:00 (external edit)