I am having trouble when trying to send information through the PC parallel port. I am using the following code to try to send data:

#include <stdio.h>
#include <asm/io.h>
#include <unistd.h>

int main(void){
ioperm( 0x378, 1, 1 );
outb( 0x378, 0xAA );
sleep( 2000 );
outb( 0x378, 0x55 );
sleep( 2000 );
outb( 0x378, 0x00 );
return 0;
}

Because it failed (for a reason that I am unaware of) I tried the following program which, I think, is equivalent to the previous one:

#include <asm/io.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>

int main(void){
const int PORT = 0x378;
int fd = open("/dev/port", O_WRONLY, 0);
unsigned char val;

if( fd < 0 ){
exit(1);
}
lseek(fd, PORT, SEEK_SET);
val = 0x50;
write(fd, &val, sizeof(val));
sleep(2000);
val = 0xA0;
write(fd, &val, sizeof(val));
sleep(2000);
val = 0x00;
write(fd, &val, sizeof(val));
close(fd);
return 0;
}
I'm running the program as the root user, so I have all priveleges. I am verifying the output with a bunch of LEDs that are supposed to turn on when I send a 1. I've already tried a similar code in Windows and it works as expected. Help or suggestions are warmly welcome.

Hi
I studied your code. It is better that you use

outportb(PORTADDRESS,databyte);
for example outportb(0x378,0x01);
Another mistake i found is sleep() is a function used to suspend the program execution by that much seconds you specify as the arguments.
You wrote sleep(2000); i.e. you are asking the program to hold for 2000 seconds. Instead you use
delay(milliseconds); to control the port.
All the best. If any queries regarding parallel port control in C. please be free to mail me to this ID : muralikvn81@yahoo.com

I am having trouble when trying to send information through the PC parallel port. I am using the following code to try to send data:

#include <stdio.h>
#include <asm/io.h>
#include <unistd.h>

int main(void){
ioperm( 0x378, 1, 1 );
outb( 0x378, 0xAA );
sleep( 2000 );
outb( 0x378, 0x55 );
sleep( 2000 );
outb( 0x378, 0x00 );
return 0;
}

Because it failed (for a reason that I am unaware of) I tried the following program which, I think, is equivalent to the previous one:

#include <asm/io.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>

int main(void){
const int PORT = 0x378;
int fd = open("/dev/port", O_WRONLY, 0);
unsigned char val;

if( fd < 0 ){
exit(1);
}
lseek(fd, PORT, SEEK_SET);
val = 0x50;
write(fd, &val, sizeof(val));
sleep(2000);
val = 0xA0;
write(fd, &val, sizeof(val));
sleep(2000);
val = 0x00;
write(fd, &val, sizeof(val));
close(fd);
return 0;
}
I'm running the program as the root user, so I have all priveleges. I am verifying the output with a bunch of LEDs that are supposed to turn on when I send a 1. I've already tried a similar code in Windows and it works as expected. Help or suggestions are warmly welcome.

Hi!

I'm having a similar program. Here is the code I am using -

#define DATA 0x378
void strober()
{
    int c;
    if (ioperm(DATA,3,1))
        fprintf(stderr,"You need to be root!\n");
    for (c=0; c<50; c++){
        fprintf(stdout,"LED should turn on.\n");    fflush(stdout);
        outb(0x07,DATA);
        sleep(t);
        fprintf(stdout,"LED should turn off.\n");   fflush(stdout);
        outb(0x00,DATA);
        sleep(t);
    }
}
int main(){
    strober();
}

The intention is to see a '1' (or 5V) on pins no. 2, 3 and 4 (D0, D1 and D2). However, when I check with the multimeter, the output is always 0V.
I am using pin 25 as the reference for ground.

I checked if the parallel port is working by checking the potential at pin 1. It was +5V.

Thanks
Kedar

PS: I am working on a laptop running Ubuntu. I am using a docking station since there is no parallel port on the laptop itself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.