element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Raspberry Pi and /dev/mem access
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 30 replies
  • Subscribers 680 subscribers
  • Views 4893 views
  • Users 0 members are here
  • raspberry
  • pi
  • raspberry_pi
Related

Raspberry Pi and /dev/mem access

Former Member
Former Member over 11 years ago

Question(s) regarding Raspberry Pi and GPIO.

 

Been working with RPi.GPIO for several weeks trying to get the following script to run: (from Third Eye Visions, using the GPIO Python library that they suggest to use)

 

import RPi.GPIO as GPIO

GPIO.setmode (GPIO,BOARD)

GPIO.setup (7, GPIO.OUT)

GPIO.output (7, True)

 

When running from the Lynux command line, an error is posted “no access to /dev/mem.  Try running as root”

 

I go to command line and run sudo/home/pi/file name and another error is returned “command not found”.

 

Doing a brief search on line, I don’t seem to be the only person having this problem.  I simply don’t understand the solutions that are presented and it is not clear that the solutions solve the problem.

 

When using RPi.GPIO 0.5.4 revision of the Python Package Index, isn’t this problem resolved?

 

I am using the Debian Wheezy Rasbiabn 2012.12.img 192, Debian GNU/Linux 7 raspberry pi tty1

 

I have Python 2.7, 3.2 and 3 on the SD and Python IDE.

 

I want to program the raspberry pi using GPIO functions with Python not C or any other script.

 

Why won’t the current upgrade of Raspberry Pi from Third Eye Visions work with the above script?  This is not the only GPIO program (from other sources) that I have tried and receive the same result.  I can control the pins in the command line of Linux from the keyboard using set mode, etc and read the status of the pins using readall.

 

I have used Pygame with this distribution of Debian and it works perfectly.

 

Can anyone supply a simple fix for this problem?  Can it be solved with a new SD loaded with a raspberry pi compatible program set-up with Python?  If so. where can I find one.? Or can I simply add one or two lines to the script that will allow access into

/dev/mem and then reinstate root privileges to /dev/mem at the point of exit from the program?

 

Help!

morgsterdad

  • Sign in to reply
  • Cancel
