H3E9e

Python and Arduino. Serial Communication
========================================

Prerequisites (Windows):

  Arduino 1.8.1 installed
  
Arduino side
------------

Arduino UNO: Z,ENTER -- Turns LED 13 (BUILD_IN) ON
             A,ENTER -- Turns LED 13 (BUILD_IN) OFF

  void setup() {
    Serial.begin( 9600);
    pinMode( 13, OUTPUT);
    // wait for serial port to connect. 
    // Needed for native USB port only.
    while ( !Serial); 
  }

  void loop() {
    if ( Serial.available() > 0) {
      int inByte = Serial.read();
      if ( inByte == 'Z') digitalWrite( 13, HIGH);
      if ( inByte == 'A') digitalWrite( 13, LOW);
    }
  }
  
Two Pythons (2.7.13) + Lubuntu 17.04 VM
---------------------------------------

  # apt-get install python python-visual idle-python2.7 putty
  
  $ cat ~/Desktop/idle.desktop
  [Desktop Entry]
  Type=Application
  Name=IDLE Python 2.7
  Exec=/usr/bin/idle-python2.7
  Icon=/usr/lib/python2.7/idlelib/Icons/idle_48.png
  Terminal=false
  
Set IDLE options::font size 14

Install pip
  
  # apt-get install pip
  $ pip install pyserial
  
Sender ~/send.py:

  import serial
  import time

  ser = serial.Serial('/dev/tnt0',9600)

  time.sleep(2)
  while True:
    ser.write('Z')
    time.sleep(1)
    ser.write('A')
    time.sleep(1)
  ser.close()

Receiver:

  import serial
  import time

  ser = serial.Serial('/dev/tnt1',9600)

  o = ''
  while True:
      if ser.in_waiting :
          o = ser.read()
          print( o)
          
Visual receiver:
----------------

  from visual import *
  import serial
  import time

  def zoom( s):
      for k in [1,-1]:
          for i in range(0,10):
              s.radius = (1.0 if k == 1 else 1.9) + k*i/10.0
              time.sleep(0.1)

  def main():
      color.pink = (0.95, 0.72, 0.8)
      ser = serial.Serial('/dev/tnt0',9600)
      s = sphere( color = (0.3,0.3,0.3))
      b = box( color = (0,0,0), pos=(0,0,-1.1), size=(3,3,0.1))
      o = ''

      # black, white, red, green, blue, 
      # yellow, cyan, magenta, orange, (pink) 

      while True:
          if ser.in_waiting :
              o = ser.read()
              if o == 'w' : s.color = color.white; zoom( s)
              if o == 'r' : s.color = color.red; zoom( s)
              if o == 'g' : s.color = color.green; zoom( s)
              if o == 'b' : s.color = color.blue; zoom( s)
              if o == 'y' : s.color = color.yellow; zoom( s)
              if o == 'c' : s.color = color.cyan; zoom( s)
              if o == 'm' : s.color = color.magenta; zoom( s)
              if o == 'o' : s.color = color.orange; zoom( s)
              if o == 'p' : s.color = color.pink; zoom( s)

  main()

Serial Virtulization: tty0tty
-----------------------------

  $ git clone https://github.com/freemed/tty0tty
  $ cd tty0tty-1.2/module
  $ make
  $ sudo cp tty0tty.ko /lib/modules/$(uname -r)/kernel/drivers/misc/
  $ sudo depmod
  $ sudo modprobe tty0tty
  $ ls -l /dev/tnt*
  $ sudo chmod 666 /dev/tnt*

Persisting across boot:

  $ sudo cat /etc/sudoers | grep NOPASSWD
  %admin  ALL=(ALL) NOPASSWD:ALL 
  %sudo   ALL=(ALL) NOPASSWD:ALL
  $ cat .bashrc | tail -n 1
  sudo chmod 666 /dev/tnt*
  $ cat /etc/modules | tail -n 1
  tty0tty

Note: Rebuild and reinstall tty0tty on kernel update.
 
3. Test connection as user (virtu)

   Terminal 1: cat /dev/tnt0
   Terminal 2: echo "hello from /dev/tnt1" > /dev/tnt1
   
   Terminal 1: cat /dev/tnt1
   Terminal 2: echo "hello from /dev/tnt0" > /dev/tnt0

Note: to stop cat press: ctrl + c

Links on serial virtulization
-----------------------------

1. Linux: tty0tty

  https://sourceforge.net/projects/tty0tty/
  
2. http://stackoverflow.com/questions/36960920
  /how-to-send-data-to-tty0tty-null-modem-emulator-port-opened-in-qt
  
3. http://askubuntu.com/questions/588800
  /setup-virtual-serial-ports-linux-null-modem-emulator-using-tty0tty
  
4. Windows: com0com (Vyacheslav Frolov)

  http://com0com.sourceforge.net/

Python 3.x on Raspbian (with Pixel) Raspberry Pi
------------------------------------------------

Raspbian (with Pixel)

  python3 -m pip install pyserial
  Requirement already satisfied (use --upgrade to upgrade): pyserial in /usr/lib/python3/dist-packages
  Cleaning up...
  
