473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Unix Device File Emulation

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
Jun 27 '08 #1
10 1965
blaine 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
It looks as if you need something like the Unix 'tail -f' command.
Perhaps here is some help

http://aspn.activestate.com/ASPN/Coo.../Recipe/414771
http://mail.python.org/pipermail/pyt...st/157510.html
http://pyinotify.sourceforge.net/
--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
Jun 27 '08 #2
blaine wrote:
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!
Assuming you are on unix, have you considered FIFO's, os.mkfifo()?
Jun 27 '08 #3
(let's try this again, and actually send it to the list this time)

On Wed, Apr 23, 2008 at 11:02 AM, blaine <fr*****@gmail.comwrote:
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
Jun 27 '08 #4
On Apr 23, 11:17 am, "Ville M. Vainio" <vivai...@gmail.comwrote:
blaine wrote:
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!

Assuming you are on unix, have you considered FIFO's, os.mkfifo()?
Thank you - this is exactly what I need, I believe. I'm having a
problem though. The os.mkfifo() works fine, but when I read from the
file my blocking calls dont work as intended... See below:

# Fake Nokia Screen Emulator
import sys, os

class nokia_fkscrn:
def __init__(self, file):
if not os.path.exists(file):
os.mkfifo(file)
self.fifodev = open(file, 'r')
def read(self):
while 1:
r = self.fifodev.readline()
print r

nokia = nokia_fkscrn('dev.file')
nokia.read()

This works at first, but when I write to the 'dev.file' for the first
time, the text is displayed as intended, but then the program just
keeps spitting out blank lines. I can continue to write to the file
(using echo 'test\n' dev.file) and this shows up in my output, but
amist a giant mass of scrolling blank lines. This also causes my CPU
usage to shoot up to 100%.

Any ideas? This is OS X 10.4
-Blaine
Jun 27 '08 #5
"blaine" schrieb
>
# Fake Nokia Screen Emulator
import sys, os

class nokia_fkscrn:
def __init__(self, file):
if not os.path.exists(file):
os.mkfifo(file)
self.fifodev = open(file, 'r')
def read(self):
while 1:
r = self.fifodev.readline()
print r

nokia = nokia_fkscrn('dev.file')
nokia.read()

This works at first, but when I write to the 'dev.file'
for the first time, the text is displayed as intended,
but then the program just keeps spitting out blank lines.
I can continue to write to the file
(using echo 'test\n' dev.file)
and this shows up in my output, but amist a giant mass
of scrolling blank lines. This also causes my CPU
usage to shoot up to 100%.

Any ideas? This is OS X 10.4
while 1:
r = self.fifodev.readline()
if r: print r

According to my docs, readline() returns an empty string
at the end of the file.
Also, you might want to sleep() between reads a little bit.
IMHO. HTH.
Martin

Jun 27 '08 #6
On Apr 23, 12:27 pm, "Martin Blume" <mbl...@freesurf.chwrote:
"blaine" schrieb


# Fake Nokia Screen Emulator
import sys, os
class nokia_fkscrn:
def __init__(self, file):
if not os.path.exists(file):
os.mkfifo(file)
self.fifodev = open(file, 'r')
def read(self):
while 1:
r = self.fifodev.readline()
print r
nokia = nokia_fkscrn('dev.file')
nokia.read()
This works at first, but when I write to the 'dev.file'
for the first time, the text is displayed as intended,
but then the program just keeps spitting out blank lines.
I can continue to write to the file
(using echo 'test\n' dev.file)
and this shows up in my output, but amist a giant mass
of scrolling blank lines. This also causes my CPU
usage to shoot up to 100%.
Any ideas? This is OS X 10.4

while 1:
r = self.fifodev.readline()
if r: print r

According to my docs, readline() returns an empty string
at the end of the file.
Also, you might want to sleep() between reads a little bit.

IMHO. HTH.
Martin
Oh ok, that makes sense. Hmm. So do I not want to use readline()? Or
is there a way to do something like 'block until the file is not
empty'?
Jun 27 '08 #7
"blaine" schrieb

while 1:
r = self.fifodev.readline()
if r: print r

According to my docs, readline() returns an empty
string at the end of the file.
Also, you might want to sleep() between reads a
little bit.

Oh ok, that makes sense. Hmm. So do I not want to use
readline()? Or is there a way to do something like
'block until the file is not empty'?
No,
while 1:
r = self.fifodev.readline()
if r: print r
else: time.sleep(0.1)
is ok (note the "if r:" clause).

Martin

Jun 27 '08 #8
On Apr 23, 2:01 pm, "Martin Blume" <mbl...@freesurf.chwrote:
"blaine" schrieb
while 1:
r = self.fifodev.readline()
if r: print r
According to my docs, readline() returns an empty
string at the end of the file.
Also, you might want to sleep() between reads a
little bit.
Oh ok, that makes sense. Hmm. So do I not want to use
readline()? Or is there a way to do something like
'block until the file is not empty'?

No,
while 1:
r = self.fifodev.readline()
if r: print r
else: time.sleep(0.1)
is ok (note the "if r:" clause).

Martin
Beautiful! Thanks Martin!
Jun 27 '08 #9
On 2008-04-23, blaine <fr*****@gmail.comwrote:
On Apr 23, 2:01 pm, "Martin Blume" <mbl...@freesurf.chwrote:
>"blaine" schrieb
>No,
while 1:
r = self.fifodev.readline()
if r: print r
else: time.sleep(0.1)
is ok (note the "if r:" clause).

Martin

Beautiful! Thanks Martin!
yes, but you have to follow the usual file handling rules, and close/re-open
the fifo after detecting EOF. You will be blocked on attempting to re-open
until there is another writing process.

while 1:
fp = open('my_fifo', 'r')
while 1:
line = fp.readline()
if line == '':
break
print line.rstrip() # To prevent printing of \n in line
fp.close()

Albert
Jun 27 '08 #10
On Apr 24, 3:38 am, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
On 2008-04-23, blaine <frik...@gmail.comwrote:
On Apr 23, 2:01 pm, "Martin Blume" <mbl...@freesurf.chwrote:
"blaine" schrieb
No,
while 1:
r = self.fifodev.readline()
if r: print r
else: time.sleep(0.1)
is ok (note the "if r:" clause).
Martin
Beautiful! Thanks Martin!

yes, but you have to follow the usual file handling rules, and close/re-open
the fifo after detecting EOF. You will be blocked on attempting to re-open
until there is another writing process.

while 1:
fp = open('my_fifo', 'r')
while 1:
line = fp.readline()
if line == '':
break
print line.rstrip() # To prevent printing of \n in line
fp.close()

Albert
Oh, good call. Thanks a lot, that really helps. I felt that the
previous solution was workable, but not perhaps 100% correct.

Thanks Albert!
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gilles Cadorel | last post by:
I'd like to add in a HTML page a button, that open a Unix Emulation on a new Windows, when I clik on it. I'm using WRQ Reflection to connect to Unix hosts. The problem is that I don't want the...
6
by: Matthew | last post by:
How would I go about creating a simple "hello world" program that will run in Unix. I am using MS Visual C++.
11
by: stu | last post by:
I have several databases that are opened using various versions of Access and VB. Up till recently everything worked fine, then I started getting a variety of lock file error messages, both on my...
0
by: sangui | last post by:
Helllo. this is biginner programmer. Would u check the file that I programed on c#(winform)? I tryed to make the program reading the binary file by C# programming but I failed. If u have...
0
by: sangui | last post by:
Hello. this is beginner programmer. If u have more time, would you check this code.? please give me the answer how to slove this problem...? ÷ºÎ ÆÄÀÏ ==> wtmpx.dat ·Î±× ÆÄÀÏ ...
10
by: emmitsuks | last post by:
Can I compile C source in an PC editor like Miracle, build it, and then take that .exe over to Unix and run it there? Is there a preferred group for beginners such as myself to join?
2
by: anand.ba | last post by:
Hi all I need to find equivalent windows libraries for the following unix ones. #include <sys/prctl.h> #include <sys/ioctl.h> #include <unistd.h>
1
by: dr3586 | last post by:
Hi, I am new at this so I apologize if all is not correct. In ksh unix script I am trying to read values from text delimited file into oracle db table. I know that the read from the text file is...
1
Nepomuk
by: Nepomuk | last post by:
In most modern distributions of Linux and Unix (at least ones with graphical environments like KDE, Gnome or XFCE), you get to mount your partitions easily from the desktop. In some cases however, it...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.