modbus

modbus Commit Details

Date:2012-11-23 20:50:45 (5 years 8 months ago)
Author:Antoine Elias
Branch:master
Commit:518084d371aea2839f241d4389311e1f749705e6
Parents: ac922483557422dcc1378c537d1cb1cf794c1f79
Message:read and write functions can return 2 values

Changes:
Mmacros/modbus_readInRegsFloat.sci (2 diffs)
Mmacros/modbus_readRegsFloat.sci (2 diffs)
Mmacros/modbus_writeBit.sci (2 diffs)
Mmacros/modbus_writeReg.sci (2 diffs)
Msci_gateway/c/sci_modbus_connect.c (2 diffs)
Msci_gateway/c/sci_modbus_flush.c (2 diffs)
Msci_gateway/c/sci_modbus_readBits.c (4 diffs)
Msci_gateway/c/sci_modbus_readInBits.c (4 diffs)
Msci_gateway/c/sci_modbus_readInRegs.c (4 diffs)
Msci_gateway/c/sci_modbus_readRegs.c (4 diffs)
Msci_gateway/c/sci_modbus_setSlave.c (1 diff)
Msci_gateway/c/sci_modbus_tcpListen.c (1 diff)
Msci_gateway/c/sci_modbus_writeBits.c (4 diffs)
Msci_gateway/c/sci_modbus_writeRegs.c (4 diffs)
Mtests_dev.txt (1 diff)

File differences

