Unix Device File Emulation

Dan Upton upton at virginia.edu
Wed Apr 23 11:27:08 EDT 2008


(let's try this again, and actually send it to the list this time)

On Wed, Apr 23, 2008 at 11:02 AM, blaine <frikker at gmail.com> wrote:
> Hey everyone,
>   So I've got a quick query for advice.
>
>   We have an embedded device in which we are displaying to an LCD
>  device that sits at /dev/screen.  This device is not readily available
>  all the time, so I am needing to write an emulator.  This will
>  basically just monitor a file, /dev/screen for example, and write the
>  commands to a TK or WxWindows canvas.
>
>  So sending 'line 0 0 10 10' will draw a line on my canvas from (0,0)
>  to (10,10).
>
>  My question: Whats the best way to set up a monitor (in python) of
>  this file? Would I simply open up the file for read, check for
>  changes, get any updated data, and clear the file? Or is there some
>  standard way of doing something like this that guarantees no overlap
>  or data loss?
>
>  example usage: echo 'line 0 0 10 10' > /dev/screen
>
>  On the actual embedded device this is handled by a kernel module.  We
>  can spit commands into it as fast as we can and the kernel module can
>  keep up.  This is typical unix device file behavior.
>
>  Any suggestions or advice would be splendid.  Thanks!
>  Blaine
>  --
>  http://mail.python.org/mailman/listinfo/python-list
>

've only interacted with device files from python that I was only
reading from. And I guess technically they were under /proc and /sys,
rather than /dev, although they may be handled the same way.  Anyway,
in that case my method was basically to open the file, read it, close
it, do whatever processing I needed to do on the data, sleep for some
interval...lather, rinse, repeat.  Sleeping for some interval may or
may not be appropriate in your case.  I know in the case of /proc
files and /sys files, the files are generated on demand by the kernel
and relevant kernel structures are basically printed to a string and
copied into a page in memory that the user program can access.  AFAIK,
you can seek back and forth through them, but I don't know whether the
data in the page is updated on a seek, so you may have to close and
reopen the file ever iteration to see what's changed.

HTH,
-dan



More information about the Python-list mailing list