LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-17-2009, 02:13 AM   #31
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231

yeah i was in a hurry and the last command was incorrect
 
Old 10-17-2009, 02:13 AM   #32
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 987
Blog Entries: 4

Rep: Reputation: 252Reputation: 252Reputation: 252
Re: "Teh Win" or "Fail" ?

No luck with "i386_set_ioperm". So I'm back to my previous revision;

Code:
sysop$ gcc -O2 -o rccar rccar.c
sysop$ ldd rccar
        linux-gate.so.1 =>  (0xffffe000)
        libc.so.6 => /lib/libc.so.6 (0xb7f0a000)
        /lib/ld-linux.so.2 (0xb806b000)
sysop$ ./rccar
ioperm: Operation not permitted
However, I noticed that I only get the "Op not permitted" error when I try to run it as a user.

If I try to run it as root, it loops with the following....

Code:
u pressed

u pressed

u pressed

(ad naseum)
Throwing a break at it doesn't help, I have to kill my session and fire up a new xterm.
 
Old 10-17-2009, 02:16 AM   #33
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
i think you need to run your program as root
try
Code:
sudo ./rccar
or
Code:
sudo su -
./rccar
exit
----edit----
oops just noticed your last post
the reason it is looping is there is somthing wrong

Last edited by smeezekitty; 10-17-2009 at 02:18 AM.
 
Old 10-17-2009, 02:18 AM   #34
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 987
Blog Entries: 4

Rep: Reputation: 252Reputation: 252Reputation: 252
My Current rccar.c

Okay, here's my current rccar.c version that "u pressed" loops.

Full Version of "u pressed" looper:

Code:
/*
 * * rccar.c: lpt port control of a radio controlled car
 * *
 * * MORE INFO HERE:       http://www.engr.uvic.ca/~sbowman/prog.php
 * *
 * * By:            Shaun Bowman Sept. 2003, shaun_bowman@hotmail.com
 * *
 * * Notes: This program is unlikely to be ready-2-port because of the ioctl
 * method
 * *      of changing terminal settings. This program was written so i could
 * *      control a tiny 10$ remote control car (and to procrastinate homework).
 * *      The lpt port pins (6,7,8,9) are used to control Forward, Reverse, Left
 * *      and Right in the controller. In addition to this program, I had to build
 * *      a simple circuit to bypass the buttons on the remote control. This is
 * *      because even in the low state, the Control and Ground pins still form a
 * *      complete circuit.
 * *
 * *      Crude Diagram (1 for each button):
 * *
 * *      GrndPin(18-25)   PinX(5-9)
 * *            |               |                          T1: NPN transistor, i used 2N222
 * *            |               |
 * *            |               L---R1--R2--R3          R1-R3: Resistors, i didn't have
 * *            |        T1:                 |                 ones with high enough
 * *            |--------|\                  |                 resistance, so i had to use
 * *            |        | ------------------                  3. Total R ~= 100k ohm's
 * *               |    |---|/(<-collector pin)
 * *            |    |
 * *      -ButtnLead  +ButtnLead
 * *
 * * Compile with: gcc -O2 -o rccar rccar.c (must be root)
 * *
 * * Pin bits (numbered at female end)
 * *  outb(8, x)   = pin 5      UNUSED
 * *  outb(16, x)  = pin 6      Forward
 * *  outb(32, x)  = pin 7      Reverse
 * *  outb(64, x)  = pin 8      Left
 * *  outb(128, x) = pin 9      Right
 * *  outb(80, x)  = pin 6 & 8  FORWARDLEFT
 * *  outb(144, x) = pin 6 & 9  FORWARDRIGHT
 * *  outb(96, x)  = pin 7 & 8  REVERSELEFT
 * *  outb(160, x) = pin 7 & 9  REVERSERIGHT
 * */

#include <stdio.h>
#include <unistd.h>
/* #include <io.h> */
#include <string.h>
#include <libio.h>
#include <stdlib.h>
#include <sys/io.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>

#define LPT1PORT 0x378  /* lpt1 */

