ADICON Support Forum Applied Digital, Inc.
Page 1 of 1 1
Topic Options
#18718 - 09/09/07 10:34 AM Open Source Interface to CPU-XA or Ocelot
Richard_Cooke Offline
newbie


Registered: 09/06/07
Posts: 10
Loc: Toronto, ON, Canada, eh?
The program below is written in Python (www.python.org) and gives you both an interactive terminal to do X10 commands, as well as will run whatever X10 commands you dictate as a stand-alone program.

You control the mode (interactive or program) by how you open this file. If you just RUN the file (I called it MAIN.PY on my system) then it executes all instructions at the bottom of the file inside the "If __name__ == __main__" block. If you run Python first, then IMPORT this file like this: "from main import *" then you can interactively give X10 commands and view the results. This is an easy way to develop and test your commands before you try and run them as a script.

To use this you have to first install Python and pySerial, both are free and available on www.python.org. Windows users also have to install pyWin - 32-bit Windows extensions for Python, also free and on the same site.

You also download the file from http://www.TurnkeyAutomation.net/main.py

Code:

'''CPU-XA Interface program'''

import sys, os, time
import serial

GETTYPE = chr(42) + chr(0) + chr(0) + chr(1) + chr(176) + chr(0) + chr(148) + chr(39)
GETVER = chr(42) + chr(0) + chr(0) + chr(130) + chr(176) + chr(0) + chr(246) + chr(45)
GETX10 = chr(200) + chr(56) + chr(0) + chr(0) + chr(0) + chr(0) + chr(0) + chr(0)

X10NODATA = chr(6) + chr(0) + chr(6) + chr(99) + chr(99) + chr(0)
X10HOUSE = 'ABCDEFGHIJKLMNOP'

X10KEY = ['1' , \
'2' , \
'3' , \
'4' , \
'5' , \
'6' , \
'7' , \
'8' , \
'9' , \
'10', \
'11', \
'12', \
'13', \
'14', \
'15', \
'16', \
'All Units Off', \
'All Lights On', \
'On', \
'Off', \
'Dim', \
'Bright', \
'All Lights Off', \
'Extend Code', \
'Hail Req.', \
'Hail Ack.', \
'Preset Dim 0', \
'Extend Data', \
'Status ON', \
'Status OFF', \
'Status Req', \
'Preset Dim 1']

ser = serial.Serial(2, 9600, timeout=1, xonxoff=0, rtscts=0, dsrdtr=0 ) # COM3


def main(argv=[]):
'''Main function'''
pass

def sendX10(house, key, repeat=0):
'''Send passed X10 command'''
Tx = chr(200) + chr(55) + chr(X10HOUSE.index(house)) + chr(X10KEY.index(key)) + chr(repeat) + chr(0) + chr(0)
Tx += chr(csum(Tx))
# dumpString(Tx)
ser.write(Tx)


def csum(str):
'''Calculate simple 8-bit check-sum'''
cs = 0
for x in str:
cs += ord(x)
return (cs & 0xFF)


def getX10():
'''Ask the CPU-XA if it has any X10 messages'''
ser.write(GETX10); time.sleep(1)
if ser.inWaiting() > 0:
rcv = ser.read(ser.inWaiting())
if X10NODATA != rcv:
parseX10(rcv)
else:
print 'No X10 data available at', time.asctime()
# dumpString(rcv)


def parseX10(rx):
'''Parse Received X10 message'''
if X10NODATA in rx:
print 'No X10 data available at ', time.asctime()
return -1
housecode = ord(rx[3])
keycode = ord(rx[4])
if 0 > keycode < 32 and 0 > housecode < 16:
print 'X10 House code:', X10HOUSE[housecode], 'X10 Keycode:', X10KEY[keycode]
else:
print 'invalid X10 data'
dumpString(rx)
# if ord(rx[5]) > 0:
print 'data left in buffer: ', repr(rx[5])
return ord(rx[5])


def dumpString(rcv):
'''Dump asci values of a string'''
count = 0
for x in rcv:
print 'String position %d is dec:%03d, hex:0x%02x prints as ASCII: %s' % (count, ord(x), ord(x), x)
count += 1





if __name__ == '__main__':
# main()

print 'Opened port:', ser.portstr
print 'settings:', ser

print 'Flushing buffers...'
ser.flush() # Make sure no noise in serial buffers
time.sleep(1)

# Turn lamp E7 OFF:
print 'E 7'
sendX10('E', '7')
time.sleep(1)
print 'E OFF'
sendX10('E', 'Off')

print '3-second delay...'
time.sleep(1) # Give the lamp 3-seconds to comply...
print 'getX10()'
getX10()
time.sleep(1)

# Turn the lamp ON:
print 'E 7'
sendX10('E', '7')
time.sleep(1)
print 'E ON'
sendX10('E', 'On')

# See what is in the X10 receive buffer:
getX10()


#EOF

_________________________
Regards, Richard Cooke, Turnkey Automation Inc.

Top
#19260 - 01/17/08 02:29 PM Re: Open Source Interface to CPU-XA or Ocelot [Re: Richard_Cooke]
Toon Offline
newbie


Registered: 01/17/08
Posts: 1
Hoi,

