LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2004, 08:40 AM   #1
zaheer031
LQ Newbie
 
Registered: Aug 2004
Posts: 9

Rep: Reputation: 0
Parallel port program not working correctly


Hello,

I am trying to write a small user land program for
transfer of data b/w 2 computers via parallel port. I
am using the Linux 2.4 parport system.

While receiving data, I get the same character
repeated multiple time, if I send 'good' I receive
'gggoooooddd' at the other end. I tried using
select(fd +1, &rfds, NULL, NULL, NULL); along with
PPWCTLOIRQ and PPWCONTROL, but I was not able to catch
any interrupt. I do not know what I am doing wrong.
Pls help....


/*CODE for transmitting data*/

#include<fcntl.h>
#include<stdio.h>
#include<errno.h>
#include<sys/ioctl.h>
#include<linux/ppdev.h>
#include<linux/parport.h>
#include<unistd.h>
#define BASE 0x378
int main(void){
int fd;
int mode;
char buf;
size_t got = 1;
int fd1, j=0;
int dir =0;// direction is 0 coz we need to transmit

/*Open the serial port for writing*/
fd = open ("/dev/parport0" , O_RDWR);

/* Open the file in which data to be transmitted is
present*/
fd1 = open("par",O_RDONLY);

/*gain access or permission for the port*/
if (ioctl (fd, PPCLAIM))

/*assign mode */
mode = IEEE1284_MODE_COMPAT;

/*set the desired mode*/
ioctl (fd, PPNEGOT, &mode);

/*set the direction of data flow(write) */
ioctl(fd, PPDATADIR, &dir);

/*until 1 or more character is there to read in the
file*/
for (;got == 1 {

/*read from the data file*/
got = read (fd1 , &buf, 1);

/*Write to the serial port*/
ioctl (fd, PPWDATA, &buf);

/*print the written data */
fflush(stdout);
printf("%c\n", buf);

/*delay a bit*/
usleep(1000);

/*output a value to control register*/
ioctl(fd, PPWCONTROL, &j);
j++;

/*if 5th bit of Control register is set, the data
lines cannot output data but they can only read, hence
j is changed from 1 to 31. i.e all the 4 bits of
control register are used*/
if(j == 31)
j=0;

}
}

/*End of code for transmitting data*/

I am printing out the received data in the CODE for
receivng data.

/*CODE for receiving data*/
#include<fcntl.h>
#include<sys/ioctl.h>
#include<linux/ppdev.h>
#include<linux/parport.h>
#include<stdio.h>
#include<sys/times.h>
#include<sys/types.h>
#include<sys/unistd.h>
#include<signal.h>

int fd;

/* this catcher() func is invoked when ctrl-C is
pressed*/
catcher(){
ioctl(fd, PPRELEASE);
exit();
}

int main(void){

int retval;
char buf;
int dir = 1; //since we are receiving data,
direction should be 1
int mode = IEEE1284_MODE_COMPAT; //used as a
parameter to set mode
char ch;

/* Open the serial port */
fd = open("/dev/parport0" , O_RDONLY );

/*catcht ctrl-c*/
signal(SIGINT, catcher);

/*gain access or claim the parallel port*/
ioctl(fd , PPCLAIM);

/*set the desired mode of communication(COMPAT in
this case*/
ioctl(fd, PPSETMODE, &mode);

/*set the direction of transmission(reception in
this case) */
ioctl(fd, PPDATADIR, &dir);

/*read value from Control register */
ioctl(fd, PPRCONTROL, &ch);
printf("Intial values of Control register is %d\n",
ch);

/*read value from Status register */
ioctl(fd, PPRSTATUS, &ch);
printf("Intial value of status register is %d\n",
ch);

while(1){
printf("B4 read{");//before reading from data
register

/*read value from Status register */
ioctl(fd, PPRSTATUS, &ch);
printf("Status->%d", ch);

/*read value from Control register*/
ioctl(fd, PPRCONTROL, &ch);
printf("Control->%d}\t",ch);

/* READ the Sent Data from data register*/
ioctl(fd, PPRDATA, &buf);
printf("Data->%c\t",buf);

printf("After read{"); // after reading from data
register

/*read from Control register*/
ioctl(fd, PPRCONTROL, &ch);
printf("Control->%d",ch);

/*read from Status register*/
ioctl(fd, PPRSTATUS, &ch);
printf("Status->%d}\n", ch);

/*delay a bit*/
usleep(1000);
}

return 0;
}
/*END of CODE for receiving data*/

------------SAMPLE OUTPUT-------------

Intial values of Control register is 12
Intial value of status register is 127

B4 read{Status->127Control->12} Data->ÿ After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->J After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->J After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->J After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->e After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->e After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->s After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->s After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->u After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->u After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->s After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->s After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->s After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->! After
read{Control->12Status->127}
B4 read{Status->127Control->12} Data->! After
read{Control->12Status->127}

----------END of output---------------
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I check to make sure my parallel port is working? M$ISBS Linux - Hardware 11 05-14-2005 09:40 AM
parallel port not working in suse 9.3 greenthing Linux - Hardware 1 05-06-2005 08:53 AM
Allow program to access parallel port LongName Linux - Newbie 5 08-27-2004 06:08 PM
How to get Parallel Port Joysticks (eg SNES pad) working in linux 2.6.4 kernel Tyco Linux - Hardware 1 06-06-2004 02:59 PM
Parallel Port Crashed_Again Linux - Hardware 1 01-20-2003 08:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 06:04 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
Open Source Consulting | Domain Registration