#include <stdio.h>
#include <string.h>
#include <mdslib.h>
#define EXIT_FAILURE -1
#define status_ok(status) (((status) & 1) == 1)
/定义get_signal_length函数/
int get_signal_length(const char signal)
{
/ local vars */
int dtype_long = DTYPE_LONG;
char buf[1024];
int size;
int null = 0;
int idesc = descr(&dtype_long, &size, &null);
int status;
/* init buffer */
memset(buf,0,sizeof(buf));
/* put SIZE() TDI function around signal name */
snprintf(buf,sizeof(buf)-1,“SIZE(%s)”,signal);
/* use MdsValue to get the signal length */
status = MdsValue(buf, &idesc, &null, 0);
if ( !status_ok(status) )
{
fprintf(stderr,“Unable to get length of %s.\n”,signal);
return EXIT_FAILURE;
}
/* return signal length */
return size;
}
int main(int argc, char argv[])
{
/ local vars */
int dtype_float = DTYPE_FLOAT;
int null = 0;
int shot = 107000;
int status;
int socket;
float data; / array of floats used for signal */
float timebase; / array of floats used for timebase /
int sigdesc; / signal descriptor /
int timedesc; / descriptor for timebase /
int size; / length of signal */
int i, len;
/* 连接MDSplus服务器 */
socket = MdsConnect("202.127.204.3");
if ( socket == -1 )
{
fprintf(stderr,"Error connect.\n");
return EXIT_FAILURE;
}
/*打开shot树 */
status = MdsOpen("EFIT01", &shot);
if ( !status_ok(status) )
{
fprintf(stderr,"Error opening EFIT01 tree for shot %l.\n",shot);
return EXIT_FAILURE;
}
size = get_signal_length("\\AMINOR");
if ( size < 1 )
{
/* error */
fprintf(stderr,"Error retrieving length of signal\n");
return EXIT_FAILURE;
}
/* 用malloc() 给信号分配内存 */
data = (float *)malloc(size * sizeof(float));
if ( !data )
{
fprintf(stderr,"Error allocating memory for data\n");
return EXIT_FAILURE;
}
/* 创建信号描述符 */
sigdesc = descr(&dtype_float, data, &size, &null);
/*获取信号 */
status = MdsValue("\\AMINOR",&sigdesc, &null, &len );
if ( !status_ok(status) )
{
/* error */
fprintf(stderr,"Error retrieving signal\n");
free( (void *)data );
return EXIT_FAILURE;
}
/* 使用malloc()为timebase分配内存 */
timebase = (float *)malloc(size * sizeof(float));
if ( !timebase )
{
fprintf(stderr,"Error allocating memory for timebase");
free( (void *)data );
return EXIT_FAILURE;
}
/* 创建timebase描述符 */
timedesc = descr(&dtype_float, timebase, &size, &null);
/* 获取信号的 timebase */
status = MdsValue("DIM_OF(\\AMINOR)", &timedesc, &null, 0);
if ( !status_ok(status) )
{
/* error */
fprintf(stderr,"Error retrieving timebase\n");
free( (void *)data );
free( (void *)timebase );
return EXIT_FAILURE;
}
/* 例如打印出信号中的十个数 */
for ( i = 0 ; i < size && i < 10 ; i++ )
printf("%i X:%f Y:%f\n", i, timebase[i], data[i]);
/* 释放内存*/
free( (void *)data );
free( (void *)timebase );
return 0;
}
此段代码是利用C语言读取MDSplus数据库中某一棵树某个节点的信息,里面有详细的注释,具体参见MDSplus官网http://www.mdsplus.org/index.php?title=Introduction&open=50764574999721279519&page=Introduction