Patch the code where the serial.Serial is instantiated. Replace it with:
try:
s = serial.serial_for_url(...)
except AttributeError:
s = serial.Serial(...)
Assuming the application already stores port names as strings that’s all that is required. The user just needs a way to change the port setting of your application to an rfc2217:// URL (e.g. by editing a configuration file, GUI dialog etc.).
Please note that this enables all URL types supported by pySerial and that those involving the network are unencrypted and not protected against eavesdropping.
Is the device not working as expected? Maybe it’s time to check the connection before proceeding. serial.tools.miniterm from the Examples can be used to open the serial port and do some basic tests.
To test cables, connecting RX to TX (loop back) and typing some characters in serial.tools.miniterm is a simple test. When the characters are displayed on the screen, then at least RX and TX work (they still could be swapped though).
The RTS and DTR lines are switched when the port is opened. This may cause some processing or reset on the connected device. In such a cases an immediately following call to write() may not be received by the device.
A delay after opening the port, before the first write(), is recommended in this situation. E.g. a time.sleep(1)
py2exe and similar packaging programs scan the sources for import statements and create a list of modules that they package. pySerial may create two issues with that:
serial.serial_for_url() can be used to access “virtual” serial ports identified by an URL scheme. E.g. for the RFC 2217: rfc2217:://.
Custom URL handlers can be added by extending the module search path in serial.protocol_handler_packages. This is possible starting from pySerial V2.6.
Copyright (c) 2001-2015 Chris Liechti <cliechti@gmx.net> All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.