sudo python3 -m pip install --upgrade pyserial
Downloading/unpacking pyserial from https://pypi.python.org/packages/50/a0/4159b8050dc4804f8e863b21bb0ad1bd02c4d35ad9954ee0a8c2e2c2c9a2/pyserial-3.3-py2.py3-none-any.whl#md5=d37e07020d54bf44318117b959bed13a
  Downloading pyserial-3.3-py2.py3-none-any.whl (189kB): 189kB downloaded
Installing collected packages: pyserial
  Found existing installation: pyserial 2.6
    Not uninstalling pyserial at /usr/lib/python3/dist-packages, owned by OS
Successfully installed pyserial
Cleaning up...

Test.py

  import serial
  import time

  # The default is 8 data bits, no parity, one stop bit. 
  # (https://www.arduino.cc/en/serial/begin)

  ser = serial.Serial('COM6',9600)
  
  # First read http://www.akeric.com/blog/?p=1140 now sleep :)
  
  time.sleep(2)
  while True:
      ser.write('Z')
      time.sleep(1)
      ser.write('A')
      time.sleep(1)
  ser.close()

Install Thonny on Lubuntu so it doesn't break pip
-------------------------------------------------

1. Download thonny-2.0.7.sh as a part of installation process for Linux.

2. chmod +x thonny-2.0.7.sh

3. $ sudo -s
   # ./thonny-2.0.7.sh

4. # ./apps/thonny/bin/thonny
   >>> import pip
   >>> pip.main(['install','pyserial'])
   
5. Start -> Programming -> Thonny
   >>> import pip
   >>> pip.main(['list'])
   ..
   pip (9.0.1)
   pyserial (3.3)
   ..

What should i know about pip?
-----------------------------

  Help

    pip help
    
  Search for the package

    pip search <packagename>

  Install/Upgrade the package

    pip install --upgrade <packagename>
    
  Remove and list removed files

    pip uninstall <packagename>
    
  What is installed

    pip list

  Package information

    pip show <packagename>