int main()
{

        struct termios oldT, newT;      /* create our termios identifiers, uphere because C++ is retarted*/

        void forward( char key ){
                        outb(16, LPT1PORT);     /* Set pin 9 to high */
                                usleep(10000);  /* Sleep for a while (100 ms) */
        }

        void reverse( char key ){
                        outb(32, LPT1PORT);
                                usleep(10000);
        }

        void left( char key ){
                        outb(64, LPT1PORT);
                                usleep(10000);
        }

        void right( char key ){
                        outb(128, LPT1PORT);
                                usleep(10000);
        }

        void forwardleft( char key ){
                        outb(80, LPT1PORT);
                                usleep(10000);
        }

        void forwardright( char key ){
                        outb(144, LPT1PORT);
                                usleep(10000);
        }

        void reverseleft( char key ){
                        outb(96, LPT1PORT);
                                usleep(10000);
        }

        void reverseright( char key ){
                        outb(160, LPT1PORT);
                                usleep(10000);
        }


        void stop( char key ){
                        outb(0, LPT1PORT);      /* Set the data signals (D0-7) of the port to all low (0) */
                                usleep(10000);
        }

        void end () {           /* The Beggining of the end.... */
        outb(0, LPT1PORT);
        usleep(10000);
        if (ioperm(LPT1PORT, 3, 0)) {perror("ioperm"); exit(1);}                /* We don't need the port anymore */
        ioctl(0,TCSETS,&oldT);  /* Better set the terminal back to normal... :-) */
                                                        exit(0);        /* Buh bye */
        }


        if (ioperm(LPT1PORT, 3, 1)) {perror("ioperm"); exit(1);}        /* Get access to the ports */

        ioctl(0,TCGETS,&oldT);  /* get current terminal mode */
        newT=oldT;              /* copy it into our new structure */
        newT.c_lflag &= ~ECHO;  /* toggle echo */
        newT.c_lflag &= ~ICANON;        /* toggle no line delimination */
        ioctl(0,TCSETS,"wT");   /* initialize our new terminal mode */
        char i[1];

        while(1==1){

        read(0,&i[0],1);        /* read one char from the buffer */
        printf("\nu pressed %c\n", i[0]);
        if (i[0] == '8') { forward(i[0]); }
        if (i[0] == '2') { reverse(i[0]); }
        if (i[0] == '4') { left(i[0]); }
        if (i[0] == '6') { right(i[0]); }
        if (i[0] == '7') { forwardleft(i[0]); }
        if (i[0] == '9') { forwardright(i[0]); }
        if (i[0] == '1') { reverseleft(i[0]); }
        if (i[0] == '3') { reverseright(i[0]); }
        if (i[0] == '5') { stop(i[0]); }
        if (i[0] == 'q') { end(); }
}
}
/* end of rccar.c */
 
Old 10-17-2009, 02:20 AM   #35
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
i think you should replace read(0,&i[0],1);
with i[0]=getch();
 
Old 10-17-2009, 02:21 AM   #36
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 987
Blog Entries: 4

Rep: Reputation: 252Reputation: 252Reputation: 252
Looper Chunk?

I think the problem is somewhere in this chunk;

Code:
   read(0,&i[0],1);        /* read one char from the buffer */
   printf("\nu pressed %c\n", i[0]);
   if (i[0] == '8') { forward(i[0]); }
   if (i[0] == '2') { reverse(i[0]); }
   if (i[0] == '4') { left(i[0]); }
   if (i[0] == '6') { right(i[0]); }
   if (i[0] == '7') { forwardleft(i[0]); }
   if (i[0] == '9') { forwardright(i[0]); }
   if (i[0] == '1') { reverseleft(i[0]); }
   if (i[0] == '3') { reverseright(i[0]); }
   if (i[0] == '5') { stop(i[0]); }
   if (i[0] == 'q') { end(); }
However, I'm really starting to run dry. I haven't exactly had a lot of practice with C.
 
Old 10-17-2009, 02:21 AM   #37
Danteleo
LQ Newbie
 
Registered: Oct 2006
Posts: 28

Original Poster
Rep: Reputation: 16
YEAH!!!!!

It works!!!

Thank you both for all of your help.

BTW... "q" quits.
 
Old 10-17-2009, 02:22 AM   #38
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
please read my last post
i find read is not that good for getting a single char from stdin
 
