Option Explicit 'default (OFF) -implicit variable definition not used Option Base 0 'arrays start with 0 not 1! ' '======================================================================================== 'Module Title: I_Bridge_R2.Bas '======================================================================================== 'Author: UnKnown '======================================================================================== 'Date: 4-3-2006 '======================================================================================== 'Operating System: XP '======================================================================================== 'ZBasic IDE Version: 1.0.4 / ZBasic Compiler version 1-1-18 / ZX-40 Firmware 1.1.6 '======================================================================================== 'Target: ZX-40 '======================================================================================== 'Hardware: Super Comm Z R1 (4 [4 software serial & 1 Hardware] RS232 port ZX-40 PCB) 'Main/Support Modules Needed: ComSupport_R4.bas - for basic serial communications subroutines. ' Super_Comm_Z_R1.bas - "Sub Main" basic module '======================================================================================== 'Comport used: ZX-40 comport 6 @ 2400 8 N 1 - Do not change these parameters! 'To change to port just replace Port6 with Portx or x '======================================================================================== 'Bugs fixes and updates: ' ' ' ' ' ' ' ' '======================================================================================== 'Module Description: ' 'There is X-10 and there is "I" from Smarthome.com. "I" is the newer more reliable '(X-10 upgradable/compatable) PLC (Power Line Controller) technology for home automation. 'I cannot use the full "I" Word (because I signed a non-disclosure agreement - TOP SECRET!!!). ' 'This ZBasic RS232 driver will control their serial 2414S PLC, which in turn controls all 'the I??? devices connected to the A.C. wiring in the home. ' 'I chose only the basic Lamp on/off/all on/all off/ and dim/bright commands for use on my home 'automation serial controller - The "Super Comm Z" e.g. '---------------- 'I-Bridge Commands '---------------- 'Call Lamp1_On() 'Call Lamp1_Off() 'Call All_ON() 'Call All_Off() 'Call LampX_Dim_Bright(1,&H7F) 'lampx, &H00-&HFF - Off to full On '-------------- ' 'For further information on the "I" devices and the 2414s serial PLC controller go to 'smarthome.com ' '========================================================================================= ' ' '========================================================================================= 'Program Definitions: '========================================================================================= '===================================================================== 'Constants '[Public | Private] Const As = (default private) '===================================================================== '----------------------------- ' Port Constants for all ports '----------------------------- ' Do not modify! Private Const Port3 as byte = 3 Private Const Port4 as byte = 4 Private Const Port5 as byte = 5 Private Const Port6 as byte = 6 '----------------------------- '===================================================================== 'Variables '{Public | Private | Dim} As (default private) '========================================================================================== Dim ID(20) As Byte 'array start at 0! '=========================================================================================== 'Subroutines '============================================================================================ Public Sub Lamp1_On() Call Get_LampLinc1_Address() 'Get address of lamplinc1 Call Get_I_On() 'I command On for LampLinc1 Call I_Download_Command(False) End Sub '-------------------------------------------------------------------------------------------- Public Sub Lamp1_OFF() Call Get_LampLinc1_Address() 'Get address of lamplinc1 Call Get_I_Off() 'I command Off for LampLinc1 Call I_Download_Command(False) End Sub '-------------------------------------------------------------------------------------------- Public Sub All_ON() Call Get_Group1_Address() 'Get address of group1 Call Get_I_All_On() 'I broadcast command All ON Call I_Download_Command(False) End Sub '-------------------------------------------------------------------------------------------- Public Sub All_OFF() Call Get_Group1_Address() 'Get address of group1 Call Get_I_All_Off() 'I broadcast command All OFF Call I_Download_Command(False) End Sub '-------------------------------------------------------------------------------------------- Public Sub LampX_Dim_Bright(ByVal Lamp_Number as byte, ByVal Lamp_Value as byte) Call Get_LampLinc_Database(Lamp_Number) 'Get address of lamplincx Call Get_I_On() 'I command On for LampLinc1 ID(13) = Lamp_Value Call I_Download_Command(False) End Sub Private Sub Get_LampLinc_Database(Byval Lamp_Number as byte) Select Case Lamp_Number Case 1 Call Get_LampLinc1_Address() Case 2 'Future Call Get_LampLinc2_Address Case 3 'future Case 4 'future End Select End Sub '============================================================================================= Private Sub Compute_Download_Checksum() 'Writes to global array Dim CS As Integer CS = CInt(ID(2)) + CInt(ID(3)) + CInt(ID(4)) + _ CInt(ID(5)) + CInt(ID(8)) + CInt(ID(9)) + CInt(ID(10)) + _ CInt(ID(11)) + CInt(ID(12)) + CInt(ID(13)) 'Add command header and msg bytes together CS = Not CS '2 compliments of sum CS = CS + &H01 'Add 1 to sum ID(6) = Cbyte(hibyte(CS)) ID(7) = Cbyte(lobyte(CS)) End Sub '================================================================================================================ Private sub Get_Download_Header() 'Command - Do not modify ID(0) = &H02 ID(1) = &H40 ID(2) = &H01 ID(3) = &HA4 ID(4) = &H00 ID(5) = &H06 End Sub '--------------------------------------------------------------------------------------------- Private Sub Get_Mask_Header() 'Command - Do not modify ID(0) = &H02 ID(1) = &H46 ID(2) = &H01 ID(3) = &H42 ID(4) = &H10 ID(5) = &HFF End Sub '------------------------------------------------------------------------------------- Private Sub Get_LampLinc1_Address() 'LAMP #1 Lamplinc - Address found on back of device in hex (&H00,&H66,&H91) ID(8) = &H00 ID(9) = &H66 ID(10) = &H91 End Sub '------------------------------------------------------------------------------------- 'Private Sub Get_LampLinc2_Address() 'LAMP #2 Lamplinc - Address found on back of device in hex (&Hxx,&Hxx,&Hx) 'ID(8) = &H00 'ID(9) = &H00 'ID(10) = &H00 'End Sub '------------------------------------------------------------------------------------- Private Sub Get_Group1_Address() 'Group 1 Address - for all devices -> allon/alloff commands ID(8) = &H00 ID(9) = &H00 ID(10) = &H01 End Sub '------------------------------------------------------------------------------------ Private Sub Get_I_On() 'I Lamplinc On "Full ON" 3 hops ID(11) = &H0F ID(12) = &H11 ID(13) = &HFF End Sub '------------------------------------------------------------------------------------ Private Sub Get_I_Off() 'I Lamplinc Off "Full OFF" 3 hops ID(11) = &H0F ID(12) = &H13 ID(13) = &H00 End Sub '------------------------------------------------------------------------------------- Private Sub Get_I_All_On() 'I Broadcast All ON 3 hops ID(11) = &HCF ID(12) = &H11 ID(13) = &HFF End Sub '--------------------------------------------------------------------------------------- Private Sub Get_I_All_Off() 'I Broadcast All OFF 3 hops ID(11) = &HCF ID(12) = &H13 ID(13) = &H00 End Sub '-------------------------------------------------------------------------------------------- Private Sub I_DownLoad_Command(Byval DownLoad_OK as boolean) 'Send first two download bytes - wait for download command echo of &H02 and &H40 'Send download header and message command bytes with 20 ms wait between chars. 'Then Send mask command Const rt_delay as single = 1.0 'Retry delay - depends on hardware. .500 no .750 yes 1 yes Default 2000 Const hdr_dly as single = 0.015 '20ms 'Download delay - 15 will work? default 20 dim rx1 as byte dim rx2 as byte dim success1 as boolean dim success2 as boolean dim retry as byte dim i as byte dim counter as byte Debug.Print "DownLoad Started" Retry = 3 Counter = 0 DownLoad_OK = True Call Delay(1.0) ' wait for "i-bridge timeout" ClearInputBuffer(port6) ClearOutputBuffer(port6) Restart: Call delay(RT_Delay) If Counter > 3 then Debug.Print "DownLoad Failure - > 3 retries" DownLoad_Ok = False Exit sub End if DownLoad_Ok = False Counter = Counter + 1 Debug.Print "Retry Counterx = "; Cstr(counter) Call Get_Download_Header() Call Compute_Download_Checksum() Call Delay(1.0) ClearInputBuffer(port4) ClearOutputBuffer(port4) success1 = false success2 = false 'Send Command Bytes Call PutByte(Port6,ID(0)) 'send command byte 1 &H02 call delay(hdr_dly) Call PutByte(Port6,ID(1)) 'send command bytes 2 &H40 call delay(hdr_dly) 'Get Command Byte Echo CALL GetByte(6,Rx1,Success1) CALL GetByte(6,Rx2,Success2) If ((Success1 And Success2) = False) Then Goto Restart End If 'Make sure command byte echo chars are OK If ((Rx1 <> &H02) OR (Rx2 <> &H40)) then Goto Restart End If 'Send Command Header For i = 2 to 7 Call PutByte(Port6,ID(i)) Call delay(Hdr_dly) Next i ClearOutputBuffer(port6) 'Send Command Message For i = 8 to 13 Call PutByte(Port6,ID(i)) Call Delay(Hdr_dly) Next i Call Get_Mask_Header() ClearOutputBuffer(port6) Debug.print "Send Mask Header" 'Send Mask Header For i = 0 to 5 Call PutByte(Port6,ID(i)) Call delay(Hdr_dly) Next i Debug.print "Download Command Complete!" DownLoad_OK = True End Sub '===========================================================================================