B
    ÛùÒa9  ã               @   sR   d dl Z d dlZd dlZd dlZd dlZd dlZG dd„ deƒZG dd„ dƒZdS )é    Nc               @   s   e Zd Zdd„ Zdd„ ZdS )ÚMultimeterErrorc             C   s
   || _ d S )N)Úvalue)Úselfr   © r   ú"/home/cbp/keithley/keithley6514.pyÚ__init__   s    zMultimeterError.__init__c             C   s
   t | jƒS )N)Úreprr   )r   r   r   r   Ú__str__   s    zMultimeterError.__str__N)Ú__name__Ú
__module__Ú__qualname__r   r	   r   r   r   r   r      s   r   c               @   sŒ   e Zd Zd&dd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Z	d'dd„Z
dd„ Zdd„ Zdd„ Zdd„ Zd(dd „Zd)d"d#„Zd$d%„ ZdS )*Ú
Multimeterú
/dev/ttyS0Té€%  c             C   sœ   || _ || _d| _d| _d| _d| _d| _tj| _	tj
| _tj| _d| _d| _|| _d| _d| _ddd	d
dddœ| _t| j ¡ ƒ| _dddddddœ| _dS )z/
        Create a Multimeter instance.
        r   g      à?Núé
   é   zCURR:ACzCURR:DCzVOLT:ACzVOLT:DCZRESZFRES)z
current:acz
current:dcz
voltage:acz
voltage:dcZ
resistancezresistance:4gÍÌÌÌÌÌ@g     ¬‡@g     @@g    8œœA)ÚportÚbaudrateÚechoÚrtsctsÚxonxoffÚtimeoutZ	repr_modeÚserialZPARITY_NONEÚparityZ	EIGHTBITSÚbytesizeZSTOPBITS_ONEÚstopbitsÚserial_portÚEOLÚdebugZaction_timeoutÚcyclesÚmodesÚlistÚkeysÚmodes_namesÚranges)r   r   r   r   r   r   r   r   #   s8    
zMultimeter.__init__c          
   C   s²   | j rtd| j tjd tj| j| j| j| j	| j
| j| j| jd| _| jdksZ| j ¡ sltdd| j  ƒ‚| j ¡  |  ¡ sŒtd| j ƒ‚| j r¦td| j tjd |  ¡  dS )	za
        Open and initialize the serial port to communicate
        with the instrument.
        z!Keithley6514: Opening port %s ...)Úfile)r   r   r   r   r   r   r   r   NzKeithley6514: zFailed to open serial port %sz9Keithley6514: Multimeter is not echoing on serial port %sz#Keithley6514: Opening port %s done.)r   Úprintr   ÚsysÚstderrr   ZSerialr   r   r   r   r   r   r   r   ÚisOpenÚIOErrorÚflushOutputÚechotestÚclear)r   r   r   r   ÚopenZ   s,     



zMultimeter.openc             C   s   | j r| j  ¡ r| j  ¡  dS )z(
        Close the serial port.
        N)r   r*   Úclose)r   r   r   r   r0   €   s    
zMultimeter.closec             C   s   |   ¡  d S )N)r0   )r   r   r   r   Ú__del__Š   s    zMultimeter.__del__c             C   s$   | j stdƒ‚| j  ¡ s |  ¡  dS )z3
        Reopen the serial port if needed.
        z<Keithley6514: Multimeter serial port should be opened first.N)r   r+   r*   r/   )r   r   r   r   Úreopen_if_needed   s    
zMultimeter.reopen_if_neededc             C   s"   | j  ¡  | j  ¡  | j  ¡  dS )z@
        Purge the serial port to avoid framing errors.
        N)r   r,   Z
flushInputÚreadline)r   r   r   r   Úpurge›   s    

zMultimeter.purgec             C   s8   | j rtd| d tjd | j || j  d¡¡ dS )z9
        Send a command through the serial port.
        zKeithley6514: Sending command [ú])r&   ÚASCIIN)r   r'   r(   r)   r   Úwriter   Úencode)r   Úcommandr   r   r   r7   §   s     zMultimeter.writeNc             C   sv   | j rtdtjd |dkr:|| j_| j r:td|tjd | j ¡ }| j| j_| ¡ }| j rrtd| d tjd |S )zÜ
        Read the answer from the serial port.
        Return it as a string.

        If <timeout> is specified, the function will wait
        for data with the specified timeout (instead of the default one). 
        z(Keithley6514: Reading serial port buffer)r&   Nz!Keithley6514: Timeout specified: s   Keithley6514: Received [ó   ])r   r'   r(   r)   r   r   r3   Ústrip)r   r   Úanswerr   r   r   Úread¯   s    	 
 

 zMultimeter.readc             C   s.   |   ¡  |  ¡  |  d¡ |  ¡ }|s*dS dS )zŸ
        Verify communications with the Multimeter.
        Should return True if the communication has been established,
        and False otherwise.
        z*IDN?FT)r2   r4   r7   r=   )r   r<   r   r   r   r-   Î   s    
