An update to this - I have done some more investigation and discovered that 
in the 'Not Working' scenario the RTS output is mostly high but goes low 
whilst the port is open, and in the 'Working' scenario the RTS output has 
somehow been forced low and so stays low when the port is opened. 
The Dongle gets reset by a high-to-low transition of RTS, so no falling 
edge, no Reset and everything works. For reference my Dongle uses an FTDI 
device (FT230X) and hence the FTDI driver supplied with the latest Raspbian 
(Jessie) release.

The question now, therefore, is how Web2py forces RTS low - either by the 
saving of a file or when I access the index page of my Site, and How I can 
replicate this in the Scheduler code! Using 'touch' to update the file does 
not work, it has to be saved from within Web2py.

Richard


On Friday, November 25, 2016 at 10:46:26 AM UTC, Richard Brown wrote:

> I have used Web2py to create a Home Heating Control application for 
> Raspberry Pi with a web front end to define the and I am using the Web2py 
> Scheduler to perform the control tasks every 30 seconds. I have also 
> designed a radio linked system for temperature measurement and boiler 
> control based around a USB dongle plugged into the Pi.
>
>   
>
> The problem I have is how to initialise and open the /dev/ttyUSB0 port 
> once at start up, but not reopen it on every subsequent Scheduler run as 
> this resets the dongle via the RTS line as the dongle is based on the 
> Arduino architecture. I have some simple test code running, this section is 
> called by the Scheduler as follows:
>
>  
>
> In file 'poll.py:' 
>
> def ch_scheduler():
>     from utilities import ch_control
>
>     import time
>
>     t = time.ctime()
>
>     ch_control()
>
>     return t
>
> In file 'utilites.py':
>
> from control import *
>
>
> def ch_control():
>
>     RelCtrl(1,0)
>
>     time.sleep(0.1)
>
>     RelCtrl(2,1)
>
>     time.sleep(0.2)
>
>     RelCtrl(2,0)
>
>     time.sleep(0.1)
>
>     RelCtrl(1,1)
>
>     return
>
> In file 'control.py':
>
> import serial 
>
>
> ser=serial.Serial('/dev/ttyUSB0', 57600, timeout=1, xonxoff=0, rtscts=0)
>
>
> def RelCtrl(in1,in2):
> # convert relay parameters to integers
>
>     relay = int(in1)
>
> # 1: ON, 0: OFF
>
>     relon = int(in2)
>
>
>     if relay > 0 :
>         if relon == 1 :
>
>             command = 's1 ' + str(relay - 1) + '\x0d\x0a'
>
>         else :
>
>             command = 'c1 ' + str(relay - 1) + '\x0d\x0a'
>
>         ser.write(command)
>
>
> # wait for reply, read characters but ignore
>         time.sleep(0.1)
>
>         characters = ser.inWaiting()
>
>         if characters > 0 :
>
>             reply = ser.read(characters)
>
>     return
>
> Unsurprisingly this opens the port (and resets my dongle) each time the 
> scheduler runs ch_control(). However, and bizarrely, if I save 'control.py' 
> without changing its contents from the Web2py Admin Interface, or access my 
> Site via a browser then the port does not get reopened on every Scheduler 
> run and the system operates perfectly. This is of no help in practice as 
> the background tasks have to operate autonomously from power on, but may 
> offer a clue as how to make it work.
>
> I have attached oscilloscope traces of the behaviour which show the 
> transmitted commands to the dongle from each of the four calls to RelCtrl() 
> (Yellow upper trace) and received data from the dongle (Cyan lower trace). 
> 'Not Working from Boot.bmp' shows the port being reopened, the dongle being 
> reset and not responding until the third command. 'Working after File 
> Save.bmp' show what happens after 'control.py' was saved from within 
> Web2py, and shows what I need to happen all the time.
>
>  
> So my question is: how can I configure and open the serial port the first 
> time the Scheduler runs, and then access it without reopening it from then 
> on? I'm not aware of any way to preserve 'ser' from run to run, nor of 
> generating it on each run without reopening the port. I can test if a port 
> is open, but only if I can reference it - which forces it to be reopened.
>
> Thanks,
>
> Richard
>
>  
>
>  
>
>   
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to