Difference between revisions of "Lantronix Python Programmers Guide"

From Lantronix Wiki!
Jump to navigation Jump to search
m
m
Line 35: Line 35:
 
=== First Program "Hello World" ===
 
=== First Program "Hello World" ===
 
==== Write Simple Python Program ====
 
==== Write Simple Python Program ====
In your favorite text editor, create a file named helloworld.py, with the following contents:
+
In your favorite text editor, create a file named ''''helloworld.py'''', with the following contents:
  
 
  <nowiki>
 
  <nowiki>
Line 45: Line 45:
  
 
==== Transfer Program Onto Lantronix Device ====
 
==== Transfer Program Onto Lantronix Device ====
 +
# Log in to the Web UI for Lantronix Device. Enter the configured username and password. Default values are '''admin''' and '''PASS'''.
 +
# Using the '''Filesystem''' menu option, go to the '''Browse''' sub-menu and upload the '''''helloworld.py''''' file to the device.
 +
 +
<br>
 +
[[File:File_transfer_getting_started.jpg]]
  
 
==== Run The Program ====
 
==== Run The Program ====
 +
# Navigate to the '''Applications''' menu option in the Web UI.
 +
# For script 1, enter the script name as '''''helloworld.py''''' and mark the '''Enable''' checkbox to enable this script.
 +
# Enter the output filename as '''''helloworld.out'''''. This file will be created in the filesystem to capture all ''stdout'' output from the script.
 +
# Hit the '''Run''' button for Script 1 to run the transferred program.
 +
<br>
 +
[[File:Python_application_getting_started.jpg]]
 +
 +
 +
Open the output file '''''helloworld.out''''' from the '''Filesystem -> Browse''' Menu to check the expected '''Hello World''' output from the script.
 +
 +
=== System Requirements ===
 +
Integrated Python runtime is available from software version 7.9.0.0 onwards on the supported Lantronix products.<br>
 +
If you have products with software version older than 7.9.0.0, please follow the upgrade instructions for the respective products. The software and the upgrade notes are available from the Lantronix support site at www.lantronix.com/support/downloads.
 +
 +
=== Recommended Python Version ===
 +
The version of Python supported on Lantronix PremierWave family of Intelligent Gateway products is '''2.7.3'''. We recommend that you install the same version on your development machine (Windows/Linux).
 +
 +
== Using Python on Lantronix Devices ==
 +
Configuration and control of Python programs can be accomplished by accessing the Command Line Interface (CLI) using Telnet/SSH or serial port. Web configuration UI is accessible via the browser on any Windows/Linux host or on smartphone/tablet. Supported browsers are Firefox, Chrome and Internet Explorer.
 +
 +
=== Using CLI (Command Line Interface) ===
 +
The CLI provides a quick and easy way for developers used to working in the Command Prompt on Windows or using the Shell on Linux to interact with the Lantronix devices.
 +
 +
==== Load Python Programs ====
 +
To upload Python program to Lantronix device, use FTP on the command line. The following is an example of this procedure at the Linux shell prompt.
 +
 +
#'''ftp ''<Lantronix Device IP Address>'''''
 +
''-- login --''
 +
ftp> ''put helloworld.py helloworld.py''
 +
ftp> ''exit''
 +
 +
 +
==== Installing Python Module Packages ====
 +
 +
==== Configure Python Programs ====
 +
To configure uploaded Python programs to run, a series of commands needs to be entered in the CLI. The example below shows connecting to the CLI via telnet and assumes that Python program '''''helloworld.py''''' was uploaded using the earlier steps.
 +
 +
<tt>
 +
#'''telnet ''<Lantronix Device IP Address>'''''
 +
''--login (if enabled) --''
 +
> '''enable'''
 +
(enable)# '''configure'''
 +
(config)# '''applications'''
 +
(config-applications)# '''python ''<instance>'''''
 +
(config-applications-python:<instance>)# '''file ''<python program>.py'''''
 +