zMultimeter.echotestc             C   s6   |   ¡  |  ¡  | jr$tdtjd d}|  |¡ dS )z…
        Reset the instrument to the factory default settings
        (with the exception of all remote interface settings).
        zKeithley6514: Reset)r&   z*RSTN)r2   r4   r   r'   r(   r)   r7   )r   r9   r   r   r   Úresetã   s     zMultimeter.resetc             C   s6   |   ¡  |  ¡  | jr$tdtjd d}|  |¡ dS )z.
        Clear the instrument status.
        z$Keithley6514: Clear Status registers)r&   z*CLSN)r2   r4   r   r'   r(   r)   r7   )r   r9   r   r   r   r.   ñ   s     zMultimeter.clearc             C   sl  |   ¡  |  ¡  t d¡ d}|  |¡ |  ¡ }|s<tdƒ‚yt|ƒ}W n tk
rd   tdƒ‚Y nX |  	¡  |d@ r~t
dƒ‚|d@ rDd}|  |¡ |  ¡ }|sªtd	ƒ‚yt|ƒ}W n tk
rÒ   td
ƒ‚Y nX |dkrì|dkrìt
dƒ‚|dkrüt
dƒ‚|dkrt
dƒ‚|dkr t
dƒ‚|dkr2t
dƒ‚|dkrDt
dƒ‚|d@ rVt
dƒ‚|d@ rht
dƒ‚dS )zz
        Get the various error status, clear them, and may
        raise the corresponding Exception (if needed).
        r   z*ESR?z/Keithley6514: *ESR? command failed (no answer).z8Keithley6514: *ESR? command failed (invalid answer [1]).é    z¿Keithley6514: PowerSupply Command Error. A syntax type error has been detected in a command from the bus. The parser has been reset and parsing continues at the next byte in the input stream.é   zEER?z.Keithley6514: EER? command failed (no answer).z7Keithley6514: EER? command failed (invalid answer [1]).éc   zKeithley6514: Hardware error.ét   z%Keithley6514: Invalid recall of data.éu   z&Keithley6514: Corrupted internal data.éx   zAKeithley6514: Numerical specified value was too big or too small.é{   z'Keithley6514: Illegal recall requested.é|   z-Keithley6514: Illegal range change requested.é   zƒKeithley6514: Verify Timeout Error. Set when a parameter is set with 'verify' specified and the value is not reached within 5 secs.é   zKeithley6514: Query Error.N)r2   r4   ÚtimeÚsleepr7   r=   r+   ÚintÚ
ValueErrorr.   r   )r   r9   r<   ZesrZeerr   r   r   Úcheck_error_statusþ   sX    









zMultimeter.check_error_statusú
voltage:dcr   c             C   sÂ   || j krtdƒ‚|dk s"|dkr*tdƒ‚|  ¡  |  ¡  d| j|  }|  |¡ |  ¡  d| j| |f }|  |¡ |  ¡  || _|dkr’d}nt|ƒ}d	| j| |f }|  |¡ |  ¡  dS )
a  
        Instrument Setup. Choose the measurement mode 'mode' between
        the following value:
                            'current:ac'
                            'current:dc'
                            'voltage:ac'
                            'voltage:dc'
                            'resistance'
                            'resistance:4'

        'rang' is the multimeter range.

        'cycles' is number of power line frequency cycles between
        measurements : e.g. 1 => dt = 1/50Hz or 1/60Hz.
        z'Keithley6514: unknown measurement mode.g{®Gáz„?r   z.Keithley6514: <cycles> out of [0.01-10] range.z
:FUNC '%s'z:SENS:%s:NPLC %fNÚDEFz:SENS:%s:RANG %s)	r$   rL   r2   r4   r!   r7   rM   r    Ústr)r   ÚmodeÚrangr    r9   Ú	range_strr   r   r   Úsetup\  s&    



zMultimeter.setupFc             C   sÚ   |   ¡  |  ¡  |  d¡ |  d¡ |  dt|ƒ ¡ |sD|  d¡ |  d¡ |  d¡ d| jd |  }| jr‚td	|d
tjd t	 
|¡ |sš|  d¡ |  d¡ | jr¸tdtjd |  d¡}|sÎtdƒ‚|  ¡  |S )z@
        Take a measurement and get the resulting data.
        z:ABORz:INIT:CONT OFFz:TRIG:COUN z:DISP:ENAB ONz:TRAC:FEED:CONT NEXTz:INITg      $@g      I@ZWaitingÚsec)r&   z:TRAC:DATA?zReading bufferé   z,Keithley6514: Measurement: no data returned.)r2   r4   r7   rP   r    r   r'   r(   r)   rI   rJ   r=   r+   rM   )r   ÚcountÚdisplayÚwaitr<   r   r   r   Úmeasure  s.    




 


 
zMultimeter.measurec             C   sŒ   |   ¡  |  ¡  d}t|ƒ}||d  }|  d¡ |  d¡ x@t|ƒD ]4}|| }|d }|  d|||…  ¡ t d¡ qFW |  d¡ d S )Né   ú z:ABORz:DISP:TEXT:STAT 1z:DISP:TEXT:DATA '%s'g      à?z:DISP:TEXT:STAT 0)r2   r4   Úlenr7   ÚrangerI   rJ   )r   ÚmsgÚlengthÚlZmsgmsgÚiÚstartÚstopr   r   r   Úscroll_textº  s    

zMultimeter.scroll_text)r   Tr   )N)rN   Nr   )F)r
   r   r   r   r/   r0   r1   r2   r4   r7   r=   r-   r>   r.   rM   rT   rZ   re   r   r   r   r   r      s     
5&

^
3
+r   )	r(   ÚosÚos.pathrI   Údatetimer   Ú	Exceptionr   r   r   r   r   r   Ú<module>   s   