bind line-oriented device output?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

bind line-oriented device output?

Tom Roche

I'd like to learn to pythonically redirect the output from a
line-oriented character device to a particular file or process,
regardless of focus, on a generic graphical OS. But I don't want to
redirect stdin entirely. Here's the usecase:

My school has a seminar for which we record attendance by scanning the
student ID card, or rather the barcode on its back, with a handheld
USB scanner. This is pretty straightforward, except that the barcode
scanner is like a keyboard writing the ASCII equivalent of the barcode
(== the student's ID#) to stdin. FWIW, the scanner writes lines: I'm
not sure if EOL is \r, \n, or \r\n, but I suspect the latter.

Since the scanner is connected to an ordinary multiprocessing laptop on
which one will likely be doing other things while scanning (notably
setting up to record the presenter), it sometimes happens (especially
until one learns to pay attention to this) that one writes to a frame
other than the text file into which we want record attendee ID#s. This
semester recurs every {fall, spring}, so someone faces this {pitfall,
learning curve} at regular intervals.

How to prevent writing the wrong target? One trivial solution--shlep a
dedicated USB host for the scanner--is deprecated. An OS-specific
solution (e.g. relying on a linux raw device) is also undesirable: I
use ubuntu, but others will probably use mac or windows.

Rather, It Would Be Nice, and useful for this seminar's mechanics, to
be able to run some code to which one could say, see this device? and
this file? Make the device's output go only to that file. For extra
credit, don't let anything else write that file while this code is
running.

Can python do that? Or does one need to get closer to the metal?

TIA, Tom Roche <[hidden email]>
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: bind line-oriented device output?

Wayne Werner
On Sun, Mar 28, 2010 at 6:51 PM, Tom Roche <[hidden email]> wrote:

I'd like to learn to pythonically redirect the output from a
line-oriented character device to a particular file or process,
regardless of focus, on a generic graphical OS. But I don't want to
redirect stdin entirely. Here's the usecase:

My school has a seminar for which we record attendance by scanning the
student ID card, or rather the barcode on its back, with a handheld
USB scanner. This is pretty straightforward, except that the barcode
scanner is like a keyboard writing the ASCII equivalent of the barcode
(== the student's ID#) to stdin. FWIW, the scanner writes lines: I'm
not sure if EOL is \r, \n, or \r\n, but I suspect the latter.

Since the scanner is connected to an ordinary multiprocessing laptop on
which one will likely be doing other things while scanning (notably
setting up to record the presenter), it sometimes happens (especially
until one learns to pay attention to this) that one writes to a frame
other than the text file into which we want record attendee ID#s. This
semester recurs every {fall, spring}, so someone faces this {pitfall,
learning curve} at regular intervals.

How to prevent writing the wrong target? One trivial solution--shlep a
dedicated USB host for the scanner--is deprecated. An OS-specific
solution (e.g. relying on a linux raw device) is also undesirable: I
use ubuntu, but others will probably use mac or windows.

Rather, It Would Be Nice, and useful for this seminar's mechanics, to
be able to run some code to which one could say, see this device? and
this file? Make the device's output go only to that file. For extra
credit, don't let anything else write that file while this code is
running.

Can python do that? Or does one need to get closer to the metal?

It's certainly possible. Technically you can do /anything/, it's just a matter of knowing how to do it. Whether or not it's worth doing is something completely beside the point.

OTOH, if you can assure that the target computer will be running a certain set of python features (i.e. Tkinter/pyGTK+/wxPython, etc) you can fairly easily write a program that will run fullscreen and continually grab focus to whatever widget (in this case a text entry field) you want.

with pyGTK you could do something like this:

gobject.timeout_add(100,  myentrywidget.grab_focus)

the function at the end may be different - perhaps you write your own function to comply with the timeout_add requirements - but it it will continually (every 100ms) grab the "keyboard" focus.

OTOH, if the device is something like the CueCat and reports a control character (I think it's alt+f7 for the cuecat), then at least on linux/windows I think it's much more trivial. You just have to register a global hotkey handler that will send the focus to that particular window.

There are probably a few other approaches to this problem, but those are two of the "simplest".

HTH,
Wayne



_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: bind line-oriented device output?

Tom Roche

http://mail.python.org/pipermail/tutor/2010-March/075438.html
>> I'd like to learn to pythonically redirect the output from a
>> line-oriented character device to a particular file or process,
>> regardless of focus, on a generic graphical OS. But I don't want to
>> redirect stdin entirely.
...
>> It Would Be Nice, and useful for this seminar's mechanics, to be
>> able to run some code to which one could say, see this device? and
>> this file? Make the device's output go only to that file. For extra
>> credit, don't let anything else write that file while this code is
>> running.

http://mail.python.org/pipermail/tutor/2010-March/075443.html
> if you can assure that the target computer will be running a certain
> set of python features (i.e. Tkinter/pyGTK+/wxPython, etc)

Presuming they're available on the user's platform and not too onerous
to install, I can definitely say, "if you want <goodness/>, you need
to install <necessaries/>."

> you can fairly easily write a program that will run fullscreen and
> continually grab focus to whatever widget (in this case a text entry
> field) you want.

*Continually* grabbing focus to a UI is functionally (from the user's
point of view) equivalent to redirecting stdin, no? The user is made
unable to do anything but take attendance for the duration of the
task. In fact, for this usecase, continually grabbing focus is worse,
since the user would be unable to, e.g., read the directions for
setting up the audio. To clarify the usecase:

The barcode scanner (here, a Wasp WLS9500) is connected to an ordinary
multiprocessing laptop (which may be linux, mac, or windows) on which
the user will want to do other things while taking attendance: the
attendees don't come in all at once, and the user must accomplish
other tasks while taking attendance (notably setting up to record the
seminar audio). Hence the desired application

0 is portable.

1 does not redirect all of stdin, only the output from the scanner.
  The user is allowed to continue to work on other tasks (e.g. with
  output from the laptop's keyboard going to whatever frame), with
  only the output from the scanner being bound to a particular file or
  process.

2 does not continually grab focus. The user is allowed to continue to
  work on other tasks (e.g. with output from the laptop's keyboard
  going to whatever frame), with only scanner events being bound to a
  particular frame or widget.

But what might satisfy the usecase is

> if the device is something like the CueCat and reports a control
> character (I think it's alt+f7 for the cuecat), then at least on
> linux/windows I think it's much more trivial. You just have to
> register a global hotkey handler that will send the focus to that
> particular window.

I'll read the Product Reference Guide and hope it tells me that the
Wasp WLS9500 "reports a control character." Unfortunately when I
search the PDF

http://tinyurl.com/waspWLS9500manual

for "control character" I get no hits. Are there synonyms for that
term in this context?

TIA, Tom Roche <[hidden email]>
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: bind line-oriented device output?

Wayne Werner
On Mon, Mar 29, 2010 at 10:56 AM, Tom Roche <[hidden email]> wrote:
I'll read the Product Reference Guide and hope it tells me that the
Wasp WLS9500 "reports a control character." Unfortunately when I
search the PDF

http://tinyurl.com/waspWLS9500manual

for "control character" I get no hits. Are there synonyms for that
term in this context?


Take a look at the bottom of 8-5, it has some prefix/suffix values. Apparently you can set it to send <prefix><data> and it looks like by perusing the manual and probably a bit of trial and error you can set it up to send one of any number of CTRL characters as the prefix. I don't know of any windows program that uses ctrl-] as a hotkey though I'm sure there are some.

For Python registering hotkeys, a quick google search pulled up these:

I wasn't able to find anything super definitive on mac, though, which is too bad.

Anyhow, HTH,
Wayne



_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: bind line-oriented device output?

Fiyawerx
In reply to this post by Tom Roche


On Mon, Mar 29, 2010 at 11:56 AM, Tom Roche <[hidden email]> wrote:

http://mail.python.org/pipermail/tutor/2010-March/075438.html
>> I'd like to learn to pythonically redirect the output from a
>> line-oriented character device to a particular file or process,
>> regardless of focus, on a generic graphical OS. But I don't want to
>> redirect stdin entirely.
...
>> It Would Be Nice, and useful for this seminar's mechanics, to be
>> able to run some code to which one could say, see this device? and
>> this file? Make the device's output go only to that file. For extra
>> credit, don't let anything else write that file while this code is
>> running.
 
1 does not redirect all of stdin, only the output from the scanner.
 The user is allowed to continue to work on other tasks (e.g. with
 output from the laptop's keyboard going to whatever frame), with
 only the output from the scanner being bound to a particular file or
 process.


This may be out of my league, but what about pyUSB? (http://pyusb.sourceforge.net/docs/1.0/tutorial.html)

Possibly a program that just runs in the background, waits for input from the usb scanner, and appends the id to a specified file when it catches it? That way you also don't need to keep the file open the whole time, in case someone accidentally forgets to plug it in or shuts down while the program is still writing? I'm sure there are a lot of better ways to handle the actual file writing, but thought it might help.

Just a noobs $.02



_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor