Хочу немного рассказать о тонкостях работы с функцией DS_GetDataType, нобходимой для работы с OPC в CVI.
Эта функция возвращаят тип данных для выбранного OPC тега. Например:
CAVT_SHORT Contains a short
CAVT_LONG Contains a long
CAVT_INT Contains an int (same as CAVT_LONG
CAVT_FLOAT Contains a float
CAVT_DOUBLE Contains a double
Подробности см. в help к CVI.
Эти поименованные константы определены в перичесляемом типе (enum) в заголовочном файле cviauto.h
Тонкость проявляет себя следующим образом. Предположим существует на OPC сервере тег Tag1 типа DOUBLE. Который считывается OPC сервером с контроллера (ПЛК). Когда связь с ПЛК стабильна, возвращаемый функцией DS_GetDataType тип CAVT_DOUBLE. Все логично, но когда связь с ПЛК нарушается DS_GetDataType возвращает CAVT_DOUBLE при продолжающемся (DS_Open) подключении к OPC тегу, и CAVT_LONG при новом (DS_Open) подключении к OPC тегу. Т.е. тип данных начинает не соотвтетствовать действительному. Удалось найти следующие рекомендации по этому поводу: http://digital.ni.com/public.nsf/allkb/ ... 20006B9455
Тут говорится о том, что при открыти тега на чтение (read), пока передаются данные, тип будет определяться корректно, в отличи от открытия тега на запись. Похоже, что при нарушении связи с ПЛК и изменение статуса тега на сервере срабатывает тот же механизм. Вывод: Если Вам требуется использовать функцию DS_GetDataType, проверяйте статус тега с помощью DS_GetStatus.Why Does DataSocket Report Different Data Types in Read Mode Than in Write Mode?
Primary Software: LabWindows/CVI Development Systems>>Full Development System
Primary Software Version: 5.5
Primary Software Fixed Version: N/A
Secondary Software: Other NI Software>>DataSocket
Problem: I am using DataSocket to connect to an OPC item. If I call DS_GetDataType function to open an item in Read mode, then the reported datatype is DOUBLE, which is correct; however, if I open the item in Write mode, then the datatype is incorrectly reported as LONG.
Solution: There are different scenarios for opening an item in Read and Write modes. If an item is opened in Read mode, then DataSocket receives data. When it receives data it also gets information on how to interpret it, in terms of data type. You can also call the DS_GetDataType function to get the actual data type.
In Write mode DataSocket does not receive any data to derive the data type. In this case DS_GetDataType returns the "default" LONG data type. This does not mean that you cannot write a value with a different data type to this item. If you know that the actual data type is DOUBLE, then you can go ahead and write a DOUBLE value. For example, if "opc:/Server1/Item1" is opened in Write mode, then DS_GetDataType gives you back LONG, but you can still send a DOUBLE value to the server:
hr = DS_SetDataValue (opcHandle, CAVT_DOUBLE, &NewVal, 0, 0);
Generally, you should not rely on the data type returned by DS_GetDataType, if the item is opened in Write mode. Either use the data type that the DS_GetDataType function returns in Read mode, or refer to the server documentation to determine the actual data type of the item.
Related Links:
Attachments:
Report Date: 18.07.2000
Last Updated: 28.05.2004
Document ID: 1ZHDIHJ6