Difference between revisions of "Python Smart Terminal Technical Details"

From OpenCircuits
Jump to navigation Jump to search
Line 13: Line 13:
 
For example the terminal may send "v<cr>", recieve "GreenHouse Monitor v3 2017 01 24.01<cr>", and look for "GreenHouse" in the recieved string.  This would validate the port.
 
For example the terminal may send "v<cr>", recieve "GreenHouse Monitor v3 2017 01 24.01<cr>", and look for "GreenHouse" in the recieved string.  This would validate the port.
  
So what does the terminal send and what does it look for -- see the parameter file"
+
So what does the terminal send and what does it look for -- see the parameter file:
  
 
<pre>
 
<pre>
Line 23: Line 23:
 
         self.arduino_version        = "GreenHouse"
 
         self.arduino_version        = "GreenHouse"
 
</pre>
 
</pre>
 +
 +
== What are the Possible Comm Ports ==
 +
 +
The SmartTerminal uses two methods for this.  One is just back to the parameter file:
 +
 +
<pre>
 +
        # used to probe around for ports
 +
        if  self.os_win:
 +
            self.port_list  =  [ "COM1",  "COM2",  "COM3",  "COM4",  "COM5",  "COM6",  "COM7",  "COM8",  "COM9",  "COM10",
 +
                                "COM11", "COM12", "COM13", "COM14", "COM15", "COM16", "COM17", "COM18",  "COM19", "COM20",
 +
                              ]
 +
 +
        else:
 +
            self.port_list  =  [ "/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2",
 +
                                "/dev/ttyACM0", "/dev/ttyACM1", "/dev/ttyACM2",
 +
 
 +
</pre>
 +
 +
The above uses the automatic detection of the operating system.  You could of course skip that part of the implementation.
 +
 +
The second method.  There is a Python function for detecting ports.  It is documented as not always being right.  Here it is:
 +
 +
<pre>
 +
    list(serial.tools.list_ports.comports())
 +
</pre>
 +
 +
So finally when making a list of ports to probe I make a list ( in the order the ports will be tested ):
 +
 +
* Start with port in the parameter file.
 +
* Add the ports reported by python ( no duplicates ).
 +
* Add the ports in the parameter port list ( no duplicates ).
 +
 +
If no ports are actually present you can test 20 ports in seconds.  Sending and receiving strings takes longer.  Not that all of this is done using the baud rate and other parameters in the parameter file.

Revision as of 08:27, 17 February 2017

This page is for some fairly fine scale detail on how things are implemented in the Smart Terminal, they may be useful as techniques in other applications. See also: Russ Python Tips and Techniques

Comm Port Details

In the parameter file there is a setting for the comm port, this is typical of all communications programs. Other parameters hold other comm port values. Again typical. There are however, some less typical additions.

Comm Port Validation

  • So you open a comm port, the first level of validation is that the port opens.
  • The second level of validation is that the port can send and receive, this is checked by sending a text string and looking for a response.
  • The third level is to look at the received string. If it contains the correct substring then the port is valid.

For example the terminal may send "v<cr>", recieve "GreenHouse Monitor v3 2017 01 24.01<cr>", and look for "GreenHouse" in the recieved string. This would validate the port.

So what does the terminal send and what does it look for -- see the parameter file:

        # next used in a port probe routine to help identify the port with an arduino
        # the arduino is supposed to respond to a version request with a string
        # containing this, they are part of the name of an Arduino application
        self.get_arduino_version    = "v"

        self.arduino_version        = "GreenHouse"

What are the Possible Comm Ports

The SmartTerminal uses two methods for this. One is just back to the parameter file:

        # used to probe around for ports
        if  self.os_win:
            self.port_list  =  [ "COM1",  "COM2",  "COM3",  "COM4",  "COM5",  "COM6",  "COM7",  "COM8",  "COM9",  "COM10",
                                "COM11", "COM12", "COM13", "COM14", "COM15", "COM16", "COM17", "COM18",  "COM19", "COM20",
                               ]

        else:
            self.port_list  =  [ "/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2",
                                "/dev/ttyACM0", "/dev/ttyACM1", "/dev/ttyACM2",
  

The above uses the automatic detection of the operating system. You could of course skip that part of the implementation.

The second method. There is a Python function for detecting ports. It is documented as not always being right. Here it is:

     list(serial.tools.list_ports.comports())

So finally when making a list of ports to probe I make a list ( in the order the ports will be tested ):

  • Start with port in the parameter file.
  • Add the ports reported by python ( no duplicates ).
  • Add the ports in the parameter port list ( no duplicates ).

If no ports are actually present you can test 20 ports in seconds. Sending and receiving strings takes longer. Not that all of this is done using the baud rate and other parameters in the parameter file.