本文整理汇总了Python中serial.serial_for_url方法的典型用法代码示例。如果您正苦于以下问题:Python serial.serial_for_url方法的具体用法?Python serial.serial_for_url怎么用?Python serial.serial_for_url使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类serial
的用法示例。
在下文中一共展示了serial.serial_for_url方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_serial_connection
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def create_serial_connection(loop, protocol_factory, *args, **kwargs):
ser = serial.serial_for_url(*args, **kwargs)
protocol = protocol_factory()
transport = SerialTransport(loop, protocol, ser)
return (transport, protocol)
示例2: __init__
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def __init__(self, port=DEFAULT_PORT, baud=ESP_ROM_BAUD, trace_enabled=False):
"""Base constructor for ESPLoader bootloader interaction
Don't call this constructor, either instantiate ESP8266ROM
or ESP32ROM, or use ESPLoader.detect_chip().
This base class has all of the instance methods for bootloader
functionality supported across various chips & stub
loaders. Subclasses replace the functions they don't support
with ones which throw NotImplementedInROMError().
"""
if isinstance(port, basestring):
self._port = serial.serial_for_url(port)
else:
self._port = port
self._slip_reader = slip_reader(self._port, self.trace)
# setting baud rate in a separate step is a workaround for
# CH341 driver on some Linux versions (this opens at 9600 then
# sets), shouldn't matter for other platforms/drivers. See
# https://github.com/espressif/esptool/issues/44#issuecomment-107094446
self._set_port_baudrate(baud)
self._trace_enabled = trace_enabled
# set write timeout, to prevent esptool blocked at write forever.
try:
self._port.write_timeout = DEFAULT_SERIAL_WRITE_TIMEOUT
except NotImplementedError:
# no write timeout for RFC2217 ports
# need to set the property back to None or it will continue to fail
self._port.write_timeout = None
示例3: __init__
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def __init__(self, port=0, baud=ESP_ROM_BAUD):
self._port = serial.serial_for_url(port)
self._slip_reader = slip_reader(self._port)
# setting baud rate in a separate step is a workaround for
# CH341 driver on some Linux versions (this opens at 9600 then
# sets), shouldn't matter for other platforms/drivers. See
# https://github.com/espressif/esptool/issues/44#issuecomment-107094446
self._port.baudrate = baud
示例4: setUp
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def setUp(self):
self.dummy_serial = serial.serial_for_url("loop://logging=debug")
self.comm_dummy = AyabCommunication(self.dummy_serial)
示例5: _doSerialCommand
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def _doSerialCommand(self, command):
"""
Opens serial connection, sends command (multiple times if needed)
and returns the byte_response
"""
command.clearByteResponse()
response_line = None
log.debug('port %s, baudrate %s', self._serial_device, self._baud_rate)
try:
with serial.serial_for_url(self._serial_device, self._baud_rate) as s:
# Execute command multiple times, increase timeouts each time
for x in range(1, 5):
log.debug('Command execution attempt %d...', x)
s.timeout = 1 + x
s.write_timeout = 1 + x
s.flushInput()
s.flushOutput()
s.write(command.byte_command)
time.sleep(0.5 * x) # give serial port time to receive the data
response_line = s.readline()
log.debug('serial byte_response was: %s', response_line)
command.setByteResponse(response_line)
return command
except Exception as e:
log.warning("Serial read error: {}".format(e))
log.info('Command execution failed')
return command
示例6: __init__
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def __init__(self, port=DEFAULT_PORT, baud=ESP_ROM_BAUD, trace_enabled=False):
"""Base constructor for ESPLoader bootloader interaction
Don't call this constructor, either instantiate ESP8266ROM
or ESP32ROM, or use ESPLoader.detect_chip().
This base class has all of the instance methods for bootloader
functionality supported across various chips & stub
loaders. Subclasses replace the functions they don't support
with ones which throw NotImplementedInROMError().
"""
self.secure_download_mode = False # flag is set to True if esptool detects the ROM is in Secure Download Mode
if isinstance(port, basestring):
self._port = serial.serial_for_url(port)
else:
self._port = port
self._slip_reader = slip_reader(self._port, self.trace)
# setting baud rate in a separate step is a workaround for
# CH341 driver on some Linux versions (this opens at 9600 then
# sets), shouldn't matter for other platforms/drivers. See
# https://github.com/espressif/esptool/issues/44#issuecomment-107094446
self._set_port_baudrate(baud)
self._trace_enabled = trace_enabled
# set write timeout, to prevent esptool blocked at write forever.
try:
self._port.write_timeout = DEFAULT_SERIAL_WRITE_TIMEOUT
except NotImplementedError:
# no write timeout for RFC2217 ports
# need to set the property back to None or it will continue to fail
self._port.write_timeout = None
示例7: test_load_ram
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def test_load_ram(self):
""" Verify load_ram command
The "hello world" binary programs for each chip print
"Hello world!\n" to the serial port.
"""
self.run_esptool("load_ram images/helloworld-%s.bin" % chip)
p = serial.serial_for_url(serialport, default_baudrate)
p.timeout = 0.2
output = p.read(100)
print("Output: %r" % output)
self.assertIn(b"Hello world!", output)
p.close()
示例8: __init__
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def __init__(self, port, baudrate, parity, rtscts, xonxoff, echo=False, convert_outgoing=CONVERT_CRLF, repr_mode=0):
try:
self.serial = serial.serial_for_url(port, baudrate, parity=parity, rtscts=rtscts, xonxoff=xonxoff, timeout=1)
except AttributeError:
# happens when the installed pyserial is older than 2.5. use the
# Serial class directly then.
self.serial = serial.Serial(port, baudrate, parity=parity, rtscts=rtscts, xonxoff=xonxoff, timeout=1)
self.echo = echo
self.repr_mode = repr_mode
self.convert_outgoing = convert_outgoing
self.newline = NEWLINE_CONVERISON_MAP[self.convert_outgoing]
self.dtr_state = True
self.rts_state = True
self.break_state = False
示例9: __init__
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def __init__(self, port=None, baudrate=19200, parity=serial.PARITY_NONE,
reconnect_dt=10):
"""Constructor.
The client will not be connected until connect() is called.
Either manually or by the network manager.
Args:
port (str): The serial device or URL to connect to. See
the serial.serial_for_url() documentation for details.
baudrate (int): Baud rate to use in the connection.
parity: serial.PARITY value to use.
reconnect_dt (int): Time in seconds to try and reconnect if the
connection drops.
"""
# Public signals to connect to for read/write notification.
self.signal_read = Signal() # (Serial, bytes)
self.signal_wrote = Signal() # (Serial, bytes)
super().__init__()
self._port = port
self._baudrate = baudrate
self._parity = parity
self._reconnect_dt = reconnect_dt
self._fd = None
# List of packets to write. Each is a tuple of (bytes, time) where
# the time is the time after which to do the write.
self._write_buf = []
# Create the serial client but don't open it yet. We'll wait for a
# connection call to do that.
self.client = None
if port:
self._open_client()
self.signal_connected.connect(self._connected)
#-----------------------------------------------------------------------
示例10: _open_client
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def _open_client(self):
"""Create the serial client.
The connection is not actually opened yet - that happens in
connect().
"""
client = serial.serial_for_url(self._port, do_not_open=True, timeout=0,
write_timeout=0)
client.baudrate = self._baudrate
client.parity = self._parity
return client
#-----------------------------------------------------------------------
示例11: test_send_message_with_timeout
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def test_send_message_with_timeout():
""" Test if TimoutError is raised when serial port doesn't receive enough
data.
"""
s = serial_for_url('loop://', timeout=0)
# As we are using a loop, the sent request will be read back as response.
# To test timeout use a request that needs more bytes for the response.
message = read_coils(slave_id=0, starting_address=1, quantity=40)
with pytest.raises(ValueError):
send_message(message, s)
示例12: test_rtu_server_send_empty_message
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def test_rtu_server_send_empty_message(rtu_server):
rtu_server.serial_port = serial_for_url('loop://')
rtu_server.serial_port.write(b'')
with pytest.raises(ValueError):
rtu_server.serve_once()
示例13: sync_connect
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def sync_connect(transport):
"""Connect to the serial port.
This should be run in a new thread.
"""
while transport.protocol:
_LOGGER.info("Trying to connect to %s", transport.gateway.port)
try:
ser = serial.serial_for_url(
transport.gateway.port,
transport.gateway.baud,
timeout=transport.timeout,
)
except serial.SerialException:
_LOGGER.error("Unable to connect to %s", transport.gateway.port)
_LOGGER.info(
"Waiting %s secs before trying to connect again",
transport.reconnect_timeout,
)
time.sleep(transport.reconnect_timeout)
else:
serial_transport = serial.threaded.ReaderThread(
ser, lambda: transport.protocol
)
serial_transport.daemon = False
serial_transport.start()
serial_transport.connect()
return
示例14: change_port
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def change_port(self):
"""Have a conversation with the user to change the serial port"""
with self.console:
try:
port = ask_for_port()
except KeyboardInterrupt:
port = None
if port and port != self.serial.port:
# reader thread needs to be shut down
self._stop_reader()
# save settings
settings = self.serial.getSettingsDict()
try:
new_serial = serial.serial_for_url(port, do_not_open=True)
# restore settings and open
new_serial.applySettingsDict(settings)
new_serial.rts = self.serial.rts
new_serial.dtr = self.serial.dtr
new_serial.open()
new_serial.break_condition = self.serial.break_condition
except Exception as e:
sys.stderr.write('--- ERROR opening new port: {} ---\n'.format(e))
new_serial.close()
else:
self.serial.close()
self.serial = new_serial
sys.stderr.write('--- Port changed to: {} ---\n'.format(self.serial.port))
# and restart the reader thread
self._start_reader()
示例15: setUp
# 需要导入模块: import serial [as 别名]
# 或者: from serial import serial_for_url [as 别名]
def setUp(self):
self.serial_handler = SerialHandler("LOOP FOR TESTS") # device_name will not be used
self.serial_handler.serial_device = serial.serial_for_url('loop://')