02-09-2020 02:07 PM
Hello
I am developing an aplication and I need to read and write tags to a Micrologix 1400 via ethernet, I am trying to comunicate using OPC data sockets, I have already read the example installed for reading but I found none for writing and this is the part that is giving me problems.
I already had set my OPC connections with NI OPC server, I am pointing to the PLC addresses N40:0 to N40:9 for writing and N50:0 to N50:9 for reading, I can read correctly.
First problem is that when I used DS_GetDataType it it returned always 113(unknow) only for the handles that I have opened like this:
hr = DS_Open (opcURL[i], DSConst_WriteAutoUpdate, OurDSCallback,
(void*)i, &(opcHandles[i]));
hr = DS_Open (opcURL[i], DSConst_Write, OurDSCallback, (void*)i,
&(opcHandles[i]));
But if I changed the instruction to open as READ when I retrived the data type it sent 118 (USHORT) the same data type I am using for the reading part, later changed the open to this:
hr = DS_Open (opcURL[i], DSConst_ReadWriteAutoUpdate, OurDSCallback,
(void*)i, &(opcHandles[i]));
And finaly the data type is returning 118, now I try to write using this code:
hr = DS_Update (opcHandles[k]);
if (!SUCCEEDED(hr))
{
ShowDataSocketError(hr);
goto Error;
}
hr = DS_GetDataType (opcHandles[k], &type, &dim1, &dim2);
if (!SUCCEEDED(hr))
{
ShowDataSocketError(hr);
goto Error;
}
//CAVT_USHORT=118
int isArray = (type & CAVT_ARRAY);
if (type != (CAVT_USHORT))
{
continue;
}
hr = DS_SetDataValue (opcHandles[k], CAVT_USHORT, writeToOPC[i], 0, 0);
if (!SUCCEEDED(hr))
{
ShowDataSocketError(hr);
goto Error;
}
}
but it returns an error about bad data type, I am using variables unsigned short for writeToOPC array, what I assumed was the correct variable type since the code itself returned the type as USHORT
Any help is appreciated
Thanks