macros/modbus_readInRegsFloat.sci
88
99
1010
11
11
1212
13
14
13
14
1515
1616
1717
......
2323
2424
2525
26
27
28
26
27
28
29
30
31
32
33
2934
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
//
//
function retVal = modbus_readInRegsFloat(modbusContext, address, readCount)
function varargout = modbus_readInRegsFloat(modbusContext, address, readCount)
[lhs, rhs] = argn(0);
if lhs <> 1 then
error(msprintf(gettext("%s: Wrong number of output argument(s): %d expected.\n"),"modbus_readInRegFloat", 1));
if lhs <> [1, 2] then
error(msprintf(gettext("%s: Wrong number of output argument(s): %d or %d expected.\n"),"modbus_readInRegFloat", 1, 2));
end
if rhs <> 3 then
end
//read values from network as short (16 bits)
retValInt = modbus_readInRegs(modbusContext, address, readCount);
//convert two shorts (2 * 16 bits) to float (32 bits)
retVal = modbus_getFloat(retValInt);
[retValInt, ierr] = modbus_readInRegs(modbusContext, address, readCount);
varargout(2) = ierr;
if ierr <> 0 then
varargout(1) = [];
else
//convert two shorts (2 * 16 bits) to float (32 bits)
varargout(1) = modbus_getFloat(retValInt);
end
endfunction
macros/modbus_readRegsFloat.sci
88
99
1010
11
11
1212
13
14
13
14
1515
1616
1717
......
2323
2424
2525
26
27
28
26
27
28
29
30
31
32
33
2934
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
//
//
function retVal = modbus_readRegsFloat(modbusContext, address, readCount)
function varargout = modbus_readRegsFloat(modbusContext, address, readCount)
[lhs, rhs] = argn(0);
if lhs <> 1 then
error(msprintf(gettext("%s: Wrong number of output argument(s): %d expected.\n"),"modbus_writeBit", 1));
if lhs <> [1 2] then
error(msprintf(gettext("%s: Wrong number of output argument(s): %d or %d expected.\n"),"modbus_writeBit", 1, 2));
end
if rhs <> 3 then
end
//read values from network as short (16 bits)
retValInt = modbus_readRegs(modbusContext, address, readCount);
//convert two shorts (2 * 16 bits) to float (32 bits)
retVal = modbus_getFloat(retValInt);
[retValInt, ierr] = modbus_readRegs(modbusContext, address, readCount);
varargout(2) = ierr;
if ierr <> 0 then
varargout(1) = [];
else
//convert two shorts (2 * 16 bits) to float (32 bits)
retVal = modbus_getFloat(retValInt);
end
endfunction
macros/modbus_writeBit.sci
88
99
1010
11
11
1212
13
14
13
14
1515
1616
1717
......
2222
2323
2424
25
25
26
27
28
29
30
31
2632
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
//
//
function retVal = modbus_writeBit(modbusContext, address, value)
function varargout = modbus_writeBit(modbusContext, address, value)
[lhs, rhs] = argn(0);
if lhs <> 1 then
error(msprintf(gettext("%s: Wrong number of output argument(s): %d expected.\n"),"modbus_writeBit", 1));
if lhs <> [1 2] then
error(msprintf(gettext("%s: Wrong number of output argument(s): %d or %d expected.\n"),"modbus_writeBit", 1, 2));
end
if rhs <> 3 then
error(msprintf(_("%s: Wrong value for input argument #%d: a scalar expected.\n"), "modbus_writeBit"), 3);
end
retVal = modbus_writeBits(modbusContext, address, value);
[retVal, ierr] = modbus_writeBits(modbusContext, address, value);
varargout(2) = ierr;
if ierr <> 0 then
varargout(1) = [];
else
varargout(1) = retVal;
end
endfunction
macros/modbus_writeReg.sci
1010
1111
1212
13
14
13
14
1515
1616
1717
......
2222
2323
2424
25
25
26
27
28
29
30
31
2632
//
function retVal = modbus_writeReg(modbusContext, address, value)
[lhs, rhs] = argn(0);
if lhs <> 1 then
error(msprintf(gettext("%s: Wrong number of output argument(s): %d expected.\n"),"modbus_writeReg", 1));
if lhs <> [1 2] then
error(msprintf(gettext("%s: Wrong number of output argument(s): %d or %d expected.\n"),"modbus_writeReg", 1, 2));
end
if rhs <> 3 then
error(msprintf(_("%s: Wrong value for input argument #%d: a scalar expected.\n"), "modbus_writeReg"), 3);
end
retVal = modbus_writeRegs(modbusContext, address, value);
[retVal, ierr] = modbus_writeRegs(modbusContext, address, value);
varargout(2) = ierr;
if ierr <> 0 then
varargout(1) = [];
else
varargout(1) = retVal;
end
endfunction
sci_gateway/c/sci_modbus_connect.c
2323
2424
2525
26
27
28
29
26
3027
3128
3229
3330
3431
35
32
3633
3734
3835
......
5653
5754
5855
56
5957
60
58
59
60
61
6162
62
63
6364
6465
6566
66
67
6768
69
6870
6971
72
73
7074
7175
7276
7377
78
79
80
81
82
83
84
85
86
87
88
7489
7590
76
7791
7892
7993
int sci_modbus_connect(char *fname, unsigned long fname_len)
{
SciErr sciErr;
//WARNING ALL NEW DECALRATIONS ARE HERE IF YOUR HAVE MANY FUNCTIONS
//IN THE FILE YOU HAVE PROBABLY TO MOVE DECLARATIONS IN GOOD FUNCTIONS
int errsv = 0;
modbus_t *pstrMod = NULL;
int *piAddr1 = NULL;
double dErrReturned = 0;
CheckInputArgument(pvApiCtx, 1, 1);
CheckOutputArgument(pvApiCtx, 1, 1);
CheckOutputArgument(pvApiCtx, 1, 2);
sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
if (sciErr.iErr)
if (pstrMod)
{
errno = 0;
dErrReturned = (double)modbus_connect(pstrMod);
if(dErrReturned == -1)
errsv = errno;
errno = 0;
if (dErrReturned == -1 && nbOutputArgument(pvApiCtx) != 2)
{
Scierror(999, "%s: %s", fname, modbus_strerror(errno));
Scierror(999, "%s: %s\n", fname, modbus_strerror(errsv));
return 1;
}
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dErrReturned) == 0)
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dErrReturned))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
}
else
{
Scierror(999, _("%s: Invalid pointer #%d.\n"), fname, 1);
return 1;
}
if(nbOutputArgument(pvApiCtx) == 2)
{
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
ReturnArguments(pvApiCtx);
return 0;
}
sci_gateway/c/sci_modbus_flush.c
2121
2222
2323
24
2425
2526
2627
2728
2829
29
30
3031
3132
3233
......
5051
5152
5253
53
54
55
56
57
58
59
5460
55
61
5662
5763
5864
65
66
67
68
69
70
71
72
73
74
75
76
77
78
5979
6080
6181
6282
6383
84
6485
6586
6687
int sci_modbus_flush(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int errsv = 0;
modbus_t *pstrMod = NULL;
int *piAddr1 = NULL;
CheckInputArgument(pvApiCtx, 1, 1);
CheckOutputArgument(pvApiCtx, 1, 1);
CheckOutputArgument(pvApiCtx, 1, 2);
sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
if (sciErr.iErr)
if (pstrMod)
{
if(modbus_flush(pstrMod) != 0)
int ret = 0;
errno = 0;
ret = modbus_flush(pstrMod);
errsv = errno;
errno = 0;
if(ret == -1 && nbOutputArgument(pvApiCtx) != 2)
{
Scierror(999, "%s: %s", fname, modbus_strerror(errno));
Scierror(999, "%s: %s\n", fname, modbus_strerror(errsv));
return 1;
}
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, ret))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
ReturnArguments(pvApiCtx);
}
else
{
Scierror(999, _("%s: Invalid pointer #%d.\n"), fname, 1);
return 1;
}
return 0;
sci_gateway/c/sci_modbus_readBits.c
2323
2424
2525
26
2627
2728
2829
......
3738
3839
3940
40
41
4142
4243
4344
......
111112
112113
113114
115
114116
115
117
118
119
116120
117121
118
122
119123
120124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
121146
122147
123148
......
137162
138163
139164
140
165
166
167
168
169
170
171
172
173
174
175
141176
142177
143178
int sci_modbus_readBits(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int errsv = 0;
modbus_t *pstrMod = NULL;
int *piAddr1 = NULL;
int *piAddr2 = NULL;
uint8_t *pcRead = NULL;
CheckInputArgument(pvApiCtx, 3, 3);
CheckOutputArgument(pvApiCtx, 1, 1);
CheckOutputArgument(pvApiCtx, 1, 2);
sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
if (sciErr.iErr)
int i = 0;
int rc = 0;
memset(pcRead, 0x00, iNbBits * sizeof(uint8_t));
errno = 0;
rc = modbus_read_bits(pstrMod, iAddr, iNbBits, pcRead);
if (rc != iNbBits)
errsv = errno;
errno = 0;
if (rc == 1 && nbOutputArgument(pvApiCtx) != 2)
{
FREE(pcRead);
Scierror(999, "%s: %s", fname, modbus_strerror(errno));
Scierror(999, "%s: %s\n", fname, modbus_strerror(errsv));
return 1;
}
else if(rc == -1)
{
if (createEmptyMatrix(pvApiCtx, nbInputArgument(pvApiCtx) + 1))
{
FREE(pcRead);
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
FREE(pcRead);
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
ReturnArguments(pvApiCtx);
return 0;
}
//alloc output variable
sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iNbBits, 1, &pdblOut);
FREE(pcRead);
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
ReturnArguments(pvApiCtx);
if(nbOutputArgument(pvApiCtx) == 2)
{
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
}
ReturnArguments(pvApiCtx);
}
else
{
sci_gateway/c/sci_modbus_readInBits.c
2222
2323
2424
25
2526
2627
2728
......
3637
3738
3839
39
40
4041
4142
4243
......
110111
111112
112113
114
113115
114
116
117
118
115119
116120
117
121
118122
119123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
120146
121147
122148
......
136162
137163
138164
165
166
167
168
169
170
171
172
173
174
139175
140176
141177
int sci_modbus_readInBits(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int errsv = 0;
modbus_t *pstrMod = NULL;
int *piAddr1 = NULL;
int *piAddr2 = NULL;
uint8_t *pcRead = NULL;
CheckInputArgument(pvApiCtx, 3, 3);
CheckOutputArgument(pvApiCtx, 1, 1);
CheckOutputArgument(pvApiCtx, 1, 2);
sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
if (sciErr.iErr)
int i = 0;
int rc = 0;
memset(pcRead, 0x00, iNbBits * sizeof(uint8_t));
errno = 0;
rc = modbus_read_input_bits(pstrMod, iAddr, iNbBits, pcRead);
if (rc != iNbBits)
errsv = errno;
errno = 0;
if (rc == -1 && nbOutputArgument(pvApiCtx) != 2)
{
FREE(pcRead);
Scierror(999, "%s: %s", fname, modbus_strerror(errno));
Scierror(999, "%s: %s\n", fname, modbus_strerror(errsv));
return 1;
}
else if(rc == -1)
{
if (createEmptyMatrix(pvApiCtx, nbInputArgument(pvApiCtx) + 1))
{
FREE(pcRead);
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
FREE(pcRead);
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
ReturnArguments(pvApiCtx);
return 0;
}
//alloc output variable
sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iNbBits, 1, &pdblOut);
FREE(pcRead);
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
if(nbOutputArgument(pvApiCtx) == 2)
{
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
}
ReturnArguments(pvApiCtx);
}
else
sci_gateway/c/sci_modbus_readInRegs.c
2222
2323
2424
25
2526
2627
2728
......
3536
3637
3738
38
39
3940
4041
4142
......
110111
111112
112113
114
113115
114
116
117
118
115119
116120
117
121
118122
119123
120
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
121145
122146
123147
......
134158
135159
136160
137
138161
162
163
164
165
166
167
168
169
170
171
139172
140173
141174
int sci_modbus_readInRegs(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int errsv = 0;
modbus_t *pstrMod = NULL;
int *piAddr1 = NULL;
int *piAddr2 = NULL;
uint16_t *psRead = NULL;
CheckInputArgument(pvApiCtx, 3, 3);
CheckOutputArgument(pvApiCtx, 1, 1);
CheckOutputArgument(pvApiCtx, 1, 2);
sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
if (sciErr.iErr)
int rc = 0;
double *pdblOut = NULL;
memset(psRead, 0x00, iNbBits * sizeof(uint16_t));
errno = 0;
rc = modbus_read_input_registers(pstrMod, iAddr, iNbBits, psRead);
if (rc != iNbBits)
errsv = errno;
errno = 0;
if (rc == -1 && nbOutputArgument(pvApiCtx) != 2)
{
FREE(psRead);
Scierror(999, "%s: %s", fname, modbus_strerror(errno));
Scierror(999, "%s: %s\n", fname, modbus_strerror(errsv));
return 1;
}
else if (rc == -1)
{
if (createEmptyMatrix(pvApiCtx, nbInputArgument(pvApiCtx) + 1))
{
FREE(psRead);
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
FREE(psRead);
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
ReturnArguments(pvApiCtx);
return 0;
}
//alloc output variable
sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iNbBits, 1, &pdblOut);
if (sciErr.iErr)
}
FREE(psRead);
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
if(nbOutputArgument(pvApiCtx) == 2)
{
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
}
ReturnArguments(pvApiCtx);
}
else
sci_gateway/c/sci_modbus_readRegs.c
2222
2323
2424
25
2526
2627
2728
......
3536
3637
3738
38
39
3940
4041
4142
......
110111
111112
112113
114
113115
114
116
117
118
115119
116120
117
121
118122
119123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
120145
121146
122147
......
136161
137162
138163
164
165
166
167
168
169
170
171
172
173
139174
140175
141176
int sci_modbus_readRegs(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int errsv = 0;
modbus_t *pstrMod = NULL;
int *piAddr1 = NULL;
int *piAddr2 = NULL;
uint16_t *psRead = NULL;
CheckInputArgument(pvApiCtx, 3, 3);
CheckOutputArgument(pvApiCtx, 1, 1);
CheckOutputArgument(pvApiCtx, 1, 2);
sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
if (sciErr.iErr)
int rc = 0;
double *pdblOut = NULL;
memset(psRead, 0x00, iNbBits * sizeof(uint16_t));
errno = 0;
rc = modbus_read_registers(pstrMod, iAddr, iNbBits, psRead);
if (rc != iNbBits)
errsv = errno;
errno = 0;
if (rc == -1 && nbOutputArgument(pvApiCtx) != 2)
{
FREE(psRead);
Scierror(999, "%s: %s", fname, modbus_strerror(errno));
Scierror(999, "%s: %s\n", fname, modbus_strerror(errsv));
return 1;
}
else if (rc == -1)
{
if (createEmptyMatrix(pvApiCtx, nbInputArgument(pvApiCtx) + 1))
{
FREE(psRead);
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
FREE(psRead);
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
ReturnArguments(pvApiCtx);
return 0;
}
//alloc output variable
sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iNbBits, 1, &pdblOut);
FREE(psRead);
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
if(nbOutputArgument(pvApiCtx) == 2)
{
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
}
ReturnArguments(pvApiCtx);
}
else
sci_gateway/c/sci_modbus_setSlave.c
8383
8484
8585
86
86
8787
8888
8989
iRet = modbus_set_slave(pstrMod, iSlave);
if(iRet == -1)
{
Scierror(999, "%s: %s", fname, modbus_strerror(errno));
Scierror(999, "%s: %s\n", fname, modbus_strerror(errno));
return 1;
}
sci_gateway/c/sci_modbus_tcpListen.c
8484
8585
8686
87
87
8888
8989
9090
iRet = modbus_tcp_listen(pstrMod, iNbConnection);
if(iRet == -1)
{
Scierror(999, "%s: %s", fname, modbus_strerror(errno));
Scierror(999, "%s: %s\n", fname, modbus_strerror(errno));
return 1;
}
sci_gateway/c/sci_modbus_writeBits.c
2222
2323
2424
25
2526
2627
2728
......
4243
4344
4445
45
46
4647
4748
4849
......
139140
140141
141142
143
142144
145
146
143147
144
148
145149
146
150
147151
148152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
149172
150173
151174
......
153176
154177
155178
179
180
181
182
183
184
185
186
187
188
189
156190
157191
158192
int sci_modbus_writeBits(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int errsv = 0;
modbus_t *pstrMod = NULL;
int *piAddr1 = NULL;
int *piAddr2 = NULL;
double dErrReturned = 0;
CheckInputArgument(pvApiCtx, 3, 3);
CheckOutputArgument(pvApiCtx, 1, 1);
CheckOutputArgument(pvApiCtx, 1, 2);
sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
if (sciErr.iErr)
}
}
errno = 0;
iSize = modbus_write_bits(pstrMod, iAddr, iSize3, pcVal);
errsv = errno;
errno = 0;
FREE(pcVal);
if(iSize != iSize3)
if(iSize == -1 && nbOutputArgument(pvApiCtx) != 2)
{
Scierror(999, "%s: %s", fname, modbus_strerror(errno));
Scierror(999, "%s: %s\n", fname, modbus_strerror(errsv));
return 1;
}
else if (iSize == -1)
{
if (createEmptyMatrix(pvApiCtx, nbInputArgument(pvApiCtx) + 1))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
ReturnArguments(pvApiCtx);
return 0;
}
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iSize))
{
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
if(nbOutputArgument(pvApiCtx) == 2)
{
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
}
ReturnArguments(pvApiCtx);
return 0;
}
sci_gateway/c/sci_modbus_writeRegs.c
2323
2424
2525
26
2627
2728
2829
......
4243
4344
4445
45
46
4647
4748
4849
......
117118
118119
119120
121
120122
123
124
121125
122
126
123127
124
128
125129
126130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
127150
128151
129152
......
131154
132155
133156
157
158
159
160
161
162
163
164
165
166
134167
135168
136169
int sci_modbus_writeRegs(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int errsv = 0;
modbus_t *pstrMod = NULL;
int *piAddr1 = NULL;
int *piAddr2 = NULL;
double dErrReturned = 0;
CheckInputArgument(pvApiCtx, 3, 3);
CheckOutputArgument(pvApiCtx, 1, 1);
CheckOutputArgument(pvApiCtx, 1, 2);
sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
if (sciErr.iErr)
psVal[i] = (uint16_t)pdblVal[i];
}
errno = 0;
iSize = modbus_write_registers(pstrMod, iAddr, iSize3, psVal);
errsv = errno;
errno = 0;
FREE(psVal);
if(iSize != iSize3)
if(iSize == -1 && nbOutputArgument(pvApiCtx) != 2)
{
Scierror(999, "%s: %s(%d -> %d)\n", fname, modbus_strerror(errno), iSize3, iSize);
Scierror(999, "%s: %s(%d -> %d)\n", fname, modbus_strerror(errsv), iSize3, iSize);
return 1;
}
else if (iSize == -1)
{
if (createEmptyMatrix(pvApiCtx, nbInputArgument(pvApiCtx) + 1))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
ReturnArguments(pvApiCtx);
return 0;
}
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, (double)iSize))
{
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
if(nbOutputArgument(pvApiCtx) == 2)
{
if (createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, errsv))
{
Scierror(999, _("%s: Memory error.\n"), fname);
return 1;
}
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
}
ReturnArguments(pvApiCtx);
return 0;
}
tests_dev.txt
11
22
33
4
4
55
66
77
88
99
10
11
12
10
1311
1412
1513
1614
1715
16
1817
1918
20
19
2120
2221
2322
2423
2524
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
5098
5199
52100
53101
54102
55103
56
57
58
59
104
105
106
107
// install modbus IP emulator
// http://www.soft-control.fr/downloads.php?id=20
setenv('DEBUG_SCILAB_DYNAMIC_LINK','YES');
//setenv('DEBUG_SCILAB_DYNAMIC_LINK','YES');
exec d:\git\toolboxes\modbus\cleaner.sce;
exec d:\git\toolboxes\modbus\builder.sce;
exec d:\git\toolboxes\modbus\loader.sce;
function checkVal(val, ref)
if val == ref then
disp("OK");
else
if val <> ref then
disp("NOK")
end
endfunction
//init object
pstrMod = modbus_newTcp("127.0.0.1", 502);
//create connection
modbus_connect(pstrMod);
[status, ierr] = modbus_connect(pstrMod);
//////////////////////////////////////
// communication with master only //
//////////////////////////////////////
//write 128 1-bits @0
modbus_writeBits(pstrMod, 0, ones(128, 1));
//read 128 bits from @0
val = modbus_readBits(pstrMod, 0, 128);
//check values
checkVal(val, 1);
//write 128 0-bits @0
modbus_writeBits(pstrMod, 0, zeros(128, 1));
//read 128 bits from @0
val = modbus_readBits(pstrMod, 0, 128);
//check values
checkVal(val, 0);
//write 64 values @2 of holding registers
modbus_writeRegs(pstrMod, 2, 1:64);
//read 64 values @2 from holding registers
val = modbus_readRegs(pstrMod, 2, 64);
checkVal([1:64]', val);
//read 16 input digital values from @0
val = modbus_readInRegs(pstrMod, 0, 16)
//read 16 input analogue values from @0
val = modbus_readInBits(pstrMod, 0, 16)
iFailed = 0;
iPassed = 0;
while(%t)
//write 128 1-bits @0
[val, ierr] = modbus_writeBits(pstrMod, 0, ones(128, 1));
if ierr <> 0 then
printf("error modbus_writeBits\n");
iFailed = iFailed + 1;
end
//read 128 bits from @0
[val, ierr] = modbus_readBits(pstrMod, 0, 128);
if ierr <> 0 then
printf("error modbus_readBits\n");
iFailed = iFailed + 1;
end
//check values
checkVal(val, 1);
//write 128 0-bits @0
[val, ierr] = modbus_writeBits(pstrMod, 0, zeros(128, 1));
if ierr <> 0 then
printf("error modbus_readBits\n");
iFailed = iFailed + 1;
end
//read 128 bits from @0
[val, ierr] = modbus_readBits(pstrMod, 0, 128);
if ierr <> 0 then
printf("error modbus_readBits\n");
iFailed = iFailed + 1;
end
//check values
checkVal(val, 0);
//write 64 values @2 of holding registers
[val, ierr] = modbus_writeRegs(pstrMod, 2, 1:64);
if ierr <> 0 then
printf("error modbus_readBits\n");
iFailed = iFailed + 1;
end
//read 64 values @2 from holding registers
[val, ierr] = modbus_readRegs(pstrMod, 2, 64);
if ierr <> 0 then
printf("error modbus_readBits\n");
iFailed = iFailed + 1;
end
checkVal([1:64]', val);
//read 16 input digital values from @0
[val, ierr] = modbus_readInRegs(pstrMod, 0, 16);
if ierr <> 0 then
printf("error modbus_readBits\n");
iFailed = iFailed + 1;
end
//read 16 input analogue values from @0
[val, ierr] = modbus_readInBits(pstrMod, 0, 16);
if ierr <> 0 then
printf("error modbus_readBits\n");
iFailed = iFailed + 1;
end
if iFailed > 10 then
printf("too many errors\n");
break;
end
iPassed = iPassed + 1;
if iPassed > 10 then
printf("Sounds good!\n");
break;
end
end
modbus_close(pstrMod)
modbus_free(pstrMod)
// test modbus_set_slave
pstrMod = modbus_new_tcp("127.0.0.1", 502);
modbus_set_debug(pstrMod,%t)
modbus_connect(pstrMod)
modbus_set_slave(pstrMod,2)
//pstrMod = modbus_new_tcp("127.0.0.1", 502);
//modbus_set_debug(pstrMod,%t)
//modbus_connect(pstrMod)
//modbus_set_slave(pstrMod,2)

Archive Download the corresponding diff file

Branches