When I do the following:

s= serial.Serial('/dev/',9600)
# or set a function and pass via command line
    s= serial.Serial(arg1,9600)

The code executes without any error.
But, when I use AJAX and POST the parameters to the script

def index(req):
    info = req.form
    sys.stderr = sys.stdout
    s= serial.Serial(info['device'],9600)

An error occurs:

Traceback (most recent call last):

File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)

File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1229, in _process_target
result = _execute_target(config, req, object, arg)

File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1128, in _execute_target
result = object(arg)

File "/usr/lib/python2.6/dist-packages/mod_python/publisher.py", line 213, in handler
published = publish_object(req, object)

File "/usr/lib/python2.6/dist-packages/mod_python/publisher.py", line 425, in publish_object
return publish_object(req,util.apply_fs_data(object, req.form, req=req))

File "/usr/lib/python2.6/dist-packages/mod_python/util.py", line 554, in apply_fs_data
return object(**args)

File "/var/www/md-result/libs/system/usb-mobile/balance.py", line 23, in index
s= serial.Serial(info,9600),

File "/usr/lib/python2.6/dist-packages/serial/serialutil.py", line 154, in __init__
self.port = port

File "/usr/lib/python2.6/dist-packages/serial/serialutil.py", line 201, in setPort
self.portstr = self.makeDeviceName(port)

File "/usr/lib/python2.6/dist-packages/serial/serialposix.py", line 297, in makeDeviceName
return device(port)

File "/usr/lib/python2.6/dist-packages/serial/serialposix.py", line 34, in device
return '/dev/ttyS%d' % port

TypeError: %d format: a number is required, not StringField

I have no idea why it is doing this, I checked the POST parameters passed by printing them and they are coming through.

Any suggestions would be greatly appreciated.

Thanks,
Spogs

First of all, don't you have to pass the complete device name?

For a serial port, this is '/dev/ttyS0'.

Edit:
I tried to figure out your code via the error messages.

It's a problem with integer - string conversion.
Your 'port' variable get a string value. You have to convert that to a number.
Or you can use:

return '/dev/ttyS%s' % port

%s in stead of %d

Edited 6 Years Ago by Firewolf: n/a

Hi Firewolf,

Thanks for the feedback you were right re string conversion I corrected it:
s= serial.Serial(str(info),9600)

Cheers,
Spogs

First of all, don't you have to pass the complete device name?

For a serial port, this is '/dev/ttyS0'.

Edit:
I tried to figure out your code via the error messages.

It's a problem with integer - string conversion.
Your 'port' variable get a string value. You have to convert that to a number.
Or you can use:

return '/dev/ttyS%s' % port

%s in stead of %d

This question has already been answered. Start a new discussion instead.