http://www.perlmonks.org?node_id=1011907

redss has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible for a perl program to detect continuity between two pins of a RS-232 serial port or of a parallel port?

I want my program to be triggered by the closing/opening of a physical switch which can be connected to my serial or parallel port

  • Comment on Need to detect continuity between 2 terminals

Replies are listed 'Best First'.
Re: Need to detect continuity between 2 terminals
by BrowserUk (Patriarch) on Jan 06, 2013 at 20:24 UTC

    If you have a physical parallel port on your machine and are using Windows, you could do it using Inline::C/Win32::API/XS via Inpout32.dll. (There is also a 64-bit version linked there.)

    See also Reading from Parallel Port using Inpout32-dll.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Need to detect continuity between 2 terminals
by jmlynesjr (Deacon) on Jan 06, 2013 at 21:17 UTC

    This is a pretty common thing to do. You should be able to detect RTS/CTS or DCD assuming as martell said the handshaking signals are supported by Device::SerialPort.

    Update1: Looks like both modem control(DCD) and handshaking(RTS/CTS) are both supported.

    if ($PortObj->can_wait_modemlines) { $rc = $PortObj->wait_modemlines( MS_RLSD_ON ); if (!$rc) { print "carrier detect changed\n"; } } if ($PortObj->can_modemlines) { $ModemStatus = $PortObj->modemlines; if ($ModemStatus & $PortObj->MS_RLSD_ON) { print "carrier detecte +d\n"; } }
    # controlling outputs from the port $PortObj->dtr_active(T); # sends outputs direct to hard +ware $PortObj->rts_active(Yes); # return status of ioctl call # return undef on failure

    James

    There's never enough time to do it right, but always enough time to do it over...

      Yeah that code example is just what I was looking for.

      I did forget to mention that this is for Win32.

      Unfortunately, Device::SerialPort is not available for Windows, and the Win32::SerialPort module doesnt seem to be available in the Activestate ppm repository.

      Which module should I be using?

        Win32::SerialPort and Win32API::CommPort are both listed on CPAN. Maybe you can install them from there. I am not familiar with Active state.

        James

        There's never enough time to do it right, but always enough time to do it over...

Re: Need to detect continuity between 2 terminals
by moritz (Cardinal) on Jan 06, 2013 at 19:17 UTC
Re: Need to detect continuity between 2 terminals
by Old_Gray_Bear (Bishop) on Jan 06, 2013 at 20:10 UTC
    Typically this kind of problem is simply addressed with a physical device (a VOM, for example) attached to the pins in question.

    Update: Now that I have had some coffee (and reread the original question thrice) -- you are not asking to detect a physical short circuit between two pins on the serial/parallel cable, right? If you are asking 'How Do I Detect a Data-Ready condition on the line?', then the answer is 'it depends'.

    Basically you will end up polling the connection (either in your code or in the OS), waiting for the next buffer of data from the line. We will need a little more information to start making educated guesses as to a solution.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Need to detect continuity between 2 terminals
by martell (Hermit) on Jan 06, 2013 at 20:59 UTC

    Short answer: No

    Long answer:

    Those are data communication ports and not general input/output pins (gpio) as found on some devices as the raspberry pi. Those gpio can be used to detect a closed or open state. Albeit you need to be careful not to fry the chip.

    As Old_Gray_Bear said: you need something bridges your input to the protocol on the serial and parallel port. There is plenty things available, but you need to go to more dedicated 'electronic' hacker forums for a good answer. From there you can probably use something like Device::SerialPort to read from the serial port.

    Kind regards

    Martell

      Those are data communication ports and not general input/output pins

      I beg to differ. From the perspective of the code, a parallel port is just a set of 3 registers -- 1x8-bit (data), 1x5-bit(status), 1x4bit(control) -- that can be read and written using the appropriate (INx/OUTx) instructions.

      On modern OSs these are privileged instructions, so you do need a device driver that exposes them to application code, but given that access, when the data port is read, any of the data pins (2-9 on a db25 plug) held high will be 1; with those held low as 0.

      With an appropriate device driver, this is easily programmed under windows (I've done it). And I read on wikipedia that "On Linux, inb() and outb() can be used when a process is run as root and an ioperm() command is used to allow access to its base address; alternatively, ppdev allows shared access and can be used from userspace if the appropriate permissions are set.".


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        I stand corrected. ;)

        Greetings

        Martell