Pse,帮我理解这段代码 [英] Pse, help me to understand this code

查看:64
本文介绍了Pse,帮我理解这段代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码使用宏和标签。


我会更改它并使用

a更具可读性的功能并避免使用标签。


宏DAQmxFailed检查返回代码

各种函数:

DAQmxCreateTask()

DAQmxStartTask()

DAQmxReadAnalogScalarF64()

和其他人一个


如果这些函数返回一个int< 0表示

发生错误,因此DAQmxGetExtendedErrorInfo

显示有关错误的信息。


DAQmxFailed是另一个宏(在NIDAQmx中定义。 h):


#define DAQmxFailed(错误)((错误)< 0)

Taskhandle是unsigned long的typedef;

浮点__int64的64(MS VC ++); int32很长


提前致谢


/ *****************开始******************* /


#include< stdio.h>

#include NIDAQmx.h


#define DAQmxErrChk(functionCall){if(

DAQmxFailed(error =(functionCall))){goto Error; } $

int main(int argc,char * argv [])

{

int32 error = 0;

TaskHandle taskHandle;

float64 value;

char errBuff [2048] = {''\ 0''};


/ ******************************************* ** /

/ * / DAQmx配置代码

/ ************************ ********************* /

DAQmxErrChk(DAQmxCreateTask(""& taskHandle));

DAQmxErrChk

(DAQmxCreateAIVoltageChan(taskHandle," Dev1 / ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,""));


/ ****************************************** *** /

/ * / DAQmx启动代码

/ *********************** ********************** /

DAQmxErrChk(DAQmxStartTask(taskHandle));


/ *********************************************/ />
/ * / DAQmx阅读代码

/ ***************************** **************** /

DAQmxErrChk(DAQmxReadAnalogScalarF64(taskHandle,10.0,& value,0));


printf("获取读数:%f \ n",值);


错误:

if(DAQmxFailed(error))

DAQmxGetExtendedErrorInfo(errBuff,2048);

if(taskHandle!= 0){

/ ********************************* ************ /

/ * / DAQmx停止代码

/ ************** *********************************** /

DAQmxStopTask(taskHandle);

DAQmxClearTask(taskHandle);

}

if(DAQmxFailed(错误))

printf(" DAQmx错误:%s \\ \\ n,errBuff);

printf(程序结束,按Enter键退出\ n;)

getchar();

返回0;

}

The following code use a macro and a label.

I would to change it and use instead
a more readable function and avoid the label.

The macro DAQmxFailed checks for the return code
of the various functions:
DAQmxCreateTask()
DAQmxStartTask()
DAQmxReadAnalogScalarF64()
and others one

If these functions return a int < 0 means
that an error occured so DAQmxGetExtendedErrorInfo
displays info about error.

DAQmxFailed is another macro (defined in NIDAQmx.h):

#define DAQmxFailed(error) ((error)<0)

Taskhandle is a typedef of unsigned long;
float 64 of a __int64 (MS VC++); int32 is a long

Thanks in advance

/***************** Begin ******************* /

#include <stdio.h>
#include "NIDAQmx.h"

#define DAQmxErrChk(functionCall) { if(
DAQmxFailed(error=(functionCall)) ) { goto Error; } }

int main(int argc, char *argv[])
{
int32 error=0;
TaskHandle taskHandle;
float64 value;
char errBuff[2048]={''\0''};

/*********************************************/
/*/ DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk
(DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,""));

/*********************************************/
/*/ DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));

/*********************************************/
/*/ DAQmx Read Code
/*********************************************/
DAQmxErrChk (DAQmxReadAnalogScalarF64(taskHandle,10.0,&value,0 ));

printf("Acquired reading: %f\n",value);

Error:
if( DAQmxFailed (error) )
DAQmxGetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 ) {
/*********************************************/
/*/ DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if( DAQmxFailed (error) )
printf("DAQmx Error: %s\n",errBuff);
printf("End of program, press Enter key to quit\n");
getchar();
return 0;
}

推荐答案

__ frank__写道:
__frank__ wrote:
以下代码使用宏和标签。

我想改变它并使用
更可读的功能并避免标签。


一般来说我是反goto,但是为了处理特殊条件,

可能很有用,这就是它在这里使用的方式。我的主要抱怨是

隐藏宏中的控制语句(goto)。

宏DAQmxFailed检查各种函数的返回码
: DAQmxCreateTask()
DAQmxStartTask()
DAQmxReadAnalogScalarF64()
和其他人一样如果这些函数返回一个int< 0表示发生错误,因此DAQmxGetExtendedErrorInfo
显示有关错误的信息。

DAQmxFailed是另一个宏(在NIDAQmx.h中定义):

#定义DAQmxFailed(错误)((错误)< 0)


传统的宏都是大写的,例如

#define DAQMXFAILED(error)((错误) )< 0)


这使它脱颖而出非常重要,因为宏可以有非b / b
非显而易见的效果。

Taskhandle是一个unsigned long的typedef;
__int64的浮点数64(MS VC ++); int32很长

提前致谢

/ *****************开始******** *********** /

#include< stdio.h>
#include" NIDAQmx.h"

#define DAQmxErrChk(functionCall){if(DAQmxFailed(error =(functionCall))){goto Error; int main(int argc,char * argv [])
{
int32 error = 0;
TaskHandle taskHandle;
float64值;
char errBuff [2048] = {''\ 0''};


做{

/ *************************** ****************** /
/ * / DAQmx配置代码
/ **************** ***************************** /
DAQmxErrChk(DAQmxCreateTask("",& taskHandle));


error = QmxCreateTask(""& taskHandle);

if(error< 0)

break ;

DAQmxErrChk
(DAQmxCreateAIVoltageChan(taskHandle," Dev1 / ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,""));


错误= ......

if(错误< 0)

休息;

/ *********************************************
/ * / DAQmx启动代码
/ *********************************** ********** /
DAQmxErrChk(DAQmxStartTask(taskHandle));


error = DAQmxStartTask(taskHandle);

if(error< 0)

break;

/ ********************************************* /
/ * / DAQmx阅读代码
/ ************************************ ********* /
DAQmxErrChk(DAQmxReadAnalogScalarF64(taskHandle,10.0,& value,0));


错误= ......

if(错误< 0)

休息;

printf(获取阅读:%f \ n,值);


} while(0);

错误:


使用循环和中断的问题是添加其他控件

结构,例如其他循环和switch语句,因为休息

只会让你脱离最顶层。


另一种选择是使用两个函数,一个用于完成工作,另一个用于返回错误,第二个用于调用它并处理清理。然后

你可以做一些聪明的事情,比如


int worker(......)

{

...做的东西

if(....)

返回-1;

...做东西

if(....)

返回-2;

......

}


int控制器(......)

{

int ret = worker(......)

开关(续){

案例0:.....

案例-1:....

......

}


有时你不希望在上面的案例之间休息

开关(例如,如果它只是做清理)有时你会。


请记住,美丽是在旁观者的眼中,所以无论什么方法

你用一些人会不喜欢它。我已经使用了几种不同的方法

,具体取决于具体情况以及我认为最好的每种情况下每个b $ b。

if(b) DAQmxFailed(错误))


if(error< 0)

DAQmxGetExtendedErrorInfo(errBuff,2048);
if(taskHandle!= 0) {
/ ******************************************** * /
/ * / DAQmx停止代码
/ ********************************* ************ /
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if(DAQmxFailed(error))
printf(DAQmx错误:%s \ n,errBuff);
printf(程序结束,按Enter键退出\ n;);
getchar();
返回0;
}
The following code use a macro and a label.

I would to change it and use instead
a more readable function and avoid the label.
In general I am anti-goto, but for handling exceptional conditions it
can be useful, and that is how it is used here. My main complaint would
be hiding the control statement (goto) within the macro.
The macro DAQmxFailed checks for the return code
of the various functions:
DAQmxCreateTask()
DAQmxStartTask()
DAQmxReadAnalogScalarF64()
and others one

If these functions return a int < 0 means
that an error occured so DAQmxGetExtendedErrorInfo
displays info about error.

DAQmxFailed is another macro (defined in NIDAQmx.h):

#define DAQmxFailed(error) ((error)<0)
Traditionally macros are all upper case, e.g.
#define DAQMXFAILED(error) ((error)<0)

This makes it stand out which is important because macros can have
non-obvious effects.
Taskhandle is a typedef of unsigned long;
float 64 of a __int64 (MS VC++); int32 is a long

Thanks in advance

/***************** Begin ******************* /

#include <stdio.h>
#include "NIDAQmx.h"

#define DAQmxErrChk(functionCall) { if( DAQmxFailed(error=(functionCall)) ) { goto Error; } }

int main(int argc, char *argv[])
{
int32 error=0;
TaskHandle taskHandle;
float64 value;
char errBuff[2048]={''\0''};
do {
/*********************************************/
/*/ DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
error = QmxCreateTask("",&taskHandle);
if (error < 0)
break;
DAQmxErrChk
(DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,""));
error = ......
if (error < 0)
break;
/*********************************************/
/*/ DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));
error = DAQmxStartTask(taskHandle);
if (error < 0)
break;
/*********************************************/
/*/ DAQmx Read Code
/*********************************************/
DAQmxErrChk (DAQmxReadAnalogScalarF64(taskHandle,10.0,&value,0 ));
error = ......
if (error < 0)
break;
printf("Acquired reading: %f\n",value);
} while (0);
Error:
The problem with using a loop and break is when add in other control
structures such as other loops and switch statements since the break
only takes you out of the top most.

Another alternative is to use two functions, one which does the work and
returns on error and the second that calls it and handles clean up. Then
you can do clever things like

int worker(......)
{
... do stuff
if (....)
return -1;
... do stuff
if (....)
return -2;
......
}

int controller(......)
{
int ret = worker(......)
switch (ret) {
case 0: .....
case -1: ....
......
}

Sometimes you would not want breaks between the cases in the above
switch (e.g. if it is just doing clean up) sometimes you would.

Remember that beauty is in the eye of the beholder, so whatever method
you use some people will dislike it. I''ve used several different methods
depending on the exact circumstances and what I thought would be best in
each case.
if( DAQmxFailed (error) )
if (error < 0)
DAQmxGetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 ) {
/*********************************************/
/*/ DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if( DAQmxFailed (error) )
printf("DAQmx Error: %s\n",errBuff);
printf("End of program, press Enter key to quit\n");
getchar();
return 0;
}



-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址是垃圾邮件,但它是真的,我读了它。


--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


F lash Gordon< sp ** @ flash-gordon.me.uk>写道:
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
__frank__写道:
__frank__ wrote:
以下代码使用宏和标签。

我想改变它并使用相反,一个更具可读性的功能,避免使用标签。
一般来说,我是反goto,但是为了处理特殊情况,它可能是有用的,这就是它在这里使用的方式。我的主要抱怨是隐藏宏中的控制语句(goto)。
The following code use a macro and a label.

I would to change it and use instead
a more readable function and avoid the label.
In general I am anti-goto, but for handling exceptional conditions it
can be useful, and that is how it is used here. My main complaint would
be hiding the control statement (goto) within the macro.




我同意。因此......

做{



I agree. Therefore...
do {

/ *********************** ********************** /
/ * / DAQmx配置代码
/ ************ ********************************* /
DAQmxErrChk(DAQmxCreateTask("",& taskHandle) ));
/*********************************************/
/*/ DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));



error = QmxCreateTask("",& taskHandle);
if(error< 0)
break;



error = QmxCreateTask("",&taskHandle);
if (error < 0)
break;


printf("获取阅读:%f \ n",value);
printf("Acquired reading: %f\n",value);



} while(0);



} while (0);




....这是邪恶的,因为你仍然使用goto生效,但你已经

隐藏在一个虚假的循环结构中。

如果你_must_使用goto(有时你必须,或者至少它是

最不好的选择关于它的问题。


Richard



....this is evil, since you''re still in effect using a goto, but you''ve
hidden it inside a bogus loop construct.
If you _must_ use goto (and sometimes you must, or at least it''s the
least bad choice), be up-front about it.

Richard


2005-10-28,__ frank __< no ** *********@indirizzo.n on.valido>写道:
On 2005-10-28, __frank__ <no***********@indirizzo.non.valido> wrote:
/ *************************************** ****** /
/ * / DAQmx配置代码
/ **************************** ***************** /
/*********************************************/
/*/ DAQmx Configure Code
/*********************************************/




这是什么?你确实意识到,如果你在

中做了一个多行注释,这种风格的每一行都不会被评论,对吧?



What is this? You do realize that if you did a multiline comment in
this style every second line wouldn''t be commented, right?


这篇关于Pse,帮我理解这段代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