Examples

  >>> import pip
  >>> pip.main(['install','pyserial'])
  Collecting pyserial
    Using cached pyserial-3.3-py2.py3-none-any.whl
  Installing collected packages: pyserial
  Successfully installed pyserial-3.3
  0

  >>> pip.main(['list'])
  beautifulsoup4 (4.5.3)
  distro (1.0.1)
  jedi (0.9.0)
  pip (9.0.1)
  pyserial (3.3)
  requests (2.12.4)
  setuptools (20.10.1)
  thonny (2.0.7)
  tkinterhtml (0.7)
  0

  >>> pip.main(['show','pyserial'])
  Name: pyserial
  Version: 3.3
  Summary: Python Serial Port Extension
  Home-page: https://github.com/pyserial/pyserial
  Author: Chris Liechti
  Author-email: cliechti@gmx.net
  License: BSD
  Location: c:\users\kiril\appdata\local\programs\thonny\lib\site-packages
  Requires: 
  0

  >>> pip.main(['uninstall','pyserial'])
  Uninstalling pyserial-3.3:
    lib\site-packages\pyserial-3.3.dist-info\description.rst
    lib\site-packages\pyserial-3.3.dist-info\installer
    lib\site-packages\pyserial-3.3.dist-info\metadata
    lib\site-packages\pyserial-3.3.dist-info\metadata.json
    lib\site-packages\pyserial-3.3.dist-info\record
    lib\site-packages\pyserial-3.3.dist-info\top_level.txt
    lib\site-packages\pyserial-3.3.dist-info\wheel
    lib\site-packages\serial\__init__.py
    lib\site-packages\serial\__init__.pyc
    lib\site-packages\serial\__pycache__\__init__.cpython-35.pyc
    lib\site-packages\serial\__pycache__\aio.cpython-35.pyc
    lib\site-packages\serial\__pycache__\rfc2217.cpython-35.pyc
    lib\site-packages\serial\__pycache__\rs485.cpython-35.pyc
    lib\site-packages\serial\__pycache__\serialcli.cpython-35.pyc
    lib\site-packages\serial\__pycache__\serialjava.cpython-35.pyc
    lib\site-packages\serial\__pycache__\serialposix.cpython-35.pyc
    lib\site-packages\serial\__pycache__\serialutil.cpython-35.pyc
    lib\site-packages\serial\__pycache__\serialwin32.cpython-35.pyc
    lib\site-packages\serial\__pycache__\win32.cpython-35.pyc
    lib\site-packages\serial\aio.py
    lib\site-packages\serial\rfc2217.py
    lib\site-packages\serial\rfc2217.pyc
    lib\site-packages\serial\rs485.py
    lib\site-packages\serial\rs485.pyc
    lib\site-packages\serial\serialcli.py
    lib\site-packages\serial\serialcli.pyc
    lib\site-packages\serial\serialjava.py
    lib\site-packages\serial\serialjava.pyc
    lib\site-packages\serial\serialposix.py
    lib\site-packages\serial\serialposix.pyc
    lib\site-packages\serial\serialutil.py
    lib\site-packages\serial\serialutil.pyc
    lib\site-packages\serial\serialwin32.py
    lib\site-packages\serial\serialwin32.pyc
    lib\site-packages\serial\threaded\__init__.py
    lib\site-packages\serial\threaded\__init__.pyc
    lib\site-packages\serial\threaded\__pycache__\__init__.cpython-35.pyc
    lib\site-packages\serial\tools\__init__.py
    lib\site-packages\serial\tools\__init__.pyc
    lib\site-packages\serial\tools\__pycache__\__init__.cpython-35.pyc
    lib\site-packages\serial\tools\__pycache__\hexlify_codec.cpython-35.pyc
    lib\site-packages\serial\tools\__pycache__\list_ports.cpython-35.pyc
    lib\site-packages\serial\tools\__pycache__\list_ports_common.cpython-35.pyc
    lib\site-packages\serial\tools\__pycache__\list_ports_linux.cpython-35.pyc
    lib\site-packages\serial\tools\__pycache__\list_ports_osx.cpython-35.pyc
    lib\site-packages\serial\tools\__pycache__\list_ports_posix.cpython-35.pyc
    lib\site-packages\serial\tools\__pycache__\list_ports_windows.cpython-35.pyc
    lib\site-packages\serial\tools\__pycache__\miniterm.cpython-35.pyc
    lib\site-packages\serial\tools\hexlify_codec.py
    lib\site-packages\serial\tools\hexlify_codec.pyc
    lib\site-packages\serial\tools\list_ports.py
    lib\site-packages\serial\tools\list_ports.pyc
    lib\site-packages\serial\tools\list_ports_common.py
    lib\site-packages\serial\tools\list_ports_common.pyc
    lib\site-packages\serial\tools\list_ports_linux.py
    lib\site-packages\serial\tools\list_ports_linux.pyc
    lib\site-packages\serial\tools\list_ports_osx.py
    lib\site-packages\serial\tools\list_ports_osx.pyc
    lib\site-packages\serial\tools\list_ports_posix.py
    lib\site-packages\serial\tools\list_ports_posix.pyc
    lib\site-packages\serial\tools\list_ports_windows.py
    lib\site-packages\serial\tools\list_ports_windows.pyc
    lib\site-packages\serial\tools\miniterm.py
    lib\site-packages\serial\tools\miniterm.pyc
    lib\site-packages\serial\urlhandler\__init__.py
    lib\site-packages\serial\urlhandler\__init__.pyc
    lib\site-packages\serial\urlhandler\__pycache__\__init__.cpython-35.pyc
    lib\site-packages\serial\urlhandler\__pycache__\protocol_alt.cpython-35.pyc
    lib\site-packages\serial\urlhandler\__pycache__\protocol_hwgrep.cpython-35.pyc
    lib\site-packages\serial\urlhandler\__pycache__\protocol_loop.cpython-35.pyc
    lib\site-packages\serial\urlhandler\__pycache__\protocol_rfc2217.cpython-35.pyc
    lib\site-packages\serial\urlhandler\__pycache__\protocol_serve-rfc2217.cpython-35.pyc
    lib\site-packages\serial\urlhandler\__pycache__\protocol_socket.cpython-35.pyc
    lib\site-packages\serial\urlhandler\__pycache__\protocol_spy.cpython-35.pyc
    lib\site-packages\serial\urlhandler\protocol_alt.py
    lib\site-packages\serial\urlhandler\protocol_alt.pyc
    lib\site-packages\serial\urlhandler\protocol_hwgrep.py
    lib\site-packages\serial\urlhandler\protocol_hwgrep.pyc
    lib\site-packages\serial\urlhandler\protocol_loop.py
    lib\site-packages\serial\urlhandler\protocol_loop.pyc
    lib\site-packages\serial\urlhandler\protocol_rfc2217.py
    lib\site-packages\serial\urlhandler\protocol_rfc2217.pyc
    lib\site-packages\serial\urlhandler\protocol_serve-rfc2217.py
    lib\site-packages\serial\urlhandler\protocol_socket.py
    lib\site-packages\serial\urlhandler\protocol_socket.pyc
    lib\site-packages\serial\urlhandler\protocol_spy.py
    lib\site-packages\serial\urlhandler\protocol_spy.pyc
    lib\site-packages\serial\win32.py
    lib\site-packages\serial\win32.pyc
    scripts\__pycache__\miniterm.cpython-35.pyc
    scripts\miniterm.py
  Proceed (y/n)? y
    Successfully uninstalled pyserial-3.3
  0

Annex A. $ pip --help
---------------------

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring
                              environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be
                              used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be
                              used up to 3 times (corresponding to WARNING,
                              ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form
                              [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should
                              attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists:
                              (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
  --trusted-host <hostname>   Mark this host as trusted, even though it does
                              not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file
                              containing the private key and the certificate
                              in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine
                              whether a new version of pip is available for
                              download. Implied with --no-index.

