Skip to content

Instantly share code, notes, and snippets.

@ashevchuk
Last active May 3, 2018 08:03
Show Gist options
  • Save ashevchuk/4ab49f923103d30f436571a7373c0dbc to your computer and use it in GitHub Desktop.
Save ashevchuk/4ab49f923103d30f436571a7373c0dbc to your computer and use it in GitHub Desktop.
SDM220 modbus
#!/usr/bin/env perl
use strict;
use warnings;
use Device::Modbus::RTU::Client;
my $client = Device::Modbus::RTU::Client->new(
port => '/dev/ttyUSB0',
baudrate => 9600,
parity => 'none',
);
$client->send_request(Device::Modbus::Request->new(
code => 0x04,
address => 0x0000,
quantity => 0x0002,
unit => 0x01
));
my $resp = $client->receive_response;
if ($resp->success) {
my $values = $resp->values;
my $hi = $values->[0];
my $lo = $values->[1];
my $hi_l=$hi & 0xff;
my $hi_h=($hi>>8) & 0xff;
my $lo_l=$lo & 0xff;
my $lo_h=($lo>>8) & 0xff;
my $word = ($hi_h << 24) + ($hi_l << 16) + ($lo_h << 8) + $lo_l;
my $num = (($word & 0x80000000) ? -1 : 1) * (2 ** ((($word & 0x7F800000) >> 23) - 127)) * ( ($word & 0x007FFFFF | 0x00800000) / (1 << 23));
printf "V %f\n", $num;
}
$client->disconnect;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment