Difference between revisions of "Python Smart Terminal"

From OpenCircuits
Jump to navigation Jump to search
Line 62: Line 62:
 
= Notes on the Code =
 
= Notes on the Code =
  
In some ways I am proud of the code, it was sometimes a slog, and I had to learn a lot of Python to get it going.  On the other hand it kind of sucks, it has lots of dead code, ass backwards was of doing things, poor naming...... Until I loose interest in it it will probably improve.  However, adding features is more part of the life of a programmer than polishing old features; making something better that seems good enough is not always the priority it should be.  If you do not like it, mostly keep it to yourself unless it is accompanied by an offer to improve it.  I do not need ideas, I need time.
+
In some ways I am proud of the code, it was sometimes a slog, and I had to learn a lot of Python to get it going.  On the other hand it kind of sucks, it has lots of dead code, ass backwards was of doing things, poor naming...... There is also a good bit of code on features that I am adding, but that is unfinished.  Until I loose interest in it it will probably improve.  However, adding features is more part of the life of a programmer than polishing old features; making something better that seems good enough is not always the priority it should be.  If you do not like it, mostly keep it to yourself unless it is accompanied by an offer to improve it.  I do not need ideas, I need time. That said if you think you have a helpful comment contact me, my page will tell you how: [[User:Russ_hensel]]
  
 
= Design =
 
= Design =

Revision as of 04:46, 23 September 2016

Draft

This page is a very early draft, somewhat my notes as I go along. Code... not yet posted.

What/Why

Almost all my Arduino and other microcontroller projects use serial communications for a least debugging. And in many cases I pair the Arduino with a Raspberry Pi for a very flexible system. A good serial monitor, or terminal program is useful in this sort of project. Because I wanted features not in most terminals and the ability to program it myself, I started one from scratch using Python.

So what are the features?

  • Free open source
  • Runs across OSs Linux ( inc Raspberry Pi ), Mac or Windows
  • Python
  • Easily adjusted serial communications parameters
  • Multiple, preloaded with data entry fields.
  • Clear receive area.
  • Copy all or part of receive area.
  • Programmable
  • Database Interface
  • Easily Modified or Replaceable GUI
  • Parameter file for wide range of modifications of program behavior.
  • Easy to run multiple instances with different parameters.
  • Uses standard Python logging class.
  • Includes some advanced data logging features in addition to database data logging.


A much earlier version of this terminal was described in my instructable [Python Terminal for Cheap Pi Arduino Connection] It may have some information that is useful, but the program has grown a lot since then.

Installation

This program is intended for those who at least occasionally develop in Python. I expect that they already run some things in Python and will just add this as an additional project. There is no install program you just download the files, place where you keep your projects and run. A bit more later in this section.

Before you begin to install you should know a bit about the environment that I have used to build, test and run the terminal. If your environment differs too much you may have trouble getting it to run.

I run Python mostly using the install that comes with Anaconda Spyder and often use the IDE it installs. This is not necessary, it is just an nice install that downloads a lot of stuff that technical folks find useful. I have use conda an pip to add to this install and do not know offhand all that is in it. It is Python 2.7. You can look at the include statements to get some idea of what you might need to add. Or you can just keep running it and add the packages it complains about.

Download the files ( I have not decided just how they will be on git hub in at least one form ) Uncompress if they are compressed. The directory structure should looks like this

         whatever --|
                    |-- rshlib
                    |
                    |-- SmartTerminal

Put them in your system making whatever anything convenient for your Python. It should run ( at least to put up its GUI ) by running smart_terminal.py. Note that there may be a certain amount of left over, dead code, in the directory I am cleaning out bit by bit, someday it may be nice and neat. For now if you want to tinker look at the design info below first.

Run it until it stops complaining about dependencies ( in the console ), after that ( and perhaps even before ) the GUI should come up. You are installed.


I have run the program on both Windows 10 and Rasperian on a RPi. It should work in most OS's. Let me know about issues.

Some parts of the program think that mySql is available. It should run fine without it ( there will be a message or two in the console ), untill you try to use the database then it is not so gracious. Straight ahead use as a terminal does not use the database.

Configure to Run

Basic configuration of comm parameters like port, baud rate .... is all done in a file called parameters.py. It seemed easier to simply use a Python text file instead of some other format like an ini file. Pretty much all the file does is set instance variables in itself. It is used by the program controller ( smart_terminal.SmartTermianl ) to create an instance of Parameters and then the values can be used. Save the original ( maybe I will include a backup maybe not ) in case you mess it up too much.

Parameters starts out with some "meta" parameters. These are defined early in the creation of the objects and may effect other values. In any case you can always define a value twice, the last one always wins.

All the com port values are defined in pretty much one place, find it an change it to what you need.


Notes on the Code

In some ways I am proud of the code, it was sometimes a slog, and I had to learn a lot of Python to get it going. On the other hand it kind of sucks, it has lots of dead code, ass backwards was of doing things, poor naming...... There is also a good bit of code on features that I am adding, but that is unfinished. Until I loose interest in it it will probably improve. However, adding features is more part of the life of a programmer than polishing old features; making something better that seems good enough is not always the priority it should be. If you do not like it, mostly keep it to yourself unless it is accompanied by an offer to improve it. I do not need ideas, I need time. That said if you think you have a helpful comment contact me, my page will tell you how: User:Russ_hensel

Design

These notes are here so you can more easily modify the code. Contact me Russ Hensel if you need additional help.

Before modifying the code it is best to understand how it works. Here is an overview of the general plan, details can be filled out by reading the code.

The architecture is called the model view controller or MVC. The class SmartTerminal ( in smart_terminal.py ) could be viewed as the main class. To run the program run its file ( see code at end of file) SmartTerminal is the controller in MVC it is responsible for all overall control, it directly or indirectly creates all other program objects.

The view component is called GUI ( in gui.py ). It creates all the visible components, and relays user input to the controller.

The model component is the component that actually does the communication it is called RS232Driver ( in rs232driver.py ) and like the GUI is controlled by the controller.

The GUI is not allowed to directly communicate with the model and vise versa. Thus you can unplug them from the application and plug in new components. Don't like the GUI? You could modify mine, or you could make a modification and choose which one to use. This is sort of like a skin for an application. You can even set up to run with no GUI at all. The RS232Driver like the GUI easy to remove and replace in the program, its use has been parameterized in to the Parameter object, so to use SPI instead of RS232 all we have to do is write an SPI object and change the values in Parameter.

Two other important components are called Logger ( in logger.py ) and Parameters ( in parameters.py ). The controller creates one of each, and make them available to the other components. The other components can interact with them, and uses them respectively for logging events, and getting access to parameters ( those aspects of the application that are particularly easy to change ).

With this background you should be ready to read and modify the code. There are lots of comments, also a fair amount of commented out debugging code. Questions? Let me know.

Possible Expansion/Modifications, these are the things that I am currently thinking about:

The GUI could be much nicer, clean it up, fix colors, fonts..... Add clear buttons to the send areas. Move at least some of the parameters to a text ini file. Add other communications protocols. Make the application search the available comm ports to find any attached arduino. Let the user save the transmitted and received data to a file. Display available comm ports.