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

Hey everyone!

I am currently working on a summer project. What I need to do is control two relay boards, and they are linked to a device which will hold and execute the program, which sends a 4 byte packed signal to the boards. I know that the program works, because I made it first linking the boards to a computer. I used a module called Win32::SerialPort, which made it quite easy to connect to the boards. For the device I would use Device::SerialPort, but for some complicated reason it is nearly impossible to add this module to the device. Thus, I need to open a port without using a module like Device::SerialPort.

I need the following settings for the port:

Baud: 19200

Data: 8 bits

Stop: 1 bit

Parity: no

Handshake signal: no

Then after opening the port, I need to send two 4 byte signals. The boards will then send two 4 byte signals back, which I need to read and evauate.

Any help will be greatly appreciated!

Thomas Brouwer

  • Comment on Open serial port without Device::SerialPort

Replies are listed 'Best First'.
Re: Open serial port without Device::SerialPort
by BrowserUk (Patriarch) on Aug 04, 2011 at 13:22 UTC
    Thus, I need to open a port without using a module like Device::SerialPort.

    What OS does the "device" run?


    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 don't really know :P how can I find out? By the way, the ports are RS232 serial ports, if that's of any importance.

      Edit: It's a Linux operating system, kernel version 2.6.
Re: Open serial port without Device::SerialPort
by AnomalousMonk (Archbishop) on Aug 04, 2011 at 14:23 UTC

    From your OP and subsequent reply to BrowserUk, I get the impression that you are dealing with an embedded system of some kind, one that runs Linux and already has Perl installed. If so, is there any 'native' way to transfer a file onto this system? (How do you 'talk' to this board?) If a file or files can be transferred to the system, cannot the file or files that comprise Device::SerialPort be transferred in this way and installed by hand? See Yes, even you can use CPAN.

Re: Open serial port without Device::SerialPort
by tmaly (Monk) on Aug 04, 2011 at 14:36 UTC

    I have been able to use Device::SerialPort to communicate with an X11 home automation system. However, I ended up using a usb to serial converter as my netbook did not have a serial port connection. I run Ubuntu on my netbook, so I cannot comment on a windows system. Have a look at the article on doing this PDF from linux magazine

    Best regards
    Ty

Re: Open serial port without Device::SerialPort
by Anonymous Monk on Aug 05, 2011 at 05:56 UTC

    I suppose your "complicated reason" for not using Device::SerialPort is that you would need to cross-compile the XS part to ARM. I'm not sure why XS is used here but I suspect it may be about signal handling or some timing issue. I haven't tried this in Perl but it looks fairly straightforward.

      Yeah that sounds exactly like it :P one of the employees explained it to me, but due to my lack of expertise in programming I didn't fully understand it.

      Thanks also for your link. I tried it, but that program crashes for me (when running that piece of code the program freezes). Now, I came across another piece of code which seems to connect just fine (no error) but which does not properly send or receive any messages. Good thing to know is that when the boards receive a message they reply to it, even when the message sent is completely random. So it's likely that the sending part isn't properly functioning... Here is the code to set up the port:

      my $PORT = "/dev/ttyS0"; system ("baud=19200 stop=1 data=8 parity=n"); open DEV,"+<$PORT" or die "Failed to open port\n"; my $ofh = select(DEV); $| = 0; $/='>'; select($ofh);
      And here to send one of the 4 byte package and receive the output (and since there is no output, the program freezes there). This message is directed at the first board (that's why @data1 = 1).
      my @data = (3, 1, 255, 253); my $packeddata = pack("C*", @data); print DEV $packeddata; my $output = <DEV>; #print "Output: $output ";
      Thanks for all the replies so far! Let's hope we can solve this!

        You can check if the write succeeded by testing the return value of print. e.g. die $! unless print DEV $packeddata;