【问题标题】:Understanding Perl Script for GPIB Interface了解 GPIB 接口的 Perl 脚本
【发布时间】:2017-12-08 08:21:55
【问题描述】:

我是 perl 和 python 的新手。作为我目前工作的一部分,我被要求将 perl 脚本转换为 python。该脚本的目的是自动化 magnum 测试仪和参数分析仪的任务。你们中的任何人都能够理解 get_gpib_status 函数试图做什么吗?具体问题是

  1. if(/Error/) 在 perl 中是什么意思?
  2. 这是什么意思

            chomp;
            s/\+//g;
            $value = $_;
            $foundError = 0;
    

    在 perl 中? get_gpib_status 函数的 python 等价物是什么??

    我们非常感谢任何形式的帮助。提前致谢。脚本如下所示。

    BEGIN {unshift(@INC, "." , ".." , 
    "\\Micron\\Nextest\\perl_modules");}
    
    
    use runcli;                                                                     
    # Enable input from perl script as nextest cli command, Runcli is the 
    command 
    that you’ll use to communicate with the tester
    use getHost;                                                                    
    # To latch in module/ "library into current script, here the 
    getHost.pm 
    is 
    loaded, used once on nextest system
    
    
    
    #open FILE,">","iv.txt" or die $!;                                              
    # Make file ready for reading from FILE
    
    
    $k=148;                                                                         
    # Time period T = 38ns corresponds to data value of 140
    #$i=0;
    while($k<156)
    {
    $i=3;
    while($i<4)
    {
    #$logfile = "vpasscp8stg_SS_vppmode"."stg"."$i"."freq"."$k".".txt";             
    # Give the name to the logfile
    #open(LOG,">$logfile") or die $!;                                               
    # Makes the file ready for reading from LOG
    
    
    #******************* SUBS ****************************
    
    if($i==3)
    {
    
    runcli("gpibinit;");
    runcli("gpibsend(0x1,\":PAGE:CHAN:MODE SWEEP\")");                              
    # PAGE, CHANnel, MODE, Set the mode to sweep
    
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU1:VNAME 'Vout'\")");                       
    # Source Monitor Unit, voltage name Vout
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU1:INAME 'Iout'\")");                       
    # current name Iout
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU1:MODE V\")");                             
    # voltage output node
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU1:FUNCTION VAR1\")");                      
    # function Variable
    
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU2:VNAME 'Vcc'\")");
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU2:INAME 'Icc'\")");
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU2:MODE V\")");
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU2:FUNCTION CONSTANT\")");                  
    # function constant
    
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU3:VNAME 'Vpp'\")");
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU3:INAME 'Ipp'\")");
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU3:MODE V\")");
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU3:FUNCTION CONSTANT\")");
    
    #runcli("gpibsend(0x1,\":PAGE:CHAN:SMU3:DIS\")");
    
    runcli("gpibsend(0x1,\":PAGE:CHAN:SMU4:DIS\")");
    
    runcli("gpibsend(0x1,\":PAGE:CHAN:VSU1:DIS\")");                                
    # Voltage Source Unit DISabled
    
    runcli("gpibsend(0x1,\":PAGE:CHAN:VSU2:DIS\")");
    
    runcli("gpibsend(0x1,\":PAGE:DISP:LIST:SELECT 'Vout'\")");                      
    # DISPlay LIST
    runcli("gpibsend(0x1,\":PAGE:DISP:LIST:SELECT 'Iout'\")");
    runcli("gpibsend(0x1,\":PAGE:DISP:LIST:SELECT 'Vcc'\")");
    runcli("gpibsend(0x1,\":PAGE:DISP:LIST:SELECT 'Icc'\")");
    
    
    
    
    runcli("gpibsend(0x1,\":PAGE:MEAS:SWEEP:VAR1:MODE SINGLE\")");                  
    # Single Stair Sweep
    runcli("gpibsend(0x1,\":PAGE:MEAS:SWEEP:VAR1:SPACING LINEAR\")");               
    # The sweep is incremented (decremented) by the
    # stepsize until the stop value is reached.
    
    runcli("gpibsend(0x1,\":PAGE:MEAS:SWEEP:VAR1:START 2.8\")");                    
    # Setting the sweep range of Vout
    runcli("gpibsend(0x1,\":PAGE:MEAS:SWEEP:VAR1:STOP 18\")");
    runcli("gpibsend(0x1,\":PAGE:MEAS:SWEEP:VAR1:STEP 0.1\")");
    runcli("gpibsend(0x1,\":PAGE:MEAS:SWEEP:VAR1:COMPLIANCE 0.05\")");              
    # Compliance: meaning the stable state of voltage, on the parametric 
    analyzer
    runcli("gpibsend(0x1,\":PAGE:MEAS:SWEEP:VAR1:PCOMPLIANCE:STATE 
    0\")");         
    # PCOMPLIANCE: Might be the state before the stable state
    
    runcli("gpibsend(0x1,\":PAGE:MEAS:DEL 2\")");                                   
    # Delay
    runcli("gpibsend(0x1,\":PAGE:MEAS:HTIM 50\")");                                 
    # Hold Time
    
    runcli("gpibsend(0x1,\":PAGE:MEAS:SWEEP:CONS:SMU2:SOURCE 3.3\")");              
    # Setting the values for VCC
    runcli("gpibsend(0x1,\":PAGE:MEAS:SWEEP:CONS:SMU2:COMP 0.1\")");
    runcli("gpibsend(0x1,\":PAGE:MEAS:SWEEP:CONS:SMU3:SOURCE 12\")");               
    # Setting the values for VPP
    runcli("gpibsend(0x1,\":PAGE:MEAS:SWEEP:CONS:SMU3:COMP 0.1\")");
    
    
    runcli("gpibsend(0x1,\":PAGE:SCON:SING\")");
    
    sleep(2);
    runcli("ctst");
    runcli("stst");
    sleep(2);
    runcli("pu;rs");
    runcli("B16R_vpasscp_vpp.txt()");
    runcli("regaccess(static_load,0x9,0x9,$k)");                                    
    # Using the Cregs 0x9 to modulate the frequency
    runcli("adputr(0xcf,0x03)");
    runcli("rs");
    poll_4156c();
    }
    
    
    
    
    sub poll_4156c{
    runcli("gpibsend(0x1,\":STAT:OPER:COND?\");");                                  
    # This command returns the present status of the Operation
    # Status "CONDITION" register. Reading this register does not clear 
    it.
    runcli("gpibreceive(0x1);");
    while((get_gpib_status() > 0)&&($foundError < 1) ){
    sleep(3);
    runcli("gpibsend(0x1,\":STAT:OPER:COND?\")");
    runcli("gpibreceive(0x1);");
    }
    
    }#end poll_4156c subroutine
    
    
    
    
    
    sub get_gpib_status{
    # get file info
    $host_meas = getHost();                                                         
    # Retrieve the nextest station detail, will return something like 
    mav2pt 
    - 0014
    $file_meas = $host_meas."_temp.cli";                                            
    # Define the file_meas as the nextest cli temporary file, Contains 
    all 
    the text as displayed on Nextest CLI
    open(STATUS, "$file_meas" ) || die("Can't open logfile: $!");
    print "\nSTATUS received from GPIB:";
    while(<STATUS>)
    {
    if(/Error/){
    runcli("gpibinit;");
    $foundError = 1;
    }
    else
    {
    chomp;
    s/\+//g;
    $value = $_;
    $foundError = 0;
    }
    } # End of while(<INMEAS>) loop.
    close(STATUS);
    #print "value = $value";
    return($value);
    }#End of get_gpib_status subroutine.
    
    
    $i=$i+1;
    }
    $k=$k+8;
    }
    