Old 10-17-2009, 02:24 AM   #39
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
please press the blue thumbs up on any posts that help you from anybody
and mark this thread solved
 
Old 10-17-2009, 02:26 AM   #40
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 987
Blog Entries: 4

Rep: Reputation: 252Reputation: 252Reputation: 252
Can you post the whole rccar.c that you got to work?
 
Old 10-17-2009, 02:30 AM   #41
Danteleo
LQ Newbie
 
Registered: Oct 2006
Posts: 28

Original Poster
Rep: Reputation: 16
Here is the working (no code errors) script rccar.c



Code:
/*
* rccar.c: lpt port control of a radio controlled car
*
* MORE INFO HERE:       http://www.engr.uvic.ca/~sbowman/prog.php
*
* By: 	    Shaun Bowman Sept. 2003, shaun_bowman@hotmail.com 
*
* Notes: This program is unlikely to be ready-2-port because of the ioctl 
method
*	  of changing terminal settings. This program was written so i could
*	  control a tiny 10$ remote control car (and to procrastinate homework).
*	  The lpt port pins (6,7,8,9) are used to control Forward, Reverse, Left
*	  and Right in the controller. In addition to this program, I had to build
*	  a simple circuit to bypass the buttons on the remote control. This is
*	  because even in the low state, the Control and Ground pins still form a
*	  complete circuit.
*
*	  Crude Diagram (1 for each button):
*
*	  GrndPin(18-25)   PinX(5-9)
*		|		|			   T1: NPN transistor, i used 2N222
*		|		|
*		|		L---R1--R2--R3		R1-R3: Resistors, i didn't have
*		|	 T1:		     |	               ones with high enough
*		|--------|\		     |                 resistance, so i had to use
*		|        | ------------------                  3. Total R ~= 100k ohm's
*               |    |---|/(<-collector pin)
*		|    |
*      -ButtnLead  +ButtnLead
*
* Compile with: gcc -O2 -o rccar rccar.c (must be root)
*
* Pin bits (numbered at female end)
*  outb(8, x)   = pin 5      UNUSED
*  outb(16, x)  = pin 6      Forward
*  outb(32, x)  = pin 7      Reverse
*  outb(64, x)  = pin 8      Left
*  outb(128, x) = pin 9      Right
*  outb(80, x)  = pin 6 & 8  FORWARDLEFT
*  outb(144, x) = pin 6 & 9  FORWARDRIGHT
*  outb(96, x)  = pin 7 & 8  REVERSELEFT
*  outb(160, x) = pin 7 & 9  REVERSERIGHT
*/
#define outb(n, port) outportb(port, n)
#define inpb inportb#define outb(n, port) outportb(port, n)
#define inpb inportb

#include <sys/io.h>

#include <stdio.h>
#include <unistd.h>
/* #include <io.h> */
#include <string.h>
#include <libio.h>
#include <stdlib.h>

#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>

#define LPT1PORT 0x378 								/* lpt1 */

