I'm not an expert in web2py, but this one approach I think should work
for your application. If others have better ways of doing it, please
share them:
def readfile():
'''
returns the file to be read
'''
response.view = '/path/to/file_to_be_read.txt'
return locals()
def writefile():
'''
writes the values of the form in basic, unescaped CSV format
'''
form = SQLFORM.factory(
Field('name'),
Field('choice', requires=IS_IN_SET(['1','2','3'], zero=None)),
)
if form.validate():
fh = open('path/to/file_to_write_to.txt', 'w')
fh.write(','.join(form.vars.values()))
fh.close()
response.flash = 'Your data has been submitted'
return dict(form=form)
It sounds like in your application, you are reading the serial port at
fixed periodic intervals and writing the data to a file. If you only
need to read or write to it when a user requests a page, this is what I
was suggesting instead:
def readdata():
'''
read data from serial port and return it in a view
see the Python PySerial module documentation for details
'''
import serial
try:
ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
data = ser.readline() # read a '\n' terminated line
ser.close()
except:
data = 'cannot read serial port'
return dict(data=data)
writing to the serial port would be just as easy:
def writedata():
'''
writes form values in basic, unescaped CSV to the serial port
'''
import serial
form = SQLFORM.factory(
Field('name'),
Field('choice', requires=IS_IN_SET(['1','2','3'], zero=None)),
)
if form.validate():
try:
ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
ser.write(','.join(form.vars.values()))
ser.close()
response.flash = 'Your data has been written'
except:
response.flash = 'Cannot write to serial port'
return dict(form=form)
HTH,
-Jim
----- Kenneth Lundstr�m <
kenneth.t...@gmail.com> wrote:-----
> --- You received this message because you are subscribed to a topic
> in the Google Groups "web2py-users" group.
> To unsubscribe from this topic, visit
https://groups.google.com/d/topic/web2py/_jYbslU46R4/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
web2py+un...@googlegroups.com.