Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Need help with Modbus

by Khen1950fx (Canon)
on Jan 25, 2012 at 05:32 UTC ( #949829=note: print w/ replies, xml ) Need Help??


in reply to Need help with Modbus

Protocol::Modbus is the module that you want to use. The documentation is very sparse, but the source has the info that you need. I gave it quick look and managed to get this going. I used the generic approach.

#!/usr/bin/perl -l use strict; use warnings; $|=1; my $proto = Protocol::Modbus->new( driver => 'TCP', transport => 'TCP +' ); my $request1 = $proto->request( function => 1, address => 1, quantity => 1, ); my $request2 = $proto->request( function => 2, address => 1, quantity => 1, ); my $request3 = $proto->request( function => 3, address => 1, uantity => 1, ); my $request4 = $proto->request( function => 4, address => 1, quantity => 1, ); print "=" x 60; print "Using unpack: \n"; print unpack ('H*', $request1->pdu()); print unpack ('H*', $request2->pdu()); print unpack ('H*', $request3->pdu()); print unpack ('H*', $request4->pdu()); print "=" x 60; print "Using Pretty-Print: \n"; print $request1; print $request2; print $request3; print $request4; print "=" x 60; print "stringify: "; print $request1->stringify(); print $request2->stringify(); print $request3->stringify(); print $request4->stringify(); print "=" x 60;


Comment on Re: Need help with Modbus
Download Code
Re^2: Need help with Modbus
by pashanoid (Scribe) on Jan 27, 2012 at 04:40 UTC

    Thank you! However, could you please elaborate: if my IP is 192.168.0.57 port 502, then do I format the like this? $proto = Protocol::Modbus->new( driver => 'TCP', transport => 'TCP', ip=>'192.168.0.57', port=>'502' );

    if in c ret = read_input_registers(&mb_param, SUNSAVERMPPT, 0x0000, 75, data); Then Which are the function, address and quantity? in my $request4 = $proto->request( function => 4, address => 1, quantity => 1, );

    Which handle/variable gets the modbus reply?

      The $req variable gets the modbus reply.

      #!/usr/bin/perl -l use strict; use warnings; use Protocol::Modbus; $|=1; my $modbus = Protocol::Modbus->new( driver => 'TCP', transport => 'TCP', ); my $trs = Protocol::Modbus::Transport->new( driver => 'TCP', address => '192.168.0.57', port => 502, timeout => 3, ); my $req = $modbus->request( function => 4, address => 1, quantity => 75, value => 0x0000, ); print "=" x 60; print "Response: ", $req; print "=" x 60; $trs->disconnect;
        Hello. Did this work for you? I am trying to communicate with a charge controller that supports modbus/tcp but when I modify code above I don't even see traffic leave my host. I get a response from the program that looks like "Response: ModbusRequest PDU(000000000006ff040001000a)" but I don't know what that means. I am going crazy over this. Please help. Thank you.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://949829]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (18)
As of 2014-09-16 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (24 votes), past polls