Tested this on my Linux box. And it works. That's prety cool. Thank you for writing and posting this. Very inspiring.

I glue some stuff I have running in the house together with some perl scripts.

What I'm currently looking for is, because I can already send X10 commands via heyu/CM11a is to send IR commands on the fly. Currently I do that by binding them to X10 commands in the Ocelot. But communicating directly via the serial interface from the perl scripts to the ocelot would be much quicker/reliable and scalable.

Top
#21996 - 07/20/10 06:15 PM Re: Open Source Interface to CPU-XA or Ocelot [Re: Toon]
bvnoldguy Offline
journeyman


Registered: 07/20/10
Posts: 50
Haven't been active on this forum since 2002 with my Hyena (WinLeopard) project. I have just converted the CPUXA/IP server from VB-6 to Python. It runs on XP, Centos-5 and Fedora-13.
---
The original Hyena IP protocol has not changed from it's 2002 implementation, though I have dropped the file transfer transaction with the Python implementation. TFTP and remote desktop make the transaction redundant.
---
I am still running the VB-6 WinLeopard client (version 66 of the GUI) but it looks tacky on Linux (under "wine").
---
I intend to convert the protocol to UDP data-gram (full duplex) as soon as I can convert the client package to wxGlade / wxPython.
---
Then I will convert the DMAX assembler (currently a hybrid of Delphi (Pascal) and ANSI C).
---
Any favorite place to publish the code? My preference is Google code (code.google.com) which uses SVN version control. It is several thousand lines of code in 5 modules, so I am not going to copy-pasta it here.

Top
#21997 - 07/20/10 06:21 PM Re: Open Source Interface to CPU-XA or Ocelot [Re: bvnoldguy]
bvnoldguy Offline
journeyman


Registered: 07/20/10
Posts: 50
Just found my old screen name - "bvntoys"
Top
#21998 - 07/20/10 06:32 PM Re: Open Source Interface to CPU-XA or Ocelot [Re: bvnoldguy]
bvnoldguy Offline
journeyman


Registered: 07/20/10
Posts: 50
FYI. The WinLeopard Python server runs on a $160 refurbished Acer AR1600 with a garden variety $5 USB - serial adapter. Because a client needed the AR1600 to replace a machine, I upgraded to a Foxconn SFF R10-S3 ($140) + $50 for 2gB of SDRAM.
---
I have been controlling my HVAC, water distribution system, lighting controls, and security system with an Ocelot since 2002. Only had one problem, a blown filter electrolytic, which Guy Levoi helped me repair.
---
I've got to say ADI, builds great hardware.

Top
#22044 - 08/21/10 06:46 PM Re: Open Source Interface to CPU-XA or Ocelot [Re: bvnoldguy]
bvnoldguy Offline
journeyman


Registered: 07/20/10
Posts: 50
I've got the pxa_server (Python XALIB) up, running and stable. I've started placing documents out on Google Docs
wxPython threading
wxPython for wxGlade
wxGlade GUI design.
The new project is code named Panther and for the foreseeable future it a plugin replacement for my 2002 Hyena.
- The VB-6 implementation required 35% of a 1.7gHz AMD 2500+ uni-processor.
- The Python implementation to 20% of the same and is 5 times more responsive.
- The SMP tolerant version on a 1.6gHz Intel Atom 330 uses less than 7% of a single processor (out of 4). It been tested on Windows XP, CentOS (rhel) 5, and Fedora FC-13.
As soon as I get fluent with Google Code and subversion, I'll begin publishing my code under the GPL.
For those that like XALIB, One of the modules mirrors it - the one named ocelot.py
---
I am beginning development on the Python replacement for the VB-6 client. It, like the server, is a threaded implementation that complies with my "wxPython threaded" practice.
- The Leopard Editor will be depreciated in favor of wxGlade as the GUI designer.

Top
#22124 - 09/13/10 05:55 PM Re: Open Source Interface to CPU-XA or Ocelot [Re: bvnoldguy]
bvnoldguy Offline
journeyman


Registered: 07/20/10
Posts: 50
open source (Python) can be downloaded from adicon-panther.googlecode.com (down load source)
Top
#22132 - 09/27/10 04:33 PM Re: Open Source Interface to CPU-XA or Ocelot [Re: bvnoldguy]
bvnoldguy Offline
journeyman


Registered: 07/20/10
Posts: 50
VB client is no longer in use here. Am using pxa_client on all non-server hosts in my house (Fedora & Windows). See Python thread.
---
pxa_server runs on Python 2.4 & 2.7
pxa_client runs on 2.6 & 2.7

Top
Page 1 of 1 1


Moderator:  Dan Smith, Monte G, ADI Tech Support, Guy Lavoie 
Hop to:
Who's Online
0 registered and 56 anonymous users online.
Recent Posts
Comms Retry Error
by HDTVPete
09/18/17 04:04 PM
Cmax Message: Failed to set data for a 'delay'
by HDTVPete
09/16/17 04:36 PM
Shout Box

Newest Members
brigiel, vevevie, zhutree, 416, saiqul
3003 Registered Users
Forum Stats
3003 Members
19 Forums
4001 Topics
23433 Posts

Max Online: 132 @ 11/13/16 10:07 AM
September
Su M Tu W Th F Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30