User:Sine over cosine

From DSL Wiki

Contents

DSL for X10 control

Intro I'm fairly new to DSL and currently have it installed on an old system with a 4 GB hard drive and 32MB of memory. The install was done as a hard drive install. After reading more about DSL, I realize that a Frugal install would have been better... If/when I have time I may redo the install...

DSL as an X10 controller

Summary

  • System connected to home network
  • monkey web server set up
  • cron30.dsl installed
  • perl cgi scripts to communicate over serial port to old CP-290 X-10 controller


Functionality

Allows control of my many X10 modules by a web page as well as the option of using cron for scheduling on/off/dim events.

Perl Serial Port Subroutine

In order to talk to the CP290 with DSL I needed a way to communicate without using the Device:SerialPort module. I figured it wasn't worth the effort for me to get that working in DSL. With a little research I found what I needed.

OK my code is not a thing of beauty... and I'm sure it could be done shorter and better but it does work. The lines involved with opening the serial port and writing/reading were inspired by an old web page of Paul Haas(1).

Part of my cgi script

         sub output_cmd
         {
          $err = 1;
          # Set up command bytes
          $cmd_out = $byte1_to_16 . $byte17 . $byte18 ;
          $cmd_out .= $byte19 . $byte20 . $byte21 . $byte22;
          # $port = /dev/ttyS0 for my system
          open ("sPORT","+<$port") || die "failed opening $port : $!";
          # Set up serial port for 600 baud, no echo, 8 Bit, 1 Stop, raw in/out
          system("stty 600 -echo cs8 -cstopb raw < $port"); 
          #output command
          $len= length $cmd_out;
          syswrite sPORT, $cmd_out, $len;
          #get response from CP290, does not use handshaking
          # waits a maximum of 9 seconds to get a complete response
          # .... 6 x (  1sec from sleep command   + .5 sec from select command )
          # (Dim commands take a while to complete) 
          $len= 0;
          $count=1;
          $inbuf="";
          #Using the vec ,select, and sysread commands to get the data
          vec($rin,fileno("sPORT"),1) = 1;
          READ: while($count < 7)
           {
            sleep(1);
            while(select($rout=$rin, undef, undef, .5))
             {
              $char="";
              sysread ("sPORT", $char,1);
              $inbuf .= $char;
              $len = length $inbuf;
             }
            $count +=1;
            # If successful CP290 will respond with 
            # an acknowledge that is 19 char long 
            if ($len == 19)   # equals success
             {
              $err = 0;       # clear error flag
              last READ;      # exit loop
             }
            }
          return($err)

References

(1) Paul Haas - http://www.hamjudo.com/rcx "Talk to an RCX from a serial port on a Linux box"

Future

If/when I have time I hope to provide more detail .... such as describing the 22 byte direct command format for the CP290