(config-applications-python:<instance>)# '''state enable'''
 +
(config-applications-python:<instance>)# '''write'''
 +
(config-applications-python:<instance>)# '''exit'''
 +
(config)# '''exit'''
 +
(enable)# '''exit'''
 +
</tt>
 +
 +
==== Running Python Programs ====
 +
 +
=== Using Web UI ===
 +
 +
==== Load Python Programs ====
 +
 +
==== Installing Python Module Packages ====
 +
 +
==== Configure Python Programs ====
 +
 +
==== Running Python Programs ====
 +
 +
 +
 +
== Development Workflow ==
 +
This section provides an overview of the development workflow when creating Python programs and packages for use on Lantronix devices.<br>
 +
Since Python is available on a number of platforms, most development for Python can be done on a Windows/Linux host and tested locally too. The Python program can then be transferred to the Lantronix devices for further debugging, testing, packaging and deployment.
 +
 +
The current version of Python installed on Lantronix devices is '''2.7.3'''. We recommend that you install the same version on your development machine. Also, only use the python modules known to be compatible with this version. For download, installation instructions and additional documentation please visit https://www.python.org/.
 +
 +
=== Command Line Based Workflow ===
 +
=== Develop/Test on Windows/Linux Host ===
 +
==== Using Python Interpreter Command Line ====
 +
==== Using IDE ====
 +
 +
=== Create Python Package For Lantronix Devices ===
 +
 +
=== Transfer/Run Python Programs on Lantronix Devices ===
 +
Follow the instructions in the section above on '''''Using Python on Lantronix devices''''' to test and deploy the program on Lantronix devices.
 +
 +
== Python Runtime Support ==
 +
=== Python Version Supported ===
 +
==== Python Language/Runtime ====
 +
==== Python Standard Library ====
 +
 +
=== Preinstalled Python Packages ===
 +
==== PySerial ====
 +
This module encapsulates the access for the serial port.<br>
 +
More information can be found at: http://pyserial.sourceforge.net/<br>
 +
 +
While using this module for a particular serial port, please ensure that no other applications are using that serial device. Serial port access for standard Lantronix applications, can be disabled using the <tt>'''no protocol'''</tt> command at the <tt>''''line'''</tt> menu level in the CLI or via selecting <tt>'''Protocol: None'''</tt> under the '''Line''' menu on the Web UI.

Revision as of 04:51, 18 November 2014


Introduction

Overview

This guide introduces developers to the support for the Python runtime on Lantronix line of embedded and ready-to-deploy Intelligent Gateway products. It describes how to load and run Python programs on Lantronix devices, reviews available Python modules including those that expose Lantronix specific APIs and describes sample Python programs for common use cases that provide a head-start in customizing Lantronix Intelligent Gateway platforms.


What is Python?

Python is a programming language that lets you work more quickly and integrate your systems more effectively. It is a dynamically typed, object-oriented programming language offering an elegant syntax and an extendable Python interpreter. This makes it an ideal language for scripting and rapid application development for most applications and specifically connected device applications. The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms (Windows, Linux\Unix, Mac OS X) and on machine-to-machine (M2M) or Internet of Things (IoT) focused Intelligent Gateway platforms from Lantronix.


Additional Python Reference

