3
M]W @ s@ d dl T dd Zdd ZeddgZdd ZG d
d deZdS )
)*c C s6 t | }| jd}x|dd D ]}t||}q W |S )N. )
__import__splitgetattr)namemodZ
componentscomp r /usr/lib/python3.6/serialjava.py my_import
s
r
c
C sF x8| D ]0}yt |}|j |S ttfk
r4 Y qX qW tddS )z1try given list of modules and return that importsz/No Java Communications API implementation foundN)r
SerialPortImportErrorAttributeError)namesr r r r r detect_java_comm s
r z
javax.commzgnu.ioc C sJ t jj }g }x.|j r<|j }|j t jjkr|j| qW || j S )z%Turn a port number into a device name) commCommPortIdentifierZgetPortIdentifiersZhasMoreElementsZnextElementZgetPortTypeZPORT_SERIALappendZgetName)Z
portnumberenumZportsZelr r r device* s
r c @ s e Zd ZdZdd Zdd Zdd Zedd Zd%ddZ d
d Z
dd Zdd Zd&ddZ
dd Zdd Zdd Zedd Zedd Zed d! Zed"d# Zd$S )'Serialz Serial port class, implemented with Java Communications API and
thus usable with jython and the appropriate java extension.
c C s | j dkrtd| jr tdt| j tdkrBtjj| j }ntjjt| j }y|jdd| _ W n4 t
k
r } zd| _ td| W Y dd}~X nX | j | j j | _
| j j | _d| _dS ) zx Open port with current settings. This may throw a SerialException
if the port cannot be opened.
Nz.Port must be configured before it can be used.zPort is already open. zpython serial module
zCould not open port: %sT)Z_portSerialExceptionis_opentyper r ZgetPortIdentifierr opensPort Exception_reconfigurePortZgetInputStream _instreamZgetOutputStream
_outstream)selfZportIdmsgr r r r ; s
zSerial.openc C s | j std| j jd | jtkr.tjj}nJ| jtkrBtjj }n6| jt
krVtjj}n"| jtkrjtjj
}ntd| j | jtkrtjj}n6| jtkrtjj}n"| jtkrtjj}ntd| j | jtkrtjj}nd| jtkrtjj}nP| jtkr tjj}n:| jtkrtjj}n$| jtkr,tjj}ntd| j d }}| jrb|tjjO }|tjjO }| jr|tjj O }|tjj!O }| j j"| j#||| | j j$||B | j%dkr| j jt&| j%d n
| j j' dS ) z,Set communication parameters on opened port.z'Can only operate on a valid port handle zunsupported bytesize: %rz"unsupported number of stopbits: %rzunsupported parity type: %rr i N)(r r ZenableReceiveTimeoutZ _bytesizeZFIVEBITSr r Z
DATABITS_5ZSIXBITSZ
DATABITS_6Z SEVENBITSZ
DATABITS_7Z EIGHTBITSZ
DATABITS_8
ValueErrorZ _stopbitsZSTOPBITS_ONEZ
STOPBITS_1ZSTOPBITS_ONE_POINT_FIVEZSTOPBITS_1_5ZSTOPBITS_TWOZ
STOPBITS_2Z_parityZPARITY_NONEZPARITY_EVENZ
PARITY_ODDZPARITY_MARKZPARITY_SPACEZ_rtsctsZFLOWCONTROL_RTSCTS_INZFLOWCONTROL_RTSCTS_OUTZ_xonxoffZFLOWCONTROL_XONXOFF_INZFLOWCONTROL_XONXOFF_OUTZsetSerialPortParamsZ _baudrateZsetFlowControlModeZ_timeoutintZdisableReceiveTimeout)r$ Z jdatabitsZ jstopbitsZjparityZjflowinZjflowoutr r r r! R sT
zSerial._reconfigurePortc C s: | j r6| jr0| jj | jj | jj d| _d| _ dS )z
Close portNF)r r r" closer# )r$ r r r r) s
zSerial.closec C s | j s
t| jj S )z>Return the number of characters currently in the input buffer.)r portNotOpenErrorr" available)r$ r r r
in_waiting s zSerial.in_waitingr c C s\ | j s
tt }|dkrTx:t||k rR| jj }|dkrF| jdkrPP q|j| qW t|S )z Read size bytes from the serial port. If a timeout is set it may
return less characters as requested. With no timeout it will block
until the requested number of bytes is read.
r r ) r r* bytearraylenr" readZtimeoutr bytes)r$ sizer0 xr r r r0 s
zSerial.readc C s@ | j s
tt|ttfs,tdtt|f | jj| t |S )z-Output the given string over the serial port.z expected %s or bytearray, got %s)
r r*
isinstancer1 r. TypeErrorr r# writer/ )r$ datar r r r6 s zSerial.writec C s | j s
t| jj| jj dS )z9Clear input buffer, discarding all that is in the buffer.N)r r* r" skipr+ )r$ r r r reset_input_buffer s zSerial.reset_input_bufferc C s | j s
t| jj dS )zs Clear output buffer, aborting the current output and
discarding all that is in the buffer.
N)r r* r# flush)r$ r r r reset_output_buffer s zSerial.reset_output_buffer ?c C s | j s
t| j j|d dS )zHSend break condition. Timed, returns to idle state after given duration.g @@N)r r* Z sendBreak)r$ Zdurationr r r
send_break s zSerial.send_breakc C s | j dkrttddS )zBSet break: Controls TXD. When active, to transmitting is possible.Nz