【问题讨论】:

  • 嗯...在 Perl 中,chomp 是一个函数,只要变量包含字符串,它就应该(取决于 PERL 的版本)从变量末尾删除换行符。它不应该对不以换行符结尾的字符串做任何事情。 Chomp 将返回被 chomped 的字符数......当您询问 gpib 状态时,您正在谈论(可能)您的程序应该与之通信以开展业务的通用接口板。你真的有很大的学习曲线才能像这样到达 PERL 的顶峰......只是说
  • @Drt "Perl", not PERL ;) Perl 是语言的名称,perl 是实际二进制文件的名称。首字母缩略词也不是。
  • @stevieb 谢谢。我说 PERL 是为了强调思考任何人将如何克服从 Perl 转换为 Python 的困难......你的 cmets 是正确的。
  • 非常感谢 t 博士和 stevieb。在您的 cmets 的帮助下,我能够将其转换为 python

标签: python python-3.x perl scripting gpib


【解决方案1】:

广告 1./Error/ 是一个正则表达式,匹配任何包含“Error”作为其子字符串的字符串;在这种情况下(if(/Error/),它被评估为布尔表达式,即。如果在$_ 变量中找到匹配项,则为真;

广告。 2. chomp 从其参数中删除尾随换行符,或者在缺少参数的情况下,从 $_ 变量中删除; s/\+//g;$_ 中的任何+ 符号替换为空(即删除它们)。您可能已经注意到,如果没有另外指定,许多 Perl 构造都在 $_ 上运行。

get_gpib_status 在您的脚本中定义 - sub get_gpib_status 是定义的开始(在 Perl 中,函数使用 sub 关键字定义,代表“子例程”)。

最后,我表示诚挚的哀悼。将程序从一种语言改写成另一种语言的任务交给之前没有使用过任何一种语言的人可能不是我听说过的最愚蠢的管理决定,但在列表中排名靠前。

【讨论】:

  • 感谢 Blotosmetek。很久以来我一直试图理解 s/\+//g 。但这帮助我弄清楚我需要在 python 中做什么
猜你喜欢
  • 1970-01-01
  • 2018-12-26
  • 2016-10-08
  • 1970-01-01
  • 2010-11-05
  • 2012-10-26
  • 1970-01-01
  • 2014-01-11
  • 2012-10-25
相关资源
最近更新 更多