Parallel port



  • Hello!

    Can someone tell me how to access the Parallel port through the shell? I am trying to write a program to activate a specific pin and to deactivate it depending on the input, but I don't know a whole lot about programming. So I started to make just a simple hello world program, but I can't compile it with gcc; it cannot find stdio.h or stdlib.h etc.

    Is the kernel somewhat incomplete in pfsense or am I just missing something?


  • Netgate Administrator

    There are no build tools in pfSense. Including them would be only a security risk on a firewall.
    To compile something for pfSense 2.0.x you need to do it on a FreeBSD 8.1 box.

    There should be no problem writing to the parallel port.

    Steve



  • Hi,
    Thanks for a quick answer!
    I am not so concerned with security on this server so is it possible to add the required build tools to pfsense?



  • It is stil a bad idea, but if you really have to, you could do something like pkg_add -r gcc. I have not done this before and it might not be the correct package, but pkg_add is the way to do it.
    i would still use a different freebsd box to do the build on.



  • I have tried to find the right package with pkg_add but nothing I have tried seems to work. Guess I will have to keep looking for the right package. I would use a different FreeBSD box if I could, but that is the only one with a parallel port and it's a bit harder to do it without the interface.



  • You should be able to compile on the second box, copy over and test. Just have to be creative with any debugging. It is interesting that you don't have another box with parallel port, even some of the newer high end MBs still have that port. Well anyway … good luck. BTW ... What is going to happen at the other end of the parallel port when you activate the pin? Just being curious.



  • This is a home environment so I don't have too many boxes just sitting around and that is also the reason I am not so concerned with the security. I started to do it on another machine, but I was missing some files associated with the parallel port that I guess is only present if you have a parallel port installed or something.

    I am going to build a power strip that I can control via the Parallel port. On the other end of that cable I will connect a transistor that controls a relay that control the power strip. In that way I could powercycle anything I want by using the program I am trying to create.


  • Netgate Administrator

    @matumbo:

    is it possible to add the required build tools to pfsense?

    No.  ;)

    Technically it may be possible but by the time you have added all the required tools and dependencies you will almost certainly have broken pfSense. Even if you did manage to do it the resulting system could not be upgraded without breaking I suspect.
    You can do this by booting from a USB stick or live CD (I think PC-BSD 8.1 has a live CD). Even if your spare box doesn't have a parallel port you can compile code and transfer it to test. That's what I did for my own hardware coding challenge.

    Steve



  • Okey, but is it possible to make the program on another distribution or would that not work? Does it have to be FreeBSD 8.1?



  • If you use another distro, you run the risk of missing libraries.



  • Okey. I guess I will try to boot up some computer with FreeBSD 8.1 or use another Distro and make sure I have got all libs.
    Thanks for the help!


  • Netgate Administrator

    There aren't that many FreeBSD based distros to choose from. BSD is not Linux.  ;)
    If your program is sufficiently basic then it will probably work across FreeBSD versions. The various lcdproc drivers that connect via the parallel port continued to function between pfSense 1.2.3 (FreeBSD 7.3) and 2.0 (FreeBSD 8.1).

    Steve

    Edit: you could try this: http://forum.pfsense.org/index.php/topic,52691.0.html
    You might want to use 32bit sources, whatever is appropriate for you.



  • Are there any restrictions to execute programs aswell? I tried to execute a program as root that I compiled on another distro but it returns Permission Denied.



  • I'd just install a virtual machine of FreeBSD 8.1 (assuming you're running 2.0.1, you can get the 8.1 iso from ftp-archive.freebsd.org), compile it there, copy it over. The only thing you'll have to do after copying over a binary is make it executable, chmod +x filename.



  • I tried the thing with chroot, but it seems like I don't have the rsync-command supported.
    I installed FreeBSD 8.1 on another computer in VirtualBox. I compiled my program there and copied it to my pfsense router, made it executable and tried to run it. It seems like somthing else is missing because I got the following error: /libexec/ld-elf.so.1: /root/program/a.out: Shared object has no run-time symbol table

    When I tried to run it on my Virtual machine with FreeBSD it worked just fine except that I could write anything to the parallel port due to segfault. I suspect that it might be because it's in a Virtual machine.


  • Netgate Administrator

    Hmm, you have probably included a library that isn't present in pfSense. Which you probably don't need anyway.
    I included code in my arm/disarm LED program to switch a single pin on the parallel port, in this case it switches the LCD backlight in the Firebox. Have a look at the code I used and try not to laugh too hard at my terrible coding style.  ;)
    http://forum.pfsense.org/index.php/topic,32013.msg270534.html#msg270534

    Steve



  • Can't say that I am good at programming, so I am not in a position to laugh at anything ;)
    From what I understand I need ioperm to access the parallel port and I guess that is included in one of these:
    #include <machine cpufunc.h="">#include <machine sysarch.h="">Since I have no libs nor headers at all in pfsense I guess it is not possible to include anything in the code? Or how does it work?</machine></machine>



  • I've managed with help from a friend to create a program that should work and I compile it in my VirtualBox FreeBSD 8.1 and it seems to work there but when I copy it to PFsense it doesn't. It complains about
    /libexec/ld-elf.so.1: /root/program/a.out: Shared object has no run-time symbol table
    or
    Exec format error. Binary file not executable.
    or
    /libexec/ld-elf.so.1: /usr/lib/: invalid file format.


  • Netgate Administrator

    Hmm, something odd here. As is says in the code I cut and pasted from lcdproc:

    Use i386_get_ioperm, i386_set_ioperm from <machine sysarch.h="">and inb and outb from <machine cpufunc.h="">(FreeBSD)</machine></machine>

    Compiles and runs using gcc no problems for me.  :-\

    Steve



  • Did you compile it on another machine and copy it to PFsense?
    I tried to run your program on pfsense and it worked just fine, but if I compiled it on my virtual box and moved it to Pfsense it wouldn't work. I thought that should work since it's the same release of FreeBsd.

    Edit: Do I need to use any special options with gcc to compile it or just "gcc code.c" ?



  • @matumbo:

    Do I need to use any special options with gcc to compile it or just "gcc code.c" ?

    Just a guess, but you probably want to compile and statically link to all libraries that are not available on your pfsense box.



  • @gderf:

    @matumbo:

    Do I need to use any special options with gcc to compile it or just "gcc code.c" ?

    Just a guess, but you probably want to compile and statically link to all libraries that are not available on your pfsense box.

    How can I do that? I am not very good at programming nor compiling.


  • Netgate Administrator

    I am using a real machine, an old laptop, and the only other option I specified was the output file:

    root@.box/home/admin# gcc -o WGXepc WGXepc.c
    
    

    Is this a 32bit vs 64bit problem?

    Steve



  • Okey, but it should work with a Virtual Machine aswell. I am running a 32bit-machine.


  • Netgate Administrator

    Yep, should work.  :-
    I'm out of my depth here.  ;)

    Steve



  • Found out that this means that the libraries the headers refer to are not found in the system:
    /libexec/ld-elf.so.1: /root/program/a.out: Shared object has no run-time symbol table

    Are there no libs present in pfsense? And can someone tell me how to statically link libraries to the program?


  • Netgate Administrator

    About the only thing I can think you may have done is used some cut down version of FreeBSD. Alternatively I have installed some extra stuff in my FreeBSD install.  :-
    I don't remember doing that but some package or other may have pulled in some dependencies. It's a few years since I installed it.

    Steve



  • I installed with the dvd image, which I think is the full version and I installed all standard packets or something like that. But it seems like the problem is not with my virtual FreeBSD, it's with my pfsense missing libraries which I suppose is normal. From what I understand, the only possible solution is for me to somehow statically link the required libraries into the program so that they are not needed on pfsense when I execute the program. But what I really don't understand is why I can run your program with no problem but if I compile it by myself it won't work. Especially when you didn't use any option with gcc to compile it.


  • Netgate Administrator

    @matumbo:

    what I really don't understand is why I can run your program with no problem but if I compile it by myself it won't work. Especially when you didn't use any option with gcc to compile it.

    It's exactly that reason makes me think it's a problem with your compile box.
    One way to test would be if I compile your code (unless it's top secret).

    Steve



  • @stephenw10:

    It's exactly that reason makes me think it's a problem with your compile box.
    One way to test would be if I compile your code (unless it's top secret).

    Steve

    You are welcome to compile it, maybe that would do the trick :P But I'm not sure the program is working correctly yet since I haven't been able to try it at all.

    code.c.png


  • Netgate Administrator

    Ok, Here is your compile code.
    However it doesn't seem to work. It's hard to say because it doesn't have many printf statements so it just does nothing.  ;)

    Running it on the compile box:

    root@.box/home/admin# ./readio 378
    Reading 378 :4
    root@.box/home/admin# ./code 2 378
    fel antal argroot@.box/home/admin# ./code 2
    root@.box/home/admin# ./readio 378
    Reading 378 :32
    
    

    Running on my pfSense box:

    [2.0.1-RELEASE][root@pfsense.fire.box]/tmp(6): ./code
    fel antal arg[2.0.1-RELEASE][root@pfsense.fire.box]/tmp(7):
    
    

    Seems to run fine. I didn't try to actually write any registers as that box needs to stay up!

    When I was investigating the firebox LED I wrote a program almost the same as yours: writeio and another to compliment it: readio. I now think there is a function in FreeBSD to do this already but it was a fun exercise. See attachments.

    Steve

    Edit: Actually your code seems to work, just not as I'd expected.

    code.png
    writeio.png
    writeio.c.png
    readio.png



  • Thanks!
    I know, but as I said it's not really complete :P I want to add some messages that tell me what I am doing, and I want to be able to control specific pins like this: "./code 1h 2l" which would set the first pin to 1 and the second to 0. This program was a test to see if me and my friend had found the right way to communicate with the port.

    What does Readio and writeio do?

    I tried to statically link the library libc.a to my program while compiling(thought that might have been what was missing in pfsense) and after that I didn't get the same error message on my pfsense box. Instead it was a segmentation fault. Don't know which of the error-messages I prefer :P

    I haven't received my equipment that is going to be connected to the parallel port yet, so I can't try if it works correctly right now. I am thinking that I might be able to use a multimeter/multitester to see If I can change one of the datapins to 1. The supported command for the program should be: 0, 1, 2 or 3.
    That would translate to:
    0= set both to 0
    1= set first datapin to 1
    2= set second datapin to 1
    3= set first and second datapin to 1


  • Netgate Administrator

    readio and writeio allow you to read and write any memory mapped io space, such as the parallel port or in my case the southbridge gpio pins.
    They are used:
    readio (address)
    writeio (address, value)
    Values in Hex.

    e.g.:

    root@.box/home/admin# ./readio 378
    Reading 378 :34
    root@.box/home/admin# ./writeio 378 21
    Setting 378 to 21
    root@.box/home/admin# ./readio 378
    Reading 378 :21
    
    

    Steve



  • I just tried your programs(writeio and readio) and they are great :) I used a multimeter to verify that the port did output 50 mA when I set a pin to 1 and it seems to work. I tried my program as well and it worked :) Can't understand why it works when you compile it, but I am happy it does :P

    I received the equipment I've been waiting for today, so now I will start to build the circuit to control my power strip.
    Thank you for all the help you have given me!

    //Matumbo


Locked