int main()
{

struct termios oldT, newT; 							/* create our termios identifiers, uphere because C++ is retarted*/

void forward( char key ){
	outb(16, LPT1PORT);							/* Set pin 9 to high */
	usleep(10000);								/* Sleep for a while (100 ms) */
}

void reverse( char key ){
	outb(32, LPT1PORT);
	usleep(10000);
}

void left( char key ){
	outb(64, LPT1PORT);
	usleep(10000);
}

void right( char key ){
	outb(128, LPT1PORT);
	usleep(10000);
}

void forwardleft( char key ){
	outb(80, LPT1PORT);
	usleep(10000);
}

void forwardright( char key ){
	outb(144, LPT1PORT);
	usleep(10000);
}

void reverseleft( char key ){
	outb(96, LPT1PORT);
	usleep(10000);
}

void reverseright( char key ){
	outb(160, LPT1PORT);
	usleep(10000);
}


void stop( char key ){
	outb(0, LPT1PORT);							/* Set the data signals (D0-7) of the port to all low (0) */
	usleep(10000);
}

void end () {									/* The Beggining of the end.... */
	outb(0, LPT1PORT);
	usleep(10000);
	if (ioperm(LPT1PORT, 3, 0)) {perror("ioperm"); exit(1);}		/* We don't need the port anymore */
	ioctl(0,TCSETS,&oldT);							/* Better set the terminal back to normal... :-) */
	exit(0);								/* Buh bye */
}


if (ioperm(LPT1PORT, 3, 1)) {perror("ioperm"); exit(1);}			/* Get access to the ports */

ioctl(0,TCGETS,&oldT); 								/* get current terminal mode */
newT=oldT; 									/* copy it into our new structure */
newT.c_lflag &= ~ECHO;								/* toggle echo */
newT.c_lflag &= ~ICANON;							/* toggle no line delimination */
ioctl(0,TCSETS,"wT");								/* initialize our new terminal mode */
char i[1];

while(1==1){

	read(0,&i[0],1);							/* read one char from the buffer */
	printf("\nu pressed %c\n", i[0]);
	if (i[0] == '8') { forward(i[0]); }
	if (i[0] == '2') { reverse(i[0]); }
	if (i[0] == '4') { left(i[0]); }
	if (i[0] == '6') { right(i[0]); }
	if (i[0] == '7') { forwardleft(i[0]); }
	if (i[0] == '9') { forwardright(i[0]); }
	if (i[0] == '1') { reverseleft(i[0]); }
	if (i[0] == '3') { reverseright(i[0]); }
	if (i[0] == '5') { stop(i[0]); }
	if (i[0] == 'q') { end(); }

}

}

/* end of rccar.c */
 
Old 10-17-2009, 02:41 AM   #42
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 987
Blog Entries: 4

Rep: Reputation: 252Reputation: 252Reputation: 252
Strange, that doesn't go into a looping fit of "u pressed" for you? Maybe it's because I'm SSH'ing into a box, rather than being on the console.


Smeezekitty, I'm not able to get this version of rccar.c to compile with "i[0]=getch();" in lieu of the "read" line. But I'll give this another crack later this morning.

I'll be sure to post back if I have any luck
 
Old 10-17-2009, 08:13 PM   #43
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by xeleema View Post
Smeezekitty, I'm not able to get this version of rccar.c to compile with "i[0]=getch();" in lieu of the "read" line. But I'll give this another crack later this morning.

I'll be sure to post back if I have any luck
what errors did you get?
 
Old 10-17-2009, 09:03 PM   #44
ShaunBowman
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Rep: Reputation: 1
Wow!

Nice work guys.

It's been a long time, and many distrobution releases, since I created this program.

I got an email from one of you (?Danteleo?) who forwarded me a link to this forum.

I'd liked to have helped, but it's been a while since i've had linux on my computer. Congrats and many thanks to all of you!


This was a super fun project and you wouldn't believe all the people who have written to me about it over the years. I will take the working code, Danteleo your 12:30 post, and replace the source on my website's, www.engr.uvic.ca/~sbowman, and www.shaunbowman.com

Please let me know / send me photo's of the finished goods. BTW this program originally got "out on the net" when it was posted on the front page of this website: http://hackaday.com/?s=rccar.c, they didn't tell me they did it, i just got thousands of visitors to my site all of the sudden and a ton of emails.

Because of the way the program works, changing the operating system to not wait for a carridge return, and accessing the LPT port directly, you must run it as root.

Also, as posted earlier, the program run's until you enter 'q'.
 
Old 10-18-2009, 01:10 AM   #45
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 987
Blog Entries: 4

Rep: Reputation: 252Reputation: 252Reputation: 252
Hey Shaun! Thanks for dropping by!
I was really curious about that as-root requirement; so it's only needed during runtime, and not during compile-time, eh?

FYI: It worked on my distro - Slackware 13 - 32-bit - stock kernel 2.6.29.6 - glibc 2.9
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
make: *** [all] Error 2 ? Need Help compiling DOSBOX source code! d3vi4nt Linux - Software 7 01-08-2010 12:41 AM
Getting cannot find symbol error compiling java code using NetBeans 6.5 IDE rstewart Programming 1 02-13-2009 04:43 PM
c++ code compiling error k1ll3r_x Programming 4 10-03-2006 02:41 AM
error in compiling simple code Tinku Programming 6 09-17-2004 01:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration