Parallel Port Access

Linux specific questions.
Post Reply
jessedavis
Posts: 8
Joined: Jul 18, 2015 12:47

Parallel Port Access

Post by jessedavis »

If I weren't bald I'd be pulling my hair out now. I want to output one bit via the first parallel port. My bios says it's at 378 hex. The Linux operating system says it's there and functioning. FreeBasic says I just need to use the OUT command---->
OUT
Outputs a value to a hardware port.
Syntax:
declare function Out ( byval port as ushort , byval data as ubyte ) as long
Usage: Out port,value
Parameters: port - Hardware port to write to.
data - Data value to write.
Description: This function sends value to port and returns immediately.

The manual offers an example which I shall not quote here. I test it but nothing happens....not even a little error message. I know the hardware works because I wrote the routine in Python and it works just fine. To avoid operating system problems I removed the lp package and ran the FB routine as root. Nothing. I have a nagging suspicion that the FB compiler knows better than I do what I want to do and is disallowing direct hardware writes. Or maybe they are being intercepted by the OS and Python has a way around this. In any event, the documentation is not correct. My code follows:

Code: Select all

dim as ushort basepnt
dim as ubyte a, b
dim as double t0
dim as integer i

basepnt = &h378
a = 0
b = 255
for i = 1 to 100
    out basepnt, a
    print using "     &    &";bin(a); bin(b)
    t0 = timer()
    while abs(t0 - timer()) <1
    wend
    out basepnt, b
next i
Any thoughts as to what I'm missing would be appreciated.

Thanks in advance,
jd
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Parallel Port Access

Post by counting_pine »

I'm not sure. But it looks like you can find the specific runtime lib source at http://sourceforge.net/p/fbc/code/ci/ma ... s_portio.c:

Code: Select all

int fb_hOut( unsigned short port, unsigned char value )
{
#if defined HOST_X86 || defined HOST_X86_64
	if (!__fb_con.has_perm)
		return fb_ErrorSetNum( FB_RTERROR_NOPRIVILEGES );
	__asm__ volatile ("outb %0, %1" : : "a" (value), "d" (port));
	return FB_RTERROR_OK;
#else
	return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL );
#endif
}
It looks straightforward enough. If you're happy using C, you could try the __asm__ volatile line and see if it works on its own.
You could also try capturing the return value of Out, and see if it returns a nonzero value.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Parallel Port Access

Post by caseih »

Linux has a driver called ppdev that provides an interface to the parallel port at a file like /dev/parport0. It's possible that whatever module you used in Python is using this interface, rather than the direct i/o address. You might try removing the ppdev driver and see if your FB code functions (will need to be root I think).

If you could rewrite your code to use /dev/parport0, if you gave it the correct permissions your FB code should be able to access it as a non-root user.

A quick google search shows me this document which may or may not be relevant:
http://as6edriver.sourceforge.net/Paral ... ssing.html
jessedavis
Posts: 8
Joined: Jul 18, 2015 12:47

Re: Parallel Port Access

Post by jessedavis »

Linux has a driver called ppdev that
Thanks Caseih. You are correct that Python uses uses a driver. It was just an easy way to test the port. My intended application needs speed so I am reluctant to use Python. I could use C as suggested by counting_pine. I just thought that FreeBasic was an easy way to get the job done quickly. I was puzzled when it did not function as the manual said it would.

Thanks for all your help,

Regards.
jd
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Parallel Port Access

Post by caseih »

I see no reason why you can't use FB. If you can do it in C you can also do it in FB. The example code in the link I posted should translate fairly simply and quickly to FB, as it's just using libc calls (file open and ioctl). I'm not sure how to use ioctl on FB file handles but I'm sure there's a fairly straight-forward way.
Post Reply