Parents
  • Former Member
    0 Former Member over 11 years ago

    Try running "sudo su", then you will be in a shell that has all root privileges.  When you run your Python script and it will definitely have all rights to access /dev/mem.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    Hello All,

     

    Thank you for your input.  The program works!  My problems were related to permissions, paths and misunderstands on my part.  I still have many questions that probably will be answered with experience.  I am using "Raspberry Pi for Dummies" and "Programming the Raspberry Pi" as references, but they are very limited when trying to answer questions pertaining to Raspberry Pi, GPIO, Linux, Python and how they all work together.  Is there another reference that I could be using or is "trial and error" and Google the best references?

     

    Again thank you.  Next step is PWM.

     

     

    Regards,

     

    Bill

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 11 years ago in reply to iagorubio

    That's definitely one way around it image

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 11 years ago in reply to mconners

    Yes, same here. Guess these args have changed over time.

    I just tried reading man man, and think I've found something ok-ish:

    man -k [a-z] | more

     

    That lists over 3000 entries, single-line.

    Could be good for a general overview.

     

    Or, of course, to search for anything hex related as an example:

    man -k hex | more

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 11 years ago in reply to shabaz

    yeah, man -k, apropos

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Problemchild
    0 Problemchild over 11 years ago in reply to shabaz

    Also in the same vein the command line switch "--help" as a good punt when there's no "man" page

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Problemchild
    0 Problemchild over 11 years ago in reply to Problemchild

    Showing how often I actually use it.. it took ages to remember the other command  'info' but since it's sometimes populated you can add it in to complete the ensemble  of self help image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Problemchild
    0 Problemchild over 11 years ago in reply to Problemchild

    Can I also shamelessly plug Steve Parker's Blog site and book  it's an excellent resource to programming Unix command lines in general and bash in particular. I also must say I had a good time working with him .....yes his scripting is pretty good image

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Problemchild

    I looked at Parker's site and it seems that he is missing one or more of the most powerful constructs in shell scripts.  It involves external commands.  For example,

     

    #!/bin/sh

    for a in `ls /var/log`

    do

    echo $a

    done

     

    then there is the

     

    NOW=`date`

    and it's bretheren.  For example:

     

    #!/bin/sh

    FOO=3

    RES=`expr $FOO + 1`

    echo $RES

     

    There is one other item that is almost always missed when dealing with shell scripts.  This is one item that is really difficult to resolve unless it is explained.

     

    In the following shell script, each line is supposed to be echoed to STDOUT.

     

    #!/bin/sh

    for a in `ls -l /var/log'`
    do
    echo $a
    done

     

    here is partial output.

    total

    724

    -rw-r--r--

    1

    root

    root

    0

    Feb

    1

    06:25

     

    Clearly this is not what is needed.

     

     

    What happens is that the preset variable IFS is getting in the way.  IFS is defined by default to  tab. space, and newline.  That means that if a space or tab or newline is encountered, the shell assumes the end of a record.  To fix the program needs the following to be set

     

    #!/bin/sh

    IFS="

    "
    for a in `ls -l /var/log`
    do
    echo $a
    done

     

    Once that is done the output looks like this:

     

    total 724

    -rw-r--r-- 1 root        root      0 Feb  1 06:25 alternatives.log

    -rw-r--r-- 1 root        root   1037 Jan 31 07:07 alternatives.log.1

    -rw-r--r-- 1 root        root    162 Jan 17 10:34 alternatives.log.2.gz

    -rw-r--r-- 1 root        root    669 Sep 29  2013 alternatives.log.3.gz

    -rw-r--r-- 1 root        root    174 Aug 11  2013 alternatives.log.4.gz

    -rw-r--r-- 1 root        root   3476 May 23  2013 alternatives.log.5.gz

    drwxr-xr-x 2 root        root   4096 Mar  1 06:25 apt

    -rw-r--r-- 1 root        root      0 Jan 29 06:25 aptitude

    -rw-r--r-- 1 root        root   1599 Jan 28 21:50 aptitude.1.gz

     

     


     

     

    One of the handier things is the forever loop.:

     

    #!/bin/sh

    while true

    do

    ps -ef | grep cron|grep -v grep

    done

     

    Armed with this info, the lowly shell script is raised to new levels of usefulness.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gdstew
    0 gdstew over 11 years ago in reply to Problemchild

    For several reasons the Advanced Bash-Scripting Guide is by far the best reference available. The first reason is that it is available for free in HTML and PDF at http://www.tldp.org/guides.html. The second is that it is crammed

    full of useful examples. The third is that it is actively being maintained. The last revision (10) was March of this year so it tracks most of the latest bash changes/additions. About the only downside is that it is 916 pages long

    (LOTS of examples). You don't have to read the whole thing, although you probably should at least skim through all the chapters to see what it can do.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • iagorubio
    0 iagorubio over 11 years ago in reply to Former Member

    It's also worth to note the almighty pair find and xargs for this kind of sequential process.

     

    As example the script:

     

    #!/bin/sh

    IFS="

    "
    for a in `ls -l /var/log`
    do
    echo $a
    done


    Is equivalent to:

     


    find /var/log -maxdepth 1 -print0 -type f | xargs -0 ls -ld

     

    In this case find searches for all the files in /var/log but terminates them with a null character to avoid problems with the IFS. xargs gets the output of find and per each field - taking into account it's terminated with null and not with a space - executes the command ls -ld.

     

    It's a pair of commands I use almost daily.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to iagorubio

    In all the years that I have been administering Unix systems, I can remember only one time where I actually tried xargs.  In short, I have no use for xargs at all.

     

    find /var/log  -type f -exec ls -ld {} \;

     

    or better yet.

     

    find /var/log/ -type f -ls

     

    On multi-user office systems where there are impossible filenames like "foo*bar.***"  and worse exist, getting the inode number of a file comes in handy

     

    cd /home/noobie

    ls -lai | | cat -v | more

    the cat -v turns escape char sequences like <clear screen> into something visible.

    when you see the offending filename the inode number will be first field

    Then it is a simple matter of

    find . -inum 12345 -exec mv {} newfilename \;

     

    I wonder what would break if I removed xargs from my systems.

     

    as a matter of fact, ls isn't really needed either.  When performing emergency surgery on a system, "echo *" or "echo .* *" is a reasonable substitute.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Former Member
    0 Former Member over 11 years ago in reply to iagorubio

    In all the years that I have been administering Unix systems, I can remember only one time where I actually tried xargs.  In short, I have no use for xargs at all.

     

    find /var/log  -type f -exec ls -ld {} \;

     

    or better yet.

     

    find /var/log/ -type f -ls

     

    On multi-user office systems where there are impossible filenames like "foo*bar.***"  and worse exist, getting the inode number of a file comes in handy

     

    cd /home/noobie

    ls -lai | | cat -v | more

    the cat -v turns escape char sequences like <clear screen> into something visible.

    when you see the offending filename the inode number will be first field

    Then it is a simple matter of

    find . -inum 12345 -exec mv {} newfilename \;

     

    I wonder what would break if I removed xargs from my systems.

     

    as a matter of fact, ls isn't really needed either.  When performing emergency surgery on a system, "echo *" or "echo .* *" is a reasonable substitute.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • rew
    0 rew over 11 years ago in reply to Former Member

    Your  ' -exec ls {} \' ' will execute ls for each file. If you want to do something very small for lots of files executing the new process ends up taking lots of time. So xargs to the rescue to bunch up a  number of arguments and execute the program many times less often. Nowadays we have '-exec ls {} +' .

     

    On the other hand, I have a raid array that has ten disks. When I do "find -ls", the find process will run until it needs something from disk wait for the result and then go on to need something new from the disk. In all either a single disk is doing something or the CPU is doing something. very little overhead. And just one of my ten disks is running. With xargs, I can tell it to run X processes in parallel, each having N arguments. The result is that I can get more of my disks to "DO" something (i.e. now almost all ten disks are acctive at the same time as opposed to max 1 in the simple case). The result is that I get almost a 10fold performance improvement of the overall process.

     

    Anyway, that's an example of where I used xargs that couldn't be solved with find alone.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to rew

    As with most thing  *nix, there is no one right way to do something.  I have gone almost 30 years without xargs.  I think I will do without it some more.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • rew
    0 rew over 11 years ago in reply to Former Member

    I've gone almost 20 years before discovering the wonders-of-xargs.... image

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to rew

    I still don't have any use for it..

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube