summaryrefslogtreecommitdiff
path: root/inst/@octave_modbus/read.m
blob: a4ffd92f477a4b5c36cbb9f4f63a1f02d87fac30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
## Copyright (C) 2022 John Donoghue <john.donoghue@ieee.org>
## 
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

## -*- texinfo -*- 
## @deftypefn {} {@var{data} =} read (@var{dev}, @var{target}, @var{address})
## @deftypefnx {} {@var{data} =} read (@var{dev}, @var{target}, @var{address}, @var{count})
## @deftypefnx {} {@var{data} =} read (@var{dev}, @var{target}, @var{address}, @var{count}, @var{serverId}, @var{precision})
## Read data from modbus device @var{dev} target @var{target} starting at address @var{address}.
##
## @subsubheading Inputs
## @var{dev} - connected modbus device
##
## @var{target} - target type to read. One of 'coils', 'inputs', 'inputregs' or 'holdingregs'
##
## @var{address} - address to start reading from.
##
## @var{count} - number of elements to read. If not provided, count is 1.
##
## @var{serverId} - address to send to (0-247). Default of 1 is used if not specified.
##
## @var{precision} - Optional precision for how to interpret the read data.
## Currently known precision values are uint16 (default), int16, uint32, int32, uint64, uint64, single, double.
##
## @subsubheading Outputs
## @var{data} - data read from the device
##
## @seealso{modbus}
## @end deftypefn

function data = read (dev, target, address, count, serverid, precision)
  if nargin < 3
    print_usage();
  endif

  if nargin < 4
    count = 1;
  endif
  if nargin < 5
    serverid = 1;
  endif
  if nargin < 6
    precision = "uint16";
  endif

  if ! isnumeric(address) || address < 0
    error ("Expected address to be a number.");
  endif

  if ! isnumeric(count) || count < 1
    error ("Expected count to be a positive number.");
  endif

  if ! isnumeric(serverid) || serverid < 0 || serverid > 247
    error ("Expected serverId to be a number between 0 .. 247");
  endif

  if !ischar(precision)
    error ("Expected precision to be a character type");
  endif

  toread = count;

  # precision only used for inputregs and holdingregs
  switch (precision)
  case {"int16" "short"}
    toclass = "int16";
    tosize = 2;
    toread = toread * 2;
  case {"uint16" "ushort"}
    toclass = "uint16";
    tosize = 2;
    toread = toread * 2;
  case {"int32" "int"}
    toclass = "int32";
    tosize = 4;
    toread = toread * 4;
  case {"uint32" "uint"}
    toclass = "uint32";
    tosize = 4;
    toread = toread * 4;
  case {"long" "int64"}
    toclass = "int64";
    toread = toread * 8;
    tosize = 8;
  case {"ulong" "uint64"}
    toclass = "uint64";
    toread = toread * 8;
    tosize = 8;
  case {"single" "float" "float32"}
    toclass = "single";
    tosize = 4;
    toread = toread * 4;
  case {"double" "float64"}
    toclass = "double";
    tosize = 8;
    toread = toread * 8;
  otherwise
    error ("precision not supported");
  endswitch

  switch (target)
  case "coils"
    # single bit output bits (count = 1 .. 2000)
  case "inputs"
    # single bit input regs
  case "inputregs"
    # 16 bit input read regs (count = 1 ... 125)
  case "holdingregs"
    # 16 bit read/write reg
  otherwise
    error ("Invalid target type");
  endswitch

  data = __modbus_read__(dev, target, address, count, serverid);

  # TODO: use precision to combine regs etc

endfunction