The Python Programming Language website (http://www.python.org) is the definitive and official source for all Python information. It also contains additional links to other Python references including other sites, books, etc. For a quick and informal introduction to Python, the tutorial at Python Tutorial is a great start.


Supported Lantronix Products

The following Lantronix products provided integrated Python runtime support.

Support for the following Lantronix products is planned (Alpha Software Available)


Getting Started

This section describes how to create a simple Python program and the steps to run the program on a Lantronix device.

First Program "Hello World"

Write Simple Python Program

In your favorite text editor, create a file named 'helloworld.py', with the following contents:

# Hello World - a simple text output program
def main():
print 'Hello World!'

main()

Transfer Program Onto Lantronix Device

  1. Log in to the Web UI for Lantronix Device. Enter the configured username and password. Default values are admin and PASS.
  2. Using the Filesystem menu option, go to the Browse sub-menu and upload the helloworld.py file to the device.


File transfer getting started.jpg

Run The Program

  1. Navigate to the Applications menu option in the Web UI.
  2. For script 1, enter the script name as helloworld.py and mark the Enable checkbox to enable this script.
  3. Enter the output filename as helloworld.out. This file will be created in the filesystem to capture all stdout output from the script.
  4. Hit the Run button for Script 1 to run the transferred program.


Python application getting started.jpg


Open the output file helloworld.out from the Filesystem -> Browse Menu to check the expected Hello World output from the script.

System Requirements

Integrated Python runtime is available from software version 7.9.0.0 onwards on the supported Lantronix products.
If you have products with software version older than 7.9.0.0, please follow the upgrade instructions for the respective products. The software and the upgrade notes are available from the Lantronix support site at www.lantronix.com/support/downloads.

Recommended Python Version

The version of Python supported on Lantronix PremierWave family of Intelligent Gateway products is 2.7.3. We recommend that you install the same version on your development machine (Windows/Linux).

Using Python on Lantronix Devices

Configuration and control of Python programs can be accomplished by accessing the Command Line Interface (CLI) using Telnet/SSH or serial port. Web configuration UI is accessible via the browser on any Windows/Linux host or on smartphone/tablet. Supported browsers are Firefox, Chrome and Internet Explorer.

Using CLI (Command Line Interface)

The CLI provides a quick and easy way for developers used to working in the Command Prompt on Windows or using the Shell on Linux to interact with the Lantronix devices.

Load Python Programs

To upload Python program to Lantronix device, use FTP on the command line. The following is an example of this procedure at the Linux shell prompt.

#ftp <Lantronix Device IP Address>
-- login --
ftp> put helloworld.py helloworld.py
ftp> exit


Installing Python Module Packages

Configure Python Programs

To configure uploaded Python programs to run, a series of commands needs to be entered in the CLI. The example below shows connecting to the CLI via telnet and assumes that Python program helloworld.py was uploaded using the earlier steps.

#telnet <Lantronix Device IP Address>
--login (if enabled) --
> enable
(enable)# configure
(config)# applications
(config-applications)# python <instance>
(config-applications-python:<instance>)# file <python program>.py
(config-applications-python:<instance>)# state enable
(config-applications-python:<instance>)# write
(config-applications-python:<instance>)# exit
(config)# exit
(enable)# exit

Running Python Programs

Using Web UI

Load Python Programs

Installing Python Module Packages

Configure Python Programs

Running Python Programs

Development Workflow

This section provides an overview of the development workflow when creating Python programs and packages for use on Lantronix devices.
Since Python is available on a number of platforms, most development for Python can be done on a Windows/Linux host and tested locally too. The Python program can then be transferred to the Lantronix devices for further debugging, testing, packaging and deployment.

The current version of Python installed on Lantronix devices is 2.7.3. We recommend that you install the same version on your development machine. Also, only use the python modules known to be compatible with this version. For download, installation instructions and additional documentation please visit https://www.python.org/.

Command Line Based Workflow

Develop/Test on Windows/Linux Host

Using Python Interpreter Command Line

Using IDE

Create Python Package For Lantronix Devices

Transfer/Run Python Programs on Lantronix Devices

Follow the instructions in the section above on Using Python on Lantronix devices to test and deploy the program on Lantronix devices.

Python Runtime Support

Python Version Supported

Python Language/Runtime

Python Standard Library

Preinstalled Python Packages

PySerial

This module encapsulates the access for the serial port.
More information can be found at: http://pyserial.sourceforge.net/

While using this module for a particular serial port, please ensure that no other applications are using that serial device. Serial port access for standard Lantronix applications, can be disabled using the no protocol command at the 'line menu level in the CLI or via selecting Protocol: None under the Line menu on the Web UI.